use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testConcurrentEventExpiryAndTake.
/**
* The basis of HARegionQueue's Add & remove operations etc , is that the event being added first
* goes into DACE , Region etc and finally it is published by adding into the available IDs Set.
* Similarly if an event is to be removed it should first be removed from availableIDs set & then
* from behind the scenes. It will be the responsibility of the thread removing from available IDs
* successfully which will remove the entry from all other places. Now if the expiry task makes
* the event from underlying null before removing from available IDs , there is a potential
* violation. This test will validate that behaviour
*/
@Test
public void testConcurrentEventExpiryAndTake() throws Exception {
AtomicBoolean complete = new AtomicBoolean(false);
AtomicBoolean expiryCalled = new AtomicBoolean(false);
AtomicBoolean allowExpiryToProceed = new AtomicBoolean(false);
HARegionQueueAttributes haa = new HARegionQueueAttributes();
haa.setExpiryTime(3);
RegionQueue regionqueue = new HARegionQueue.TestOnlyHARegionQueue(this.testName.getMethodName(), this.cache, haa) {
@Override
CacheListener createCacheListenerForHARegion() {
return new CacheListenerAdapter() {
@Override
public void afterInvalidate(EntryEvent event) {
if (event.getKey() instanceof Long) {
synchronized (HARegionQueueJUnitTest.this) {
expiryCalled.set(true);
HARegionQueueJUnitTest.this.notifyAll();
}
Thread.yield();
synchronized (HARegionQueueJUnitTest.this) {
while (!allowExpiryToProceed.get()) {
try {
HARegionQueueJUnitTest.this.wait();
} catch (InterruptedException e) {
errorCollector.addError(e);
break;
}
}
}
try {
expireTheEventOrThreadIdentifier(event);
} catch (CacheException e) {
errorCollector.addError(e);
} finally {
synchronized (HARegionQueueJUnitTest.this) {
complete.set(true);
HARegionQueueJUnitTest.this.notifyAll();
}
}
}
}
};
}
};
EventID ev1 = new EventID(new byte[] { 1 }, 1, 1);
Conflatable cf1 = new ConflatableObject("key", "value", ev1, true, this.testName.getMethodName());
regionqueue.put(cf1);
synchronized (this) {
while (!expiryCalled.get()) {
wait();
}
}
try {
Object o = regionqueue.take();
assertThat(o, nullValue());
} catch (Exception e) {
throw new AssertionError("Test failed due to exception ", e);
} finally {
synchronized (this) {
allowExpiryToProceed.set(true);
notifyAll();
}
}
synchronized (this) {
while (!complete.get()) {
wait();
}
}
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testConcurrentDispatcherAndRemovalForSameRegionSameThreadId.
/**
* This test tests remove operation is causing the insertion of sequence ID for existing
* ThreadIdentifier object and concurrently the QRM thread is iterating over the Map to form the
* Data Set for dispatch. There should not be any Data Loss
*
* In this test, first we add x number of events for unique thread id for the same region then we
* start two concurrent threads. One which does add to dispatched events map with sequence id's
* greater than x but the same ThreadIds as previous. The second thread does createMessageList
* (which removes from the dispatched events map) and stores the list
*
* After the two threads have operated, createMessageList is called again and the data is stored
* in another list.
*
* The data in the list is populated on a map against the threadId.
*
* It is then verified to see that all the sequence should be greater than x
*/
@Test
public void testConcurrentDispatcherAndRemovalForSameRegionSameThreadId() throws Exception {
long numberOfIterations = 1000;
HARegionQueue hrq = createHARegionQueue(this.testName.getMethodName());
HARegionQueue.stopQRMThread();
ThreadIdentifier[] ids = new ThreadIdentifier[(int) numberOfIterations];
for (int i = 0; i < numberOfIterations; i++) {
ids[i] = new ThreadIdentifier(new byte[] { 1 }, i);
hrq.addDispatchedMessage(ids[i], i);
}
Thread thread1 = new Thread() {
@Override
public void run() {
try {
Thread.sleep(600);
} catch (InterruptedException e) {
errorCollector.addError(e);
}
list1 = HARegionQueue.createMessageListForTesting();
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
try {
Thread.sleep(480);
} catch (InterruptedException e) {
errorCollector.addError(e);
}
for (int i = 0; i < numberOfIterations; i++) {
hrq.addDispatchedMessage(ids[i], i + numberOfIterations);
}
}
};
thread1.start();
thread2.start();
ThreadUtils.join(thread1, 30 * 1000);
ThreadUtils.join(thread2, 30 * 1000);
List list2 = HARegionQueue.createMessageListForTesting();
Iterator iterator = list1.iterator();
boolean doOnce = false;
EventID id;
Map map = new HashMap();
while (iterator.hasNext()) {
if (!doOnce) {
iterator.next();
iterator.next();
doOnce = true;
} else {
id = (EventID) iterator.next();
map.put(new Long(id.getThreadID()), id.getSequenceID());
}
}
iterator = list2.iterator();
doOnce = false;
while (iterator.hasNext()) {
if (!doOnce) {
iterator.next();
iterator.next();
doOnce = true;
} else {
id = (EventID) iterator.next();
map.put(id.getThreadID(), id.getSequenceID());
}
}
iterator = map.values().iterator();
Long max = numberOfIterations;
while (iterator.hasNext()) {
Long next = (Long) iterator.next();
assertThat(" Expected all the sequence ID's to be greater than " + max + " but it is not so. Got sequence id " + next, next.compareTo(max), greaterThanOrEqualTo(0));
}
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testPutPath.
/**
* test all relevant data structures are updated on a local put
*/
@Test
public void testPutPath() throws Exception {
HARegionQueue regionQueue = createHARegionQueue(this.testName.getMethodName());
Conflatable cf = new ConflatableObject("key", "value", new EventID(new byte[] { 1 }, 1, 1), true, this.testName.getMethodName());
regionQueue.put(cf);
assertThat(" Expected region peek to return cf but it is not so ", regionQueue.peek(), is(cf));
assertThat(" Expected the available id's size not to be zero since expiry time has not been exceeded but it is not so ", !regionQueue.getAvalaibleIds().isEmpty(), is(true));
assertThat(" Expected conflation map to have entry for this key since expiry time has not been exceeded but it is not so ", ((Map) regionQueue.getConflationMapForTesting().get(this.testName.getMethodName())).get("key"), is(1L));
assertThat(" Expected eventID map size not to be zero since expiry time has not been exceeded but it is not so ", !regionQueue.getEventsMapForTesting().isEmpty(), is(true));
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARQAddOperationJUnitTest method testConcurrentPutAndQRM.
/**
* Concurrent arrival of put & QRM messagge for a ThreadIdentifier coming for 1st time. The
* LastDispatchedObject gets operated first by the put thread , the QRM thread should be blocked
* till the completion of add operation. Thus before QRM thread acts , the object should be
* present in the lastDispatchedSet & AvailableID. Then the QRM thread gets unblocked , it should
* remove from the available ID.
*/
@Test
public void testConcurrentPutAndQRM() throws Exception {
testFailed = false;
message = new StringBuffer();
final HARegionQueue regionqueue = createHARegionQueue("testConcurrentPutAndQRM");
final EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
final EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
Thread t1 = new Thread() {
public void run() {
try {
regionqueue.put(new ConflatableObject(KEY1, VALUE1, id1, true, "region1"));
regionqueue.put(new ConflatableObject(KEY2, VALUE2, id2, true, "region1"));
} catch (Exception e) {
message.append("Put in region queue failed");
testFailed = true;
}
}
};
t1.setPriority(Thread.MAX_PRIORITY);
Thread t2 = new Thread() {
public void run() {
try {
regionqueue.removeDispatchedEvents(id2);
} catch (Exception e) {
message.append("Removal by QRM in region queue failed");
testFailed = true;
}
}
};
t1.start();
t2.start();
ThreadUtils.join(t1, 180 * 1000);
ThreadUtils.join(t2, 180 * 1000);
if (testFailed)
fail("Test failed due to " + message);
assertEquals(0, regionqueue.getAvalaibleIds().size());
assertEquals(2, regionqueue.getLastDispatchedSequenceId(id2));
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARQAddOperationJUnitTest method testConcurrentQRMAndPut.
/**
* Concurrent arrival of put & QRM messagge for a ThreadIdentifier coming for 1st time. The
* LastDispatchedObject gets operated first by the QRM thread , the Put thread should be blocked
* till the completion of QRM operation. Thus put thread should see that last Sequence is > than
* the current & hence the put operation shud remove from region without adding the ID anywhere.
*/
@Test
public void testConcurrentQRMAndPut() throws Exception {
testFailed = false;
final HARegionQueue regionqueue = createHARegionQueue("testConcurrentQRMAndPut");
final EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
final EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
Thread t1 = new Thread() {
public void run() {
try {
regionqueue.put(new ConflatableObject(KEY1, VALUE1, id1, true, "region1"));
regionqueue.put(new ConflatableObject(KEY2, VALUE2, id2, true, "region1"));
} catch (Exception e) {
message.append("Put in region queue failed");
testFailed = true;
}
}
};
Thread t2 = new Thread() {
public void run() {
try {
regionqueue.removeDispatchedEvents(id2);
} catch (Exception e) {
message.append("Removal of Events by QRM in Region queue failed");
testFailed = true;
}
}
};
t2.setPriority(Thread.MAX_PRIORITY);
t2.start();
t1.start();
ThreadUtils.join(t1, 180 * 1000);
ThreadUtils.join(t2, 180 * 1000);
if (testFailed)
fail("Test failed due to " + message);
assertEquals(0, regionqueue.getAvalaibleIds().size());
assertEquals(2, regionqueue.getLastDispatchedSequenceId(id2));
}
Aggregations