Example usage for java.util.concurrent Future isDone

List of usage examples for java.util.concurrent Future isDone

Introduction

In this page you can find the example usage for java.util.concurrent Future isDone.

Prototype

boolean isDone();

Source Link

Document

Returns true if this task completed.

Usage

From source file:com.mobilyzer.Checkin.java

private synchronized boolean checkGetCookie() {
    if (phoneUtils.isTestingServer(phoneUtils.getServerUrl())) {
        authCookie = getFakeAuthCookie();
        return true;
    }// ww  w. java2 s .  co  m
    Future<Cookie> getCookieFuture = accountSelector.getCheckinFuture();
    if (getCookieFuture == null) {
        Logger.i("checkGetCookie called too early");
        return false;
    }
    if (getCookieFuture.isDone()) {
        try {
            authCookie = getCookieFuture.get();
            Logger.i("Got authCookie: " + authCookie);
            return true;
        } catch (InterruptedException e) {
            Logger.e("Unable to get auth cookie", e);
            return false;
        } catch (ExecutionException e) {
            Logger.e("Unable to get auth cookie", e);
            return false;
        }
    } else {
        Logger.i("getCookieFuture is not yet finished");
        return false;
    }
}

From source file:org.apache.storm.grouping.LoadAwareShuffleGroupingTest.java

@Test
public void testLoadAwareShuffleGroupingWithEvenLoadMultiThreaded()
        throws InterruptedException, ExecutionException {
    final int numTasks = 7;

    final LoadAwareShuffleGrouping grouper = new LoadAwareShuffleGrouping();

    // Task Id not used, so just pick a static value
    final int inputTaskId = 100;
    // Define our taskIds - the test expects these to be incrementing by one up from zero
    final List<Integer> availableTaskIds = getAvailableTaskIds(numTasks);
    final LoadMapping loadMapping = buildLocalTasksEvenLoadMapping(availableTaskIds);

    final WorkerTopologyContext context = mockContext(availableTaskIds);
    grouper.prepare(context, null, availableTaskIds);

    // force triggers building ring
    grouper.refreshLoad(loadMapping);//from w  ww. ja  v  a2 s.  c  o m

    // calling chooseTasks should be finished before refreshing ring
    // adjusting groupingExecutionsPerThread might be needed with really slow machine
    // we allow race condition between refreshing ring and choosing tasks
    // so it will not make exact even distribution, though diff is expected to be small
    // given that all threadTasks are finished before refreshing ring,
    // distribution should be exactly even
    final int groupingExecutionsPerThread = numTasks * 5000;
    final int numThreads = 10;
    int totalEmits = groupingExecutionsPerThread * numThreads;

    List<Callable<int[]>> threadTasks = Lists.newArrayList();
    for (int x = 0; x < numThreads; x++) {
        Callable<int[]> threadTask = new Callable<int[]>() {
            @Override
            public int[] call() throws Exception {
                int[] taskCounts = new int[availableTaskIds.size()];
                for (int i = 1; i <= groupingExecutionsPerThread; i++) {
                    List<Integer> taskIds = grouper.chooseTasks(inputTaskId, Lists.newArrayList());

                    // Validate a single task id return
                    assertNotNull("Not null taskId list returned", taskIds);
                    assertEquals("Single task Id returned", 1, taskIds.size());

                    int taskId = taskIds.get(0);

                    assertTrue("TaskId should exist", taskId >= 0 && taskId < availableTaskIds.size());
                    taskCounts[taskId]++;
                }
                return taskCounts;
            }
        };

        // Add to our collection.
        threadTasks.add(threadTask);
    }

    ExecutorService executor = Executors.newFixedThreadPool(threadTasks.size());
    List<Future<int[]>> taskResults = executor.invokeAll(threadTasks);

    // Wait for all tasks to complete
    int[] taskIdTotals = new int[numTasks];
    for (Future taskResult : taskResults) {
        while (!taskResult.isDone()) {
            Thread.sleep(1000);
        }
        int[] taskDistributions = (int[]) taskResult.get();
        for (int i = 0; i < taskDistributions.length; i++) {
            taskIdTotals[i] += taskDistributions[i];
        }
    }

    int minPrCount = (int) (totalEmits * ((1.0 / numTasks) - ACCEPTABLE_MARGIN));
    int maxPrCount = (int) (totalEmits * ((1.0 / numTasks) + ACCEPTABLE_MARGIN));

    for (int i = 0; i < numTasks; i++) {
        assertTrue("Distribution should be even for all nodes with small delta",
                taskIdTotals[i] >= minPrCount && taskIdTotals[i] <= maxPrCount);
    }
}

From source file:org.janusgraph.graphdb.log.StandardTransactionLogProcessor.java

private void fixSecondaryFailure(final StandardTransactionId txId, final TxEntry entry) {
    logRecoveryMsg("Attempting to repair partially failed transaction [%s]", txId);
    if (entry.entry == null) {
        logRecoveryMsg("Trying to repair expired or unpersisted transaction [%s] (Ignore in startup)", txId);
        return;/*from ww  w.  java 2  s  .  c  o  m*/
    }

    boolean userLogFailure = true;
    boolean secIndexFailure = true;
    final Predicate<String> isFailedIndex;
    final TransactionLogHeader.Entry commitEntry = entry.entry;
    final TransactionLogHeader.SecondaryFailures secFail = entry.failures;
    if (secFail != null) {
        userLogFailure = secFail.userLogFailure;
        secIndexFailure = !secFail.failedIndexes.isEmpty();
        isFailedIndex = new Predicate<String>() {
            @Override
            public boolean apply(@Nullable String s) {
                return secFail.failedIndexes.contains(s);
            }
        };
    } else {
        isFailedIndex = Predicates.alwaysTrue();
    }

    // I) Restore external indexes
    if (secIndexFailure) {
        //1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
        final SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
        BackendOperation.execute(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
                try {
                    for (TransactionLogHeader.Modification modification : commitEntry
                            .getContentAsModifications(serializer)) {
                        InternalRelation rel = ModificationDeserializer.parseRelation(modification, tx);
                        //Collect affected vertex indexes
                        for (MixedIndexType index : getMixedIndexes(rel.getType())) {
                            if (index.getElement() == ElementCategory.VERTEX
                                    && isFailedIndex.apply(index.getBackingIndexName())) {
                                assert rel.isProperty();
                                indexRestores.put(index.getBackingIndexName(), new IndexRestore(
                                        rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
                            }
                        }
                        //See if relation itself is affected
                        for (RelationType relType : rel.getPropertyKeysDirect()) {
                            for (MixedIndexType index : getMixedIndexes(relType)) {
                                if (index.getElement().isInstance(rel)
                                        && isFailedIndex.apply(index.getBackingIndexName())) {
                                    assert rel.id() instanceof RelationIdentifier;
                                    indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(),
                                            ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
                                }
                            }
                        }
                    }
                } finally {
                    if (tx.isOpen())
                        tx.rollback();
                }
                return true;
            }
        }, readTime);

        //2) Restore elements per backing index
        for (final String indexName : indexRestores.keySet()) {
            final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
            try {
                BackendTransaction btx = tx.getTxHandle();
                final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
                BackendOperation.execute(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
                        for (IndexRestore restore : indexRestores.get(indexName)) {
                            JanusGraphSchemaVertex indexV = (JanusGraphSchemaVertex) tx
                                    .getVertex(restore.indexId);
                            MixedIndexType index = (MixedIndexType) indexV.asIndexType();
                            JanusGraphElement element = restore.retrieve(tx);
                            if (element != null) {
                                graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
                            } else { //Element is deleted
                                graph.getIndexSerializer().removeElement(restore.elementId, index,
                                        restoredDocs);
                            }
                        }
                        indexTx.restore(restoredDocs);
                        indexTx.commit();
                        return true;
                    }

                    @Override
                    public String toString() {
                        return "IndexMutation";
                    }
                }, persistenceTime);

            } finally {
                if (tx.isOpen())
                    tx.rollback();
            }
        }

    }

    // II) Restore log messages
    final String logTxIdentifier = (String) commitEntry.getMetadata().get(LogTxMeta.LOG_ID);
    if (userLogFailure && logTxIdentifier != null) {
        TransactionLogHeader txHeader = new TransactionLogHeader(txCounter.incrementAndGet(), times.getTime(),
                times);
        final StaticBuffer userLogContent = txHeader.serializeUserLog(serializer, commitEntry, txId);
        BackendOperation.execute(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                final Log userLog = graph.getBackend().getUserLog(logTxIdentifier);
                Future<Message> env = userLog.add(userLogContent);
                if (env.isDone()) {
                    env.get();
                }
                return true;
            }
        }, persistenceTime);
    }

}

From source file:de.swm.nis.logicaldecoding.RefreshCacheService.java

@Scheduled(fixedDelayString = "${scheduling.interval}")
public void refreshCache() {

    log.debug("Start pulling changes...");
    Collection<ChangeSetDAO> changes = changesetFetcher.fetch(replicationSlotName, numChangestoFetch);
    Collection<DmlEvent> events = new ArrayList<DmlEvent>();
    Map<Long, ZonedDateTime> transactionCommitTimes = new HashMap<Long, ZonedDateTime>();

    for (ChangeSetDAO change : changes) {
        log.info(change.toString());//  www . ja va  2  s  . c  om
        Event event = parser.parseLogLine(change.getData());
        event.setTransactionId(change.getTransactionId());
        // This is a change to consider
        if (event instanceof DmlEvent) {
            events.add((DmlEvent) event);
        } else if (event instanceof TxEvent) {
            if (event.getCommitTime() != null) {
                transactionCommitTimes.put(event.getTransactionId(), event.getCommitTime());
            }
        }
    }

    for (DmlEvent event : events) {
        event.setCommitTime(transactionCommitTimes.get(event.getTransactionId()));
    }

    Predicate<DmlEvent> predicate = new Predicate<DmlEvent>() {
        @Override
        public boolean apply(DmlEvent input) {
            return schemasToInclude.contains(input.getSchemaName());
        }
    };

    Collection<DmlEvent> relevantEvents = Collections2.filter(events, predicate);
    log.info("Pulled changes [max:" + numChangestoFetch + ", found:" + events.size() + ", relevant:"
            + relevantEvents.size() + "]");

    if (relevantEvents.size() == 0) {
        // Nothing to do
        return;
    }

    Future<String> seeding = null;
    Future<String> publishing = null;
    if (doSeed) {
        seeding = gwcInvalidator.postSeedRequests(relevantEvents);
    }
    if (doPublish) {
        publishing = trackTablePublisher.publish(relevantEvents);
    }

    // Wait for Publish task to be finished and check for Errors
    if (doPublish) {
        while (!(publishing.isDone())) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                log.warn("InterruptedExcepton during waiting of completion of Tasks", e);
            }
        }
        try {
            publishing.get();
        } catch (InterruptedException e) {
            log.warn("InterruptedExcepton during excecution of 'publishing' Task", e);
        } catch (ExecutionException e) {
            log.warn("Exception occurrred during execution of 'publishing' Task.", e);
        }
    }

    // Wait for Seed task to be finished and check for Errors
    if (doSeed) {
        while (!(seeding.isDone())) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                log.warn("InterruptedExcepton during waiting of completion of Tasks", e);
            }
        }
        try {
            seeding.get();
        } catch (InterruptedException e) {
            log.warn("InterruptedExcepton during excecution of 'seeding' Task", e);
        } catch (ExecutionException e) {
            log.warn("Exception occurrred during execution of 'seeding' Task.", e);
        }
    }
}

From source file:net.yacy.document.importer.MediawikiImporter.java

public static void createIndex(final File dumpFile) throws IOException {
    // calculate md5
    //String md5 = serverCodings.encodeMD5Hex(dumpFile);

    // init reader, producer and consumer
    final PositionAwareReader in = new PositionAwareReader(dumpFile);
    final indexProducer producer = new indexProducer(100, idxFromMediawikiXML(dumpFile));
    final wikiConsumer consumer = new wikiConsumer(100, producer);
    final ExecutorService service = Executors.newCachedThreadPool(
            new NamePrefixThreadFactory(MediawikiImporter.class.getSimpleName() + ".createIndex"));
    final Future<Integer> producerResult = service.submit(consumer);
    final Future<Integer> consumerResult = service.submit(producer);
    service.shutdown();//from   ww  w  . ja  va2 s. com

    // read the wiki dump
    long start, stop;
    while (in.seek(pagestartb)) {
        start = in.pos() - 6;
        in.resetBuffer();
        if (!in.seek(pageendb))
            break;
        stop = in.pos();
        consumer.consume(new wikiraw(in.bytes(), start, stop));
        in.resetBuffer();
    }

    // shut down the services
    try {
        consumer.consume(wikiConsumer.poison);
        try {
            consumerResult.get(5000, TimeUnit.MILLISECONDS);
        } catch (final TimeoutException e) {
        }
        producer.consume(indexProducer.poison);
        if (!consumerResult.isDone())
            consumerResult.get();
        producerResult.get();
    } catch (final InterruptedException e) {
        ConcurrentLog.logException(e);
        return;
    } catch (final ExecutionException e) {
        ConcurrentLog.logException(e);
        return;
    }
    in.close();
}

From source file:info.magnolia.imaging.caching.CachingImageStreamerRepositoryTest.java

/**
 * This test is not executed by default - too long !
 * Used to reproduce the "session already closed issue", see MGNLIMG-59.
 * Set the "expiration" property of the jobs map in CachingImageStreamer to a longer value
 * to have more chances of reproducing the problem.
 *//*from w  ww .  j av  a  2 s .  c  o m*/
@Ignore
@Test
public void testConcurrencyAndJCRSessions() throws Exception {
    final HierarchyManager srcHM = MgnlContext.getHierarchyManager("website");
    final String srcPath = "/foo/bar";
    ContentUtil.createPath(srcHM, srcPath);

    // ParameterProvider for tests - return a new instance of the same node everytime
    // if we'd return the same src instance everytime, the purpose of this test would be null
    final ParameterProviderFactory<Object, Content> ppf = new TestParameterProviderFactory(srcHM, srcPath);

    final OutputFormat png = new OutputFormat();
    png.setFormatName("png");

    final ImageOperationChain<ParameterProvider<Content>> generator = new ImageOperationChain<ParameterProvider<Content>>();
    final URLImageLoader<ParameterProvider<Content>> load = new URLImageLoader<ParameterProvider<Content>>();
    load.setUrl(getClass().getResource("/funnel.gif").toExternalForm());
    generator.addOperation(load);
    generator.setOutputFormat(png);
    generator.setName("foo blob bar");
    generator.setParameterProviderFactory(ppf);

    // yeah, we're using a "wrong" workspace for the image cache, to avoid having to setup a custom one in this test
    final HierarchyManager hm = MgnlContext.getHierarchyManager("config");

    final ImageStreamer streamer = new CachingImageStreamer(hm, ppf.getCachingStrategy(),
            new DefaultImageStreamer());

    // thread pool of 10, launching 8 requests, can we hit some concurrency please ?
    final ExecutorService executor = Executors.newFixedThreadPool(10);
    final ByteArrayOutputStream[] outs = new ByteArrayOutputStream[8];
    final Future[] futures = new Future[8];
    for (int i = 0; i < outs.length; i++) {
        final int ii = i;
        outs[i] = new ByteArrayOutputStream();
        futures[i] = executor.submit(new Runnable() {
            @Override
            public void run() {
                final ParameterProvider p = generator.getParameterProviderFactory()
                        .newParameterProviderFor(null);
                try {
                    streamer.serveImage(generator, p, outs[ii]);
                } catch (Exception e) {
                    throw new RuntimeException(e); // TODO
                }
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);

    for (Future<?> future : futures) {
        assertTrue(future.isDone());
        assertFalse(future.isCancelled());
        // ignore the results of TestJob - but if there was an exception thrown by TestJob.call(),
        // it is only thrown back at us when we call get() below. (so the test will fail badly if the job threw an exception)
        Object ignored = future.get();
    }

    shutdownRepository(true);

    // sleep for a while so that the jobs map's expiration thread can kick in !
    Thread.sleep(10000);
}

From source file:com.alibaba.otter.node.etl.load.loader.db.FileLoadAction.java

/**
 * ? fast-fail /* w  w  w  .j a  v a2 s . c o m*/
 */
private void moveFiles(FileLoadContext context, List<FileData> fileDatas, File rootDir) {
    Exception exception = null;
    adjustPoolSize(context);
    ExecutorCompletionService<Exception> executorComplition = new ExecutorCompletionService<Exception>(
            executor);

    List<Future<Exception>> results = new ArrayList<Future<Exception>>();
    for (FileData fileData : fileDatas) {
        Future<Exception> future = executorComplition.submit(new FileLoadWorker(context, rootDir, fileData));
        results.add(future);

        // fast fail
        if (future.isDone()) { // ( CallerRunsPolicy)
            try {
                exception = future.get();
            } catch (Exception e) {
                exception = e;
            }
            if (exception != null) {
                for (Future<Exception> result : results) {
                    if (!result.isDone() && !result.isCancelled()) {
                        result.cancel(true);
                    }
                }
                throw exception instanceof LoadException ? (LoadException) exception
                        : new LoadException(exception);
            }
        }

    }

    int resultSize = results.size();
    int cursor = 0;
    while (cursor < resultSize) {
        try {
            Future<Exception> result = executorComplition.take();
            exception = result.get();
        } catch (Exception e) {
            exception = e;
            break;
        }
        cursor++;
    }

    if (cursor != resultSize) { // ??
        for (Future<Exception> future : results) {
            if (!future.isDone() && !future.isCancelled()) {
                future.cancel(true);
            }
        }

    }

    if (exception != null) {
        throw exception instanceof LoadException ? (LoadException) exception : new LoadException(exception);
    }
}

From source file:org.wso2.carbon.bpmn.people.substitution.scheduler.SubstitutionScheduler.java

@Override
public void runTask(final ScheduledTask task) {

    Future future = _exec.submit(new Callable<Void>() {
        public Void call() throws Exception {
            try {
                ((SchedulerScheduledTask) task).run();
            } catch (Exception ex) {
                log.error("Error during scheduled task execution for substitutes", ex);
            }//from w ww.  j  ava  2 s  . c o  m
            return null;
        }
    });

    if (log.isDebugEnabled()) {
        log.debug("Scheduled task : " + task.toString() + ", completed : " + future.isDone());
    }

}

From source file:com.google.wireless.speed.speedometer.Checkin.java

private synchronized boolean checkGetCookie() {
    if (isTestingServer()) {
        authCookie = getFakeAuthCookie();
        return true;
    }/* www . ja  v a  2s. c  o  m*/
    Future<Cookie> getCookieFuture = accountSelector.getCheckinFuture();
    if (getCookieFuture == null) {
        Log.i(SpeedometerApp.TAG, "checkGetCookie called too early");
        return false;
    }
    if (getCookieFuture.isDone()) {
        try {
            authCookie = getCookieFuture.get();
            Log.i(SpeedometerApp.TAG, "Got authCookie: " + authCookie);
            return true;
        } catch (InterruptedException e) {
            Log.e(SpeedometerApp.TAG, "Unable to get auth cookie", e);
            return false;
        } catch (ExecutionException e) {
            Log.e(SpeedometerApp.TAG, "Unable to get auth cookie", e);
            return false;
        }
    } else {
        Log.i(SpeedometerApp.TAG, "getCookieFuture is not yet finished");
        return false;
    }
}