public DistributedExecutorMassIndexer(AdvancedCache cache, SearchIntegrator searchIntegrator, KeyTransformationHandler keyTransformationHandler, TimeService timeService) { this.cache = cache; this.searchIntegrator = searchIntegrator; this.indexUpdater = new IndexUpdater(searchIntegrator, keyTransformationHandler, timeService); this.executor = new DefaultExecutorService(cache); }
@Override public <T> CompletableFuture<T> execute(String taskName, TaskContext context) { Cache<?, ?> masterCacheNode = context.getCache().get(); DefaultExecutorService des = new DefaultExecutorService(masterCacheNode); try { List<CompletableFuture<T>> tasks = des.submitEverywhere(new DistributedServerTask<>(taskName, context.getParameters())); // noinspection unchecked return (CompletableFuture<T>) CompletableFutures.sequence(tasks); } finally { des.shutdown(); } }
public void testDistributedCallableEverywhere() throws Exception { DefaultExecutorService des = new DefaultExecutorService(c1); DistributedExecutionCompletionService<Boolean> decs = new DistributedExecutionCompletionService<>(des); try { decs.submitEverywhere(new SimpleDistributedCallable(false)); Future<Boolean> f; int counter = 0; while ((f = decs.poll(1,TimeUnit.SECONDS)) != null) { assert f.get(); counter++; } AssertJUnit.assertTrue("Counter greater than 0", counter > 0); } finally { des.shutdownNow(); } }
@Test(expectedExceptions = CacheConfigurationException.class) public void testDistributedExecutor() { new DefaultExecutorService(cache()).submit(() -> null); }
public static void main(String[] args) { // Setup up a clustered cache manager GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); // Make the default cache a distributed one ConfigurationBuilder builder = new ConfigurationBuilder(); builder.clustering().cacheMode(CacheMode.DIST_SYNC); // Initialize the cache manager DefaultCacheManager cacheManager = new DefaultCacheManager(global.build(), builder.build()); // Obtain the default cache Cache<String, String> cache = cacheManager.getCache(); // Create a distributed executor service using the distributed cache to determine the nodes on which to run DefaultExecutorService executorService = new DefaultExecutorService(cache); // Submit a job to all nodes List<Future<Integer>> results = executorService.submitEverywhere((Callable & Serializable) () -> new Random().nextInt()); // Print out the results results.forEach(s -> { try { System.out.printf("%s\n", s.get(100, TimeUnit.MILLISECONDS)); } catch (Exception e) { } }); // Shuts down the cache manager and all associated resources cacheManager.stop(); System.exit(0); }
@TopologyChanged public void topologyChanged(TopologyChangedEvent<Address, ServerAddress> event) { boolean success = false; while (!success && !distributedExecutorService.isShutdown() && addressCache.getStatus().allowInvocations()) { try { List<CompletableFuture<Boolean>> futures = distributedExecutorService.submitEverywhere( new CheckAddressTask(clusterAddress)); // No need for a timeout here, the distributed executor has a default task timeout AtomicBoolean result = new AtomicBoolean(true); futures.forEach(f -> { try { if (!f.get()) { result.set(false); } } catch (InterruptedException | ExecutionException e) { throw new CacheException(e); } }); if (!result.get()) { log.debugf("Re-adding %s to the topology cache", clusterAddress); addressCache.putAsync(clusterAddress, address); } success = true; } catch (Throwable e) { log.debug("Error re-adding address to topology cache, retrying", e); } } } }
@Override public void stop() { if (removeCacheListener != null) { SecurityActions.removeListener(cacheManager, removeCacheListener); } if (viewChangeListener != null) { SecurityActions.removeListener(cacheManager, viewChangeListener); } if (topologyChangeListener != null) { SecurityActions.removeListener(addressCache, topologyChangeListener); } if (Configurations.isClustered(cacheManager.getCacheManagerConfiguration())) { InternalCacheRegistry internalCacheRegistry = cacheManager.getGlobalComponentRegistry().getComponent(InternalCacheRegistry.class); if (internalCacheRegistry != null) internalCacheRegistry.unregisterInternalCache(configuration.topologyCacheName()); } if (distributedExecutorService != null) { distributedExecutorService.shutdownNow(); } if (clientListenerRegistry != null) clientListenerRegistry.stop(); if (clientCounterNotificationManager != null) clientCounterNotificationManager.stop(); super.stop(); }
public DistributedExecutorMassIndexer(AdvancedCache cache, SearchIntegrator searchIntegrator) { this.cache = cache; this.searchIntegrator = searchIntegrator; this.indexUpdater = new IndexUpdater(cache); this.executor = new DefaultExecutorService(cache); }
@Override public <T> CompletableFuture<T> runScript(ScriptingManagerImpl scriptManager, ScriptMetadata metadata, CacheScriptBindings binding) { Cache<?, ?> masterCacheNode = (Cache<?, ?>) binding.get(SystemBindings.CACHE.toString()); if (masterCacheNode == null) { throw log.distributedTaskNeedCacheInBinding(metadata.name()); } DefaultExecutorService des = new DefaultExecutorService(masterCacheNode); try { Map<String, Object> ctxParams = extractContextParams(metadata, binding); List<CompletableFuture<T>> tasks = des.submitEverywhere(new DistributedScript<T>(metadata, ctxParams)); return (CompletableFuture<T>) CompletableFutures.sequence(tasks); } finally { des.shutdown(); } }
public void testDistributedCallableEverywhereWithKeys() throws Exception { c1.put("key1", "Manik"); c1.put("key2", "Mircea"); c1.put("key3", "Galder"); c1.put("key4", "Sanne"); DefaultExecutorService des = new DefaultExecutorService(c1); DistributedExecutionCompletionService<Boolean> decs = new DistributedExecutionCompletionService<>(des); try { decs.submitEverywhere(new SimpleDistributedCallable(true), "key1", "key2"); Future<Boolean> f; int counter = 0; while ((f = decs.poll(1,TimeUnit.SECONDS)) != null) { assert f.get(); counter++; } AssertJUnit.assertTrue("Counter greater than 0", counter > 0); } finally { des.shutdownNow(); } }
protected DistributedExecutorService createDES(Cache<?,?> cache){ ExecutorService executorService = Executors.newCachedThreadPool(getTestThreadFactory("LocalExecutor")); DistributedExecutorService des = new DefaultExecutorService(cache, executorService, true); cleanupService = des; return des; }
public void testImproperMasterCacheForDistributedExecutor() { expectException(IllegalArgumentException.class, () -> new DefaultExecutorService(null)); }
@Override public void run() { try { Thread.sleep(random.nextInt(MAX_INTERVAL_BETWEEN_TASK)); //random sleep a while before really submit the task to ISPN } catch (InterruptedException ex) { } TestKey key = new TestKey(random.nextInt(KEY_RANGE)); try { log.info("Submitting a task " + key); EmbeddedCacheManager cacheManager = cacheManagers.get(random.nextInt(cacheManagers.size())); DistributedExecutorService ispnExecutor = new DefaultExecutorService(cacheManager.getCache("serviceGroup")); Future<String> z = ispnExecutor.submit(new TransactionTask(), key); log.info("Task result=" + z.get()); } catch (Exception ex) { log.warn("error during executing task " + key, ex); } } }
Transport transport = cache.getCacheManager().getTransport(); boolean distributed = transport != null; ExecutorService executorService = distributed ? new DefaultExecutorService(cache, localExecutor) : localExecutor;
Transport transport = workCache.getCacheManager().getTransport(); boolean distributed = transport != null; ExecutorService executorService = distributed ? new DefaultExecutorService(workCache, localExecutor) : localExecutor;
public void testBasicInvocation() throws Exception { DistributedExecutorService des = new DefaultExecutorService(c1); try { DistributedExecutionCompletionService<Integer> decs = new DistributedExecutionCompletionService<>(des); decs.submit(new SimpleCallable()); CompletableFuture<Integer> future = decs.take(); Integer r = future.get(); AssertJUnit.assertEquals(1, r.intValue()); } finally { des.shutdownNow(); } }
public void testBasicInvocationWithBlockingQueue() throws Exception { DistributedExecutorService des = new DefaultExecutorService(c1); try { BlockingQueue<CompletableFuture<Integer>> queue = new ArrayBlockingQueue<>(10); DistributedExecutionCompletionService<Integer> decs = new DistributedExecutionCompletionService<>(des, queue); decs.submit(new SimpleCallable()); CompletableFuture<Integer> future = decs.take(); Integer r = future.get(); AssertJUnit.assertEquals((Integer) 1, r); } finally { des.shutdownNow(); } }
public void testImproperLocalExecutorServiceForDistributedExecutor() { EmbeddedCacheManager cacheManager = TestCacheManagerFactory.createCacheManager(false); try { Cache<Object, Object> cache = cacheManager.getCache(); expectException(IllegalArgumentException.class, () -> new DefaultExecutorService(cache, null)); } finally { TestingUtil.killCacheManagers(cacheManager); } }
public void testBasicTakeInvocationWithSleepingCallable() throws Exception { long sleepTime = 2000; DistributedExecutorService des = new DefaultExecutorService(c1); DistributedExecutionCompletionService<Integer> decs = new DistributedExecutionCompletionService<>(des); try { long start = System.currentTimeMillis(); decs.submit(new SimpleCallable(true, sleepTime)); CompletableFuture<Integer> future = decs.take(); long end = System.currentTimeMillis(); AssertJUnit.assertTrue("take() returned too soon", (end - start) >= sleepTime); AssertJUnit.assertTrue("take() returned, but future is not done yet", future.isDone()); AssertJUnit.assertEquals((Integer) 1, future.get()); } finally { des.shutdownNow(); } }
public void testBasicInvocationWithNullTask() throws Exception { DistributedExecutorService des = new DefaultExecutorService(c1); try { DistributedExecutionCompletionService<Integer> decs = new DistributedExecutionCompletionService<>(des); expectException(NullPointerException.class, () -> decs.submit(null)); } finally { des.shutdownNow(); } }