Search in sources :

Example 21 with SubscriptionAttributes

use of org.apache.geode.cache.SubscriptionAttributes 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 22 with SubscriptionAttributes

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

the class QueueMsgDUnitTest method testIllegalConfigQueueExists.

/**
   * Make sure a queued region does not allow non-queued subscribers
   */
@Ignore("TODO: test is disabled")
@Test
public void testIllegalConfigQueueExists() throws Exception {
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    createRootRegion(factory.create());
    VM vm = Host.getHost(0).getVM(0);
    vm.invoke(new SerializableRunnable() {

        public void run() {
            Properties config = new Properties();
            config.setProperty(ROLES, "pubFirst");
            getSystem(config);
        }
    });
    // now create the region
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            final String expectedExceptions = "does not allow queued messages";
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            // setting the following makes things legal
            factory.setDataPolicy(DataPolicy.NORMAL);
            factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
            try {
                createRootRegion(factory.create());
                fail("expected IllegalStateException");
            } catch (IllegalStateException expected) {
            } finally {
                getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
            }
        }
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 23 with SubscriptionAttributes

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

the class QueueMsgDUnitTest method testIllegalConfigSubscriberExists.

/**
   * Make sure a subscriber that does not allow queued messages causes a queued publisher to fail
   * creation
   */
@Ignore("TODO: test is disabled")
@Test
public void testIllegalConfigSubscriberExists() throws Exception {
    final String expectedExceptions = "does not allow queued messages";
    VM vm = Host.getHost(0).getVM(0);
    vm.invoke(new SerializableRunnable() {

        public void run() {
            Properties config = new Properties();
            config.setProperty(ROLES, "subFirst");
            getSystem(config);
        }
    });
    // now create the region
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            // setting the following makes things legal
            factory.setDataPolicy(DataPolicy.NORMAL);
            factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            createRootRegion(factory.create());
        }
    });
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
    try {
        createRootRegion(factory.create());
        fail("expected IllegalStateException");
    } catch (IllegalStateException expected) {
    } finally {
        getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 24 with SubscriptionAttributes

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

the class ProxyDUnitTest method remoteOriginOps.

/**
   * check remote ops done in a normal vm are correctly distributed to PROXY regions
   */
private void remoteOriginOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListener() {

        public void afterUpdate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterCreate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterInvalidate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterDestroy(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionInvalidate(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionDestroy(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionClear(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionCreate(RegionEvent e) {
        }

        public void afterRegionLive(RegionEvent e) {
        }

        public void close() {
        }
    };
    af.addCacheListener(cl1);
    Region r = createRootRegion("ProxyDUnitTest", af.create());
    this.clInvokeCount = 0;
    doCreateOtherVm();
    DMStats stats = getDMStats();
    long receivedMsgs = stats.getReceivedMessages();
    if (ip.isAll()) {
        getOtherVm().invoke(new CacheSerializableRunnable("do put") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.put("p", "v");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        // failure
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("p", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do create") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.create("c", "v");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do update") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.put("c", "v2");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.UPDATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals("v2", ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do invalidate") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.invalidate("c");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.INVALIDATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do destroy") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.destroy("c");
            }
        });
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.DESTROY, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
        assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do putAll") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                Map m = new HashMap();
                m.put("putAllKey1", "putAllValue1");
                m.put("putAllKey2", "putAllValue2");
                r.putAll(m);
            }
        });
        assertEquals(2, this.clInvokeCount);
        // @todo darrel; check putAll events
        this.clInvokeCount = 0;
        getOtherVm().invoke(new CacheSerializableRunnable("do netsearch") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                // total miss
                assertEquals(null, r.get("loadkey"));
            }
        });
        assertEquals(0, this.clInvokeCount);
    } else {
        getOtherVm().invoke(new CacheSerializableRunnable("do entry ops") {

            public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                r.put("p", "v");
                r.create("c", "v");
                // update
                r.put("c", "v");
                r.invalidate("c");
                r.destroy("c");
                {
                    Map m = new HashMap();
                    m.put("putAllKey1", "putAllValue1");
                    m.put("putAllKey2", "putAllValue2");
                    r.putAll(m);
                }
                // total miss
                assertEquals(null, r.get("loadkey"));
            }
        });
        assertEquals(0, this.clInvokeCount);
        assertEquals(0, r.size());
        // check the stats to make sure none of the above sent up messages
        assertEquals(receivedMsgs, stats.getReceivedMessages());
    }
    {
        AttributesMutator am = r.getAttributesMutator();
        CacheLoader cl = new CacheLoader() {

            public Object load(LoaderHelper helper) throws CacheLoaderException {
                if (helper.getKey().equals("loadkey")) {
                    return "loadvalue";
                } else if (helper.getKey().equals("loadexception")) {
                    throw new CacheLoaderException("expected");
                } else {
                    return null;
                }
            }

            public void close() {
            }
        };
        am.setCacheLoader(cl);
    }
    receivedMsgs = stats.getReceivedMessages();
    getOtherVm().invoke(new CacheSerializableRunnable("check net loader") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            // net load
            assertEquals("loadvalue", r.get("loadkey"));
            // total miss
            assertEquals(null, r.get("foobar"));
            try {
                r.get("loadexception");
                fail("expected CacheLoaderException");
            } catch (CacheLoaderException expected) {
            }
        }
    });
    assertTrue(stats.getReceivedMessages() > receivedMsgs);
    if (ip.isAll()) {
        assertEquals(1, this.clInvokeCount);
        assertEquals(Operation.NET_LOAD_CREATE, this.clLastEvent.getOperation());
        assertEquals(true, this.clLastEvent.isOriginRemote());
        assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
        assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
        assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
        this.clInvokeCount = 0;
    } else {
        assertEquals(0, this.clInvokeCount);
    }
    {
        AttributesMutator am = r.getAttributesMutator();
        am.setCacheLoader(null);
        CacheWriter cw = new CacheWriterAdapter() {

            public void beforeCreate(EntryEvent event) throws CacheWriterException {
                throw new CacheWriterException("expected");
            }
        };
        am.setCacheWriter(cw);
    }
    receivedMsgs = stats.getReceivedMessages();
    getOtherVm().invoke(new CacheSerializableRunnable("check net write") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            try {
                r.put("putkey", "putvalue");
                fail("expected CacheWriterException");
            } catch (CacheWriterException expected) {
            }
        }
    });
    assertTrue(stats.getReceivedMessages() > receivedMsgs);
    {
        AttributesMutator am = r.getAttributesMutator();
        am.setCacheWriter(null);
    }
    assertEquals(0, this.clInvokeCount);
    this.clLastEvent = null;
    getOtherVm().invoke(new CacheSerializableRunnable("check region invalidate") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            r.invalidateRegion();
        }
    });
    assertEquals(1, this.clInvokeCount);
    assertEquals(Operation.REGION_INVALIDATE, this.clLastEvent.getOperation());
    assertEquals(true, this.clLastEvent.isOriginRemote());
    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
    this.clLastEvent = null;
    getOtherVm().invoke(new CacheSerializableRunnable("check region clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            r.clear();
        }
    });
    assertEquals(2, this.clInvokeCount);
    assertEquals(Operation.REGION_CLEAR, this.clLastEvent.getOperation());
    assertEquals(true, this.clLastEvent.isOriginRemote());
    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
    this.clLastEvent = null;
    getOtherVm().invoke(new CacheSerializableRunnable("check region destroy") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            r.destroyRegion();
        }
    });
    assertEquals(3, this.clInvokeCount);
    assertEquals(Operation.REGION_DESTROY, this.clLastEvent.getOperation());
    assertEquals(true, this.clLastEvent.isOriginRemote());
    assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
    assertTrue(r.isDestroyed());
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) DMStats(org.apache.geode.distributed.internal.DMStats) CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) RegionEvent(org.apache.geode.cache.RegionEvent) CacheListener(org.apache.geode.cache.CacheListener) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheWriter(org.apache.geode.cache.CacheWriter) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashMap(java.util.HashMap) Map(java.util.Map) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 25 with SubscriptionAttributes

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

the class MultiVMRegionTestCase method testTXNonblockingGetInitialImage.

/**
   * Tests that distributed ack operations do not block while another cache is doing a
   * getInitialImage.
   */
@Test
public void testTXNonblockingGetInitialImage() throws Exception {
    assumeTrue(supportsReplication());
    assumeTrue(supportsTransactions());
    // don't run this test if global scope since its too difficult to predict
    // how many concurrent operations will occur
    assumeFalse(getRegionAttributes().getScope().isGlobal());
    assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
    final String name = this.getUniqueName();
    final byte[][] values = new byte[NB1_NUM_ENTRIES][];
    for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
        values[i] = new byte[NB1_VALUE_SIZE];
        Arrays.fill(values[i], (byte) 0x42);
    }
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm2 = host.getVM(2);
    SerializableRunnable create = new CacheSerializableRunnable("Create Mirrored Region") {

        @Override
        public void run2() throws CacheException {
            beginCacheXml();
            {
                // root region must be DACK because its used to sync up async subregions
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.NORMAL);
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
                createRootRegion(factory.create());
            }
            {
                AttributesFactory factory = new AttributesFactory(getRegionAttributes());
                if (getRegionAttributes().getDataPolicy() == DataPolicy.NORMAL) {
                    factory.setDataPolicy(DataPolicy.PRELOADED);
                }
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
                createRegion(name, factory.create());
            }
            finishCacheXml(name);
            // reset slow
            org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
        }
    };
    vm0.invoke(new CacheSerializableRunnable("Create Nonmirrored Region") {

        @Override
        public void run2() throws CacheException {
            {
                // root region must be DACK because its used to sync up async subregions
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.EMPTY);
                createRootRegion(factory.create());
            }
            {
                AttributesFactory factory = new AttributesFactory(getRegionAttributes());
                createRegion(name, factory.create());
            }
            // reset slow
            org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Put initial data") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
                region.put(new Integer(i), values[i]);
            }
            assertEquals(NB1_NUM_ENTRIES, region.keySet().size());
        }
    });
    // start asynchronous process that does updates to the data
    AsyncInvocation async = vm0.invokeAsync(new CacheSerializableRunnable("Do Nonblocking Operations") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            // wait for profile of getInitialImage cache to show up
            final org.apache.geode.internal.cache.CacheDistributionAdvisor adv = ((org.apache.geode.internal.cache.DistributedRegion) region).getCacheDistributionAdvisor();
            final int expectedProfiles = 1;
            WaitCriterion ev = new WaitCriterion() {

                @Override
                public boolean done() {
                    DataPolicy currentPolicy = getRegionAttributes().getDataPolicy();
                    if (currentPolicy == DataPolicy.PRELOADED) {
                        return (adv.advisePreloadeds().size() + adv.adviseReplicates().size()) >= expectedProfiles;
                    } else {
                        return adv.adviseReplicates().size() >= expectedProfiles;
                    }
                }

                @Override
                public String description() {
                    return "replicate count never reached " + expectedProfiles;
                }
            };
            Wait.waitForCriterion(ev, 100 * 1000, 200, true);
            // operate on every odd entry with different value, alternating between
            // updates, invalidates, and destroys. These operations are likely
            // to be nonblocking if a sufficient number of updates get through
            // before the get initial image is complete.
            CacheTransactionManager txMgr = getCache().getCacheTransactionManager();
            for (int i = 1; i < NB1_NUM_ENTRIES; i += 2) {
                Object key = new Integer(i);
                switch(i % 6) {
                    case // UPDATE
                    1:
                        // use the current timestamp so we know when it happened
                        // we could have used last modification timestamps, but
                        // this works without enabling statistics
                        Object value = new Long(System.currentTimeMillis());
                        txMgr.begin();
                        region.put(key, value);
                        txMgr.commit();
                        // }
                        break;
                    case // INVALIDATE
                    3:
                        txMgr.begin();
                        region.invalidate(key);
                        txMgr.commit();
                        if (getRegionAttributes().getScope().isDistributedAck()) {
                            // do a nonblocking netSearch
                            assertNull(region.get(key));
                        }
                        break;
                    case // DESTROY
                    5:
                        txMgr.begin();
                        region.destroy(key);
                        txMgr.commit();
                        if (getRegionAttributes().getScope().isDistributedAck()) {
                            // do a nonblocking netSearch
                            assertNull(region.get(key));
                        }
                        break;
                    default:
                        fail("unexpected modulus result: " + i);
                        break;
                }
            }
            // add some new keys
            for (int i = NB1_NUM_ENTRIES; i < NB1_NUM_ENTRIES + 200; i++) {
                txMgr.begin();
                region.create(new Integer(i), new Long(System.currentTimeMillis()));
                txMgr.commit();
            }
            // now do a put and our DACK root region which will not complete
            // until processed on otherside which means everything done before this
            // point has been processed
            getRootRegion().put("DONE", "FLUSH_OPS");
        }
    });
    // slow down image processing to make it more likely to get async updates
    if (!getRegionAttributes().getScope().isGlobal()) {
        vm2.invoke(new SerializableRunnable("Set slow image processing") {

            @Override
            public void run() {
                // if this is a no_ack test, then we need to slow down more because of the
                // pauses in the nonblocking operations
                int pause = 200;
                org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = pause;
            }
        });
    }
    AsyncInvocation asyncGII = vm2.invokeAsync(create);
    if (!getRegionAttributes().getScope().isGlobal()) {
        // wait for nonblocking operations to complete
        ThreadUtils.join(async, 30 * 1000);
        vm2.invoke(new SerializableRunnable("Set fast image processing") {

            @Override
            public void run() {
                org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
            }
        });
        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after async nonblocking ops complete");
    }
    // wait for GII to complete
    ThreadUtils.join(asyncGII, 30 * 1000);
    final long iiComplete = System.currentTimeMillis();
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Complete GetInitialImage at: " + System.currentTimeMillis());
    if (getRegionAttributes().getScope().isGlobal()) {
        // wait for nonblocking operations to complete
        ThreadUtils.join(async, 30 * 1000);
    }
    if (async.exceptionOccurred()) {
        fail("async failed", async.getException());
    }
    if (asyncGII.exceptionOccurred()) {
        fail("asyncGII failed", asyncGII.getException());
    }
    // Locally destroy the region in vm0 so we know that they are not found by
    // a netSearch
    vm0.invoke(new CacheSerializableRunnable("Locally destroy region") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.localDestroyRegion();
        }
    });
    // invoke repeating so noack regions wait for all updates to get processed
    vm2.invokeRepeatingIfNecessary(new CacheSerializableRunnable("Verify entryCount") {

        boolean entriesDumped = false;

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            // expected entry count (subtract entries destroyed)
            int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
            int actualCount = region.entrySet(false).size();
            if (actualCount == NB1_NUM_ENTRIES + 200) {
                // entries not destroyed, dump entries that were supposed to have been destroyed
                dumpDestroyedEntries(region);
            }
            assertEquals(entryCount, actualCount);
        }

        private void dumpDestroyedEntries(Region region) throws EntryNotFoundException {
            if (entriesDumped)
                return;
            entriesDumped = true;
            LogWriter logger = org.apache.geode.test.dunit.LogWriterUtils.getLogWriter();
            logger.info("DUMPING Entries with values in VM that should have been destroyed:");
            for (int i = 5; i < NB1_NUM_ENTRIES; i += 6) {
                logger.info(i + "-->" + ((org.apache.geode.internal.cache.LocalRegion) region).getValueInVM(new Integer(i)));
            }
        }
    }, 5000);
    vm2.invoke(new CacheSerializableRunnable("Verify keys/values & Nonblocking") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            // expected entry count (subtract entries destroyed)
            int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
            assertEquals(entryCount, region.entrySet(false).size());
            // determine how many entries were updated before getInitialImage
            // was complete
            int numConcurrent = 0;
            for (int i = 0; i < NB1_NUM_ENTRIES + 200; i++) {
                Region.Entry entry = region.getEntry(new Integer(i));
                Object v = entry == null ? null : entry.getValue();
                if (i < NB1_NUM_ENTRIES) {
                    // old keys
                    switch(i % 6) {
                        // even keys are originals
                        case 0:
                        case 2:
                        case 4:
                            assertNotNull(entry);
                            assertTrue(Arrays.equals(values[i], (byte[]) v));
                            break;
                        case // updated
                        1:
                            assertNotNull(v);
                            assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
                            Long timestamp = (Long) entry.getValue();
                            if (timestamp.longValue() < iiComplete) {
                                numConcurrent++;
                            }
                            break;
                        case // invalidated
                        3:
                            assertNotNull(entry);
                            assertNull("Expected value for " + i + " to be null, but was " + v, v);
                            break;
                        case // destroyed
                        5:
                            assertNull(entry);
                            break;
                        default:
                            fail("unexpected modulus result: " + (i % 6));
                            break;
                    }
                } else {
                    // new keys
                    assertNotNull(v);
                    assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
                    Long timestamp = (Long) entry.getValue();
                    if (timestamp.longValue() < iiComplete) {
                        numConcurrent++;
                    }
                }
            }
            org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage");
            // make sure at least some of them were concurrent
            {
                int min = 30;
                assertTrue("Not enough updates concurrent with getInitialImage occurred to my liking. " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage, and I'd expect at least " + min + " or so", numConcurrent >= min);
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) RegionEntry(org.apache.geode.internal.cache.RegionEntry) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) DataPolicy(org.apache.geode.cache.DataPolicy) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) LogWriter(org.apache.geode.LogWriter) VM(org.apache.geode.test.dunit.VM) 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) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Aggregations

SubscriptionAttributes (org.apache.geode.cache.SubscriptionAttributes)35 AttributesFactory (org.apache.geode.cache.AttributesFactory)24 Region (org.apache.geode.cache.Region)16 Test (org.junit.Test)15 CacheException (org.apache.geode.cache.CacheException)13 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)12 VM (org.apache.geode.test.dunit.VM)11 EntryEvent (org.apache.geode.cache.EntryEvent)10 Properties (java.util.Properties)9 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)8 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)7 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)7 Host (org.apache.geode.test.dunit.Host)7 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)7 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)7 RegionAttributes (org.apache.geode.cache.RegionAttributes)6 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)6 StoredObject (org.apache.geode.internal.offheap.StoredObject)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6