Search in sources :

Example 1 with BaseAsyncInterceptor

use of org.infinispan.interceptors.BaseAsyncInterceptor 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 BaseAsyncInterceptor

use of org.infinispan.interceptors.BaseAsyncInterceptor 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 3 with BaseAsyncInterceptor

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

the class PutForExternalReadTest method testKeyOnlyWrittenOnceOnOriginator.

// This test executes PFER on cache1, and expects that it will be relayed to cache2 == primary
// and then sent to cache1 again for backup. In scattered cache there's only one RPC.
@InCacheMode({ CacheMode.DIST_SYNC, CacheMode.REPL_SYNC })
public void testKeyOnlyWrittenOnceOnOriginator() throws Exception {
    final Cache<MagicKey, String> cache1 = cache(0, CACHE_NAME);
    final Cache<MagicKey, String> cache2 = cache(1, CACHE_NAME);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    cache1.getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            if (command instanceof PutKeyValueCommand) {
                if (!ctx.isOriginLocal()) {
                    // wait first before the check
                    TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
                    // and once more after the check
                    TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
                }
            }
            return invokeNext(ctx, command);
        }
    }, 0);
    final MagicKey myKey = new MagicKey(cache2);
    cache1.putForExternalRead(myKey, value);
    // Verify that the key was not written on the origin by the time it was looped back
    barrier.await(10, TimeUnit.SECONDS);
    assertNull(cache1.get(myKey));
    // Verify that the key is written on the origin afterwards
    barrier.await(10, TimeUnit.SECONDS);
    eventually(() -> value.equals(cache1.get(myKey)) && value.equals(cache2.get(myKey)));
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) InvocationContext(org.infinispan.context.InvocationContext) MagicKey(org.infinispan.distribution.MagicKey) CyclicBarrier(java.util.concurrent.CyclicBarrier) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) InCacheMode(org.infinispan.test.fwk.InCacheMode)

Example 4 with BaseAsyncInterceptor

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

the class AsyncInterceptorChainInvocationTest method testCompletedStage.

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

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return "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) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) InvocationContext(org.infinispan.context.InvocationContext)

Example 5 with BaseAsyncInterceptor

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

the class AsyncInterceptorChainInvocationTest method testInvokeNextAsync.

public void testInvokeNextAsync() throws Exception {
    CompletableFuture<Object> f = new CompletableFuture<>();
    AsyncInterceptorChain chain = newInterceptorChain(new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return asyncInvokeNext(ctx, command, f);
        }
    }, new BaseAsyncInterceptor() {

        @Override
        public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
            return "v1";
        }
    });
    InvocationContext context = newInvocationContext();
    CompletableFuture<Object> invokeFuture = chain.invokeAsync(context, testCommand);
    assertFalse(invokeFuture.isDone());
    f.complete("v");
    assertEquals("v1", invokeFuture.get(10, SECONDS));
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) CompletableFuture(java.util.concurrent.CompletableFuture) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) SingleKeyNonTxInvocationContext(org.infinispan.context.impl.SingleKeyNonTxInvocationContext) InvocationContext(org.infinispan.context.InvocationContext)

Aggregations

VisitableCommand (org.infinispan.commands.VisitableCommand)11 InvocationContext (org.infinispan.context.InvocationContext)11 BaseAsyncInterceptor (org.infinispan.interceptors.BaseAsyncInterceptor)11 SingleKeyNonTxInvocationContext (org.infinispan.context.impl.SingleKeyNonTxInvocationContext)10 AsyncInterceptorChain (org.infinispan.interceptors.AsyncInterceptorChain)10 CompletableFuture (java.util.concurrent.CompletableFuture)8 LockControlCommand (org.infinispan.commands.control.LockControlCommand)7 ExecutionException (java.util.concurrent.ExecutionException)5 Future (java.util.concurrent.Future)5 SECONDS (java.util.concurrent.TimeUnit.SECONDS)5 TimeoutException (java.util.concurrent.TimeoutException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Pattern (java.util.regex.Pattern)5 GetKeyValueCommand (org.infinispan.commands.read.GetKeyValueCommand)5 Exceptions.expectExecutionException (org.infinispan.commons.test.Exceptions.expectExecutionException)5 AsyncInterceptor (org.infinispan.interceptors.AsyncInterceptor)5 InvocationSuccessFunction (org.infinispan.interceptors.InvocationSuccessFunction)5 AbstractInfinispanTest (org.infinispan.test.AbstractInfinispanTest)5 TestException (org.infinispan.test.TestException)5 CompletableFutures (org.infinispan.util.concurrent.CompletableFutures)5