Search in sources :

Example 1 with NonTxInvalidationCacheAccessDelegate

use of org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate in project hibernate-orm by hibernate.

the class CollectionRegionAccessStrategyTest method testPutFromLoadRemoveDoesNotProduceStaleData.

@Test
public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception {
    if (!cacheMode.isInvalidation()) {
        return;
    }
    final CountDownLatch pferLatch = new CountDownLatch(1);
    final CountDownLatch removeLatch = new CountDownLatch(1);
    // remove the interceptor inserted by default PutFromLoadValidator, we're using different one
    PutFromLoadValidator originalValidator = PutFromLoadValidator.removeFromCache(localRegion.getCache());
    PutFromLoadValidator mockValidator = spy(originalValidator);
    doAnswer(invocation -> {
        try {
            return invocation.callRealMethod();
        } finally {
            try {
                removeLatch.countDown();
                assertFalse(pferLatch.await(2, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                log.debug("Interrupted");
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                log.error("Error", e);
                throw new RuntimeException("Error", e);
            }
        }
    }).when(mockValidator).acquirePutFromLoadLock(any(), any(), anyLong());
    PutFromLoadValidator.addToCache(localRegion.getCache(), mockValidator);
    cleanup.add(() -> {
        PutFromLoadValidator.removeFromCache(localRegion.getCache());
        PutFromLoadValidator.addToCache(localRegion.getCache(), originalValidator);
    });
    final AccessDelegate delegate = localRegion.getCache().getCacheConfiguration().transaction().transactionMode().isTransactional() ? new TxInvalidationCacheAccessDelegate(localRegion, mockValidator) : new NonTxInvalidationCacheAccessDelegate(localRegion, mockValidator);
    ExecutorService executorService = Executors.newCachedThreadPool();
    cleanup.add(() -> executorService.shutdownNow());
    final String KEY = "k1";
    Future<Void> pferFuture = executorService.submit(() -> {
        SharedSessionContractImplementor session = mockedSession();
        delegate.putFromLoad(session, KEY, "v1", session.getTimestamp(), null);
        return null;
    });
    Future<Void> removeFuture = executorService.submit(() -> {
        removeLatch.await();
        SharedSessionContractImplementor session = mockedSession();
        withTx(localEnvironment, session, () -> {
            delegate.remove(session, KEY);
            return null;
        });
        pferLatch.countDown();
        return null;
    });
    pferFuture.get();
    removeFuture.get();
    assertFalse(localRegion.getCache().containsKey(KEY));
    assertFalse(remoteRegion.getCache().containsKey(KEY));
}
Also used : NonTxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) NonTxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate) TxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.TxInvalidationCacheAccessDelegate) NonTxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate) AccessDelegate(org.hibernate.cache.infinispan.access.AccessDelegate) TxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.TxInvalidationCacheAccessDelegate) Test(org.junit.Test) AbstractRegionAccessStrategyTest(org.hibernate.test.cache.infinispan.AbstractRegionAccessStrategyTest)

Example 2 with NonTxInvalidationCacheAccessDelegate

use of org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate in project hibernate-orm by hibernate.

the class BaseTransactionalDataRegion method createAccessDelegate.

protected synchronized AccessDelegate createAccessDelegate(AccessType accessType) {
    if (accessType == null) {
        throw new IllegalArgumentException();
    }
    if (this.accessType != null && !this.accessType.equals(accessType)) {
        throw new IllegalStateException("This region was already set up for " + this.accessType + ", cannot use using " + accessType);
    }
    this.accessType = accessType;
    CacheMode cacheMode = cache.getCacheConfiguration().clustering().cacheMode();
    if (accessType == AccessType.NONSTRICT_READ_WRITE) {
        prepareForVersionedEntries();
        return new NonStrictAccessDelegate(this);
    }
    if (cacheMode.isDistributed() || cacheMode.isReplicated()) {
        prepareForTombstones();
        return new TombstoneAccessDelegate(this);
    } else {
        prepareForValidation();
        if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) {
            return new TxInvalidationCacheAccessDelegate(this, validator);
        } else {
            return new NonTxInvalidationCacheAccessDelegate(this, validator);
        }
    }
}
Also used : NonStrictAccessDelegate(org.hibernate.cache.infinispan.access.NonStrictAccessDelegate) NonTxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate) CacheMode(org.infinispan.configuration.cache.CacheMode) NonTxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate) TxInvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.TxInvalidationCacheAccessDelegate) TombstoneAccessDelegate(org.hibernate.cache.infinispan.access.TombstoneAccessDelegate)

Aggregations

NonTxInvalidationCacheAccessDelegate (org.hibernate.cache.infinispan.access.NonTxInvalidationCacheAccessDelegate)2 TxInvalidationCacheAccessDelegate (org.hibernate.cache.infinispan.access.TxInvalidationCacheAccessDelegate)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AccessDelegate (org.hibernate.cache.infinispan.access.AccessDelegate)1 NonStrictAccessDelegate (org.hibernate.cache.infinispan.access.NonStrictAccessDelegate)1 PutFromLoadValidator (org.hibernate.cache.infinispan.access.PutFromLoadValidator)1 TombstoneAccessDelegate (org.hibernate.cache.infinispan.access.TombstoneAccessDelegate)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 AbstractRegionAccessStrategyTest (org.hibernate.test.cache.infinispan.AbstractRegionAccessStrategyTest)1 CacheMode (org.infinispan.configuration.cache.CacheMode)1 Test (org.junit.Test)1