Search in sources :

Example 1 with AsyncInterceptor

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

the class AbstractPartialUpdateTest method addFailureInducingInterceptor.

private AsyncInterceptor addFailureInducingInterceptor(AdvancedCache<?, ?> cache) {
    final AsyncInterceptor interceptor = getFailureInducingInterceptor();
    cache.getAsyncInterceptorChain().addInterceptor(interceptor, FIRST.ordinal());
    log.trace("Injecting FailureInducingInterceptor into " + cache.getName());
    return interceptor;
}
Also used : AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor)

Example 2 with AsyncInterceptor

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

the class AbstractPartialUpdateTest method testPartialUpdate.

@Test
public void testPartialUpdate() throws Exception {
    final AsyncInterceptor failureInterceptor = addFailureInducingInterceptor(remoteCustomerCache);
    try {
        // Remote update latch
        CountDownLatch remoteLatch = new CountDownLatch(2);
        ExpectingInterceptor.get(remoteCustomerCache).when((ctx, cmd) -> cmd instanceof ReadWriteKeyCommand).countDown(remoteLatch);
        try {
            Statistics statsNode0 = getStatistics(localFactory);
            withTxSession(localFactory, s -> {
                Customer customer = new Customer();
                customer.setName("JBoss");
                s.persist(customer);
            });
            assertEquals(1, statsNode0.getSecondLevelCachePutCount());
            assertEquals(0, statsNode0.getSecondLevelCacheMissCount());
            assertEquals(0, statsNode0.getSecondLevelCacheHitCount());
            // Wait for value to be applied remotely
            assertTrue(remoteLatch.await(2, TimeUnit.SECONDS));
        } finally {
            ExpectingInterceptor.cleanup(remoteCustomerCache);
        }
        Statistics statsNode1 = getStatistics(remoteFactory);
        withSession(remoteFactory.withOptions(), s -> {
            Customer customer = s.load(Customer.class, 1);
            assertEquals("JBoss", customer.getName());
        });
        assertEquals(0, statsNode1.getSecondLevelCachePutCount());
        assertEquals(0, statsNode1.getSecondLevelCacheMissCount());
        assertEquals(1, statsNode1.getSecondLevelCacheHitCount());
        final boolean updated = doUpdate();
        if (updated) {
            withSession(localFactory.withOptions(), s -> {
                Customer customer = s.load(Customer.class, 1);
                assertEquals("JBoss, a division of Red Hat", customer.getName());
            });
            withSession(remoteFactory.withOptions(), s -> {
                Customer customer = s.load(Customer.class, 1);
                assertEquals("JBoss, a division of Red Hat", customer.getName());
            });
        }
    } finally {
        remoteCustomerCache.getAsyncInterceptorChain().removeInterceptor(failureInterceptor.getClass());
    }
}
Also used : AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) FIRST(org.infinispan.configuration.cache.InterceptorConfiguration.Position.FIRST) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) InfinispanMessageLogger(org.infinispan.hibernate.cache.commons.util.InfinispanMessageLogger) TxUtil.withSession(org.infinispan.test.hibernate.cache.commons.util.TxUtil.withSession) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AdvancedCache(org.infinispan.AdvancedCache) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) ExpectingInterceptor(org.infinispan.test.hibernate.cache.commons.util.ExpectingInterceptor) Customer(org.infinispan.test.hibernate.cache.commons.functional.entities.Customer) Statistics(org.hibernate.stat.Statistics) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) Customer(org.infinispan.test.hibernate.cache.commons.functional.entities.Customer) CountDownLatch(java.util.concurrent.CountDownLatch) Statistics(org.hibernate.stat.Statistics) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) Test(org.junit.Test)

Example 3 with AsyncInterceptor

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

the class PersistenceManagerImpl method disableStore.

@Override
public CompletionStage<Void> disableStore(String storeType) {
    boolean stillHasAStore = false;
    AggregateCompletionStage<Void> aggregateCompletionStage = CompletionStages.aggregateCompletionStage();
    long stamp = lock.writeLock();
    try {
        if (!checkStoreAvailability()) {
            return CompletableFutures.completedNull();
        }
        boolean allAvailable = true;
        Iterator<StoreStatus> statusIterator = stores.iterator();
        while (statusIterator.hasNext()) {
            StoreStatus status = statusIterator.next();
            NonBlockingStore<?, ?> nonBlockingStore = unwrapStore(status.store());
            if (nonBlockingStore.getClass().getName().equals(storeType) || containedInAdapter(nonBlockingStore, storeType)) {
                statusIterator.remove();
                aggregateCompletionStage.dependsOn(nonBlockingStore.stop().whenComplete((v, t) -> {
                    if (t != null) {
                        log.warn("There was an error stopping the store", t);
                    }
                }));
            } else {
                stillHasAStore = true;
                allAvailable = allAvailable && status.availability;
            }
        }
        if (!stillHasAStore) {
            unavailableExceptionMessage = null;
            enabled = false;
            stopAvailabilityTask();
        } else if (allAvailable) {
            unavailableExceptionMessage = null;
        }
        allSegmentedOrShared = allStoresSegmentedOrShared();
        listeners.forEach(l -> l.storeChanged(createStatus()));
        if (!stillHasAStore) {
            AsyncInterceptorChain chain = cache.wired().getAsyncInterceptorChain();
            AsyncInterceptor loaderInterceptor = chain.findInterceptorExtending(CacheLoaderInterceptor.class);
            if (loaderInterceptor == null) {
                PERSISTENCE.persistenceWithoutCacheLoaderInterceptor();
            } else {
                chain.removeInterceptor(loaderInterceptor.getClass());
            }
            AsyncInterceptor writerInterceptor = chain.findInterceptorExtending(CacheWriterInterceptor.class);
            if (writerInterceptor == null) {
                writerInterceptor = chain.findInterceptorWithClass(TransactionalStoreInterceptor.class);
                if (writerInterceptor == null) {
                    PERSISTENCE.persistenceWithoutCacheWriteInterceptor();
                } else {
                    chain.removeInterceptor(writerInterceptor.getClass());
                }
            } else {
                chain.removeInterceptor(writerInterceptor.getClass());
            }
        }
        return aggregateCompletionStage.freeze();
    } finally {
        lock.unlockWrite(stamp);
    }
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) IntSets(org.infinispan.commons.util.IntSets) ComponentName(org.infinispan.factories.annotations.ComponentName) LogFactory(org.infinispan.util.logging.LogFactory) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) KnownComponentNames(org.infinispan.factories.KnownComponentNames) CONFIG(org.infinispan.util.logging.Log.CONFIG) Stop(org.infinispan.factories.annotations.Stop) StoreUnavailableException(org.infinispan.persistence.spi.StoreUnavailableException) Collections.singletonList(java.util.Collections.singletonList) Scopes(org.infinispan.factories.scopes.Scopes) InvocationContext(org.infinispan.context.InvocationContext) ComponentRef(org.infinispan.factories.impl.ComponentRef) AdvancedCache(org.infinispan.AdvancedCache) SingleSegmentPublisher(org.infinispan.persistence.support.SingleSegmentPublisher) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) InterceptorChainFactory(org.infinispan.factories.InterceptorChainFactory) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) InitializationContextImpl(org.infinispan.persistence.InitializationContextImpl) Scope(org.infinispan.factories.scopes.Scope) ByteBufferFactory(org.infinispan.commons.io.ByteBufferFactory) PERSISTENCE(org.infinispan.util.logging.Log.PERSISTENCE) Predicate(java.util.function.Predicate) AsyncNonBlockingStore(org.infinispan.persistence.async.AsyncNonBlockingStore) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Collectors(java.util.stream.Collectors) IntSet(org.infinispan.commons.util.IntSet) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) DelegatingNonBlockingStore(org.infinispan.persistence.support.DelegatingNonBlockingStore) GuardedBy(net.jcip.annotations.GuardedBy) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) SegmentPublisherWrapper(org.infinispan.persistence.support.SegmentPublisherWrapper) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) PersistenceException(org.infinispan.persistence.spi.PersistenceException) Single(io.reactivex.rxjava3.core.Single) TransactionalStoreInterceptor(org.infinispan.interceptors.impl.TransactionalStoreInterceptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NonBlockingStoreAdapter(org.infinispan.persistence.support.NonBlockingStoreAdapter) CompletableFuture(java.util.concurrent.CompletableFuture) LocalOnlyCacheLoader(org.infinispan.persistence.spi.LocalOnlyCacheLoader) CompletionStages(org.infinispan.util.concurrent.CompletionStages) AtomicReference(java.util.concurrent.atomic.AtomicReference) InternalExpirationManager(org.infinispan.expiration.impl.InternalExpirationManager) CacheLoaderInterceptor(org.infinispan.interceptors.impl.CacheLoaderInterceptor) ArrayList(java.util.ArrayList) Start(org.infinispan.factories.annotations.Start) HashSet(java.util.HashSet) FlagBitSets(org.infinispan.context.impl.FlagBitSets) BiPredicate(java.util.function.BiPredicate) Characteristic(org.infinispan.persistence.spi.NonBlockingStore.Characteristic) Maybe(io.reactivex.rxjava3.core.Maybe) StoreConfiguration(org.infinispan.configuration.cache.StoreConfiguration) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) Log(org.infinispan.util.logging.Log) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) CacheWriterInterceptor(org.infinispan.interceptors.impl.CacheWriterInterceptor) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) InternalEntryFactory(org.infinispan.container.impl.InternalEntryFactory) Flowable(io.reactivex.rxjava3.core.Flowable) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) NonBlockingStore(org.infinispan.persistence.spi.NonBlockingStore) Publisher(org.reactivestreams.Publisher) NonBlockingManager(org.infinispan.util.concurrent.NonBlockingManager) Completable(io.reactivex.rxjava3.core.Completable) PutMapCommand(org.infinispan.commands.write.PutMapCommand) CacheNotifier(org.infinispan.notifications.cachelistener.CacheNotifier) Inject(org.infinispan.factories.annotations.Inject) MVCCEntry(org.infinispan.container.entries.MVCCEntry) IllegalLifecycleStateException(org.infinispan.commons.IllegalLifecycleStateException) PersistenceUtil(org.infinispan.persistence.internal.PersistenceUtil) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) Function(io.reactivex.rxjava3.functions.Function) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) BlockingManager(org.infinispan.util.concurrent.BlockingManager) Collections(java.util.Collections) StampedLock(java.util.concurrent.locks.StampedLock) TimeService(org.infinispan.commons.time.TimeService) DistributionManager(org.infinispan.distribution.DistributionManager) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) TransactionalStoreInterceptor(org.infinispan.interceptors.impl.TransactionalStoreInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor)

Example 4 with AsyncInterceptor

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

the class CustomInterceptorInjectionTest method testBaseCustomAsyncInterceptorInjection.

public void testBaseCustomAsyncInterceptorInjection() {
    AsyncInterceptor interceptor = cache.getAdvancedCache().getAsyncInterceptorChain().getInterceptors().get(0);
    assertEquals(SomeAsyncInterceptor.class, interceptor.getClass());
    SomeAsyncInterceptor someAsyncInterceptor = (SomeAsyncInterceptor) interceptor;
    assertSame(cache.getAdvancedCache().getLockManager(), someAsyncInterceptor.lm);
    assertSame(cache.getAdvancedCache().getDataContainer(), someAsyncInterceptor.dc);
}
Also used : AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) BaseCustomAsyncInterceptor(org.infinispan.interceptors.BaseCustomAsyncInterceptor)

Example 5 with AsyncInterceptor

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

the class InterceptorChainFactory method buildCustomInterceptors.

private void buildCustomInterceptors(AsyncInterceptorChain interceptorChain, CustomInterceptorsConfiguration customInterceptors) {
    for (InterceptorConfiguration config : customInterceptors.interceptors()) {
        if (interceptorChain.containsInterceptorType(config.asyncInterceptor().getClass()))
            continue;
        AsyncInterceptor customInterceptor = config.asyncInterceptor();
        SecurityActions.applyProperties(customInterceptor, config.properties());
        register(customInterceptor.getClass(), customInterceptor);
        if (config.first())
            interceptorChain.addInterceptor(customInterceptor, 0);
        else if (config.last())
            interceptorChain.addInterceptorBefore(customInterceptor, CallInterceptor.class);
        else if (config.index() >= 0)
            interceptorChain.addInterceptor(customInterceptor, config.index());
        else if (config.after() != null) {
            boolean added = interceptorChain.addInterceptorAfter(customInterceptor, config.after());
            if (!added) {
                throw new CacheConfigurationException("Cannot add after class: " + config.after() + " as no such interceptor exists in the default chain");
            }
        } else if (config.before() != null) {
            boolean added = interceptorChain.addInterceptorBefore(customInterceptor, config.before());
            if (!added) {
                throw new CacheConfigurationException("Cannot add before class: " + config.before() + " as no such interceptor exists in the default chain");
            }
        } else if (config.position() == InterceptorConfiguration.Position.OTHER_THAN_FIRST_OR_LAST) {
            interceptorChain.addInterceptor(customInterceptor, 1);
        }
    }
}
Also used : CacheConfigurationException(org.infinispan.commons.CacheConfigurationException) InterceptorConfiguration(org.infinispan.configuration.cache.InterceptorConfiguration) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor)

Aggregations

AsyncInterceptor (org.infinispan.interceptors.AsyncInterceptor)12 AsyncInterceptorChain (org.infinispan.interceptors.AsyncInterceptorChain)4 List (java.util.List)3 AdvancedCache (org.infinispan.AdvancedCache)3 Completable (io.reactivex.rxjava3.core.Completable)2 Flowable (io.reactivex.rxjava3.core.Flowable)2 Maybe (io.reactivex.rxjava3.core.Maybe)2 Single (io.reactivex.rxjava3.core.Single)2 Function (io.reactivex.rxjava3.functions.Function)2 MethodHandles (java.lang.invoke.MethodHandles)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 Executor (java.util.concurrent.Executor)2 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)2