use of org.apache.geode.cache.TransactionId in project geode by apache.
the class MultiVMRegionTestCase method testTXUpdateLoadNoConflict.
/**
* Tests that the push of a loaded value does not cause a conflict on the side receiving the
* update
*/
@Ignore("TODO: this test always hits early out")
@Test
public void testTXUpdateLoadNoConflict() throws Exception {
/*
* this no longer holds true - we have load conflicts now
*
*/
if (true) {
return;
}
assumeTrue(supportsTransactions());
assumeFalse(getRegionAttributes().getScope().isGlobal());
assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
assertTrue(getRegionAttributes().getScope().isDistributed());
CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
final String rgnName = getUniqueName();
SerializableRunnable create = new SerializableRunnable("testTXUpdateLoadNoConflict: Create Region & Load value") {
@Override
public void run() {
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = new MyTransactionListener();
txMgr2.addListener(tl);
try {
Region rgn = createRegion(rgnName);
AttributesMutator mutator = rgn.getAttributesMutator();
mutator.setCacheLoader(new CacheLoader() {
int count = 0;
@Override
public Object load(LoaderHelper helper) throws CacheLoaderException {
count++;
return "LV " + count;
}
@Override
public void close() {
}
});
Object value = rgn.get("key");
assertEquals("LV 1", value);
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: loaded Key");
flushIfNecessary(rgn);
} catch (CacheException e) {
fail("While creating region", e);
}
}
};
VM vm0 = Host.getHost(0).getVM(0);
try {
MyTransactionListener tl = new MyTransactionListener();
txMgr.addListener(tl);
AttributesFactory rgnAtts = new AttributesFactory(getRegionAttributes());
rgnAtts.setDataPolicy(DataPolicy.REPLICATE);
Region rgn = createRegion(rgnName, rgnAtts.create());
txMgr.begin();
TransactionId myTXId = txMgr.getTransactionId();
rgn.create("key", "txValue");
vm0.invoke(create);
{
TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
assertTrue(rgn.containsKey("key"));
assertEquals("LV 1", rgn.getEntry("key").getValue());
((TXManagerImpl) txMgr).internalResume(tx);
}
// make sure transactional view is still correct
assertEquals("txValue", rgn.getEntry("key").getValue());
txMgr.commit();
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
assertEquals("txValue", rgn.getEntry("key").getValue());
{
Collection events = tl.lastEvent.getCreateEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(myTXId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn);
assertEquals("key", ev.getKey());
assertEquals("txValue", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(!ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
// Now setup recreate the region in the controller with NONE
// so test can do local destroys.
rgn.localDestroyRegion();
rgnAtts.setDataPolicy(DataPolicy.NORMAL);
rgn = createRegion(rgnName, rgnAtts.create());
// now see if net loader is working
Object v2 = rgn.get("key2");
assertEquals("LV 2", v2);
// now confirm that netload does not cause a conflict
txMgr.begin();
myTXId = txMgr.getTransactionId();
rgn.create("key3", "txValue3");
{
TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
// do a get outside of the transaction to force a net load
Object v3 = rgn.get("key3");
assertEquals("LV 3", v3);
((TXManagerImpl) txMgr).internalResume(tx);
}
// make sure transactional view is still correct
assertEquals("txValue3", rgn.getEntry("key3").getValue());
txMgr.commit();
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
assertEquals("txValue3", rgn.getEntry("key3").getValue());
{
Collection events = tl.lastEvent.getCreateEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(myTXId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn);
assertEquals("key3", ev.getKey());
assertEquals("txValue3", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(!ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
// now see if tx net loader is working
// now confirm that netload does not cause a conflict
txMgr.begin();
myTXId = txMgr.getTransactionId();
Object v4 = rgn.get("key4");
assertEquals("LV 4", v4);
assertEquals("LV 4", rgn.get("key4"));
assertEquals("LV 4", rgn.getEntry("key4").getValue());
txMgr.rollback();
// confirm that netLoad is transactional
assertEquals("LV 5", rgn.get("key4"));
assertEquals("LV 5", rgn.getEntry("key4").getValue());
// make sure non-tx netsearch works
assertEquals("txValue", rgn.get("key"));
assertEquals("txValue", rgn.getEntry("key").getValue());
// make sure net-search result does not conflict with commit
rgn.localInvalidate("key");
txMgr.begin();
myTXId = txMgr.getTransactionId();
rgn.put("key", "new txValue");
{
TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
// do a get outside of the transaction to force a netsearch
// does a netsearch
assertEquals("txValue", rgn.get("key"));
assertEquals("txValue", rgn.getEntry("key").getValue());
((TXManagerImpl) txMgr).internalResume(tx);
}
// make sure transactional view is still correct
assertEquals("new txValue", rgn.getEntry("key").getValue());
txMgr.commit();
// give other side change to process commit
flushIfNecessary(rgn);
getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
assertEquals("new txValue", rgn.getEntry("key").getValue());
{
Collection events = tl.lastEvent.getPutEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(myTXId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn);
assertEquals("key", ev.getKey());
assertEquals("new txValue", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(!ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
// make sure tx local invalidate allows netsearch
Object localCmtValue = rgn.getEntry("key").getValue();
txMgr.begin();
assertSame(localCmtValue, rgn.getEntry("key").getValue());
rgn.localInvalidate("key");
assertNull(rgn.getEntry("key").getValue());
// now make sure a get will do a netsearch and find the value
// in the other vm instead of the one in local cmt state
Object txValue = rgn.get("key");
assertNotSame(localCmtValue, txValue);
assertSame(txValue, rgn.get("key"));
assertNotSame(localCmtValue, rgn.getEntry("key").getValue());
// make sure we did a search and not a load
assertEquals(localCmtValue, rgn.getEntry("key").getValue());
// now make sure that if we do a tx distributed invalidate
// that we will do a load and not a search
rgn.invalidate("key");
assertNull(rgn.getEntry("key").getValue());
txValue = rgn.get("key");
assertEquals("LV 6", txValue);
assertSame(txValue, rgn.get("key"));
assertEquals("LV 6", rgn.getEntry("key").getValue());
// now make sure after rollback that local cmt state has not changed
txMgr.rollback();
assertSame(localCmtValue, rgn.getEntry("key").getValue());
} catch (Exception e) {
CacheFactory.getInstance(getSystem()).close();
getSystem().getLogWriter().fine("testTXUpdateLoadNoConflict: Caused exception in createRegion");
throw e;
}
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class RemoteTransactionDUnitTest method testBug45556.
@Test
public void testBug45556() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
final String name = getName();
class CountingListener extends CacheListenerAdapter {
private int count;
@Override
public void afterCreate(EntryEvent event) {
LogWriterUtils.getLogWriter().info("afterCreate invoked for " + event);
count++;
}
@Override
public void afterUpdate(EntryEvent event) {
LogWriterUtils.getLogWriter().info("afterUpdate invoked for " + event);
count++;
}
}
accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE_PROXY).create(name);
r.getAttributesMutator().addCacheListener(new CountingListener());
return null;
}
});
datastore.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(name);
r.getAttributesMutator().addCacheListener(new CountingListener());
r.put("key1", "value1");
return null;
}
});
final TransactionId txid = (TransactionId) accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region r = getCache().getRegion(name);
CacheTransactionManager tm = getCache().getCacheTransactionManager();
getCache().getLogger().fine("SWAP:BeginTX");
tm.begin();
r.put("txkey", "txvalue");
return tm.suspend();
}
});
datastore.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region rgn = getCache().getRegion(name);
assertNull(rgn.get("txkey"));
TXManagerImpl txMgr = getGemfireCache().getTxManager();
TXStateProxy tx = txMgr.getHostedTXState((TXId) txid);
assertEquals(1, tx.getRegions().size());
for (LocalRegion r : tx.getRegions()) {
assertTrue(r instanceof DistributedRegion);
TXRegionState rs = tx.readRegion(r);
for (Object key : rs.getEntryKeys()) {
TXEntryState es = rs.readEntry(key);
assertEquals("txkey", key);
assertNotNull(es.getValue(key, r, false));
if (key.equals("txkey"))
assertTrue(es.isDirty());
}
}
return null;
}
});
accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region rgn = getCache().getRegion(name);
assertNull(rgn.get("txkey"));
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.resume(txid);
mgr.commit();
CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
assertEquals(0, cl.count);
assertEquals("txvalue", rgn.get("txkey"));
return null;
}
});
datastore.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region rgn = getCache().getRegion(name);
CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
assertEquals(2, cl.count);
return null;
}
});
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class ExecutionHandlerContext method executeWithTransaction.
private void executeWithTransaction(ChannelHandlerContext ctx, final Executor exec, Command command) throws Exception {
CacheTransactionManager txm = cache.getCacheTransactionManager();
TransactionId transactionId = getTransactionID();
txm.resume(transactionId);
try {
exec.executeCommand(command, this);
} catch (UnsupportedOperationInTransactionException e) {
command.setResponse(Coder.getErrorResponse(this.byteBufAllocator, RedisConstants.ERROR_UNSUPPORTED_OPERATION_IN_TRANSACTION));
} catch (TransactionException e) {
command.setResponse(Coder.getErrorResponse(this.byteBufAllocator, RedisConstants.ERROR_TRANSACTION_EXCEPTION));
} catch (Exception e) {
ByteBuf response = getExceptionResponse(ctx, e);
command.setResponse(response);
}
getTransactionQueue().add(command);
transactionId = txm.suspend();
setTransactionID(transactionId);
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class RegionProvider method getOrCreateRegion0.
private Region<?, ?> getOrCreateRegion0(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context, boolean addToMeta) {
checkDataType(key, type);
Region<?, ?> r = this.regions.get(key);
if (r != null && r.isDestroyed()) {
removeKey(key, type);
r = null;
}
if (r == null) {
String stringKey = key.toString();
Lock lock = this.locks.get(stringKey);
if (lock == null) {
this.locks.putIfAbsent(stringKey, new ReentrantLock());
lock = this.locks.get(stringKey);
}
try {
lock.lock();
r = regions.get(key);
if (r == null) {
// Can create
boolean hasTransaction = context != null && context.hasTransaction();
// without context
CacheTransactionManager txm = null;
TransactionId transactionId = null;
try {
if (hasTransaction) {
txm = cache.getCacheTransactionManager();
transactionId = txm.suspend();
}
Exception concurrentCreateDestroyException = null;
do {
concurrentCreateDestroyException = null;
r = createRegionGlobally(stringKey);
try {
if (type == RedisDataType.REDIS_LIST) {
doInitializeList(key, r);
} else if (type == RedisDataType.REDIS_SORTEDSET) {
try {
doInitializeSortedSet(key, r);
} catch (RegionNotFoundException | IndexInvalidException e) {
concurrentCreateDestroyException = e;
}
}
} catch (QueryInvalidException e) {
if (e.getCause() instanceof RegionNotFoundException) {
concurrentCreateDestroyException = e;
}
}
} while (concurrentCreateDestroyException != null);
this.regions.put(key, r);
if (addToMeta) {
RedisDataType existingType = metaPutIfAbsent(key, type);
if (existingType != null && existingType != type)
throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + existingType.toString());
}
} finally {
if (hasTransaction)
txm.resume(transactionId);
}
}
} finally {
lock.unlock();
}
}
return r;
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class TXJUnitTest method testListener.
@Test
public void testListener() {
assertTrue(this.txMgr.getListener() == null);
TransactionListener oldListener = this.txMgr.setListener(new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
listenerAfterCommit = 1;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
listenerAfterFailedCommit = 1;
te = event;
}
@Override
public void afterRollback(TransactionEvent event) {
listenerAfterRollback = 1;
te = event;
}
@Override
public void close() {
listenerClose = 1;
}
});
assertTrue(oldListener == null);
this.txMgr.begin();
TransactionId myTxId = this.txMgr.getTransactionId();
assertEquals(0, this.listenerAfterRollback);
this.txMgr.rollback();
assertEquals(1, this.listenerAfterRollback);
assertEquals(0, this.te.getCreateEvents().size());
assertEquals(0, this.te.getPutEvents().size());
assertEquals(0, this.te.getInvalidateEvents().size());
assertEquals(0, this.te.getDestroyEvents().size());
assertEquals(0, this.te.getEvents().size());
assertEquals(myTxId, this.te.getTransactionId());
this.txMgr.begin();
myTxId = this.txMgr.getTransactionId();
try {
assertEquals(0, this.listenerAfterCommit);
this.txMgr.commit();
} catch (CommitConflictException unexpected) {
fail("did not expect " + unexpected);
}
assertEquals(1, this.listenerAfterCommit);
assertEquals(0, this.te.getCreateEvents().size());
assertEquals(0, this.te.getPutEvents().size());
assertEquals(0, this.te.getInvalidateEvents().size());
assertEquals(0, this.te.getDestroyEvents().size());
assertEquals(0, this.te.getEvents().size());
assertEquals(myTxId, this.te.getTransactionId());
assertEquals(0, this.listenerClose);
oldListener = this.txMgr.setListener(new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
listenerAfterCommit = 2;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
listenerAfterFailedCommit = 2;
}
@Override
public void afterRollback(TransactionEvent event) {
listenerAfterRollback = 2;
te = event;
}
@Override
public void close() {
listenerClose = 2;
}
});
assertEquals(1, this.listenerClose);
this.txMgr.begin();
assertEquals(1, this.listenerAfterRollback);
this.txMgr.rollback();
assertEquals(2, this.listenerAfterRollback);
this.txMgr.begin();
this.txMgr.setListener(oldListener);
assertEquals(2, this.listenerClose);
this.txMgr.rollback();
assertEquals(1, this.listenerAfterRollback);
closeCache();
assertEquals(1, this.listenerClose);
}
Aggregations