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;
}
}
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());
}
}
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;
}
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);
};
}
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());
}
Aggregations