use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class DistributedTransactionDUnitTest method testTransactionalPutOnReplicatedRegion.
@Test
public void testTransactionalPutOnReplicatedRegion() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM server3 = host.getVM(2);
createRR(new VM[] { server1, server2, server3 });
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
mgr.begin();
mgr.commit();
mgr.begin();
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER_RR);
CustId custId = new CustId(1);
Customer expectedCustomer = custRegion.get(custId);
assertNull(expectedCustomer);
// Perform a put
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1", "addr1");
custRegion.put(custIdOne, customerOne);
// Rollback the transaction
mgr.rollback();
mgr.begin();
// Verify that the entry is rolled back
expectedCustomer = custRegion.get(custId);
assertNull(expectedCustomer);
// Perform two more puts and a commit
CustId custIdTwo = new CustId(2);
Customer customerTwo = new Customer("name2", "addr2");
CustId custIdThree = new CustId(3);
Customer customerThree = new Customer("name3", "addr3");
custRegion.put(custIdTwo, customerTwo);
custRegion.put(custIdThree, customerThree);
mgr.commit();
mgr.begin();
// Verify data
assertEquals(2, custRegion.size());
assertTrue(custRegion.containsKey(custIdTwo));
assertTrue(custRegion.containsKey(custIdThree));
assertEquals(customerTwo, custRegion.get(custIdTwo));
assertEquals(customerThree, custRegion.get(custIdThree));
// Perform one more put but don't commit
custRegion.put(custIdOne, customerOne);
// Verify data
assertEquals(3, custRegion.size());
assertTrue(custRegion.containsKey(custIdOne));
assertEquals(customerOne, custRegion.get(custIdOne));
mgr.commit();
return null;
}
});
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class Bug47667DUnitTest method testbug47667.
@Test
public void testbug47667() {
Host host = Host.getHost(0);
VM locator = host.getVM(0);
VM server1 = host.getVM(1);
VM server2 = host.getVM(2);
VM client = host.getVM(3);
final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
final String locatorHost = NetworkUtils.getServerHostName(host);
locator.invoke("Start Locator", () -> startLocator(locatorHost, locatorPort, ""));
String locString = getLocatorString(host, locatorPort);
server1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "R1" }, locString, new String[] { "R1" }));
server2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "R2" }, locString, new String[] { "R2" }));
client.invoke("create region and insert data in transaction", () -> {
ClientCacheFactory ccf = new ClientCacheFactory();
ccf.addPoolLocator(locatorHost, locatorPort);
ClientCache cache = ccf.create();
PoolManager.createFactory().addLocator(locatorHost, locatorPort).setServerGroup("R1").create("R1");
PoolManager.createFactory().addLocator(locatorHost, locatorPort).setServerGroup("R2").create("R2");
Region region1 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).setPoolName("R1").create("R1");
Region region2 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).setPoolName("R2").create("R2");
CacheTransactionManager transactionManager = cache.getCacheTransactionManager();
transactionManager.begin();
region1.put(1, "value1");
transactionManager.commit();
transactionManager.begin();
region2.put(2, "value2");
transactionManager.commit();
return null;
});
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class RemoteTransactionDUnitTest method testPRTXGetEntryOnRemoteSide.
/**
* Make sure that getEntry returns null properly and values when it should
*/
@Test
public void testPRTXGetEntryOnRemoteSide() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region cust = getCache().getRegion(CUSTOMER);
CustId sup = new CustId(7);
Region.Entry e = cust.getEntry(sup);
assertNull(e);
CustId custId = new CustId(5);
cust.put(custId, new Customer("customer5", "address5"));
Region.Entry ee = cust.getEntry(custId);
assertNotNull(ee);
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.begin();
Region.Entry e2 = cust.getEntry(sup);
assertNull(e2);
mgr.commit();
Region.Entry e3 = cust.getEntry(sup);
assertNull(e3);
mgr.begin();
Customer dawg = new Customer("dawg", "dawgaddr");
cust.put(sup, dawg);
Region.Entry e4 = cust.getEntry(sup);
assertNotNull(e4);
assertEquals(dawg, e4.getValue());
mgr.commit();
Region.Entry e5 = cust.getEntry(sup);
assertNotNull(e5);
assertEquals(dawg, e5.getValue());
return null;
}
});
}
use of org.apache.geode.cache.CacheTransactionManager 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.CacheTransactionManager in project geode by apache.
the class RemoteTransactionDUnitTest method testTxFunctionWithOtherOps.
@Test
public void testTxFunctionWithOtherOps() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore1 = host.getVM(1);
VM datastore2 = host.getVM(2);
initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
SerializableCallable registerFunction = new SerializableCallable() {
public Object call() throws Exception {
FunctionService.registerFunction(new TXFunction());
return null;
}
};
accessor.invoke(registerFunction);
datastore1.invoke(registerFunction);
datastore2.invoke(registerFunction);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
TXManagerImpl mgr = getGemfireCache().getTXMgr();
mgr.begin();
try {
FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
fail("Expected exception not thrown");
} catch (TransactionException expected) {
}
Set filter = new HashSet();
filter.add(expectedCustId);
FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
assertEquals(expectedCustomer, custRegion.get(expectedCustId));
TXStateProxy tx = mgr.internalSuspend();
assertNull(custRegion.get(expectedCustId));
mgr.internalResume(tx);
return null;
}
});
final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
assertEquals(1, txOnDatastore1 + txOnDatastore2);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
CacheTransactionManager mgr = getGemfireCache().getTXMgr();
mgr.commit();
assertEquals(expectedCustomer, custRegion.get(expectedCustId));
custRegion.destroy(expectedCustId);
return null;
}
});
// test onMembers
SerializableCallable getMember = new SerializableCallable() {
public Object call() throws Exception {
return getGemfireCache().getMyId();
}
};
final InternalDistributedMember ds1 = (InternalDistributedMember) datastore1.invoke(getMember);
final InternalDistributedMember ds2 = (InternalDistributedMember) datastore2.invoke(getMember);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
// get owner for expectedKey
DistributedMember owner = pr.getOwnerForKey(pr.getKeyInfo(expectedCustId));
// get key on datastore1
CustId keyOnOwner = null;
keyOnOwner = getKeyOnMember(owner, pr);
TXManagerImpl mgr = getGemfireCache().getTXMgr();
mgr.begin();
// bootstrap tx on owner
pr.get(keyOnOwner);
Set<DistributedMember> members = new HashSet<DistributedMember>();
members.add(ds1);
members.add(ds2);
try {
FunctionService.onMembers(members).execute(TXFunction.id).getResult();
fail("expected exception not thrown");
} catch (TransactionException expected) {
}
FunctionService.onMember(owner).execute(TXFunction.id).getResult();
assertEquals(expectedCustomer, pr.get(expectedCustId));
TXStateProxy tx = mgr.internalSuspend();
assertNull(pr.get(expectedCustId));
mgr.internalResume(tx);
return null;
}
});
final Integer txOnDatastore1_1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
final Integer txOnDatastore2_1 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
assertEquals(1, txOnDatastore1_1 + txOnDatastore2_1);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
CacheTransactionManager mgr = getGemfireCache().getTXMgr();
mgr.commit();
assertEquals(expectedCustomer, custRegion.get(expectedCustId));
custRegion.destroy(expectedCustId);
return null;
}
});
// test function execution on data store
final DistributedMember owner = (DistributedMember) accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
return pr.getOwnerForKey(pr.getKeyInfo(expectedCustId));
}
});
SerializableCallable testFnOnDs = new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTXMgr();
PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
CustId keyOnDs = getKeyOnMember(pr.getMyId(), pr);
mgr.begin();
pr.get(keyOnDs);
Set filter = new HashSet();
filter.add(keyOnDs);
FunctionService.onRegion(pr).withFilter(filter).execute(TXFunction.id).getResult();
assertEquals(expectedCustomer, pr.get(expectedCustId));
TXStateProxy tx = mgr.internalSuspend();
assertNull(pr.get(expectedCustId));
mgr.internalResume(tx);
return null;
}
};
SerializableCallable closeTx = new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
CacheTransactionManager mgr = getGemfireCache().getTXMgr();
mgr.commit();
assertEquals(expectedCustomer, custRegion.get(expectedCustId));
custRegion.destroy(expectedCustId);
return null;
}
};
if (owner.equals(ds1)) {
datastore1.invoke(testFnOnDs);
final Integer txOnDatastore1_2 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
final Integer txOnDatastore2_2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
// ds1 has a local transaction, not
assertEquals(0, txOnDatastore1_2 + txOnDatastore2_2);
// remote
datastore1.invoke(closeTx);
} else {
datastore2.invoke(testFnOnDs);
final Integer txOnDatastore1_2 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
final Integer txOnDatastore2_2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
// ds1 has a local transaction, not
assertEquals(0, txOnDatastore1_2 + txOnDatastore2_2);
// remote
datastore2.invoke(closeTx);
}
// test that function is rejected if function target is not same as txState target
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTXMgr();
PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
CustId keyOnDs1 = getKeyOnMember(ds1, pr);
CustId keyOnDs2 = getKeyOnMember(ds2, pr);
mgr.begin();
// bootstrap txState
pr.get(keyOnDs1);
Set filter = new HashSet();
filter.add(keyOnDs2);
try {
FunctionService.onRegion(pr).withFilter(filter).execute(TXFunction.id).getResult();
fail("expected Exception not thrown");
} catch (TransactionDataRebalancedException expected) {
}
try {
FunctionService.onMember(ds2).execute(TXFunction.id).getResult();
fail("expected exception not thrown");
} catch (TransactionDataNotColocatedException expected) {
}
mgr.commit();
return null;
}
});
}
Aggregations