use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testBlockingQueueForTakeWhenPeekInProgress.
/**
* Peek on a blocking queue is in progress & the single element is removed either by take or by
* QRM thread , the peek should block correctly.
*/
@Test
public void testBlockingQueueForTakeWhenPeekInProgress() throws Exception {
TestBlockingHARegionQueue regionQueue = new TestBlockingHARegionQueue("testBlockQueueForTakeWhenPeekInProgress", this.cache);
Thread[] threads = new Thread[3];
for (int i = 0; i < 3; i++) {
threads[i] = new Thread() {
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
Object obj = regionQueue.peek();
if (obj == null) {
errorCollector.addError(new AssertionError("Failed : failed since object was null and was not expected to be null"));
}
long totalTime = System.currentTimeMillis() - startTime;
if (totalTime < 4000) {
errorCollector.addError(new AssertionError("Failed : Expected time to be greater than 4000 but it is not so"));
}
} catch (Exception e) {
errorCollector.addError(e);
}
}
};
}
for (int k = 0; k < 3; k++) {
threads[k].start();
}
Thread.sleep(4000);
EventID id = new EventID(new byte[] { 1 }, 1, 1);
EventID id1 = new EventID(new byte[] { 1 }, 1, 2);
regionQueue.takeWhenPeekInProgress = true;
regionQueue.put(new ConflatableObject("key", "value", id, true, this.testName.getMethodName()));
Thread.sleep(2000);
regionQueue.put(new ConflatableObject("key1", "value1", id1, true, this.testName.getMethodName()));
long startTime = System.currentTimeMillis();
for (int k = 0; k < 3; k++) {
ThreadUtils.join(threads[k], 60 * 1000);
}
long totalTime = System.currentTimeMillis() - startTime;
if (totalTime >= 60000) {
fail(" Test taken too long ");
}
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testConcurrentDispatcherAndRemovalForMultipleRegionsSameThreadId.
/**
* This test tests remove operation is causing the insertion of a sequence ID for a new HA Region
* 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 2 regions then we
* start two concurrent threads. One which does add to dispatched events map to 3 new regions
*
* 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 a total of x entries are present in the map
*/
@Test
public void testConcurrentDispatcherAndRemovalForMultipleRegionsSameThreadId() throws Exception {
int numberOfIterations = 10000;
HARegionQueue hrq1 = createHARegionQueue(this.testName.getMethodName() + "-1");
HARegionQueue hrq2 = createHARegionQueue(this.testName.getMethodName() + "-2");
HARegionQueue hrq3 = createHARegionQueue(this.testName.getMethodName() + "-3");
HARegionQueue hrq4 = createHARegionQueue(this.testName.getMethodName() + "-4");
HARegionQueue hrq5 = createHARegionQueue(this.testName.getMethodName() + "-5");
HARegionQueue.stopQRMThread();
ThreadIdentifier[] ids = new ThreadIdentifier[(int) numberOfIterations];
for (int i = 0; i < numberOfIterations; i++) {
ids[i] = new ThreadIdentifier(new byte[] { 1 }, i);
hrq1.addDispatchedMessage(ids[i], i);
hrq2.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++) {
hrq3.addDispatchedMessage(ids[i], i);
hrq4.addDispatchedMessage(ids[i], i);
hrq5.addDispatchedMessage(ids[i], i);
}
}
};
thread1.start();
thread2.start();
ThreadUtils.join(thread1, 30 * 1000);
ThreadUtils.join(thread2, 30 * 1000);
List list2 = HARegionQueue.createMessageListForTesting();
Iterator iterator = list1.iterator();
boolean doOnce = true;
EventID id;
Map map = new HashMap();
while (iterator.hasNext()) {
if (!doOnce) {
// read the total message size
iterator.next();
doOnce = true;
} else {
// region name;
iterator.next();
int size = (Integer) iterator.next();
for (int i = 0; i < size; i++) {
id = (EventID) iterator.next();
map.put(new ThreadIdentifier(id.getMembershipID(), id.getThreadID()), id.getSequenceID());
}
}
}
iterator = list2.iterator();
doOnce = true;
while (iterator.hasNext()) {
if (!doOnce) {
// read the total message size
iterator.next();
doOnce = true;
} else {
// region name;
iterator.next();
int size = (Integer) iterator.next();
for (int i = 0; i < size; i++) {
id = (EventID) iterator.next();
map.put(new ThreadIdentifier(id.getMembershipID(), id.getThreadID()), id.getSequenceID());
}
}
}
assertThat(" Expected the map size to be " + numberOfIterations + " but it is " + map.size(), map.size(), is(numberOfIterations));
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testExpiryUsingSystemProperty.
/**
* tests whether expiry of entry in the region queue occurs as expected using system property to
* set expiry
*/
@Test
public void testExpiryUsingSystemProperty() throws Exception {
System.setProperty(HARegionQueue.REGION_ENTRY_EXPIRY_TIME, "1");
HARegionQueueAttributes haa = new HARegionQueueAttributes();
HARegionQueue regionQueue = createHARegionQueue(this.testName.getMethodName(), haa);
long start = System.currentTimeMillis();
regionQueue.put(new ConflatableObject("key", "value", new EventID(new byte[] { 1 }, 1, 1), true, this.testName.getMethodName()));
Map map = (Map) regionQueue.getConflationMapForTesting().get(this.testName.getMethodName());
assertThat(!map.isEmpty(), is(true));
waitAtLeast(1000, start, () -> {
assertThat(map, is(Collections.emptyMap()));
assertThat(regionQueue.getRegion().keys(), is(Collections.emptySet()));
});
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testQRM.
/**
* Tests whether the QRM message removes the events correctly from the DACE & Conflation Map. The
* events which are of ID greater than that contained in QRM should stay
*/
@Test
public void testQRM() throws Exception {
RegionQueue regionqueue = createHARegionQueue(this.testName.getMethodName());
for (int i = 0; i < 10; ++i) {
regionqueue.put(new ConflatableObject("key" + (i + 1), "value", new EventID(new byte[] { 1 }, 1, i + 1), true, this.testName.getMethodName()));
}
EventID qrmID = new EventID(new byte[] { 1 }, 1, 5);
((HARegionQueue) regionqueue).removeDispatchedEvents(qrmID);
Map conflationMap = ((HARegionQueue) regionqueue).getConflationMapForTesting();
assertThat(((Map) conflationMap.get(this.testName.getMethodName())).size(), is(5));
Set availableIDs = ((HARegionQueue) regionqueue).getAvalaibleIds();
Set counters = ((HARegionQueue) regionqueue).getCurrentCounterSet(qrmID);
assertThat(availableIDs.size(), is(5));
assertThat(counters.size(), is(5));
for (int i = 5; i < 10; ++i) {
assertThat(((Map) (conflationMap.get(this.testName.getMethodName()))).containsKey("key" + (i + 1)), is(true));
assertThat(availableIDs.contains((long) (i + 1)), is(true));
assertThat(counters.contains((long) (i + 1)), is(true));
}
Region rgn = ((HARegionQueue) regionqueue).getRegion();
assertThat(rgn.keySet().size(), is(6));
}
use of org.apache.geode.internal.cache.EventID in project geode by apache.
the class HARegionQueueJUnitTest method testBatchPeekWithRemove.
private void testBatchPeekWithRemove(boolean createBlockingQueue) throws InterruptedException, IOException, ClassNotFoundException {
HARegionQueue regionQueue = createHARegionQueue(createBlockingQueue);
for (int i = 0; i < 10; ++i) {
EventID ev1 = new EventID(new byte[] { 1 }, 1, i);
Conflatable cf1 = new ConflatableObject("key", "value", ev1, false, this.testName.getMethodName());
regionQueue.put(cf1);
}
List objs = regionQueue.peek(10, 5000);
assertThat(objs.size(), is(10));
Iterator itr = objs.iterator();
int j = 0;
while (itr.hasNext()) {
Conflatable conf = (Conflatable) itr.next();
assertThat(conf, notNullValue());
assertThat("The sequence ID of the objects in the queue are not as expected", conf.getEventId().getSequenceID(), is((long) j++));
}
regionQueue.remove();
assertThat(regionQueue.size(), is(0));
}
Aggregations