Search in sources :

Example 6 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project druid by druid-io.

the class CuratorInventoryManager method stop.

@LifecycleStop
public void stop() throws IOException {
    synchronized (lock) {
        if (childrenCache == null) {
            return;
        }
        // This close() call actually calls shutdownNow() on the executor registered with the Cache object...
        childrenCache.close();
        childrenCache = null;
    }
    Closer closer = Closer.create();
    for (ContainerHolder containerHolder : containers.values()) {
        closer.register(containerHolder.getCache());
    }
    try {
        closer.close();
    } finally {
        pathChildrenCacheExecutor.shutdown();
    }
}
Also used : Closer(com.google.common.io.Closer) LifecycleStop(io.druid.java.util.common.lifecycle.LifecycleStop)

Example 7 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class CommandLineJob method run.

@Override
public boolean run() {
    try {
        String outputPath = getJobConf().getOutputFilePath();
        LOG.info("Exec {} output to {}", mCommand, outputPath);
        Process p = java.lang.Runtime.getRuntime().exec(mCommand);
        String line;
        Closer closer = Closer.create();
        try {
            BufferedReader bri = closer.register(new BufferedReader(new InputStreamReader(p.getInputStream())));
            BufferedReader bre = closer.register(new BufferedReader(new InputStreamReader(p.getErrorStream())));
            File file = new File(outputPath);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = closer.register(new BufferedWriter(fw));
            while ((line = bri.readLine()) != null) {
                bw.write(line + "\n");
            }
            while ((line = bre.readLine()) != null) {
                bw.write(line + "\n");
            }
            bw.flush();
        } finally {
            closer.close();
        }
        p.waitFor();
        LOG.info("Exec {} output to {} done.", mCommand, outputPath);
    } catch (IOException | InterruptedException e) {
        LOG.error(e.getMessage());
        return false;
    }
    return true;
}
Also used : Closer(com.google.common.io.Closer) InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 8 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class UnderFileSystemBlockInStream method create.

/**
   * Creates an instance of {@link UnderFileSystemBlockInStream}.
   * This method keeps polling the block worker until the block is cached to Alluxio or
   * it successfully acquires a UFS read token with a timeout.
   * (1) If the block is cached to Alluxio after polling, it returns {@link BufferedBlockInStream}
   *     to read the block from Alluxio storage.
   * (2) If a UFS read token is acquired after polling, it returns
   *     {@link UnderFileSystemBlockInStream} to read the block from an Alluxio worker that reads
   *     the block from UFS.
   * (3) If the polling times out, an {@link IOException} with cause
   *     {@link alluxio.exception.UfsBlockAccessTokenUnavailableException} is thrown.
   *
   * @param context the file system context
   * @param ufsPath the UFS path
   * @param blockId the block ID
   * @param blockSize the block size
   * @param blockStart the start position of the block in the UFS file
   * @param workerNetAddress the worker network address
   * @param options the in stream options
   * @return the {@link UnderFileSystemBlockInStream} instance or the {@link BufferedBlockOutStream}
   *         that reads from Alluxio directly
   * @throws IOException if it fails to create {@link UnderFileSystemBlockInStream}
   */
public static BufferedBlockInStream create(FileSystemContext context, String ufsPath, long blockId, long blockSize, long blockStart, WorkerNetAddress workerNetAddress, InStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient blockWorkerClient = closer.register(context.createBlockWorkerClient(workerNetAddress));
        LockBlockOptions lockBlockOptions = LockBlockOptions.defaults().setUfsPath(ufsPath).setOffset(blockStart).setBlockSize(blockSize).setMaxUfsReadConcurrency(options.getMaxUfsReadConcurrency());
        LockBlockResult result = closer.register(blockWorkerClient.lockUfsBlock(blockId, lockBlockOptions)).getResult();
        if (result.getLockBlockStatus().blockInAlluxio()) {
            boolean local = blockWorkerClient.getDataServerAddress().getHostName().equals(NetworkAddressUtils.getLocalHostName());
            if (local) {
                LocalFileBlockReader reader = closer.register(new LocalFileBlockReader(result.getBlockPath()));
                return LocalBlockInStream.createWithLockedBlock(blockWorkerClient, blockId, blockSize, reader, closer, options);
            } else {
                return RemoteBlockInStream.createWithLockedBlock(context, blockWorkerClient, blockId, blockSize, result.getLockId(), closer, options);
            }
        }
        Preconditions.checkState(result.getLockBlockStatus().ufsTokenAcquired());
        return new UnderFileSystemBlockInStream(context, blockId, blockSize, blockWorkerClient, closer, options);
    } catch (AlluxioException | IOException e) {
        CommonUtils.closeQuitely(closer);
        throw CommonUtils.castToIOException(e);
    }
}
Also used : Closer(com.google.common.io.Closer) LockBlockOptions(alluxio.client.block.options.LockBlockOptions) LocalFileBlockReader(alluxio.worker.block.io.LocalFileBlockReader) LockBlockResult(alluxio.wire.LockBlockResult) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException)

Example 9 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class BlockOutStream method createRemoteBlockOutStream.

/**
   * Creates a new remote block output stream.
   *
   * @param blockId the block id
   * @param blockSize the block size
   * @param workerNetAddress the worker network address
   * @param context the file system context
   * @param options the options
   * @throws IOException if an I/O error occurs
   * @return the {@link BlockOutStream} instance created
   */
public static BlockOutStream createRemoteBlockOutStream(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, OutStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress));
        PacketOutStream outStream = PacketOutStream.createNettyPacketOutStream(context, client.getDataServerAddress(), client.getSessionId(), blockId, blockSize, options.getWriteTier(), Protocol.RequestType.ALLUXIO_BLOCK);
        closer.register(outStream);
        return new BlockOutStream(outStream, blockId, blockSize, client, options);
    } catch (IOException e) {
        closer.close();
        throw e;
    }
}
Also used : Closer(com.google.common.io.Closer) BlockWorkerClient(alluxio.client.block.BlockWorkerClient) IOException(java.io.IOException)

Example 10 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class CpCommandTest method equals.

private boolean equals(AlluxioURI file1, AlluxioURI file2) throws Exception {
    try (Closer closer = Closer.create()) {
        OpenFileOptions openFileOptions = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        FileInStream is1 = closer.register(mFileSystem.openFile(file1, openFileOptions));
        FileInStream is2 = closer.register(mFileSystem.openFile(file2, openFileOptions));
        return IOUtils.contentEquals(is1, is2);
    }
}
Also used : Closer(com.google.common.io.Closer) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions)

Aggregations

Closer (com.google.common.io.Closer)200 IOException (java.io.IOException)94 File (java.io.File)22 Test (org.junit.Test)18 Test (org.testng.annotations.Test)18 Closer (org.apache.flink.shaded.guava30.com.google.common.io.Closer)16 Path (org.apache.hadoop.fs.Path)16 ArrayList (java.util.ArrayList)15 Properties (java.util.Properties)14 FileOutputStream (java.io.FileOutputStream)13 FileInputStream (java.io.FileInputStream)12 InputStream (java.io.InputStream)12 Map (java.util.Map)12 OutputStream (java.io.OutputStream)11 UncheckedIOException (java.io.UncheckedIOException)10 NettyShuffleEnvironment (org.apache.flink.runtime.io.network.NettyShuffleEnvironment)9 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)9 AlluxioException (alluxio.exception.AlluxioException)8 InputStreamReader (java.io.InputStreamReader)8 HashMap (java.util.HashMap)8