use of org.apache.geode.internal.cache.Conflatable in project geode by apache.
the class HARegionQueueDUnitTest method concurrentOperationsDunitTest.
private void concurrentOperationsDunitTest(final boolean createBlockingQueue, final Scope rscope) {
// Create Cache and HARegionQueue in all the 4 VMs.
CacheSerializableRunnable createRgnsAndQueues = new CacheSerializableRunnable("CreateCache, mirrored Region & HARegionQueue with a CacheListener") {
@Override
public void run2() throws CacheException {
HARegionQueueDUnitTest test = new HARegionQueueDUnitTest();
System.getProperties().put("QueueRemovalThreadWaitTime", "2000");
cache = test.createCache();
AttributesFactory factory = new AttributesFactory();
factory.setScope(rscope);
factory.setDataPolicy(DataPolicy.REPLICATE);
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setExpiryTime(5);
try {
if (createBlockingQueue) {
hrq = HARegionQueue.getHARegionQueueInstance("testregion1", cache, hrqa, HARegionQueue.BLOCKING_HA_QUEUE, false);
} else {
hrq = HARegionQueue.getHARegionQueueInstance("testregion1", cache, hrqa, HARegionQueue.NON_BLOCKING_HA_QUEUE, false);
}
} catch (Exception e) {
throw new AssertionError(e);
}
factory.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterCreate(final EntryEvent event) {
Conflatable conflatable = new ConflatableObject(event.getKey(), event.getNewValue(), ((EntryEventImpl) event).getEventId(), false, event.getRegion().getFullPath());
try {
hrq.put(conflatable);
} catch (Exception e) {
fail("The put operation in queue did not succeed due to exception =", e);
}
}
@Override
public void afterUpdate(final EntryEvent event) {
Conflatable conflatable = new ConflatableObject(event.getKey(), event.getNewValue(), ((EntryEventImpl) event).getEventId(), true, event.getRegion().getFullPath());
try {
hrq.put(conflatable);
} catch (Exception e) {
fail("The put operation in queue did not succeed due to exception =", e);
}
}
});
cache.createRegion("test_region", factory.create());
}
};
vm0.invoke(createRgnsAndQueues);
vm1.invoke(createRgnsAndQueues);
vm2.invoke(createRgnsAndQueues);
vm3.invoke(createRgnsAndQueues);
CacheSerializableRunnable spawnThreadsAndperformOps = new CacheSerializableRunnable("Spawn multiple threads which do various operations") {
@Override
public void run2() throws CacheException {
opThreads = new Thread[4 + 2 + 2 + 2];
for (int i = 0; i < 4; ++i) {
opThreads[i] = new Thread(new RunOp(RunOp.PUT, i), "ID=" + i + ",Op=" + RunOp.PUT);
}
for (int i = 4; i < 6; ++i) {
opThreads[i] = new Thread(new RunOp(RunOp.PEEK, i), "ID=" + i + ",Op=" + RunOp.PEEK);
}
for (int i = 6; i < 8; ++i) {
opThreads[i] = new Thread(new RunOp(RunOp.TAKE, i), "ID=" + i + ",Op=" + RunOp.TAKE);
}
for (int i = 8; i < 10; ++i) {
opThreads[i] = new Thread(new RunOp(RunOp.TAKE, i), "ID=" + i + ",Op=" + RunOp.BATCH_PEEK);
}
for (int i = 0; i < opThreads.length; ++i) {
opThreads[i].start();
}
}
};
vm0.invokeAsync(spawnThreadsAndperformOps);
vm1.invokeAsync(spawnThreadsAndperformOps);
vm2.invokeAsync(spawnThreadsAndperformOps);
vm3.invokeAsync(spawnThreadsAndperformOps);
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
fail("Test failed as the test thread encoutered exception in sleep", e1);
}
// Asif : In case of blocking HARegionQueue do some extra puts so that the
// blocking threads
// are exited
CacheSerializableRunnable toggleFlag = new CacheSerializableRunnable("Toggle the flag to signal end of threads") {
@Override
public void run2() throws CacheException {
toCnt = false;
if (createBlockingQueue) {
try {
for (int i = 0; i < 100; ++i) {
hrq.put(new ConflatableObject("1", "1", new EventID(new byte[] { 1 }, 100, i), false, "/x"));
}
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
};
vm0.invokeAsync(toggleFlag);
vm1.invokeAsync(toggleFlag);
vm2.invokeAsync(toggleFlag);
vm3.invokeAsync(toggleFlag);
CacheSerializableRunnable joinWithThreads = new CacheSerializableRunnable("Join with the threads") {
@Override
public void run2() throws CacheException {
for (int i = 0; i < opThreads.length; ++i) {
if (opThreads[i].isInterrupted()) {
fail("Test failed because thread encountered exception");
}
ThreadUtils.join(opThreads[i], 30 * 1000);
}
}
};
vm0.invoke(joinWithThreads);
vm1.invoke(joinWithThreads);
vm2.invoke(joinWithThreads);
vm3.invoke(joinWithThreads);
System.getProperties().remove("QueueRemovalThreadWaitTime");
}
use of org.apache.geode.internal.cache.Conflatable in project geode by apache.
the class HARegionQueueJUnitTest method testSafeConflationRemoval.
/**
* This test tests safe removal from the conflation map. i.e operations should only remove old
* values and not the latest value
*/
@Test
public void testSafeConflationRemoval() throws Exception {
hrqForTestSafeConflationRemoval = new HARQTestClass("testSafeConflationRemoval", this.cache);
Conflatable cf1 = new ConflatableObject("key1", "value", new EventID(new byte[] { 1 }, 1, 1), true, "testSafeConflationRemoval");
hrqForTestSafeConflationRemoval.put(cf1);
hrqForTestSafeConflationRemoval.removeDispatchedEvents(new EventID(new byte[] { 1 }, 1, 1));
Map map = (Map) hrqForTestSafeConflationRemoval.getConflationMapForTesting().get("testSafeConflationRemoval");
assertThat("Expected the counter to be 2 since it should not have been deleted but it is not so ", map.get("key1"), is(2L));
}
use of org.apache.geode.internal.cache.Conflatable in project geode by apache.
the class HARegionQueueStatsJUnitTest method testExpiryStats.
/**
* This test does the following:<br>
* 1)Create HARegionQueue with expiry time as 1 sec<br>
* 2)Add objects with unique eventids and conflation false and sleep for some time.<br>
* 3)Verify that statistics object is not null<br>
* 4)Verify that total events added matches the eventsEnqued stats<br>
* 5)Verify that eventsExpired stats is same as total events added as all events should have
* expired by 1 sec.
*
* @throws Exception - thrown if any problem occurs in test execution
*/
@Test
public void testExpiryStats() throws Exception {
HARegionQueueAttributes haa = new HARegionQueueAttributes();
haa.setExpiryTime(1);
HARegionQueue rq = createHARegionQueue("testExpiryStats", haa);
Conflatable cf = null;
int totalEvents = 100;
for (int i = 0; i < totalEvents; i++) {
cf = new ConflatableObject("key" + i, "value" + i, new EventID(new byte[] { 1 }, 1, i), false, "testing");
rq.put(cf);
}
Thread.sleep(3000);
HARegionQueueStats stats = rq.stats;
assertNotNull("stats for HARegionQueue found null", stats);
assertEquals("eventsEnqued by stats not equal to the actual number of events added to the queue", totalEvents, stats.getEventsEnqued());
assertEquals("expiredEvents not updated", totalEvents, stats.getEventsExpired());
}
use of org.apache.geode.internal.cache.Conflatable in project geode by apache.
the class HARegionQueueStatsJUnitTest method testRemoveByQrmStats.
/**
* This test does the following:<br>
* 1)Create HARegionQueue.<br>
* 2)Add objects with unique eventids and conflation false<br>
* 3)Remove the events through QRM api ( <code>removeDispatchedEvents(EventID id)</code>) with a
* certain lastDispatchedSeqId<br>
* 4)Verify that statistics object is not null<br>
* 5)Verify that total events added matches the eventsEnqued stats<br>
* 6)Verify that eventsRemovedByQrm stats is same as the number of events removed by QRM (upto the
* event having lastDispatchedSeqId, step 3).
*
* @throws Exception - thrown if any problem occurs in test execution
*/
@Test
public void testRemoveByQrmStats() throws Exception {
HARegionQueue rq = createHARegionQueue("testRemoveByQrmStats");
Conflatable cf = null;
int totalEvents = 100;
for (int i = 0; i < totalEvents; i++) {
cf = new ConflatableObject("key" + i, "value" + i, new EventID(new byte[] { 1 }, 1, i), false, "testing");
rq.put(cf);
}
// call for removal thru QRM api
int lastDispatchedSqId = 20;
EventID id = new EventID(new byte[] { 1 }, 1, lastDispatchedSqId);
rq.removeDispatchedEvents(id);
HARegionQueueStats stats = rq.getStatistics();
assertNotNull("stats for HARegionQueue found null", stats);
assertEquals("eventsEnqued by stats not equal to the actual number of events added to the queue", totalEvents, stats.getEventsEnqued());
assertEquals("eventsRemovedByQrm stats not updated properly", (lastDispatchedSqId + 1), stats.getEventsRemovedByQrm());
}
use of org.apache.geode.internal.cache.Conflatable in project geode by apache.
the class HARegionQueueStatsJUnitTest method testRemoveStats.
/**
* This test does the following:<br>
* 1)Create HARegionQueue.<br>
* 2)Add objects with unique eventids and conflation false<br>
* 3)Do some random peek and peek-batch operations and then call remove()<br>
* 4)Verify that statistics object is not null<br>
* 5)Verify that total events added matches the eventsEnqued stats<br>
* 6)Verify that eventsRemoved stats is same as the maximum batch size peeked in above peek
* operations(step 3).
*
* @throws Exception - thrown if any problem occurs in test execution
*/
@Test
public void testRemoveStats() throws Exception {
HARegionQueue rq = createHARegionQueue("testRemoveStats");
Conflatable cf = null;
int totalEvents = 100;
for (int i = 0; i < totalEvents; i++) {
cf = new ConflatableObject("key" + i, "value" + i, new EventID(new byte[] { 1 }, 1, i), false, "testing");
rq.put(cf);
}
// do some random peek operations.
int maxPeekBatchSize = 50;
rq.peek();
rq.peek(8);
rq.peek(maxPeekBatchSize);
rq.peek(35);
rq.peek();
rq.remove();
HARegionQueueStats stats = rq.getStatistics();
assertNotNull("stats for HARegionQueue found null", stats);
assertEquals("eventsEnqued by stats not equal to the actual number of events added to the queue", totalEvents, stats.getEventsEnqued());
assertEquals("All the events peeked were not removed", maxPeekBatchSize, stats.getEventsRemoved());
}
Aggregations