public void checkAndResumePsicquicTasks() { List<Future> currentRunningTasks = new ArrayList<Future>(runningTasks); for (Future<PsicquicCountResults> f : currentRunningTasks) { try { PsicquicCountResults results = f.get(threadTimeOut, TimeUnit.SECONDS); if (results.isImex()) { if (results.isImexResponding() && results.getImexCount() > 0) { countInOtherImexDatabases += results.getImexCount(); otherImexDatabasesWithResults++; } else if (!results.isImexResponding()) { nonRespondingImexDatabases++; } } if (results.isServiceResponding() && results.getPsicquicCount() > 0) { countInOtherDatabases += results.getPsicquicCount(); otherDatabasesWithResults++; } else if (!results.isServiceResponding()) { nonRespondingDatabases++; } runningTasks.remove(f); } catch (InterruptedException e) { log.error("The psicquic task was interrupted, we cancel the task.", e); this.nonRespondingDatabases++; this.nonRespondingImexDatabases++; if (!f.isCancelled()) { f.cancel(false); } runningTasks.remove(f); } catch (ExecutionException e) { log.error("The psicquic task could not be executed, we cancel the task.", e); if (!f.isCancelled()) { f.cancel(false); } runningTasks.remove(f); } catch (TimeoutException e) { log.error("Service task stopped because of time out " + threadTimeOut + "seconds."); this.nonRespondingDatabases++; this.nonRespondingImexDatabases++; if (!f.isCancelled()) { f.cancel(false); } runningTasks.remove(f); } catch (Throwable e) { log.error("The psicquic task could not be executed, we cancel the task.", e); if (!f.isCancelled()) { f.cancel(false); } runningTasks.remove(f); } } }
public void decodePage( Object decodeKey, int pageNum, final DecodeCallback decodeCallback, float zoom, RectF pageSliceBounds) { final DecodeTask decodeTask = new DecodeTask(pageNum, decodeCallback, zoom, decodeKey, pageSliceBounds); synchronized (decodingFutures) { if (isRecycled) { return; } final Future<?> future = executorService.submit( new Runnable() { public void run() { try { Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1); performDecode(decodeTask); } catch (IOException e) { Log.e(DECODE_SERVICE, "Decode fail", e); } } }); final Future<?> removed = decodingFutures.put(decodeKey, future); if (removed != null) { removed.cancel(false); } } }
@Override public Boolean call() throws Exception { List<Future<Boolean>> futureList = new ArrayList<>(); List<Get> getList = new ArrayList<>(Constant.BATCH_GROUP_SIZE); int count = 0; for (Get get : gets) { getList.add(get); if (count % Constant.BATCH_GROUP_SIZE == 0) { Future<Boolean> f = AsyncThreadPoolFactory.ASYNC_HBASE_THREAD_POOL.submit(new GetTask(getList, result)); futureList.add(f); getList = new ArrayList<>(Constant.BATCH_GROUP_SIZE); } count++; } Future<Boolean> f = AsyncThreadPoolFactory.ASYNC_HBASE_THREAD_POOL.submit(new GetTask(getList, result)); futureList.add(f); boolean isOk = false; for (Future<Boolean> future : futureList) { try { isOk = future.get(500, TimeUnit.MILLISECONDS); } catch (TimeoutException ignored) { } catch (Exception e) { e.printStackTrace(); } finally { if (!isOk) { future.cancel(Boolean.FALSE); } } } return isOk; }
public String execute(Map<String, Object> context, File template, Object... args) { if (null == template) throw new NullPointerException(); Future<Object> f = null; try { f = exec(context, null, null, template, args); Object o = f.get(timeout, TimeUnit.MILLISECONDS); if (o instanceof RuntimeException) throw (RuntimeException) o; if (o instanceof Exchanger) throw new RuntimeException((Exception) o); return (null == o) ? "" : o.toString(); } catch (RuntimeException e) { f.cancel(true); throw e; } catch (Exception e) { f.cancel(true); throw new RuntimeException(e); } }
private void cancel() { synchronized (LOCK) { if (myFuture != null) { myFuture.cancel(false); } myTask = null; } }
private void cancelFutures() { for (Future future : futures) { try { future.cancel(true); } catch (Exception e) { logger.warn("Exception when cancelling future", e); } } }
@Test public void cancel_shouldThrowAnUnsupportedOperationException_givenAFutureReturnedByExecute() throws Exception { Future<?> result = asyncExecutor.execute(() -> {}); try { result.cancel(true); fail("Expected an exception"); } catch (UnsupportedOperationException ignore) { } }
public void doSchedule() { long next_interval = nextInterval(); if (next_interval <= 0 && !rescheduleOnZeroDelay()) { if (log.isTraceEnabled()) log.trace("task will not get rescheduled as interval is " + next_interval); return; } future = schedule(this, next_interval, TimeUnit.MILLISECONDS); if (cancelled) future.cancel(true); }
public void run() { if (cancelled) { if (future != null) future.cancel(true); return; } try { task.run(); } catch (Throwable t) { log.error(Util.getMessage("FailedRunningTask") + task, t); } if (!cancelled) doSchedule(); }
public void run() { if (cancelled) { if (future != null) future.cancel(true); return; } try { task.run(); } catch (Throwable t) { log.error("failed running task " + task, t); } if (!cancelled) doSchedule(); }
/** Closes any tasks currently in progress */ @Override public void close() { isOpen.set(false); Iterator<Future<?>> iterator = futures.iterator(); while (iterator.hasNext()) { Future<?> future = iterator.next(); iterator.remove(); if (!future.isDone() && !future.isCancelled() && !future.cancel(true)) { log.warn("Could not cancel " + future); } } if (shutdownOnClose) { executorService.shutdownNow(); } }
/** * Stops the timer, cancelling all tasks * * @throws InterruptedException if interrupted while waiting for thread to return */ public void stop() { stopRunner(); List<Runnable> remaining_tasks = pool.shutdownNow(); for (Runnable task : remaining_tasks) { if (task instanceof Future) { Future future = (Future) task; future.cancel(true); } } pool.getQueue().clear(); try { pool.awaitTermination(Global.THREADPOOL_SHUTDOWN_WAIT_TIME, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { } }
public static <T> T invokeTask(Callable<T> task, int taskTimeout) { T result = null; ExecutorService executor = Executors.newCachedThreadPool(); Future<T> future = executor.submit(task); try { result = future.get(taskTimeout, TimeUnit.SECONDS); } catch (TimeoutException | InterruptedException | ExecutionException ex) { ex.printStackTrace(); } finally { future.cancel(true); executor.shutdown(); if (!executor.isTerminated()) executor.shutdownNow(); } return result; }
@Override protected long runQueuePass() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); queueProcessor.reset(latch); Future<?> future = executor.submit(queueProcessor); long start = System.currentTimeMillis(); for (long i = 0; i < ITERATIONS; i++) { blockingQueue.put(3L); } latch.await(); long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start); queueProcessor.halt(); future.cancel(true); failIf(expectedResult, 0); return opsPerSecond; }
@Test public void testCancellationAwareTask2() { Callable task1 = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1); executor.submit(task1); Callable task2 = new BasicTestTask(); Future future = executor.submit(task2); assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
public static void main(String[] args) { // 3 ways to do it: // use Thread: Thread t = new Thread(new Worker()); t.start(); t.interrupt(); // use Executor execute: ExecutorService exec = Executors.newSingleThreadExecutor(); exec.execute(new Worker()); exec.shutdownNow(); // use Executor submit: ExecutorService exec2 = Executors.newSingleThreadExecutor(); Future<?> f = exec2.submit(new Worker()); try { TimeUnit.MILLISECONDS.sleep(100); // start task } catch (InterruptedException e) { System.out.println("Sleep interrupted in main()"); } f.cancel(true); exec2.shutdown(); }
@Test public void testEntriesAddition() throws Exception { createSchema(baseDocumentTx); createSchema(testDocumentTx); System.out.println("Start data propagation"); List<Future> futures = new ArrayList<Future>(); for (int i = 0; i < 5; i++) { futures.add(executorService.submit(new DataPropagationTask(baseDocumentTx, testDocumentTx))); } TimeUnit.MINUTES.sleep(5); System.out.println("Wait for process to destroy"); serverProcess.destroy(); serverProcess.waitFor(); System.out.println("Process was destroyed"); for (Future future : futures) { try { future.get(); } catch (Exception e) { future.cancel(true); } } testDocumentTx = new ODatabaseDocumentTx( "plocal:" + buildDir.getAbsolutePath() + "/testUniqueIndexCrashRestore"); testDocumentTx.open("admin", "admin"); testDocumentTx.close(); testDocumentTx.open("admin", "admin"); System.out.println("Start data comparison."); compareIndexes(); }
@Test public void testCancellationAwareTask() throws ExecutionException, InterruptedException { CancellationAwareTask task = new CancellationAwareTask(5000); ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask"); Future future = executor.submit(task); try { future.get(2, TimeUnit.SECONDS); fail("Should throw TimeoutException!"); } catch (TimeoutException expected) { } assertFalse(future.isDone()); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Should not complete the task successfully"); } catch (CancellationException expected) { } catch (Exception e) { fail("Unexpected exception " + e); } }
@Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (r instanceof RunnableScheduledFuture) { Object callable = ReflectionUtil.getFieldValue(r, "callable"); Object cfAsync = ReflectionUtil.getFieldValue(callable, "task"); r = (Runnable) cfAsync; } if (r instanceof ForkJoinTask && r.toString().contains("CompletableFuture$Async")) { Field field = ReflectionUtil.getField(r, "dst"); // jdk8u20 if (field == null) { field = ReflectionUtil.getField(r, "dep"); // jdk8u40 } Object dst = ReflectionUtil.getFieldValue(r, field); if (dst instanceof CompletableFuture) { ((CompletableFuture) dst).cancel(false); log.debug("Canceled completable future {}", r); } } else if (r instanceof Future) { ((Future) r).cancel(false); log.debug("Canceled future {}", r); } }
@Test public void testExecutorServiceStats() throws InterruptedException, ExecutionException { final IExecutorService executorService = createSingleNodeExecutorService("testExecutorServiceStats"); final int k = 10; final CountDownLatch latch = new CountDownLatch(k); final int executionTime = 200; for (int i = 0; i < k; i++) { executorService.execute( new Runnable() { public void run() { try { Thread.sleep(executionTime); } catch (InterruptedException e) { e.printStackTrace(); } latch.countDown(); } }); } latch.await(2, TimeUnit.MINUTES); final Future<Boolean> f = executorService.submit(new CancellationAwareTask(10000)); Thread.sleep(1000); f.cancel(true); try { f.get(); } catch (CancellationException e) { } final LocalExecutorStats stats = executorService.getLocalExecutorStats(); assertEquals(k + 1, stats.getStartedTaskCount()); assertEquals(k, stats.getCompletedTaskCount()); assertEquals(0, stats.getPendingTaskCount()); assertEquals(1, stats.getCancelledTaskCount()); }
@Override public Map<String, Map<String, String>> batch(List<GetItem> getItems) { Map<String, Map<String, String>> result = new ConcurrentHashMap<>(); List<Get> gets = new ArrayList<>(getItems.size()); for (GetItem getItem : getItems) { Get g = new Get(getItem.getRowkey()); g.addFamily(getItem.getFamily()); gets.add(g); } GetGroupTask task = new GetGroupTask(gets, result); Future<Boolean> future = AsyncThreadPoolFactory.ASYNC_HBASE_THREAD_POOL.submit(task); Boolean r = Boolean.FALSE; try { r = future.get(500, TimeUnit.MILLISECONDS); } catch (TimeoutException ignored) { } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } if (!r) { future.cancel(Boolean.FALSE); } return result; }
public void stopDecoding(Object decodeKey) { final Future<?> future = decodingFutures.remove(decodeKey); if (future != null) { future.cancel(false); } }
@Override public long generateEnvManifest(long envId, String userId, Map<String, String> platModes) { long t1 = System.currentTimeMillis(); String oldThreadName = Thread.currentThread().getName(); Thread.currentThread().setName(getProcessingThreadName(oldThreadName, envId)); List<CmsCIRelation> assemblyRels = cmProcessor.getToCIRelations(envId, BASE_REALIZED_IN, null, ACCOUNT_ASSEMBLY); CmsCI assembly = getAssembly(envId, assemblyRels); CmsCI env = getEnv(envId); String nsPath = getManifestNsPath(env); // check for openRelease check4OpenRelease(env, nsPath); Long nsId = trUtil.verifyAndCreateNS(nsPath); logger.info("Created nsId " + nsId); List<CmsCIRelation> designPlatRels = cmProcessor.getFromCIRelations(assembly.getCiId(), null, "ComposedOf", CATALOG_PLATFORM); // we need to reset all pending deletions cis just in case there was one added back cmProcessor.resetDeletionsByNs(nsPath); // check for edge case scenario when there is new design platform with the same name as old one // but different pack long releaseId = checkPlatformPackCompliance(designPlatRels, env, nsPath, userId); if (releaseId > 0) { // stop any processing and return new release id return releaseId; } final CountDownLatch latch = new CountDownLatch(designPlatRels.size()); List<Future<DesignCIManifestRfcTouple>> submittedFutureTasks = new ArrayList<>(); for (CmsCIRelation platRelation : designPlatRels) { String availMode = getPlatformAvailabiltyMode(platModes, platRelation); Future<DesignCIManifestRfcTouple> future = executorService.submit( new ManifestRfcProcessorTask(env, nsPath, userId, availMode, latch, platRelation)); submittedFutureTasks.add(future); } boolean allPlatsProcessed; try { allPlatsProcessed = latch.await( timeoutInMilliSeconds, TimeUnit .MILLISECONDS); // wait for all platform processing threads to finish with timeout // of 10 mins if (!allPlatsProcessed) { logger.error( "All platforms not processed within timeout duration of " + timeoutInMilliSeconds); throw new TransistorException( CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, "Failed to pull latest design for all platform within timeout duration of " + timeoutInMilliSeconds + " millis"); } } catch (InterruptedException ie) { for (Future<DesignCIManifestRfcTouple> job : submittedFutureTasks) { job.cancel(true); } throw new TransistorException( CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, "Design pull process interrupted. "); } Map<Long, CmsRfcCI> design2manifestPlatMap = new HashMap<>(); for (Future<DesignCIManifestRfcTouple> task : submittedFutureTasks) { DesignCIManifestRfcTouple touple; try { touple = task.get(); processPlatformRfcs(touple.manifestPlatformRfcs, userId); CmsRfcCI manifestPlatformRfc = touple.manifestPlatformRfcs.getManifestPlatformRfc(); logger.info( "Finished working on =" + manifestPlatformRfc.getNsPath() + " release id = " + manifestPlatformRfc.getReleaseId()); design2manifestPlatMap.put(touple.designPlatCI, manifestPlatformRfc); } catch (Exception e) { logger.error("Error in pulling latest design for all platforms ", e); throw new TransistorException( CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, "Error in pulling latest design for all platforms "); } } check4MissingServices(design2manifestPlatMap); // now we need to process linkedTo relations manifestRfcProcessor.processLinkedTo(design2manifestPlatMap, nsPath, userId); // now lets delete old existing plats that do not exists in new manifest manifestRfcProcessor.processDeletedPlatforms( design2manifestPlatMap.values(), env, nsPath, userId); // process global variables from design manifestRfcProcessor.processGlobalVars(assembly.getCiId(), env, nsPath, userId); long t2 = System.currentTimeMillis(); long envReleaseId = populateParentRelease(env, nsPath); logger.info( "Pull design for " + nsPath + " completed in " + (t2 - t1) + " millis (releaseId " + envReleaseId + ")"); return envReleaseId; }
private SpecificationTestCasesListener<T> run( final URL[] classpath, final Collection<TestCase> failures, ClassLoader unitTestClassLoader) { final SpecificationTestCasesListener<T> listener = new SpecificationTestCasesListener<>(runtimeValues); // create the classpath for JPF String stringClassPath = createClassPath(classpath); String mainClass = "nopol.repair.NopolTestRunner"; // TestExecutorProcessor.createMainTestClass(spoon, mainClass); List<TestCase> passedTest = new ArrayList<>(failures.size()); Iterator<TestCase> iterator = failures.iterator(); while (iterator.hasNext()) { TestCase testCase = iterator.next(); logger.debug("SYMBOLIC EXECUTION on " + sourceLocation + " Test " + testCase); String[] args = new String[1]; args[0] = testCase.className() + "." + testCase.testName(); Config conf = JPFUtil.createConfig( args, mainClass, stringClassPath, outputSourceFile.getAbsolutePath()); final JPF jpf = new JPF(conf); // executes JPF JPFListener jpfListener = new JPFListener(); jpf.addSearchListener(jpfListener); ExecutorService executor = Executors.newFixedThreadPool(1); Future<?> future = executor.submit( new Runnable() { @Override public void run() { jpf.run(); } }); executor.shutdown(); try { future.get(60, TimeUnit.SECONDS); } catch (InterruptedException e) { continue; } catch (ExecutionException e) { e.printStackTrace(); continue; } catch (TimeoutException e) { future.cancel(true); continue; } // get the JPF result Object result = jpfListener.getResult(); if (result == null) { continue; } logger.debug( "SYMBOLIC VALUE on " + sourceLocation + " for Test " + testCase + " Value: " + result); // collect runtime boolean passed = executeTestAndCollectRuntimeValues(result, testCase, unitTestClassLoader, listener); if (passed) { this.find = true; TestSuiteExecution.runTestCases(failures, unitTestClassLoader, listener); if (!passedTest.contains(testCase)) { passedTest.add(testCase); } if (passedTest.size() == failures.size()) { break; } } } return listener; }
public boolean cancel(boolean mayInterruptIfRunning) { boolean retval = !isDone(); cancelled = true; if (future != null) future.cancel(mayInterruptIfRunning); return retval; }
@Override public long generateEnvManifest(long envId, String userId, Map<String, String> platModes) { long t1 = System.currentTimeMillis(); String oldThreadName = Thread.currentThread().getName(); Thread.currentThread().setName(getProcessingThreadName(oldThreadName, envId)); List<CmsCIRelation> assemblyRels = cmProcessor.getToCIRelations(envId, BASE_REALIZED_IN, null, ACCOUNT_ASSEMBLY); CmsCI assembly = null; if (assemblyRels.size() > 0) { assembly = assemblyRels.get(0).getFromCi(); } else { String error = "Can not get assembly for envId = " + envId; logger.error(error); throw new TransistorException(CmsError.TRANSISTOR_CANNOT_GET_ASSEMBLY, error); } CmsCI env = getEnv(envId); String nsPath = env.getNsPath() + "/" + env.getCiName() + "/manifest"; if (hasOpenManifestRelease(nsPath)) { String message = "This environment has an open release. It needs to be discarded or committed before the design pull: " + env.getNsPath() + "/" + env.getCiName(); logger.info(message); throw new TransistorException(CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, message); } Long nsId = trUtil.verifyAndCreateNS(nsPath); logger.info("Created nsId " + nsId); // Long releaseId = createManifestRelease(nsPath,userId); List<CmsCIRelation> designPlatRels = cmProcessor.getFromCIRelations(assembly.getCiId(), null, "ComposedOf", CATALOG_PLATFORM); // we need to reset all pending deletions cis just in case there was one added back cmProcessor.resetDeletionsByNs(nsPath); // check for edge case scenario when there is new design platform with the same name as old one // but different pack long releaseId = checkPlatformPackCompliance(designPlatRels, env, nsPath, userId); if (releaseId > 0) { // stop any processing and return new release id return releaseId; } final CountDownLatch latch = new CountDownLatch(designPlatRels.size()); List<Future<DesignCIManifestRfcTouple>> submittedFutureTasks = new ArrayList<Future<DesignCIManifestRfcTouple>>(); Map<Long, CmsRfcCI> design2manifestPlatMap = new HashMap<Long, CmsRfcCI>(); for (CmsCIRelation platRelation : designPlatRels) { String availMode = null; if (platModes != null) { availMode = platModes.get(String.valueOf(platRelation.getToCiId())); if (availMode != null && availMode.length() == 0) { availMode = "default"; } } Future<DesignCIManifestRfcTouple> future = executorService.submit( new ManifestRfcProcessorTask(env, nsPath, userId, availMode, latch, platRelation)); submittedFutureTasks.add(future); } boolean allPlatsProcessed = false; try { // latch.await(); //wait till all platform processing threads return allPlatsProcessed = latch.await( timeoutInMilliSeconds, TimeUnit .MILLISECONDS); // wait for all platform processing threads to finish with timeout // of 10 mins if (!allPlatsProcessed) { logger.error( "All platforms not processed within timeout duration of " + timeoutInMilliSeconds); throw new TransistorException( CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, "Failed to pull latest design for all platform within timeout duration of " + timeoutInMilliSeconds + " millis"); } } catch (InterruptedException ie) { for (Future<DesignCIManifestRfcTouple> job : submittedFutureTasks) { job.cancel(true); } throw new TransistorException( CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, "Design pull process interrupted. "); } for (Future<DesignCIManifestRfcTouple> task : submittedFutureTasks) { DesignCIManifestRfcTouple touple; try { touple = task.get(); processPlatformRfcs(touple.manifestPlatformRfcs, userId); CmsRfcCI manifestPlatformRfc = touple.manifestPlatformRfcs.getManifestPlatformRfc(); Set<String> missingSrvs = cloudUtil.getMissingServices(manifestPlatformRfc.getCiId()); if (missingSrvs.size() > 0) { logger.info( ">>>>> Not all services available for platform: " + manifestPlatformRfc.getCiName() + ", the missing services: " + missingSrvs.toString()); disablePlatform(manifestPlatformRfc.getCiId(), userId); } logger.info("New release id = " + manifestPlatformRfc.getReleaseId()); logger.info("Done working on platform " + manifestPlatformRfc.getNsPath()); design2manifestPlatMap.put(touple.designPlatCI, manifestPlatformRfc); } catch (Exception e) { logger.error("Error in pulling latest design for all platforms ", e); throw new TransistorException( CmsError.TRANSISTOR_OPEN_MANIFEST_RELEASE, "Error in pulling latest design for all platforms "); } } // now we need to process linkedTo relations manifestRfcProcessor.processLinkedTo(design2manifestPlatMap, nsPath, userId); // now lets delete old existing plats that do not exists in new manifest manifestRfcProcessor.processDeletedPlatforms( design2manifestPlatMap.values(), env, nsPath, userId); // process global variables from design manifestRfcProcessor.processGlobalVars(assembly.getCiId(), env, nsPath, userId); long t2 = System.currentTimeMillis(); long envReleaseId = populateParentRelease(env, nsPath); logger.info( "Pull design for " + nsPath + " completed in " + (t2 - t1) + " millis (releaseId " + envReleaseId + ")"); return envReleaseId; }
public void doSchedule(long next_interval) { future = schedule(this, next_interval, TimeUnit.MILLISECONDS); if (cancelled) future.cancel(true); }