Search in sources :

Example 1 with VersionedEntry

use of org.infinispan.hibernate.cache.commons.util.VersionedEntry 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 2 with VersionedEntry

use of org.infinispan.hibernate.cache.commons.util.VersionedEntry in project infinispan by infinispan.

the class NonStrictAccessDelegate method putFromLoad.

@Override
public boolean putFromLoad(Object session, Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException {
    long lastRegionInvalidation = region.getLastRegionInvalidation();
    if (txTimestamp < lastRegionInvalidation) {
        log.tracef("putFromLoad not executed since tx started at %d, before last region invalidation finished = %d", txTimestamp, lastRegionInvalidation);
        return false;
    }
    assert version != null;
    if (minimalPutOverride) {
        Object prev = cache.get(key);
        if (prev != null) {
            Object oldVersion = getVersion(prev);
            if (oldVersion != null) {
                if (versionComparator.compare(version, oldVersion) <= 0) {
                    if (log.isTraceEnabled()) {
                        log.tracef("putFromLoad not executed since version(%s) <= oldVersion(%s)", version, oldVersion);
                    }
                    return false;
                }
            } else if (prev instanceof VersionedEntry && txTimestamp <= ((VersionedEntry) prev).getTimestamp()) {
                if (log.isTraceEnabled()) {
                    log.tracef("putFromLoad not executed since tx started at %d and entry was invalidated at %d", txTimestamp, ((VersionedEntry) prev).getTimestamp());
                }
                return false;
            }
        }
    }
    // we can't use putForExternalRead since the PFER flag means that entry is not wrapped into context
    // when it is present in the container. TombstoneCallInterceptor will deal with this.
    // Even if value is instanceof CacheEntry, we have to wrap it in VersionedEntry and add transaction timestamp.
    // Otherwise, old eviction record wouldn't be overwritten.
    CompletableFuture<Void> future = putFromLoadMap.eval(key, new VersionedEntry(value, version, txTimestamp));
    // Rethrow exceptions
    future.join();
    return true;
}
Also used : VersionedEntry(org.infinispan.hibernate.cache.commons.util.VersionedEntry)

Example 3 with VersionedEntry

use of org.infinispan.hibernate.cache.commons.util.VersionedEntry in project infinispan by infinispan.

the class DomainDataRegionImpl method runInvalidation.

@Override
protected void runInvalidation() {
    if (strategy == null) {
        throw new IllegalStateException("Strategy was not set");
    }
    switch(strategy) {
        case NONE:
        case VALIDATION:
            super.runInvalidation();
            return;
        case TOMBSTONES:
            removeEntries(entry -> localCache.remove(entry.getKey(), entry.getValue()));
            return;
        case VERSIONED_ENTRIES:
            // no need to use this as a function - simply override all
            VersionedEntry evict = new VersionedEntry(factory.nextTimestamp());
            removeEntries(entry -> localCache.put(entry.getKey(), evict, tombstoneExpiration, TimeUnit.MILLISECONDS));
            return;
    }
}
Also used : VersionedEntry(org.infinispan.hibernate.cache.commons.util.VersionedEntry)

Aggregations

VersionedEntry (org.infinispan.hibernate.cache.commons.util.VersionedEntry)3 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiConsumer (java.util.function.BiConsumer)1 Synchronization (javax.transaction.Synchronization)1 PessimisticLockException (org.hibernate.PessimisticLockException)1 Session (org.hibernate.Session)1 StaleStateException (org.hibernate.StaleStateException)1 AdvancedCache (org.infinispan.AdvancedCache)1 ReadWriteKeyCommand (org.infinispan.commands.functional.ReadWriteKeyCommand)1 ByRef (org.infinispan.commons.util.ByRef)1 InvocationContext (org.infinispan.context.InvocationContext)1