use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.
the class InvalidationTest method testConcurrentRemoveAndPutFromLoad.
@Test
@TestForIssue(jiraKey = "HHH-9868")
public void testConcurrentRemoveAndPutFromLoad() throws Exception {
final Item item = new Item("chris", "Chris's Item");
withTxSession(s -> {
s.persist(item);
});
Phaser deletePhaser = new Phaser(2);
Phaser getPhaser = new Phaser(2);
HookInterceptor hook = new HookInterceptor();
AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class);
pendingPutsCache.addInterceptor(hook, 0);
AtomicBoolean getThreadBlockedInDB = new AtomicBoolean(false);
Thread deleteThread = new Thread(() -> {
try {
withTxSession(s -> {
Item loadedItem = s.get(Item.class, item.getId());
assertNotNull(loadedItem);
arriveAndAwait(deletePhaser, 2000);
arriveAndAwait(deletePhaser, 2000);
log.trace("Item loaded");
s.delete(loadedItem);
s.flush();
log.trace("Item deleted");
arriveAndAwait(deletePhaser, 2000);
arriveAndAwait(deletePhaser, 4000);
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}, "delete-thread");
Thread getThread = new Thread(() -> {
try {
withTxSession(s -> {
Item loadedItem = s.get(Item.class, item.getId());
if (getThreadBlockedInDB.get()) {
assertNull(loadedItem);
} else {
assertNotNull(loadedItem);
}
});
} catch (PessimisticLockException e) {
// (delete-thread has ITEMS table write-locked and we try to acquire read-lock)
try {
arriveAndAwait(getPhaser, 2000);
arriveAndAwait(getPhaser, 2000);
} catch (Exception e1) {
throw new RuntimeException(e1);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}, "get-thread");
deleteThread.start();
// deleteThread loads the entity
arriveAndAwait(deletePhaser, 2000);
withTx(() -> {
sessionFactory().getCache().evictEntity(Item.class, item.getId());
assertFalse(sessionFactory().getCache().containsEntity(Item.class, item.getId()));
return null;
});
arriveAndAwait(deletePhaser, 2000);
// delete thread invalidates PFER
arriveAndAwait(deletePhaser, 2000);
// get thread gets the entity from DB
hook.block(getPhaser, getThread);
getThread.start();
try {
arriveAndAwait(getPhaser, 2000);
} catch (TimeoutException e) {
getThreadBlockedInDB.set(true);
}
arriveAndAwait(deletePhaser, 2000);
// delete thread finishes the remove from DB and cache
deleteThread.join();
hook.unblock();
arriveAndAwait(getPhaser, 2000);
// get thread puts the entry into cache
getThread.join();
assertNoInvalidators(pendingPutsCache);
withTxSession(s -> {
Item loadedItem = s.get(Item.class, item.getId());
assertNull(loadedItem);
});
}
use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.
the class QueryRegionImplTest method testGetDoesNotBlockPut.
@Test
public void testGetDoesNotBlockPut() throws Exception {
withQueryRegion((sessionFactory, region) -> {
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
final AdvancedCache cache = ((QueryResultsRegionImpl) region).getCache();
final CountDownLatch blockerLatch = new CountDownLatch(1);
final CountDownLatch writerLatch = new CountDownLatch(1);
final CountDownLatch completionLatch = new CountDownLatch(1);
final ExceptionHolder holder = new ExceptionHolder();
Thread reader = new Thread() {
@Override
public void run() {
GetBlocker blocker = new GetBlocker(blockerLatch, KEY);
try {
cache.addListener(blocker);
withSession(sessionFactory, session -> region.get(session, KEY));
} catch (Exception e) {
holder.addException(e);
} finally {
cache.removeListener(blocker);
}
}
};
Thread writer = new Thread() {
@Override
public void run() {
try {
writerLatch.await();
withSession(sessionFactory, session -> region.put(session, KEY, VALUE2));
} catch (Exception e) {
holder.addException(e);
} finally {
completionLatch.countDown();
}
}
};
reader.setDaemon(true);
writer.setDaemon(true);
boolean unblocked = false;
try {
reader.start();
writer.start();
assertFalse("Reader is blocking", completionLatch.await(100, TimeUnit.MILLISECONDS));
// Start the writer
writerLatch.countDown();
assertTrue("Writer finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS));
blockerLatch.countDown();
unblocked = true;
if (IsolationLevel.REPEATABLE_READ.equals(cache.getCacheConfiguration().locking().isolationLevel())) {
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
} else {
assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
}
holder.checkExceptions();
} finally {
if (!unblocked) {
blockerLatch.countDown();
}
}
});
}
use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.
the class QueryRegionImplTest method testPutDuringPut.
@Test
@TestForIssue(jiraKey = "HHH-7898")
public void testPutDuringPut() throws Exception {
withQueryRegion((sessionFactory, region) -> {
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
assertEquals(VALUE1, callWithSession(sessionFactory, session -> region.get(session, KEY)));
final AdvancedCache cache = ((QueryResultsRegionImpl) region).getCache();
CountDownLatch blockerLatch = new CountDownLatch(1);
CountDownLatch triggerLatch = new CountDownLatch(1);
ExceptionHolder holder = new ExceptionHolder();
Thread blocking = new Thread() {
@Override
public void run() {
PutBlocker blocker = null;
try {
blocker = new PutBlocker(blockerLatch, triggerLatch, KEY);
cache.addListener(blocker);
withSession(sessionFactory, session -> region.put(session, KEY, VALUE2));
} catch (Exception e) {
holder.addException(e);
} finally {
if (blocker != null) {
cache.removeListener(blocker);
}
if (triggerLatch.getCount() > 0) {
triggerLatch.countDown();
}
}
}
};
Thread blocked = new Thread() {
@Override
public void run() {
try {
triggerLatch.await();
// this should silently fail
withSession(sessionFactory, session -> region.put(session, KEY, VALUE3));
} catch (Exception e) {
holder.addException(e);
}
}
};
blocking.setName("blocking-thread");
blocking.start();
blocked.setName("blocked-thread");
blocked.start();
blocked.join();
blockerLatch.countDown();
blocking.join();
holder.checkExceptions();
assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
});
}
use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.
the class InfinispanRegionFactory method getCache.
protected AdvancedCache getCache(String regionName, DataType type, CacheDataDescription metadata) {
if (!manager.cacheExists(regionName)) {
String templateCacheName = baseConfigurations.get(regionName);
Configuration configuration = null;
ConfigurationBuilder builder = new ConfigurationBuilder();
if (templateCacheName != null) {
configuration = manager.getCacheConfiguration(templateCacheName);
if (configuration == null) {
log.customConfigForRegionNotFound(templateCacheName, regionName, type.key);
} else {
log.debugf("Region '%s' will use cache template '%s'", regionName, templateCacheName);
builder.read(configuration);
configureTransactionManager(builder);
// do not apply data type overrides to regions that set special cache configuration
}
}
if (configuration == null) {
configuration = dataTypeConfigurations.get(type);
if (configuration == null) {
throw new IllegalStateException("Configuration not defined for type " + type.key);
}
builder.read(configuration);
// overrides for data types are already applied, but we should check custom ones
}
ConfigurationBuilder override = configOverrides.get(regionName);
if (override != null) {
log.debugf("Region '%s' has additional configuration set through properties.", regionName);
builder.read(override.build(false));
}
if (getCacheKeysFactory() instanceof SimpleCacheKeysFactory) {
// the keys may not define hashCode/equals correctly (e.g. arrays)
if (metadata != null && metadata.getKeyType() != null) {
builder.dataContainer().keyEquivalence(new TypeEquivalance(metadata.getKeyType()));
}
}
if (globalStats != null) {
builder.jmxStatistics().enabled(globalStats).available(globalStats);
}
configuration = builder.build();
type.validate(configuration);
manager.defineConfiguration(regionName, configuration);
}
final AdvancedCache cache = manager.getCache(regionName).getAdvancedCache();
// TODO: not sure if this is needed in recent Infinispan
if (!cache.getStatus().allowInvocations()) {
cache.start();
}
return createCacheWrapper(cache);
}
use of org.infinispan.AdvancedCache in project hibernate-orm by hibernate.
the class InfinispanRegionFactory method buildTimestampsRegion.
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Map<String, Object> configValues) {
if (log.isDebugEnabled()) {
log.debug("Building timestamps cache region [" + regionName + "]");
}
final AdvancedCache cache = getCache(regionName, DataType.TIMESTAMPS, null);
final TimestampsRegionImpl region = createTimestampsRegion(cache, regionName);
startRegion(region);
return region;
}
Aggregations