private Object execute(String storedProcedureName, EmbeddedCacheManager embeddedCacheManager, Callable<?> callable) { AtomicReference<Object> ref = new AtomicReference<>(); try { return embeddedCacheManager.executor() .submitConsumer( ecm -> execute( storedProcedureName, callable ), ( a, r, e ) -> { if ( e != null ) { if ( e instanceof HibernateException ) { throw (HibernateException) e; } throw log.cannotExecuteStoredProcedure( storedProcedureName, e ); } ref.compareAndSet( null, r ); } ) .thenCompose( v -> CompletableFuture.supplyAsync( ref::get ) ) .get(); } catch ( Exception e ) { throw log.cannotExecuteStoredProcedure( storedProcedureName, e ); } }
@Test(dataProvider = "params") public void runTest(ClusterExecutionPolicy policy, int site, int rack, int machine, int invocationCount) throws InterruptedException, ExecutionException, TimeoutException { actualInvocations.set(0); EmbeddedCacheManager cacheManager = cacheManagers.stream().filter(cm -> { TransportConfiguration tc = cm.getCacheManagerConfiguration().transport(); return Integer.valueOf(tc.siteId()) == site && Integer.valueOf(tc.rackId()) == rack && Integer.valueOf(tc.machineId()) == machine; }).findFirst().orElseThrow(() -> new AssertionError("No cache manager matches site: " + site + " rack: " + rack + " machine: " + machine)); cacheManager.executor().filterTargets(policy).submit(() -> actualInvocations.incrementAndGet()).get(10, TimeUnit.SECONDS); assertEquals(invocationCount, actualInvocations.get()); } }
@Test public void testTimeoutOccursWithRetry() throws InterruptedException, ExecutionException, TimeoutException { CompletableFuture<Void> fut = cacheManagers.get(0).executor().timeout(10, TimeUnit.MILLISECONDS) .singleNodeSubmission(2).submit(() -> { try { Thread.sleep(TimeUnit.SECONDS.toMillis(2)); } catch (InterruptedException e) { throw new RuntimeException(e); } }); Exceptions.expectExecutionException(org.infinispan.util.concurrent.TimeoutException.class, fut); } }
@Test public void testSimpleFailover() throws InterruptedException, ExecutionException, TimeoutException { int failOverAllowed = 2; failureCount.set(failOverAllowed); CompletableFuture<Void> fut = cacheManagers.get(0).executor().singleNodeSubmission(failOverAllowed).submit(() -> { if (failureCount.decrementAndGet() != 0) { throw new IllegalArgumentException(); } }); fut.get(10, TimeUnit.SECONDS); assertEquals(0, failureCount.get()); }
ClusterExecutor executor(EmbeddedCacheManager cm) { return cm.executor().singleNodeSubmission().filterTargets(a -> local == a.equals(cm.getAddress())); }
ClusterExecutor executor = cacheManager.executor().singleNodeSubmission().filterTargets(policy); for (int i = 0; i < invocations; ++i) { executor.submitConsumer((cm) -> { TransportConfiguration tc = cm.getCacheManagerConfiguration().transport(); return tc.siteId() + tc.rackId() + tc.machineId();
executor(cm1).timeout(crazyNumber, unit).submitConsumer(m -> { ArgumentCaptor<Callable> argument = ArgumentCaptor.forClass(Callable.class);
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; CompletableFuture<Void> future = executor(cm1).filterTargets(cm1.getAddress()::equals).submitConsumer(m -> null, (a, i, t) -> { throw new NullPointerException(); }); Exchanger<Throwable> exchanger = new Exchanger<>(); future.whenCompleteAsync((v, t) -> { try { exchanger.exchange(t, 10, TimeUnit.SECONDS); } catch (InterruptedException | TimeoutException e) { throw new RuntimeException(e); } }); Throwable t = exchanger.exchange(null, 10, TimeUnit.SECONDS); assertNotNull(t); assertEquals(NullPointerException.class, t.getClass()); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; CompletableFuture<Void> future = executor(cm1).timeout(10, TimeUnit.MILLISECONDS).submit(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { fail("Unexpected interrupt: " + e); } }); // This fails when local node is invoked since timeout is not adhered to Exceptions .expectExecutionException(org.infinispan.util.concurrent.TimeoutException.class, future); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; atomicIntegerSupplier.get().set(0); SerializableSupplier<AtomicInteger> supplier = atomicIntegerSupplier; executor(cm1).submit(() -> supplier.get().getAndIncrement()).get(10, TimeUnit.SECONDS); assertSize(cms, atomicIntegerSupplier.get().get()); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; atomicIntegerSupplier.get().set(0); executor(cm1).execute(() -> atomicIntegerSupplier.get().getAndIncrement()); eventuallyEquals(1, () -> atomicIntegerSupplier.get().get()); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; atomicIntegerSupplier.get().set(0); SerializableSupplier<AtomicInteger> supplier = atomicIntegerSupplier; executor(cm1).submit(() -> supplier.get().getAndIncrement()).get(10, TimeUnit.SECONDS); assertSize(cms, atomicIntegerSupplier.get().get()); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; atomicIntegerSupplier.get().set(0); SerializableSupplier<AtomicInteger> supplier = atomicIntegerSupplier; executor(cm1).execute(() -> supplier.get().getAndIncrement()); eventuallyAssertSize(cms, () -> atomicIntegerSupplier.get().get()); } });
public static void main(String[] args) { // Setup up a clustered cache manager GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); // Initialize the cache manager DefaultCacheManager cacheManager = new DefaultCacheManager(global.build()); ClusterExecutor clusterExecutor = cacheManager.executor(); clusterExecutor.submitConsumer(cm -> new Random().nextInt(), (address, intValue, exception) -> System.out.printf("%s\n", intValue)); // Shuts down the cache manager and all associated resources cacheManager.stop(); }
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; atomicIntegerSupplier.get().set(0); SerializableSupplier<AtomicInteger> supplier = atomicIntegerSupplier; executor(cm1).filterTargets(a -> a.equals(cm1.getAddress())) .submit(() -> supplier.get().getAndIncrement()).get(10, TimeUnit.SECONDS); assertEquals(1, atomicIntegerSupplier.get().get()); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; atomicIntegerSupplier.get().set(0); SerializableSupplier<AtomicInteger> supplier = atomicIntegerSupplier; executor(cm1).submit(() -> supplier.get().getAndIncrement()).get(10, TimeUnit.SECONDS); assertSize(cms, atomicIntegerSupplier.get().get()); } });
private Object execute(String storedProcedureName, EmbeddedCacheManager embeddedCacheManager, Callable<?> callable) { AtomicReference<Object> ref = new AtomicReference<>(); try { return embeddedCacheManager.executor() .submitConsumer( ecm -> execute( storedProcedureName, callable ), ( a, r, e ) -> { if ( e != null ) { if ( e instanceof HibernateException ) { throw (HibernateException) e; } throw log.cannotExecuteStoredProcedure( storedProcedureName, e ); } ref.compareAndSet( null, r ); } ) .thenCompose( v -> CompletableFuture.supplyAsync( ref::get ) ) .get(); } catch ( Exception e ) { throw log.cannotExecuteStoredProcedure( storedProcedureName, e ); } }
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; EmbeddedCacheManager cm2 = cms[1]; atomicIntegerSupplier.get().set(0); SerializableSupplier<AtomicInteger> supplier = atomicIntegerSupplier; executor(cm1).filterTargets(Collections.singleton(cm2.getAddress())) .submit(() -> supplier.get().getAndIncrement()).get(10, TimeUnit.SECONDS); assertEquals(1, atomicIntegerSupplier.get().get()); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; CompletableFuture<Void> future = executor(cm1).submit(() -> { throw new TestException(); }); Exceptions.expectExecutionException(TestException.class, future); } });
@Override public void call() throws InterruptedException, ExecutionException, TimeoutException { EmbeddedCacheManager cm1 = cms[0]; CompletableFuture<Void> future = executor(cm1).submitConsumer(m -> null, (a, i, t) -> { throw new NullPointerException(); }); Exceptions.expectExecutionException(NullPointerException.class, future); } });