Search in sources :

Example 11 with Conflatable

use of org.apache.geode.internal.cache.Conflatable 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();
        }
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionQueue(org.apache.geode.internal.cache.RegionQueue) RegionExistsException(org.apache.geode.cache.RegionExistsException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 12 with Conflatable

use of org.apache.geode.internal.cache.Conflatable 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));
}
Also used : 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 13 with Conflatable

use of org.apache.geode.internal.cache.Conflatable 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 14 with Conflatable

use of org.apache.geode.internal.cache.Conflatable 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 15 with Conflatable

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

the class HARegionQueueDUnitTest method testBugNo35988.

/**
   * Behaviour of take() has been changed for reliable messaging feature. Region queue take()
   * operation will no longer add to the Dispatch Message Map. Hence disabling the test - SUYOG
   *
   * Test for #35988 HARegionQueue.take() is not functioning as expected
   */
@Ignore("TODO: this test was disabled")
@Test
public void testBugNo35988() throws Exception {
    CacheSerializableRunnable createQueue = new CacheSerializableRunnable("CreateCache, HARegionQueue and start thread") {

        @Override
        public void run2() throws CacheException {
            HARegionQueueDUnitTest test = new HARegionQueueDUnitTest();
            // TODO:ASIF: Bcoz of the QRM thread cannot take frequency below
            // 1 second , thus we need to carfully evaluate what to do. Though
            // in this case 1 second instead of 500 ms will work
            // System.getProperties().put("QueueRemovalThreadWaitTime", new Long(500));
            cache = test.createCache();
            cache.setMessageSyncInterval(1);
            HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
            hrqa.setExpiryTime(300);
            try {
                hrq = HARegionQueue.getHARegionQueueInstance("testregion1", cache, hrqa, HARegionQueue.NON_BLOCKING_HA_QUEUE, false);
                // Do 1000 putand 100 take in a separate thread
                hrq.put(new ConflatableObject(new Long(1), new Long(1), new EventID(new byte[] { 0 }, 1, 1), false, "dummy"));
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
    };
    vm0.invoke(createQueue);
    vm1.invoke(createQueue);
    vm0.invoke(new CacheSerializableRunnable("takeFromVm0") {

        @Override
        public void run2() throws CacheException {
            try {
                Conflatable obj = (Conflatable) hrq.take();
                assertNotNull(obj);
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
    });
    vm1.invoke(new CacheSerializableRunnable("checkInVm1") {

        @Override
        public void run2() throws CacheException {
            WaitCriterion ev = new WaitCriterion() {

                @Override
                public boolean done() {
                    // TODO is this necessary?
                    Thread.yield();
                    return hrq.size() == 0;
                }

                @Override
                public String description() {
                    return null;
                }
            };
            Wait.waitForCriterion(ev, 60 * 1000, 200, true);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) CacheException(org.apache.geode.cache.CacheException) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) EventID(org.apache.geode.internal.cache.EventID) Conflatable(org.apache.geode.internal.cache.Conflatable) Ignore(org.junit.Ignore) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

Conflatable (org.apache.geode.internal.cache.Conflatable)39 EventID (org.apache.geode.internal.cache.EventID)29 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)22 Test (org.junit.Test)22 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 Map (java.util.Map)8 CacheException (org.apache.geode.cache.CacheException)8 HashMap (java.util.HashMap)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 IOException (java.io.IOException)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 EntryEvent (org.apache.geode.cache.EntryEvent)3 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)3