Search in sources :

Example 6 with PutFromLoadValidator

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

the class CorrectnessTestCase method getPutFromLoadValidator.

private PutFromLoadValidator getPutFromLoadValidator(SessionFactoryImplementor sfi, String regionName) throws NoSuchFieldException, IllegalAccessException {
    RegionAccessStrategy strategy = sfi.getSecondLevelCacheRegionAccessStrategy(regionName);
    if (strategy == null) {
        return null;
    }
    Field delegateField = getField(strategy.getClass(), "delegate");
    Object delegate = delegateField.get(strategy);
    if (delegate == null) {
        return null;
    }
    if (InvalidationCacheAccessDelegate.class.isInstance(delegate)) {
        Field validatorField = InvalidationCacheAccessDelegate.class.getDeclaredField("putValidator");
        validatorField.setAccessible(true);
        return (PutFromLoadValidator) validatorField.get(delegate);
    } else {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) RegionAccessStrategy(org.hibernate.cache.spi.access.RegionAccessStrategy)

Example 7 with PutFromLoadValidator

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

the class CorrectnessTestCase method checkForEmptyPendingPuts.

protected void checkForEmptyPendingPuts() throws Exception {
    Field pp = PutFromLoadValidator.class.getDeclaredField("pendingPuts");
    pp.setAccessible(true);
    Method getInvalidators = null;
    List<DelayedInvalidators> delayed = new LinkedList<>();
    for (int i = 0; i < sessionFactories.length; i++) {
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactories[i];
        for (Object regionName : sfi.getCache().getSecondLevelCacheRegionNames()) {
            PutFromLoadValidator validator = getPutFromLoadValidator(sfi, (String) regionName);
            if (validator == null) {
                log.warn("No validator for " + regionName);
                continue;
            }
            ConcurrentMap<Object, Object> map = (ConcurrentMap) pp.get(validator);
            for (Iterator<Map.Entry<Object, Object>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry entry = iterator.next();
                if (getInvalidators == null) {
                    getInvalidators = entry.getValue().getClass().getMethod("getInvalidators");
                    getInvalidators.setAccessible(true);
                }
                java.util.Collection invalidators = (java.util.Collection) getInvalidators.invoke(entry.getValue());
                if (invalidators != null && !invalidators.isEmpty()) {
                    delayed.add(new DelayedInvalidators(map, entry.getKey()));
                }
            }
        }
    }
    // poll until all invalidations come
    long deadline = System.currentTimeMillis() + 30000;
    while (System.currentTimeMillis() < deadline) {
        iterateInvalidators(delayed, getInvalidators, (k, i) -> {
        });
        if (delayed.isEmpty()) {
            break;
        }
        Thread.sleep(1000);
    }
    if (!delayed.isEmpty()) {
        iterateInvalidators(delayed, getInvalidators, (k, i) -> log.warnf("Left invalidators on key %s: %s", k, i));
        throw new IllegalStateException("Invalidators were not cleared: " + delayed.size());
    }
}
Also used : PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ConcurrentMap(java.util.concurrent.ConcurrentMap) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) Field(java.lang.reflect.Field) Collection(org.hibernate.mapping.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 8 with PutFromLoadValidator

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

the class BaseTransactionalDataRegion method prepareForValidation.

protected void prepareForValidation() {
    if (strategy != null) {
        assert strategy == Strategy.VALIDATION;
        return;
    }
    validator = new PutFromLoadValidator(cache, factory);
    strategy = Strategy.VALIDATION;
}
Also used : PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator)

Example 9 with PutFromLoadValidator

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

the class EntityCollectionInvalidationTest method mockValidator.

private Runnable mockValidator(AdvancedCache cache, CountDownLatch latch) {
    PutFromLoadValidator originalValidator = PutFromLoadValidator.removeFromCache(cache);
    PutFromLoadValidator mockValidator = spy(originalValidator);
    doAnswer(invocation -> {
        try {
            return invocation.callRealMethod();
        } finally {
            latch.countDown();
        }
    }).when(mockValidator).endInvalidatingKey(any(), any());
    PutFromLoadValidator.addToCache(cache, mockValidator);
    return () -> {
        PutFromLoadValidator.removeFromCache(cache);
        PutFromLoadValidator.addToCache(cache, originalValidator);
    };
}
Also used : PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator)

Example 10 with PutFromLoadValidator

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

the class PutFromLoadValidatorUnitTest method invalidationBlocksForInProgressPutTest.

private void invalidationBlocksForInProgressPutTest(final boolean keyOnly) throws Exception {
    final PutFromLoadValidator testee = new PutFromLoadValidator(cache, regionFactory(cm));
    final CountDownLatch removeLatch = new CountDownLatch(1);
    final CountDownLatch pferLatch = new CountDownLatch(1);
    final AtomicReference<Object> cache = new AtomicReference<>("INITIAL");
    Callable<Boolean> pferCallable = () -> {
        long txTimestamp = TIME_SERVICE.wallClockTime();
        SharedSessionContractImplementor session = mock(SharedSessionContractImplementor.class);
        testee.registerPendingPut(session, KEY1, txTimestamp);
        PutFromLoadValidator.Lock lock = testee.acquirePutFromLoadLock(session, KEY1, txTimestamp);
        if (lock != null) {
            try {
                removeLatch.countDown();
                pferLatch.await();
                cache.set("PFER");
                return Boolean.TRUE;
            } finally {
                testee.releasePutFromLoadLock(KEY1, lock);
            }
        }
        return Boolean.FALSE;
    };
    Callable<Void> invalidateCallable = () -> {
        removeLatch.await();
        if (keyOnly) {
            SharedSessionContractImplementor session = mock(SharedSessionContractImplementor.class);
            testee.beginInvalidatingKey(session, KEY1);
        } else {
            testee.beginInvalidatingRegion();
        }
        cache.set(null);
        return null;
    };
    ExecutorService executor = Executors.newCachedThreadPool();
    cleanup.add(() -> executor.shutdownNow());
    Future<Boolean> pferFuture = executor.submit(pferCallable);
    Future<Void> invalidateFuture = executor.submit(invalidateCallable);
    expectException(TimeoutException.class, () -> invalidateFuture.get(1, TimeUnit.SECONDS));
    pferLatch.countDown();
    assertTrue(pferFuture.get(5, TimeUnit.SECONDS));
    invalidateFuture.get(5, TimeUnit.SECONDS);
    assertNull(cache.get());
}
Also used : PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

PutFromLoadValidator (org.hibernate.cache.infinispan.access.PutFromLoadValidator)14 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutorService (java.util.concurrent.ExecutorService)3 TimeoutException (java.util.concurrent.TimeoutException)3 Test (org.junit.Test)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 RegionAccessStrategy (org.hibernate.cache.spi.access.RegionAccessStrategy)2 TestInfinispanRegionFactory (org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory)2 Exceptions.expectException (org.infinispan.test.Exceptions.expectException)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 NavigableMap (java.util.NavigableMap)1