Search in sources :

Example 31 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project hadoop by apache.

the class TestBatchIbr method createExecutor.

static ExecutorService createExecutor() throws Exception {
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    final ExecutorCompletionService<Path> completion = new ExecutorCompletionService<>(executor);
    // initialize all threads and buffers
    for (int i = 0; i < NUM_THREADS; i++) {
        completion.submit(new Callable<Path>() {

            @Override
            public Path call() throws Exception {
                IO_BUF.get();
                VERIFY_BUF.get();
                return null;
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        completion.take().get();
    }
    return executor;
}
Also used : Path(org.apache.hadoop.fs.Path) ExecutorService(java.util.concurrent.ExecutorService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) IOException(java.io.IOException)

Example 32 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project camel by apache.

the class JmsRouteRequestReplyTest method runRequestReplyThreaded.

protected void runRequestReplyThreaded(String fromUri) throws Exception {
    // start template
    template.start();
    ExecutorService executor = context.getExecutorServiceManager().newFixedThreadPool(this, "Task", maxTasks);
    CompletionService<Task> completionService = new ExecutorCompletionService<Task>(executor);
    final AtomicInteger counter = new AtomicInteger(-1);
    for (int i = 0; i < maxTasks; i++) {
        Task task = new Task(counter, fromUri);
        completionService.submit(task);
    }
    for (int i = 0; i < maxTasks; i++) {
        Future<Task> future = completionService.take();
        Task task = future.get(60, TimeUnit.SECONDS);
        assertNotNull("Should complete the task", task);
        task.assertSuccess();
    }
    context.getExecutorServiceManager().shutdownNow(executor);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService)

Example 33 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project graphhopper by graphhopper.

the class LMAlgoFactoryDecorator method loadOrDoWork.

/**
 * This method calculates the landmark data for all weightings (optionally in parallel) or if already existent loads it.
 *
 * @return true if the preparation data for at least one weighting was calculated.
 * @see com.graphhopper.routing.ch.CHAlgoFactoryDecorator#prepare(StorableProperties) for a very similar method
 */
public boolean loadOrDoWork(final StorableProperties properties) {
    ExecutorCompletionService completionService = new ExecutorCompletionService<>(threadPool);
    int counter = 0;
    final AtomicBoolean prepared = new AtomicBoolean(false);
    for (final PrepareLandmarks plm : preparations) {
        counter++;
        final int tmpCounter = counter;
        final String name = AbstractWeighting.weightingToFileName(plm.getWeighting());
        completionService.submit(new Runnable() {

            @Override
            public void run() {
                if (plm.loadExisting())
                    return;
                LOGGER.info(tmpCounter + "/" + getPreparations().size() + " calling LM prepare.doWork for " + plm.getWeighting() + " ... (" + getMemInfo() + ")");
                prepared.set(true);
                Thread.currentThread().setName(name);
                plm.doWork();
                properties.put(Landmark.PREPARE + "date." + name, createFormatter().format(new Date()));
            }
        }, name);
    }
    threadPool.shutdown();
    try {
        for (int i = 0; i < preparations.size(); i++) {
            completionService.take().get();
        }
    } catch (Exception e) {
        threadPool.shutdownNow();
        throw new RuntimeException(e);
    }
    return prepared.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) IOException(java.io.IOException)

Example 34 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project hbase by apache.

the class SnapshotManifestV2 method loadRegionManifests.

static List<SnapshotRegionManifest> loadRegionManifests(final Configuration conf, final Executor executor, final FileSystem fs, final Path snapshotDir, final SnapshotDescription desc, final int manifestSizeLimit) throws IOException {
    FileStatus[] manifestFiles = FSUtils.listStatus(fs, snapshotDir, new PathFilter() {

        @Override
        public boolean accept(Path path) {
            return path.getName().startsWith(SNAPSHOT_MANIFEST_PREFIX);
        }
    });
    if (manifestFiles == null || manifestFiles.length == 0)
        return null;
    final ExecutorCompletionService<SnapshotRegionManifest> completionService = new ExecutorCompletionService<>(executor);
    for (final FileStatus st : manifestFiles) {
        completionService.submit(new Callable<SnapshotRegionManifest>() {

            @Override
            public SnapshotRegionManifest call() throws IOException {
                FSDataInputStream stream = fs.open(st.getPath());
                CodedInputStream cin = CodedInputStream.newInstance(stream);
                cin.setSizeLimit(manifestSizeLimit);
                try {
                    return SnapshotRegionManifest.parseFrom(cin);
                } finally {
                    stream.close();
                }
            }
        });
    }
    ArrayList<SnapshotRegionManifest> regionsManifest = new ArrayList<>(manifestFiles.length);
    try {
        for (int i = 0; i < manifestFiles.length; ++i) {
            regionsManifest.add(completionService.take().get());
        }
    } catch (InterruptedException e) {
        throw new InterruptedIOException(e.getMessage());
    } catch (ExecutionException e) {
        Throwable t = e.getCause();
        if (t instanceof InvalidProtocolBufferException) {
            throw (InvalidProtocolBufferException) t;
        } else {
            IOException ex = new IOException("ExecutionException");
            ex.initCause(e.getCause());
            throw ex;
        }
    }
    return regionsManifest;
}
Also used : Path(org.apache.hadoop.fs.Path) InterruptedIOException(java.io.InterruptedIOException) PathFilter(org.apache.hadoop.fs.PathFilter) FileStatus(org.apache.hadoop.fs.FileStatus) CodedInputStream(org.apache.hadoop.hbase.shaded.com.google.protobuf.CodedInputStream) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(org.apache.hadoop.hbase.shaded.com.google.protobuf.InvalidProtocolBufferException) SnapshotRegionManifest(org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotRegionManifest) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) ExecutionException(java.util.concurrent.ExecutionException)

Example 35 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project hbase by apache.

the class SnapshotReferenceUtil method concurrentVisitReferencedFiles.

public static void concurrentVisitReferencedFiles(final Configuration conf, final FileSystem fs, final SnapshotManifest manifest, final ExecutorService exec, final StoreFileVisitor visitor) throws IOException {
    final SnapshotDescription snapshotDesc = manifest.getSnapshotDescription();
    final Path snapshotDir = manifest.getSnapshotDir();
    List<SnapshotRegionManifest> regionManifests = manifest.getRegionManifests();
    if (regionManifests == null || regionManifests.isEmpty()) {
        LOG.debug("No manifest files present: " + snapshotDir);
        return;
    }
    final ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<>(exec);
    for (final SnapshotRegionManifest regionManifest : regionManifests) {
        completionService.submit(new Callable<Void>() {

            @Override
            public Void call() throws IOException {
                visitRegionStoreFiles(regionManifest, visitor);
                return null;
            }
        });
    }
    try {
        for (int i = 0; i < regionManifests.size(); ++i) {
            completionService.take().get();
        }
    } catch (InterruptedException e) {
        throw new InterruptedIOException(e.getMessage());
    } catch (ExecutionException e) {
        if (e.getCause() instanceof CorruptedSnapshotException) {
            throw new CorruptedSnapshotException(e.getCause().getMessage(), ProtobufUtil.createSnapshotDesc(snapshotDesc));
        } else {
            IOException ex = new IOException();
            ex.initCause(e.getCause());
            throw ex;
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) InterruptedIOException(java.io.InterruptedIOException) SnapshotRegionManifest(org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotRegionManifest) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) SnapshotDescription(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.SnapshotDescription) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)69 ArrayList (java.util.ArrayList)31 ExecutorService (java.util.concurrent.ExecutorService)30 ExecutionException (java.util.concurrent.ExecutionException)27 IOException (java.io.IOException)24 Test (org.junit.Test)21 Future (java.util.concurrent.Future)18 List (java.util.List)10 InterruptedIOException (java.io.InterruptedIOException)9 Path (org.apache.hadoop.fs.Path)8 KieSession (org.kie.api.runtime.KieSession)8 Callable (java.util.concurrent.Callable)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)6 EntryPoint (org.kie.api.runtime.rule.EntryPoint)6 HashMap (java.util.HashMap)4 Executor (java.util.concurrent.Executor)4 TimeoutException (java.util.concurrent.TimeoutException)4 KieBase (org.kie.api.KieBase)4 Random (java.util.Random)3