use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class TestFunction method doTxPutsPR.
public static void doTxPutsPR(String regionName, int numPuts, int size) throws Exception {
Region r = cache.getRegion(Region.SEPARATOR + regionName);
CacheTransactionManager mgr = cache.getCacheTransactionManager();
for (int x = 0; x < numPuts; x++) {
int temp = (int) (Math.floor(Math.random() * size));
try {
mgr.begin();
r.put(temp, temp);
mgr.commit();
} catch (org.apache.geode.cache.TransactionDataNotColocatedException txe) {
// ignore colocation issues or primary bucket issues
} catch (org.apache.geode.cache.CommitConflictException cce) {
// ignore - conflicts are ok and expected
}
}
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class LocalQueryServiceJUnitTest method testLocalQueryServiceWithTransaction.
@Test
public void testLocalQueryServiceWithTransaction() throws Exception {
CacheTransactionManager cacheTransactionManager = clientCache.getCacheTransactionManager();
cacheTransactionManager.begin();
SelectResults selectResults = executeLocalQuery(this.clientCache, "SELECT * from /localRegion");
assertEquals(this.numEntries, selectResults.size());
cacheTransactionManager.commit();
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class CopyOnReadIndexDUnitTest method helpTestTransactionsOnReplicatedRegion.
public void helpTestTransactionsOnReplicatedRegion(final String queryString, final int numPortfolios, final int numExpectedResults, final boolean hasIndex) throws Exception {
resetInstanceCount(vm0);
resetInstanceCount(vm1);
resetInstanceCount(vm2);
createReplicatedRegion(vm0, "portfolios");
createReplicatedRegion(vm1, "portfolios");
createReplicatedRegion(vm2, "portfolios");
// counts
if (hasIndex) {
vm0.invoke(new SerializableCallable() {
public Object call() throws Exception {
QueryTestUtils utils = new QueryTestUtils();
utils.createHashIndex("idIndex", "p.ID", "/portfolios p");
return null;
}
});
// let's not create index on vm1 to check different scenarios
vm2.invoke(new SerializableCallable() {
public Object call() throws Exception {
QueryTestUtils utils = new QueryTestUtils();
utils.createHashIndex("idIndex", "p.ID", "/portfolios p");
return null;
}
});
}
vm0.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region region = getCache().getRegion("/portfolios");
for (int i = 0; i < numPortfolios; i++) {
Portfolio p = new Portfolio(i);
p.status = "testStatus";
p.positions = new HashMap();
p.positions.put("" + i, new Position("" + i, 20));
region.put("key " + i, p);
}
// We should have the same number of portfolio objects that we created for the put
Wait.waitForCriterion(verifyPortfolioCount(numPortfolios), 5000, 200, true);
return null;
}
});
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
// At this point, we should only have serialized values in this vm
Region region = getCache().getRegion("/portfolios");
Wait.waitForCriterion(verifyPortfolioCount(0), 0, 200, true);
return null;
}
});
vm2.invoke(new SerializableCallable() {
public Object call() throws Exception {
// There is an index for vm2, so we should have deserialized values at this point,
Region region = getCache().getRegion("/portfolios");
if (hasIndex) {
Wait.waitForCriterion(verifyPortfolioCount(numPortfolios), 0, 200, true);
} else {
Wait.waitForCriterion(verifyPortfolioCount(0), 0, 200, true);
}
return null;
}
});
// start transaction
// execute query
// modify results
// check instance count
vm0.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region region = getCache().getRegion("/portfolios");
CacheTransactionManager txManager = region.getCache().getCacheTransactionManager();
try {
txManager.begin();
QueryService qs = getCache().getQueryService();
Query query = qs.newQuery(queryString);
SelectResults results = (SelectResults) query.execute();
assertEquals(numExpectedResults, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
p.status = "discardStatus";
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
p.status = "discardStatus";
}
}
txManager.commit();
} catch (CommitConflictException conflict) {
Assert.fail("commit conflict exception", conflict);
}
// We have created puts from our previous callable
// Now we have copied the results from the query
Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults + numPortfolios), 0, 200, true);
return null;
}
});
// Check objects in cache on vm1
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region region = getCache().getRegion("/portfolios");
QueryService qs = getCache().getQueryService();
Query query = qs.newQuery(queryString);
SelectResults results = (SelectResults) query.execute();
assertEquals(numExpectedResults, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
assertEquals("status should not have been changed", "testStatus", p.status);
p.status = "discardStatus";
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
assertEquals("status should not have been changed", "testStatus", p.status);
p.status = "discardStatus";
}
}
// first it must deserialize the portfolios in the replicated region
// then we do a copy on read of these deserialized objects for the final result set
Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults + numPortfolios), 0, 200, true);
results = (SelectResults) query.execute();
assertEquals(numExpectedResults, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
assertEquals("status should not have been changed", "testStatus", p.status);
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
assertEquals("status should not have been changed", "testStatus", p.status);
}
}
// we never created index on vm1
// so in this case, we always have to deserialize the value from the region
Wait.waitForCriterion(verifyPortfolioCount(numPortfolios * 2 + numExpectedResults * 2), 0, 200, true);
return null;
}
});
// Check objects in cache on vm2
vm2.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region region = getCache().getRegion("/portfolios");
QueryService qs = getCache().getQueryService();
Query query = qs.newQuery(queryString);
SelectResults results = (SelectResults) query.execute();
assertEquals(numExpectedResults, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
assertEquals("status should not have been changed", "testStatus", p.status);
p.status = "discardStatus";
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
assertEquals("status should not have been changed", "testStatus", p.status);
p.status = "discardStatus";
}
}
// with or without index, the values had to have been deserialized at one point
Wait.waitForCriterion(verifyPortfolioCount(numPortfolios + numExpectedResults), 0, 200, true);
results = (SelectResults) query.execute();
assertEquals(numExpectedResults, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
assertEquals("status should not have been changed", "testStatus", p.status);
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
assertEquals("status should not have been changed", "testStatus", p.status);
}
}
if (hasIndex) {
// we have an index, so the values are already deserialized
// total is now our original deserialization amount : numPortfolios
// two query results copied.
Wait.waitForCriterion(verifyPortfolioCount(numPortfolios + numExpectedResults * 2), 0, 200, true);
} else {
// we never created index on vm1
// so in this case, we always have to deserialize the value from the region
Wait.waitForCriterion(verifyPortfolioCount(numPortfolios * 2 + numExpectedResults * 2), 0, 200, true);
}
return null;
}
});
// Check objects in cache on vm0
vm0.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region region = getCache().getRegion("/portfolios");
QueryService qs = getCache().getQueryService();
Query query = qs.newQuery(queryString);
SelectResults results = (SelectResults) query.execute();
assertEquals(numExpectedResults, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
assertEquals("status should not have been changed", "testStatus", p.status);
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
assertEquals("status should not have been changed", "testStatus", p.status);
}
}
// with or without index, the values we put in the region were already deserialized values
Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults * 2 + numPortfolios), 0, 200, true);
return null;
}
});
destroyRegion("portfolio", vm0);
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class CacheXml66DUnitTest method testMultipleTXListener.
/**
* Tests multiple transaction listeners
*
* @since GemFire 5.0
*/
@Test
public void testMultipleTXListener() throws Exception {
CacheCreation cache = new CacheCreation();
CacheTransactionManagerCreation txMgrCreation = new CacheTransactionManagerCreation();
TransactionListener l1 = new MyTestTransactionListener();
TransactionListener l2 = new MySecondTestTransactionListener();
txMgrCreation.addListener(l1);
txMgrCreation.addListener(l2);
cache.addCacheTransactionManagerCreation(txMgrCreation);
testXml(cache);
{
CacheTransactionManager tm = getCache().getCacheTransactionManager();
assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l2);
assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] {}), Arrays.asList(tm.getListeners()));
tm.addListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
tm.addListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
tm.addListener(l2);
assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l2 }), Arrays.asList(tm.getListeners()));
tm.removeListener(l1);
assertEquals(Arrays.asList(new TransactionListener[] { l2 }), Arrays.asList(tm.getListeners()));
tm.initListeners(new TransactionListener[] { l1, l2 });
assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
}
}
use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.
the class CallbackArgDUnitTest method doTest.
private void doTest() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(getCurrentExpectedKey(), e.getKey());
assertEquals(callbackArg, e.getCallbackArgument());
assertEquals(true, e.isCallbackArgumentAvailable());
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
r2.createSubregion("r3", af.create());
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
ArrayList keys = new ArrayList();
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
keys.add(ee.getKey());
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
CallbackArgDUnitTest.this.invokeCount = 1;
}
};
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ctm.addListener(tl1);
this.invokeCount = 0;
this.clCount = 0;
this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
doCommitOtherVm();
assertEquals(1, this.invokeCount);
assertEquals(6, this.clCount);
}
Aggregations