use of org.apache.geode.internal.cache.execute.data.CustId in project geode by apache.
the class RemoteTransactionDUnitTest method doTestBasicBulkOP.
private void doTestBasicBulkOP(final OP op) {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore1 = host.getVM(1);
VM datastore2 = host.getVM(2);
initAccessorAndDataStore(accessor, datastore1, datastore2, 1);
if (op.equals(OP.REMOVEALL)) {
// for remove all populate more data
accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
for (int i = 0; i < 50; i++) {
custRegion.put(new CustId(i), new Customer("name" + i, "address" + i));
}
return null;
}
});
}
final List ds1Buckets = (List) datastore1.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
// do local operations with rollback and then commit
Map<CustId, Customer> custMap = new HashMap<>();
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
PartitionedRegion pr = ((PartitionedRegion) custRegion);
List localBuckets = pr.getLocalPrimaryBucketsListTestOnly();
System.out.println("localBuckets:" + localBuckets);
for (int i = 10; i < 20; i++) {
int hash = PartitionedRegionHelper.getHashKey(i, pr.getPartitionAttributes().getTotalNumBuckets());
if (localBuckets.contains(hash)) {
custMap.put(new CustId(i), new Customer("name" + i, "address" + i));
}
}
System.out.println("SWAP:custMap:" + custMap);
int regionSize = custRegion.size();
getCache().getCacheTransactionManager().begin();
if (op.equals(OP.PUTALL)) {
custRegion.putAll(custMap);
} else {
custRegion.removeAll(custMap.keySet());
}
getCache().getCacheTransactionManager().rollback();
assertEquals(regionSize, custRegion.size());
// now commit
getCache().getCacheTransactionManager().begin();
if (op.equals(OP.PUTALL)) {
custRegion.putAll(custMap);
} else {
custRegion.removeAll(custMap.keySet());
}
getCache().getCacheTransactionManager().commit();
assertEquals(getExpectedSize(custMap, regionSize), custRegion.size());
// bulk op on other member
custMap.clear();
for (int i = 10; i < 20; i++) {
int hash = PartitionedRegionHelper.getHashKey(i, pr.getPartitionAttributes().getTotalNumBuckets());
if (!localBuckets.contains(hash)) {
// not on local member
custMap.put(new CustId(i), new Customer("name" + i, "address" + i));
}
}
System.out.println("SWAP:custMap:" + custMap);
regionSize = custRegion.size();
getCache().getCacheTransactionManager().begin();
if (op.equals(OP.PUTALL)) {
custRegion.putAll(custMap);
} else {
custRegion.removeAll(custMap.keySet());
}
getCache().getCacheTransactionManager().rollback();
assertEquals(regionSize, custRegion.size());
// now commit
getCache().getCacheTransactionManager().begin();
if (op.equals(OP.PUTALL)) {
custRegion.putAll(custMap);
} else {
custRegion.removeAll(custMap.keySet());
}
getCache().getCacheTransactionManager().commit();
assertEquals(getExpectedSize(custMap, regionSize), custRegion.size());
return localBuckets;
}
private int getExpectedSize(Map<CustId, Customer> custMap, int regionSize) {
if (op.equals(OP.REMOVEALL)) {
return regionSize - custMap.size();
}
return regionSize + custMap.size();
}
});
accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
// do a transaction on one of the nodes
Map<CustId, Customer> custMap = new HashMap<>();
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
for (int i = 20; i < 30; i++) {
int hash = PartitionedRegionHelper.getHashKey(i, custRegion.getAttributes().getPartitionAttributes().getTotalNumBuckets());
if (ds1Buckets.contains(hash)) {
custMap.put(new CustId(i), new Customer("name" + i, "address" + i));
}
}
System.out.println("SWAP:custMap:" + custMap);
int regionSize = custRegion.size();
getCache().getCacheTransactionManager().begin();
if (op.equals(OP.PUTALL)) {
custRegion.putAll(custMap);
} else {
custRegion.removeAll(custMap.keySet());
}
getCache().getCacheTransactionManager().rollback();
assertEquals(regionSize, custRegion.size());
// now commit
getCache().getCacheTransactionManager().begin();
if (op.equals(OP.PUTALL)) {
custRegion.putAll(custMap);
} else {
custRegion.removeAll(custMap.keySet());
}
getCache().getCacheTransactionManager().commit();
assertEquals(getExpectedSize(custMap, regionSize), custRegion.size());
return null;
}
private int getExpectedSize(Map<CustId, Customer> custMap, int regionSize) {
if (op.equals(OP.REMOVEALL)) {
return regionSize - custMap.size();
}
return regionSize + custMap.size();
}
});
}
use of org.apache.geode.internal.cache.execute.data.CustId in project geode by apache.
the class RemoteTransactionDUnitTest method testEntriesIterationOnRR.
@Test
public void testEntriesIterationOnRR() {
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);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
Region rr = getGemfireCache().getRegion(D_REFERENCE);
TXManagerImpl mgr = getGemfireCache().getTxManager();
mgr.begin();
CustId custId = new CustId(5);
Customer customer = new Customer("customer5", "address5");
custRegion.put(custId, customer);
Set set = rr.entrySet();
Iterator it = set.iterator();
int i = 0;
while (it.hasNext()) {
i++;
it.next();
}
assertEquals(5, i);
// assertTrue(getCustIdSet(5).equals(set));
assertEquals(5, rr.entrySet().size());
rr.put(custId, customer);
set = rr.entrySet();
// assertTrue(getCustIdSet(6).equals(set));
it = set.iterator();
i = 0;
while (it.hasNext()) {
i++;
it.next();
}
assertEquals(6, i);
assertEquals(6, rr.entrySet().size());
assertNotNull(rr.get(custId));
TXStateProxy tx = mgr.internalSuspend();
// assertIndexDetailsEquals(getCustIdSet(5), rr.entrySet());
assertEquals(5, rr.entrySet().size());
assertNull(rr.get(custId));
mgr.internalResume(tx);
mgr.commit();
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.CustId in project geode by apache.
the class RemoteTransactionDUnitTest method doNonColocatedbulkOp.
private void doNonColocatedbulkOp(final OP op) {
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);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Map custMap = new HashMap();
for (int i = 0; i < 10; i++) {
CustId cId = new CustId(i);
Customer c = new Customer("name" + i, "addr" + i);
custMap.put(cId, c);
}
GemFireCacheImpl cache = getGemfireCache();
cache.getCacheTransactionManager().begin();
Region r = cache.getRegion(CUSTOMER);
try {
switch(op) {
case PUTALL:
r.putAll(custMap);
break;
case GETALL:
r.put(new CustId(1), new Customer("cust1", "addr1"));
r.getAll(custMap.keySet());
break;
default:
break;
}
fail("expected exception not thrown");
} catch (TransactionDataNotColocatedException e) {
}
cache.getCacheTransactionManager().rollback();
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.CustId in project geode by apache.
the class RemoteTransactionDUnitTest method testTXWithRICommitInDatastore.
@Test
public void testTXWithRICommitInDatastore() throws Exception {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
VM client = host.getVM(2);
initAccessorAndDataStore(accessor, datastore, 0);
int port = startServer(datastore);
createClientRegion(client, port, false, true);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
Region<OrderId, Order> orderRegion = getCache().getRegion(ORDER);
Region<CustId, Customer> refRegion = getCache().getRegion(D_REFERENCE);
CustId custId = new CustId(1);
OrderId orderId = new OrderId(1, custId);
getCache().getCacheTransactionManager().begin();
custRegion.put(custId, new Customer("foo", "bar"));
orderRegion.put(orderId, new Order("fooOrder"));
refRegion.put(custId, new Customer("foo", "bar"));
getCache().getCacheTransactionManager().commit();
return null;
}
});
client.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
Region<OrderId, Order> orderRegion = getCache().getRegion(ORDER);
Region<CustId, Customer> refRegion = getCache().getRegion(D_REFERENCE);
final ClientListener cl = (ClientListener) custRegion.getAttributes().getCacheListeners()[0];
WaitCriterion waitForListenerInvocation = new WaitCriterion() {
public boolean done() {
return cl.invoked;
}
public String description() {
return "listener was never invoked";
}
};
Wait.waitForCriterion(waitForListenerInvocation, 10 * 1000, 10, true);
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.CustId 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