Search in sources :

Example 1 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class AsyncInterceptorChainInvocationTest method testComposeSync.

public void testComposeSync() {
    AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return invokeNextAndHandle(ctx, command, (rCtx, rCommand, rv, t) -> "v1");
        }
    }, new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return "v2";
        }
    });
    InvocationContext context = newInvocationContext();
    Object returnValue = chain.invoke(context, testCommand);
    assertEquals("v1", returnValue);
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) TimeoutException(java.util.concurrent.TimeoutException) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) InvocationContext(org.infinispan.context.InvocationContext) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TestException(org.infinispan.test.TestException) Exceptions.expectExecutionException(org.infinispan.commons.test.Exceptions.expectExecutionException) VisitableCommand(org.infinispan.commands.VisitableCommand) Pattern(java.util.regex.Pattern) LockControlCommand(org.infinispan.commands.control.LockControlCommand) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) InvocationContext(org.infinispan.context.InvocationContext)

Example 2 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class AsyncInterceptorChainInvocationTest method testAsyncInvocationManyHandlersSyncException.

public void testAsyncInvocationManyHandlersSyncException() throws Exception {
    sideEffects.set("");
    CompletableFuture<Object> f = CompletableFutures.completedExceptionFuture(new TestException(""));
    AsyncInterceptorChain chain = makeChainWithManyHandlers(f);
    CompletableFuture<Object> invokeFuture = chain.invokeAsync(newInvocationContext(), testCommand);
    assertExceptionHandlers(invokeFuture);
}
Also used : TestException(org.infinispan.test.TestException) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain)

Example 3 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class AsyncInterceptorChainInvocationTest method testAsyncStageComposeAsyncStage.

public void testAsyncStageComposeAsyncStage() throws Exception {
    CompletableFuture<Object> f1 = new CompletableFuture<>();
    CompletableFuture<Object> f2 = new CompletableFuture<>();
    CompletableFuture<Object> f3 = new CompletableFuture<>();
    AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return invokeNextAndHandle(ctx, command, (rCtx, rCommand, rv, t) -> {
                InvocationSuccessFunction function = (rCtx1, rCommand1, rv1) -> asyncValue(f3);
                return asyncValue(f2).addCallback(rCtx, rCommand, function);
            });
        }
    }, new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return asyncValue(f1);
        }
    });
    InvocationContext context = newInvocationContext();
    CompletableFuture<Object> invokeFuture = chain.invokeAsync(context, testCommand);
    assertFalse(invokeFuture.isDone());
    f1.complete("v1");
    assertFalse(invokeFuture.isDone());
    f2.complete("v2");
    assertFalse(invokeFuture.isDone());
    f3.complete("v3");
    assertEquals("v3", invokeFuture.get(10, SECONDS));
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) TimeoutException(java.util.concurrent.TimeoutException) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) InvocationContext(org.infinispan.context.InvocationContext) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TestException(org.infinispan.test.TestException) Exceptions.expectExecutionException(org.infinispan.commons.test.Exceptions.expectExecutionException) VisitableCommand(org.infinispan.commands.VisitableCommand) Pattern(java.util.regex.Pattern) LockControlCommand(org.infinispan.commands.control.LockControlCommand) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) CompletableFuture(java.util.concurrent.CompletableFuture) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) InvocationContext(org.infinispan.context.InvocationContext)

Example 4 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class VersionedTest method testCollectionUpdate.

@Test
public void testCollectionUpdate() throws Exception {
    // the first insert puts VersionedEntry(null, null, timestamp), so we have to wait a while to cache the entry
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        Item item = s.load(Item.class, itemId);
        OtherItem otherItem = new OtherItem();
        otherItem.setName("Other 1");
        s.persist(otherItem);
        item.addOtherItem(otherItem);
    });
    withTxSession(s -> {
        Item item = s.load(Item.class, itemId);
        Set<OtherItem> otherItems = item.getOtherItems();
        assertFalse(otherItems.isEmpty());
        otherItems.remove(otherItems.iterator().next());
    });
    AdvancedCache collectionCache = TEST_SESSION_ACCESS.getRegion(sessionFactory(), Item.class.getName() + ".otherItems").getCache();
    CountDownLatch putFromLoadLatch = new CountDownLatch(1);
    AtomicBoolean committing = new AtomicBoolean(false);
    CollectionUpdateTestInterceptor collectionUpdateTestInterceptor = new CollectionUpdateTestInterceptor(putFromLoadLatch);
    AnotherCollectionUpdateTestInterceptor anotherInterceptor = new AnotherCollectionUpdateTestInterceptor(putFromLoadLatch, committing);
    AsyncInterceptorChain interceptorChain = collectionCache.getAsyncInterceptorChain();
    interceptorChain.addInterceptorBefore(collectionUpdateTestInterceptor, CallInterceptor.class);
    interceptorChain.addInterceptor(anotherInterceptor, 0);
    TIME_SERVICE.advance(1);
    Future<Boolean> addFuture = executor.submit(() -> withTxSessionApply(s -> {
        awaitOrThrow(collectionUpdateTestInterceptor.updateLatch);
        Item item = s.load(Item.class, itemId);
        OtherItem otherItem = new OtherItem();
        otherItem.setName("Other 2");
        s.persist(otherItem);
        item.addOtherItem(otherItem);
        committing.set(true);
        return true;
    }));
    Future<Boolean> readFuture = executor.submit(() -> withTxSessionApply(s -> {
        Item item = s.load(Item.class, itemId);
        assertTrue(item.getOtherItems().isEmpty());
        return true;
    }));
    addFuture.get();
    readFuture.get();
    interceptorChain.removeInterceptor(CollectionUpdateTestInterceptor.class);
    interceptorChain.removeInterceptor(AnotherCollectionUpdateTestInterceptor.class);
    withTxSession(s -> assertFalse(s.load(Item.class, itemId).getOtherItems().isEmpty()));
}
Also used : Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Session(org.hibernate.Session) Caches(org.infinispan.hibernate.cache.commons.util.Caches) AtomicReference(java.util.concurrent.atomic.AtomicReference) FlagBitSets(org.infinispan.context.impl.FlagBitSets) Future(java.util.concurrent.Future) PessimisticLockException(org.hibernate.PessimisticLockException) InvocationContext(org.infinispan.context.InvocationContext) VersionedEntry(org.infinispan.hibernate.cache.commons.util.VersionedEntry) AdvancedCache(org.infinispan.AdvancedCache) CallInterceptor(org.infinispan.interceptors.impl.CallInterceptor) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) OtherItem(org.infinispan.test.hibernate.cache.commons.functional.entities.OtherItem) DDAsyncInterceptor(org.infinispan.interceptors.DDAsyncInterceptor) Synchronization(javax.transaction.Synchronization) StaleStateException(org.hibernate.StaleStateException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Item(org.infinispan.test.hibernate.cache.commons.functional.entities.Item) SessionAccess(org.infinispan.hibernate.cache.commons.access.SessionAccess) ByRef(org.infinispan.commons.util.ByRef) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) Assert.assertFalse(org.junit.Assert.assertFalse) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) Assert.assertEquals(org.junit.Assert.assertEquals) OtherItem(org.infinispan.test.hibernate.cache.commons.functional.entities.OtherItem) Item(org.infinispan.test.hibernate.cache.commons.functional.entities.Item) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OtherItem(org.infinispan.test.hibernate.cache.commons.functional.entities.OtherItem) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AdvancedCache(org.infinispan.AdvancedCache) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 5 with AsyncInterceptorChain

use of org.infinispan.interceptors.AsyncInterceptorChain in project infinispan by infinispan.

the class BackupWriteCommand method invokeAsync.

@Override
public final CompletionStage<?> invokeAsync(ComponentRegistry componentRegistry) {
    WriteCommand command = createWriteCommand();
    if (command == null) {
        // No-op command
        return CompletableFutures.completedNull();
    }
    command.init(componentRegistry);
    command.setFlagsBitSet(flags);
    // Mark the command as a backup write and skip locking
    command.addFlags(FlagBitSets.SKIP_LOCKING | FlagBitSets.BACKUP_WRITE);
    command.setValueMatcher(MATCH_ALWAYS);
    command.setTopologyId(topologyId);
    InvocationContextFactory invocationContextFactory = componentRegistry.getInvocationContextFactory().running();
    InvocationContext invocationContext = invocationContextFactory.createRemoteInvocationContextForCommand(command, getOrigin());
    AsyncInterceptorChain interceptorChain = componentRegistry.getInterceptorChain().running();
    return interceptorChain.invokeAsync(invocationContext, command);
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) InvocationContext(org.infinispan.context.InvocationContext) InvocationContextFactory(org.infinispan.context.InvocationContextFactory)

Aggregations

AsyncInterceptorChain (org.infinispan.interceptors.AsyncInterceptorChain)53 InvocationContext (org.infinispan.context.InvocationContext)15 CompletableFuture (java.util.concurrent.CompletableFuture)13 VisitableCommand (org.infinispan.commands.VisitableCommand)9 AsyncInterceptor (org.infinispan.interceptors.AsyncInterceptor)9 Future (java.util.concurrent.Future)8 SingleKeyNonTxInvocationContext (org.infinispan.context.impl.SingleKeyNonTxInvocationContext)8 BaseAsyncInterceptor (org.infinispan.interceptors.BaseAsyncInterceptor)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestException (org.infinispan.test.TestException)7 CompletableFutures (org.infinispan.util.concurrent.CompletableFutures)7 SECONDS (java.util.concurrent.TimeUnit.SECONDS)6 ExecutionException (java.util.concurrent.ExecutionException)5 TimeoutException (java.util.concurrent.TimeoutException)5 LockControlCommand (org.infinispan.commands.control.LockControlCommand)5 Test (org.testng.annotations.Test)5 BasicComponentRegistry (org.infinispan.factories.impl.BasicComponentRegistry)4 EntryWrappingInterceptor (org.infinispan.interceptors.impl.EntryWrappingInterceptor)4 AbstractInfinispanTest (org.infinispan.test.AbstractInfinispanTest)4 AssertJUnit.assertEquals (org.testng.AssertJUnit.assertEquals)4