use of org.apache.geode.cache.TransactionDataNotColocatedException 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.cache.TransactionDataNotColocatedException 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;
}
});
}
use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.
the class MemberFunctionExecutor method validateExecution.
@Override
public void validateExecution(final Function function, final Set dest) {
final InternalCache cache = GemFireCacheImpl.getInstance();
if (cache != null && cache.getTxManager().getTXState() != null) {
if (dest.size() > 1) {
throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_ON_MORE_THAN_ONE_NODE.toLocalizedString());
} else {
assert dest.size() == 1;
DistributedMember funcTarget = (DistributedMember) dest.iterator().next();
DistributedMember target = cache.getTxManager().getTXState().getTarget();
if (target == null) {
cache.getTxManager().getTXState().setTarget(funcTarget);
} else if (!target.equals(funcTarget)) {
throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_EXECUTION_NOT_COLOCATED.toLocalizedString());
}
}
}
if (function.optimizeForWrite() && cache != null && cache.getInternalResourceManager().getHeapMonitor().containsHeapCriticalMembers(dest) && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
Set<InternalDistributedMember> hcm = cache.getResourceAdvisor().adviseCritialMembers();
Set<DistributedMember> sm = SetUtils.intersection(hcm, dest);
throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(new Object[] { function.getId(), sm }), sm);
}
}
use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.
the class DistributedRegionFunctionExecutor method validateExecution.
/*
* (non-Javadoc)
*
* @see
* org.apache.geode.internal.cache.execute.AbstractExecution#validateExecution(org.apache.geode.
* cache.execute.Function, java.util.Set)
*/
@Override
public void validateExecution(Function function, Set targetMembers) {
InternalCache cache = region.getGemFireCache();
if (cache != null && cache.getTxManager().getTXState() != null) {
if (targetMembers.size() > 1) {
throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_ON_MORE_THAN_ONE_NODE.toLocalizedString());
} else {
assert targetMembers.size() == 1;
DistributedMember funcTarget = (DistributedMember) targetMembers.iterator().next();
DistributedMember target = cache.getTxManager().getTXState().getTarget();
if (target == null) {
cache.getTxManager().getTXState().setTarget(funcTarget);
} else if (!target.equals(funcTarget)) {
throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_EXECUTION_NOT_COLOCATED_0_1.toLocalizedString(target, funcTarget));
}
}
}
if (!MemoryThresholds.isLowMemoryExceptionDisabled() && function.optimizeForWrite()) {
try {
region.checkIfAboveThreshold(null);
} catch (LowMemoryException ignore) {
Set<DistributedMember> htrm = region.getMemoryThresholdReachedMembers();
throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(function.getId(), htrm), htrm);
}
}
}
use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.
the class PartitionedRegion method getDataRegionForRead.
@Override
public LocalRegion getDataRegionForRead(final KeyInfo keyInfo) {
final Object entryKey = keyInfo.getKey();
BucketRegion br;
try {
PartitionedRegionDataStore ds = getDataStore();
if (ds == null) {
throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_ON_DATASTORE.toLocalizedString());
}
// TODO provide appropriate Operation and arg
int bucketId = keyInfo.getBucketId();
if (bucketId == KeyInfo.UNKNOWN_BUCKET) {
bucketId = PartitionedRegionHelper.getHashKey(this, null, entryKey, keyInfo.getValue(), keyInfo.getCallbackArg());
keyInfo.setBucketId(bucketId);
}
br = ds.getInitializedBucketWithKnownPrimaryForId(null, bucketId);
if (keyInfo.isCheckPrimary()) {
try {
br.checkForPrimary();
} catch (PrimaryBucketException pbe) {
throw new TransactionDataRebalancedException(LocalizedStrings.PartitionedRegion_TRANSACTIONAL_DATA_MOVED_DUE_TO_REBALANCING.toLocalizedString(), pbe);
}
}
} catch (RegionDestroyedException ignore) {
// TODO: why is this purposely not wrapping the original cause?
throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_KEY_0_NOT_COLOCATED_WITH_TRANSACTION.toLocalizedString(entryKey));
} catch (ForceReattemptException ignore) {
br = null;
}
return br;
}
Aggregations