Search in sources :

Example 6 with CacheEvent

use of org.apache.geode.cache.CacheEvent in project geode by apache.

the class MultiVMRegionTestCase method testEntryTtlDestroyEvent.

/**
   * Tests that an entry in a distributed region that expires with a distributed destroy causes an
   * event in other VM with isExpiration flag set.
   */
// GEODE-583: time sensitive, expiration, waitForCriterion, short
@Category(FlakyTest.class)
// timeouts
@Test
public void testEntryTtlDestroyEvent() throws Exception {
    assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
    final String name = this.getUniqueName();
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    class DestroyListener extends TestCacheListener {

        boolean eventIsExpiration = false;

        @Override
        public void afterDestroyBeforeAddEvent(EntryEvent event) {
            eventIsExpiration = event.getOperation().isExpiration();
        }

        @Override
        public void afterDestroy2(EntryEvent event) {
            if (event.isOriginRemote()) {
                assertTrue(!event.getDistributedMember().equals(getSystem().getDistributedMember()));
            } else {
                assertEquals(getSystem().getDistributedMember(), event.getDistributedMember());
            }
            assertEquals(Operation.EXPIRE_DESTROY, event.getOperation());
            assertEquals(value, event.getOldValue());
            eventIsExpiration = event.getOperation().isExpiration();
        }

        @Override
        public void afterCreate2(EntryEvent event) {
        // ignore
        }

        @Override
        public void afterUpdate2(EntryEvent event) {
        // ignore
        }
    }
    SerializableRunnable createRegion = new CacheSerializableRunnable("Create with Listener") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory fac = new AttributesFactory(getRegionAttributes());
            fac.addCacheListener(destroyListener = new DestroyListener());
            createRegion(name, fac.create());
        }
    };
    vm1.invoke(createRegion);
    vm0.invoke(new CacheSerializableRunnable("Create with TTL") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            factory.setStatisticsEnabled(true);
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
            factory.setEntryTimeToLive(expire);
            if (!getRegionAttributes().getDataPolicy().withReplication()) {
                factory.setDataPolicy(DataPolicy.NORMAL);
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            }
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            try {
                createRegion(name, factory.create());
                ExpiryTask.suspendExpiration();
            // suspend to make sure we can see that the put is distributed to this member
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
            }
        }
    });
    try {
        // let region create finish before doing put
        // pause(10);
        vm1.invoke(new SerializableCallable() {

            @Override
            public Object call() throws Exception {
                Region region = getRootRegion().getSubregion(name);
                DestroyListener dl = (DestroyListener) region.getAttributes().getCacheListeners()[0];
                dl.enableEventHistory();
                region.put(key, value);
                // reset listener after create event
                assertTrue(dl.wasInvoked());
                List<CacheEvent> history = dl.getEventHistory();
                CacheEvent ce = history.get(0);
                dl.disableEventHistory();
                assertEquals(Operation.CREATE, ce.getOperation());
                return null;
            }
        });
        vm0.invoke(new CacheSerializableRunnable("Check create received from vm1") {

            @Override
            public void run2() throws CacheException {
                final Region region = getRootRegion().getSubregion(name);
                WaitCriterion waitForCreate = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return region.getEntry(key) != null;
                    }

                    @Override
                    public String description() {
                        return "never saw create of " + key;
                    }
                };
                Wait.waitForCriterion(waitForCreate, 3000, 10, true);
            }
        });
    } finally {
        vm0.invoke(new CacheSerializableRunnable("resume expiration") {

            @Override
            public void run2() throws CacheException {
                ExpiryTask.permitExpiration();
            }
        });
    }
    // now wait for it to expire
    vm0.invoke(new CacheSerializableRunnable("Check local destroy") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            WaitCriterion waitForExpire = new WaitCriterion() {

                @Override
                public boolean done() {
                    return region.getEntry(key) == null;
                }

                @Override
                public String description() {
                    return "never saw expire of " + key + " entry=" + region.getEntry(key);
                }
            };
            Wait.waitForCriterion(waitForExpire, 4000, 10, true);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify destroyed and event") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            WaitCriterion waitForExpire = new WaitCriterion() {

                @Override
                public boolean done() {
                    return region.getEntry(key) == null;
                }

                @Override
                public String description() {
                    return "never saw expire of " + key + " entry=" + region.getEntry(key);
                }
            };
            Wait.waitForCriterion(waitForExpire, 4000, 10, true);
            assertTrue(destroyListener.waitForInvocation(555));
            assertTrue(((DestroyListener) destroyListener).eventIsExpiration);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) TimeoutException(org.apache.geode.cache.TimeoutException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) EntryExistsException(org.apache.geode.cache.EntryExistsException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheEvent(org.apache.geode.cache.CacheEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 7 with CacheEvent

use of org.apache.geode.cache.CacheEvent in project geode by apache.

the class QueueMsgDUnitTest method testQueueWhenRoleMissing.

/**
   * Make sure that cache operations are queued when a required role is missing
   */
@Ignore("TODO: test is disabled")
@Test
public void testQueueWhenRoleMissing() throws Exception {
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    DistributedRegion r = (DistributedRegion) createRootRegion(factory.create());
    final CachePerfStats stats = r.getCachePerfStats();
    int queuedOps = stats.getReliableQueuedOps();
    r.create("createKey", "createValue", "createCBArg");
    r.invalidate("createKey", "invalidateCBArg");
    r.put("createKey", "putValue", "putCBArg");
    r.destroy("createKey", "destroyCBArg");
    assertEquals(queuedOps + 4, stats.getReliableQueuedOps());
    queuedOps = stats.getReliableQueuedOps();
    {
        Map m = new TreeMap();
        m.put("aKey", "aValue");
        m.put("bKey", "bValue");
        r.putAll(m);
    }
    assertEquals(queuedOps + 2, stats.getReliableQueuedOps());
    queuedOps = stats.getReliableQueuedOps();
    r.invalidateRegion("invalidateRegionCBArg");
    assertEquals(queuedOps + 1, stats.getReliableQueuedOps());
    queuedOps = stats.getReliableQueuedOps();
    r.clear();
    assertEquals(queuedOps + 1, stats.getReliableQueuedOps());
    queuedOps = stats.getReliableQueuedOps();
    // @todo darrel: try some other ops
    VM vm = Host.getHost(0).getVM(0);
    // now create a system that fills this role since it does not create the
    // region our queue should not be flushed
    vm.invoke(new SerializableRunnable() {

        public void run() {
            Properties config = new Properties();
            config.setProperty(ROLES, "missing");
            getSystem(config);
        }
    });
    // we still should have everything queued since the region is not created
    assertEquals(queuedOps, stats.getReliableQueuedOps());
    // now create the region
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setDataPolicy(DataPolicy.NORMAL);
            factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            TestCacheListener cl = new TestCacheListener() {

                public void afterCreate2(EntryEvent event) {
                }

                public void afterUpdate2(EntryEvent event) {
                }

                public void afterInvalidate2(EntryEvent event) {
                }

                public void afterDestroy2(EntryEvent event) {
                }
            };
            cl.enableEventHistory();
            factory.addCacheListener(cl);
            createRootRegion(factory.create());
        }
    });
    // after some amount of time we should see the queuedOps flushed
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return stats.getReliableQueuedOps() == 0;
        }

        public String description() {
            return "waiting for reliableQueuedOps to become 0";
        }
    };
    Wait.waitForCriterion(ev, 5 * 1000, 200, true);
    // now check that the queued op was delivered
    vm.invoke(new CacheSerializableRunnable("check") {

        public void run2() throws CacheException {
            Region r = getRootRegion();
            assertEquals(null, r.getEntry("createKey"));
            // assertIndexDetailsEquals("putValue", r.getEntry("createKey").getValue());
            {
                int evIdx = 0;
                TestCacheListener cl = (TestCacheListener) r.getAttributes().getCacheListener();
                List events = cl.getEventHistory();
                {
                    CacheEvent ce = (CacheEvent) events.get(evIdx++);
                    assertEquals(Operation.REGION_CREATE, ce.getOperation());
                }
                {
                    EntryEvent ee = (EntryEvent) events.get(evIdx++);
                    assertEquals(Operation.CREATE, ee.getOperation());
                    assertEquals("createKey", ee.getKey());
                    assertEquals("createValue", ee.getNewValue());
                    assertEquals(null, ee.getOldValue());
                    assertEquals("createCBArg", ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
                {
                    EntryEvent ee = (EntryEvent) events.get(evIdx++);
                    assertEquals(Operation.INVALIDATE, ee.getOperation());
                    assertEquals("createKey", ee.getKey());
                    assertEquals(null, ee.getNewValue());
                    assertEquals("createValue", ee.getOldValue());
                    assertEquals("invalidateCBArg", ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
                {
                    EntryEvent ee = (EntryEvent) events.get(evIdx++);
                    assertEquals(Operation.UPDATE, ee.getOperation());
                    assertEquals("createKey", ee.getKey());
                    assertEquals("putValue", ee.getNewValue());
                    assertEquals(null, ee.getOldValue());
                    assertEquals("putCBArg", ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
                {
                    EntryEvent ee = (EntryEvent) events.get(evIdx++);
                    assertEquals(Operation.DESTROY, ee.getOperation());
                    assertEquals("createKey", ee.getKey());
                    assertEquals(null, ee.getNewValue());
                    assertEquals("putValue", ee.getOldValue());
                    assertEquals("destroyCBArg", ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
                {
                    EntryEvent ee = (EntryEvent) events.get(evIdx++);
                    assertEquals(Operation.PUTALL_CREATE, ee.getOperation());
                    assertEquals("aKey", ee.getKey());
                    assertEquals("aValue", ee.getNewValue());
                    assertEquals(null, ee.getOldValue());
                    assertEquals(null, ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
                {
                    EntryEvent ee = (EntryEvent) events.get(evIdx++);
                    assertEquals(Operation.PUTALL_CREATE, ee.getOperation());
                    assertEquals("bKey", ee.getKey());
                    assertEquals("bValue", ee.getNewValue());
                    assertEquals(null, ee.getOldValue());
                    assertEquals(null, ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
                {
                    RegionEvent re = (RegionEvent) events.get(evIdx++);
                    assertEquals(Operation.REGION_INVALIDATE, re.getOperation());
                    assertEquals("invalidateRegionCBArg", re.getCallbackArgument());
                    assertEquals(true, re.isOriginRemote());
                }
                {
                    RegionEvent re = (RegionEvent) events.get(evIdx++);
                    assertEquals(Operation.REGION_CLEAR, re.getOperation());
                    assertEquals(null, re.getCallbackArgument());
                    assertEquals(true, re.isOriginRemote());
                }
                assertEquals(evIdx, events.size());
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) TreeMap(java.util.TreeMap) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) CacheEvent(org.apache.geode.cache.CacheEvent) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Region(org.apache.geode.cache.Region) List(java.util.List) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Map(java.util.Map) TreeMap(java.util.TreeMap) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with CacheEvent

use of org.apache.geode.cache.CacheEvent in project geode by apache.

the class TXRmtEvent method getCreateEvents.

public List getCreateEvents() {
    if (this.events == null) {
        return Collections.EMPTY_LIST;
    } else {
        ArrayList result = new ArrayList(this.events.size());
        Iterator it = this.events.iterator();
        while (it.hasNext()) {
            CacheEvent ce = (CacheEvent) it.next();
            if (ce.getOperation().isCreate() && isEventUserVisible(ce)) {
                result.add(ce);
            }
        }
        if (result.isEmpty()) {
            return Collections.EMPTY_LIST;
        } else {
            return Collections.unmodifiableList(result);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) CacheEvent(org.apache.geode.cache.CacheEvent)

Example 9 with CacheEvent

use of org.apache.geode.cache.CacheEvent in project geode by apache.

the class SearchLoadAndWriteProcessor method doLocalWrite.

private boolean doLocalWrite(CacheWriter writer, CacheEvent pevent, int paction) throws CacheWriterException {
    // Return if the inhibit all notifications flag is set
    if (pevent instanceof EntryEventImpl) {
        if (((EntryEventImpl) pevent).inhibitAllNotifications()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Notification inhibited for key {}", pevent);
            }
            return false;
        }
    }
    @Released CacheEvent event = getEventForListener(pevent);
    int action = paction;
    if (event.getOperation().isCreate() && action == BEFOREUPDATE) {
        action = BEFORECREATE;
    }
    try {
        switch(action) {
            case BEFORECREATE:
                writer.beforeCreate((EntryEvent) event);
                break;
            case BEFOREDESTROY:
                writer.beforeDestroy((EntryEvent) event);
                break;
            case BEFOREUPDATE:
                writer.beforeUpdate((EntryEvent) event);
                break;
            case BEFOREREGIONDESTROY:
                writer.beforeRegionDestroy((RegionEvent) event);
                break;
            case BEFOREREGIONCLEAR:
                writer.beforeRegionClear((RegionEvent) event);
                break;
            default:
                break;
        }
    } finally {
        if (event != pevent) {
            if (event instanceof EntryEventImpl) {
                ((Releasable) event).release();
            }
        }
    }
    this.localWrite = true;
    return true;
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) CacheEvent(org.apache.geode.cache.CacheEvent) Releasable(org.apache.geode.internal.offheap.Releasable)

Aggregations

CacheEvent (org.apache.geode.cache.CacheEvent)9 ArrayList (java.util.ArrayList)6 Iterator (java.util.Iterator)5 List (java.util.List)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 CacheException (org.apache.geode.cache.CacheException)2 EntryEvent (org.apache.geode.cache.EntryEvent)2 Region (org.apache.geode.cache.Region)2 SubscriptionAttributes (org.apache.geode.cache.SubscriptionAttributes)2 Releasable (org.apache.geode.internal.offheap.Releasable)2 Released (org.apache.geode.internal.offheap.annotations.Released)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2 VM (org.apache.geode.test.dunit.VM)2 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1