Search in sources :

Example 31 with EventID

use of org.apache.geode.internal.cache.EventID in project geode by apache.

the class HARQAddOperationJUnitTest method testCleanUpForConflation.

/**
   * Concurrent arrival of put operations on different threadIDs but for same key . One should
   * conflate the other. Whosoever confaltes , that ID should be present in the availableIDs list ,
   * Region , ConflationMap & its HashSet for that ThreadIdentifier. The ID which gets conflated
   * should not be present in the availableID, Region & that ThreadIdentifier's HashSet . The
   * conflation map should contain the Old IDs position.
   */
@Test
public void testCleanUpForConflation() throws Exception {
    this.logWriter.info("HARQAddOperationJUnitTest : testCleanUpForConflation BEGIN");
    testFailed = false;
    message = null;
    final int numOfThreads = 10;
    final int numOfPuts = 567;
    final HARegionQueue regionqueue = createHARegionQueue("testCleanUpForConflation");
    this.logWriter.info("HARQAddOperationJUnitTest : testCleanUpForConflation after regionqueue create");
    /*
     * doing concurrent put operations on different threadIDs but for the same key
     */
    Thread[] threads = new Thread[10];
    for (int i = 0; i < numOfThreads; i++) {
        final long ids = i;
        threads[i] = new Thread() {

            public void run() {
                for (int j = 0; j < numOfPuts; j++) {
                    EventID id = new EventID(new byte[] { (byte) ids }, ids, j);
                    try {
                        regionqueue.put(new ConflatableObject(KEY1, id.getThreadID() + "VALUE" + j, id, true, "region1"));
                    } catch (Exception ex) {
                        testFailed = true;
                        message.append("put failed for the threadId " + id.getThreadID());
                    }
                }
            }
        };
    }
    for (int k = 0; k < numOfThreads; k++) {
        threads[k].start();
    }
    for (int k = 0; k < numOfThreads; k++) {
        ThreadUtils.join(threads[k], 180 * 1000);
    }
    this.logWriter.info("HARQAddOperationJUnitTest : testCleanUpForConflation after join");
    if (testFailed)
        fail("Test failed due to " + message);
    assertEquals("size of the conflation map should be 1 but actual size is " + regionqueue.getConflationMapForTesting().size(), 1, regionqueue.getConflationMapForTesting().size());
    assertEquals("size of the event map should be " + numOfThreads + " but actual size " + regionqueue.getEventsMapForTesting().size(), numOfThreads, regionqueue.getEventsMapForTesting().size());
    assertEquals("size of availableids should 1 but actual size " + regionqueue.getAvalaibleIds().size(), 1, regionqueue.getAvalaibleIds().size());
    int count = 0;
    for (int i = 0; i < numOfThreads; i++) {
        if ((regionqueue.getCurrentCounterSet(new EventID(new byte[] { (byte) i }, i, i))).size() > 0) {
            count++;
        }
    }
    assertEquals("size of the counter set is  1 but the actual size is " + count, 1, count);
    Long position = null;
    if (regionqueue.getAvalaibleIds().size() == 1) {
        position = (Long) regionqueue.getAvalaibleIds().iterator().next();
    }
    ConflatableObject id = (ConflatableObject) regionqueue.getRegion().get(position);
    assertEquals(regionqueue.getCurrentCounterSet(id.getEventId()).size(), 1);
    this.logWriter.info("HARQAddOperationJUnitTest : testCleanUpForConflation END");
}
Also used : EventID(org.apache.geode.internal.cache.EventID) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 32 with EventID

use of org.apache.geode.internal.cache.EventID in project geode by apache.

the class HARQAddOperationJUnitTest method testPeekForDiffBatchSizeAndRemoveSome.

/**
   * Test where a 5 separate threads do 4 puts each. 3 threads then concurrently do a peek of batch
   * size 5, 10 and 15 respectively. And all of them concurrently call remove. The remove should
   * ensure that the entries are deleted from the available IDs & the Counters set contained in
   * DACE.
   */
@Test
public void testPeekForDiffBatchSizeAndRemoveSome() throws Exception {
    testFailed = false;
    barrierCount = 0;
    message = null;
    final int numOfThreads = 5;
    final int numOfPuts = 4;
    final HARegionQueue regionqueue = createHARegionQueue("testPeekForDiffBatchSizeAndRemoveSome");
    Thread[] threads = new Thread[numOfThreads];
    for (int i = 0; i < numOfThreads; i++) {
        final long ids = i;
        threads[i] = new Thread() {

            public void run() {
                for (int j = 0; j < numOfPuts; j++) {
                    EventID id = new EventID(new byte[] { (byte) ids }, ids, j);
                    try {
                        regionqueue.put(new ConflatableObject(KEY1 + id.getThreadID() + j, id.getThreadID() + "VALUE" + j, id, false, "region1"));
                    } catch (Exception ex) {
                        testFailed = true;
                        message.append("put failed for the threadId " + id.getThreadID());
                    }
                }
            }
        };
    }
    for (int k = 0; k < numOfThreads; k++) {
        threads[k].start();
    }
    for (int k = 0; k < numOfThreads; k++) {
        ThreadUtils.join(threads[k], 180 * 1000);
    }
    if (testFailed)
        fail("Test failed due to " + message);
    testFailed = false;
    message = null;
    Thread[] threads_peek_remove = new Thread[numOfPuts - 1];
    for (int i = 1; i < numOfPuts; i++) {
        final int peakBatchSize = i * 5;
        threads_peek_remove[i - 1] = new Thread() {

            public void run() {
                try {
                    List peakObjects = regionqueue.peek(peakBatchSize);
                    assertEquals(peakBatchSize, peakObjects.size());
                    synchronized (HARQAddOperationJUnitTest.this) {
                        ++barrierCount;
                        if (barrierCount == 3) {
                            HARQAddOperationJUnitTest.this.notifyAll();
                        } else {
                            HARQAddOperationJUnitTest.this.wait();
                        }
                    }
                    regionqueue.remove();
                } catch (Exception ex) {
                    testFailed = true;
                    ex.printStackTrace();
                    message.append("Exception while performing peak operation " + ex.getStackTrace());
                }
            }
        };
    }
    for (int k = 0; k < numOfPuts - 1; k++) {
        threads_peek_remove[k].start();
    }
    for (int k = 0; k < numOfPuts - 1; k++) {
        ThreadUtils.join(threads_peek_remove[k], 180 * 1000);
    }
    if (testFailed)
        fail("Test failed due to " + message);
    assertEquals(5, regionqueue.getAvalaibleIds().size());
    this.logWriter.info("testPeekForDiffBatchSizeAndRemoveSome() completed successfully");
}
Also used : EventID(org.apache.geode.internal.cache.EventID) List(java.util.List) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 33 with EventID

use of org.apache.geode.internal.cache.EventID in project geode by apache.

the class HARQAddOperationJUnitTest method testAddWithQRMAndExpiry.

/**
   * Add with QRM & expiry : Add 10 conflatable objects (0-9). Send QRM LastDispatched as 4.
   * Validate the sequence ID field of LastDispatchedWrapper object is updated to 4. Perform Take
   * operations to remove next 5 to 9. The seqeunceId field should be 9. Allow ThreadIdenitifier to
   * expire. The expiration should fail as the original sequenceId ( -1) does not match with 9.
   * Validate reset with value 9 . The next expiry should remove the LastDisptachedWrapper
   */
@Test
public void testAddWithQRMAndExpiry() throws Exception {
    try {
        HARegionQueueAttributes attrs = new HARegionQueueAttributes();
        attrs.setExpiryTime(10);
        final HARegionQueue regionqueue = new HARegionQueue.TestOnlyHARegionQueue("testing", cache, attrs) {

            CacheListener createCacheListenerForHARegion() {
                return new CacheListenerAdapter() {

                    public void afterInvalidate(EntryEvent event) {
                        try {
                            expireTheEventOrThreadIdentifier(event);
                        } catch (CacheException ce) {
                            logger.error("HAREgionQueue::createCacheListener::Exception in the expiry thread", ce);
                        }
                        if (event.getKey() instanceof ThreadIdentifier) {
                            synchronized (HARQAddOperationJUnitTest.this) {
                                expiryCount++;
                                HARQAddOperationJUnitTest.this.notify();
                            }
                        }
                    }
                };
            }
        };
        Conflatable[] cf = new Conflatable[10];
        // put 10 conflatable objects
        for (int i = 0; i < 10; i++) {
            cf[i] = new ConflatableObject("key" + i, "value", new EventID(new byte[] { 1 }, 1, i), true, "testing");
            regionqueue.put(cf[i]);
        }
        ThreadIdentifier tID = new ThreadIdentifier(new byte[] { 1 }, 1);
        // verify that the sequence-id for Thread-identifier is -1 (default value).
        assertEquals(new Long(-1), regionqueue.getRegion().get(tID));
        // remove the first 5 - (0-4 sequence IDs)
        regionqueue.removeDispatchedEvents(new EventID(new byte[] { 1 }, 1, 4));
        // verify that the last dispatched event was of sequence id 4
        assertEquals(4, regionqueue.getLastDispatchedSequenceId(new EventID(new byte[] { 1 }, 1, 1)));
        // verify 1-5 not in region
        for (long i = 1; i < 6; i++) {
            assertTrue(!regionqueue.getRegion().containsKey(new Long(i)));
        }
        // verify 6-10 still in region queue
        for (long i = 6; i < 11; i++) {
            assertTrue(regionqueue.getRegion().containsKey(new Long(i)));
        }
        // Perform 5 take operations to remove next 5-9 sequence ids
        for (long i = 6; i < 11; i++) {
            regionqueue.take();
        }
        // verify that the last dispatched event was of sequence id 10
        assertEquals(9, regionqueue.getLastDispatchedSequenceId(new EventID(new byte[] { 1 }, 1, 1)));
        // verify that sequence ids 1-10 all are removed from the RQ
        for (long i = 1; i < 11; i++) {
            assertTrue(!regionqueue.getRegion().containsKey(new Long(i)));
        }
        // wait until expiry thread has run once
        synchronized (HARQAddOperationJUnitTest.this) {
            if (0 == expiryCount) {
                HARQAddOperationJUnitTest.this.wait();
            }
            if (1 == expiryCount) {
                // verify that the Thread-identifier has not yet expired
                assertEquals(1, regionqueue.getEventsMapForTesting().size());
                // verify that the sequence-id for Thread-identifier is updated to 9
                assertEquals(new Long(9), regionqueue.getRegion().get(tID));
                // wait until expiry thread has run again
                HARQAddOperationJUnitTest.this.wait();
            }
        }
        // verify that the Thread-identifier has expired
        assertEquals(0, regionqueue.getEventsMapForTesting().size());
        // verify that the sequence-id for Thread-identifier is null
        assertNull(regionqueue.getRegion().get(tID));
    } catch (Exception e) {
        throw new AssertionError("Exception occurred in test due to", e);
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) EventID(org.apache.geode.internal.cache.EventID) Conflatable(org.apache.geode.internal.cache.Conflatable) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 34 with EventID

use of org.apache.geode.internal.cache.EventID in project geode by apache.

the class HARQAddOperationJUnitTest method testDispatchedMsgsMapUpdateOnTakes.

/*
   * This test does the following:<br> 1)Create a blocking HARegionQueue<br> 2)Add some events to
   * the queue with same ThreadIdentifier<br> 3)Do take() operations to drain the queue<br> 4)Verify
   * that dispatchedMessagesMap is not null<br> 5)Verify that size of the dispatchedMessagesMap is 1
   * as one regionqueue is created in this test<br> 6)Verify that the map contains an entry for the
   * queue-region name<br> 7)Verify that the size of wrapper-map is 1 as all events had same
   * ThreadId<br> 8)Verify that the sequenceId against the ThreadId in the wrapper-map is same as
   * that of the last event taken<br>
   */
/**
   * Behaviour of take() has been changed for relaible messaging feature. Region queue take()
   * operation will no longer add to the Dispatch Message Map. Hence disabling the test - SUYOG
   */
@Ignore
@Test
public void testDispatchedMsgsMapUpdateOnTakes() throws Exception {
    this.logWriter.info("HARQAddOperationJUnitTest : testDispatchedEventsMapUpdateOnTakes BEGIN");
    String regionName = "testDispatchedEventsMapUpdateOnTakes";
    HARegionQueue rq = createHARegionQueue(regionName);
    Conflatable cf = null;
    EventID id = null;
    int totalEvents = 10;
    for (int i = 0; i < totalEvents; i++) {
        id = new EventID(new byte[] { 1 }, 1, i);
        cf = new ConflatableObject("key" + i, "value" + i, id, false, "testing");
        rq.put(cf);
    }
    for (int i = 0; i < totalEvents; i++) {
        rq.take();
    }
    Map dispatchedMsgMap = HARegionQueue.getDispatchedMessagesMapForTesting();
    // verify that map is not null
    assertNotNull("dispatchedMessagesMap found null", dispatchedMsgMap);
    // size of the dispatchedMessagesMap should be 1 as one regionqueue is
    // created in this test
    assertEquals("size of dispatched msgs should be 1", 1, dispatchedMsgMap.size());
    // verify that the map contains an entry for the queue-region name
    MapWrapper wrapper = (MapWrapper) dispatchedMsgMap.get(regionName);
    assertNotNull("dispatchedMsgMap should contain an entry with queueregion name as key", wrapper);
    Map dispatchedData = wrapper.map;
    assertEquals("size of wrapper-map should be 1 as all events had same ThreadId", 1, dispatchedData.size());
    ThreadIdentifier tid = new ThreadIdentifier(new byte[] { 1 }, 1);
    Long seqId = (Long) dispatchedData.get(tid);
    assertEquals("sequenceId against the ThreadId in the wrapper-map should be that of the last event taken.", id.getSequenceID(), seqId.longValue());
    this.logWriter.info("HARQAddOperationJUnitTest : testDispatchedEventsMapUpdateOnTakes END");
}
Also used : EventID(org.apache.geode.internal.cache.EventID) Conflatable(org.apache.geode.internal.cache.Conflatable) Map(java.util.Map) MapWrapper(org.apache.geode.internal.cache.ha.HARegionQueue.MapWrapper) Ignore(org.junit.Ignore) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 35 with EventID

use of org.apache.geode.internal.cache.EventID in project geode by apache.

the class HARQAddOperationJUnitTest method testQueueAddTakeOperationWithoutConflation.

/**
   * Add operation without conflation followed by a Take operation: RegionSize, available IDs ,
   * LastDispatchedWrapper's Set should have size 0. Events map containg should have size 1 (
   * corresponding to the lastDispatchedAndCurrentEvent Wrapper objcet)
   */
@Test
public void testQueueAddTakeOperationWithoutConflation() throws Exception {
    this.logWriter.info("HARegionQueueJUnitTest : testQueueAddTakeOperationWithoutConflation BEGIN");
    this.rq = createHARegionQueue("testQueueAddOperationWithConflation");
    EventID id = new EventID(new byte[] { 1 }, 1, 1);
    ConflatableObject obj = new ConflatableObject(KEY1, VALUE1, id, true, "region1");
    this.rq.put(obj);
    this.rq.take();
    assertNull(rq.getRegion().get(KEY1));
    assertEquals(0, this.rq.getAvalaibleIds().size());
    Map eventsMap = this.rq.getEventsMapForTesting();
    assertEquals(1, eventsMap.size());
    assertEquals(0, rq.getCurrentCounterSet(id).size());
    this.logWriter.info("HARegionQueueJUnitTest : testQueueAddTakeOperationWithoutConflation END");
}
Also used : EventID(org.apache.geode.internal.cache.EventID) Map(java.util.Map) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

EventID (org.apache.geode.internal.cache.EventID)147 Test (org.junit.Test)66 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)60 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)58 IOException (java.io.IOException)41 Map (java.util.Map)33 CacheException (org.apache.geode.cache.CacheException)31 Conflatable (org.apache.geode.internal.cache.Conflatable)29 LocalRegion (org.apache.geode.internal.cache.LocalRegion)23 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)20 HashMap (java.util.HashMap)16 Part (org.apache.geode.internal.cache.tier.sockets.Part)16 ByteBuffer (java.nio.ByteBuffer)14 Iterator (java.util.Iterator)14 List (java.util.List)14 LinkedHashMap (java.util.LinkedHashMap)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 EntryEventImpl (org.apache.geode.internal.cache.EntryEventImpl)13 AuthorizeRequest (org.apache.geode.internal.security.AuthorizeRequest)13 ConcurrentMap (java.util.concurrent.ConcurrentMap)12