Search in sources :

Example 1 with EntryEvent

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

the class PRDeltaPropagationDUnitTest method createClientCache.

public static void createClientCache(Integer port1, Boolean subscriptionEnable, Boolean isEmpty, Boolean isCq) throws Exception {
    PRDeltaPropagationDUnitTest test = new PRDeltaPropagationDUnitTest();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    test.createCache(props);
    lastKeyReceived = false;
    queryUpdateExecuted = false;
    queryDestroyExecuted = false;
    notADeltaInstanceObj = false;
    isFailed = false;
    procced = false;
    numValidCqEvents = 0;
    PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer("localhost", port1).setSubscriptionEnabled(true).setSubscriptionRedundancy(0).setThreadLocalConnections(true).setMinConnections(6).setReadTimeout(20000).setPingInterval(10000).setRetryAttempts(5).create("PRDeltaPropagationDUnitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setConcurrencyChecksEnabled(true);
    if (isEmpty.booleanValue()) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
        factory.setDataPolicy(DataPolicy.EMPTY);
    }
    factory.setPoolName(p.getName());
    factory.setCloningEnabled(false);
    factory.addCacheListener(new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            if (LAST_KEY.equals(event.getKey())) {
                lastKeyReceived = true;
            }
        }
    });
    RegionAttributes attrs = factory.create();
    deltaPR = cache.createRegion(REGION_NAME, attrs);
    if (subscriptionEnable.booleanValue()) {
        deltaPR.registerInterest("ALL_KEYS");
    }
    pool = p;
    if (isCq.booleanValue()) {
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListenerAdapter cqlist = new CqListenerAdapter() {

            @Override
            @SuppressWarnings("synthetic-access")
            public void onEvent(CqEvent cqEvent) {
                if (LAST_KEY.equals(cqEvent.getKey().toString())) {
                    lastKeyReceived = true;
                } else if (!(cqEvent.getNewValue() instanceof Delta)) {
                    notADeltaInstanceObj = true;
                } else if (cqEvent.getQueryOperation().isUpdate() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
                    queryUpdateExecuted = true;
                } else if (cqEvent.getQueryOperation().isDestroy() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
                    queryDestroyExecuted = true;
                }
                if (forOldNewCQVarification) {
                    if (DELTA_KEY.equals(cqEvent.getKey().toString())) {
                        if (numValidCqEvents == 0 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 8) {
                            procced = true;
                        } else if (procced && numValidCqEvents == 1 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 10) {
                            // this tell us that every thing is fine
                            isFailed = true;
                        }
                    }
                }
                numValidCqEvents++;
            }
        };
        cqf.addCqListener(cqlist);
        CqAttributes cqa = cqf.create();
        CqQuery cq = cache.getQueryService().newCq("CQ_Delta", CQ, cqa);
        cq.execute();
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) DeltaTestImpl(org.apache.geode.DeltaTestImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CqEvent(org.apache.geode.cache.query.CqEvent) Delta(org.apache.geode.Delta) CqAttributes(org.apache.geode.cache.query.CqAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes)

Example 2 with EntryEvent

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

the class ClientToServerDeltaDUnitTest method createServerCache.

/*
   * create server cache
   */
public static Integer createServerCache(Boolean attachListener, Boolean isEmpty, Boolean clone, Boolean enableDelta) throws Exception {
    // for validation
    updates = 0;
    create = 0;
    firstUpdate = null;
    secondUpdate = null;
    error = false;
    Properties props = new Properties();
    props.setProperty(DELTA_PROPAGATION, enableDelta.toString());
    new ClientToServerDeltaDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setConcurrencyChecksEnabled(true);
    if (isEmpty) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
        factory.setDataPolicy(DataPolicy.EMPTY);
    } else {
        factory.setDataPolicy(DataPolicy.REPLICATE);
    }
    factory.setCloningEnabled(clone);
    RegionAttributes attrs = factory.create();
    region = cache.createRegion(REGION_NAME, attrs);
    AttributesMutator am = region.getAttributesMutator();
    if (attachListener) {
        am.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterCreate(EntryEvent event) {
                create++;
            }

            @Override
            public void afterUpdate(EntryEvent event) {
                switch(updates) {
                    case 0:
                        // first delta
                        validateUpdates(event, firstUpdate, "FIRST");
                        updates++;
                        break;
                    case 1:
                        // combine delta
                        validateUpdates(event, firstUpdate, "FIRST");
                        validateUpdates(event, secondUpdate, "SECOND");
                        updates++;
                        break;
                    default:
                        break;
                }
            }
        });
    } else if (!isEmpty) {
        am.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterCreate(EntryEvent event) {
                switch(create) {
                    case 1:
                        validateUpdates(event, firstUpdate, "FIRST");
                        create++;
                        break;
                    case 2:
                        validateUpdates(event, secondUpdate, "SECOND");
                        create++;
                        break;
                    default:
                        create++;
                        break;
                }
            }
        });
    }
    CacheServer server = cache.addCacheServer();
    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    server.setPort(port);
    // ensures updates to be sent instead of invalidations
    server.setNotifyBySubscription(true);
    server.start();
    return new Integer(server.getPort());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) CacheServer(org.apache.geode.cache.server.CacheServer) Properties(java.util.Properties) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Example 3 with EntryEvent

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

the class CallbackArgDUnitTest method doCommitOtherVm.

private void doCommitOtherVm() {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            AttributesFactory af = new AttributesFactory();
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    assertEquals(callbackArg, e.getCallbackArgument());
                }
            };
            af.addCacheListener(cl1);
            af.setScope(Scope.DISTRIBUTED_ACK);
            Region r1 = createRootRegion("r1", af.create());
            Region r2 = r1.createSubregion("r2", af.create());
            Region r3 = r2.createSubregion("r3", af.create());
            CacheTransactionManager ctm = getCache().getCacheTransactionManager();
            TransactionListener tl1 = new TransactionListenerAdapter() {

                public void afterCommit(TransactionEvent e) {
                    assertEquals(6, e.getEvents().size());
                    Iterator it = e.getEvents().iterator();
                    while (it.hasNext()) {
                        EntryEvent ee = (EntryEvent) it.next();
                        assertEquals(callbackArg, ee.getCallbackArgument());
                        assertEquals(true, ee.isCallbackArgumentAvailable());
                    }
                }
            };
            ctm.addListener(tl1);
            ctm.begin();
            r2.put("b", "value1", callbackArg);
            r3.put("c", "value2", callbackArg);
            r1.put("a", "value3", callbackArg);
            r1.put("a2", "value4", callbackArg);
            r3.put("c2", "value5", callbackArg);
            r2.put("b2", "value6", callbackArg);
            ctm.commit();
        }
    });
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region)

Example 4 with EntryEvent

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

the class SearchAndLoadDUnitTest method testOneHopNetWriteRemoteWriter.

/** same as the previous test but the cache writer is in a third, non-replicated, vm */
@Test
public void testOneHopNetWriteRemoteWriter() throws CacheException, InterruptedException {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    final String name = this.getUniqueName() + "Region";
    final String objectName = "Object7";
    final Integer value = new Integer(483);
    final Integer updateValue = new Integer(484);
    vm0.invoke(new SerializableRunnable("Create replicate Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating empty region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Create empty Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.EMPTY);
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating empty region", ex);
            }
        }
    });
    vm2.invoke(new SerializableRunnable("Create replicated region with cacheWriter") {

        public void run() {
            netWriteInvoked = false;
            operationWasCreate = false;
            originWasRemote = false;
            writerInvocationCount = 0;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.EMPTY);
                factory.setCacheWriter(new CacheWriter() {

                    public void beforeCreate(EntryEvent e) throws CacheWriterException {
                        e.getRegion().getCache().getLogger().info("cache writer beforeCreate invoked for " + e);
                        netWriteInvoked = true;
                        operationWasCreate = true;
                        originWasRemote = e.isOriginRemote();
                        writerInvocationCount++;
                        return;
                    }

                    public void beforeUpdate(EntryEvent e) throws CacheWriterException {
                        e.getRegion().getCache().getLogger().info("cache writer beforeUpdate invoked for " + e);
                        netWriteInvoked = true;
                        operationWasCreate = false;
                        originWasRemote = e.isOriginRemote();
                        writerInvocationCount++;
                        return;
                    }

                    public void beforeDestroy(EntryEvent e) throws CacheWriterException {
                    }

                    public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
                    }

                    public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
                    }

                    public void close() {
                    }
                });
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating replicated region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("do a put that should be proxied in the other vm and invoke its cache writer") {

        public void run() {
            try {
                getRootRegion().getSubregion(name).put(objectName, value);
            } catch (CacheWriterException cwe) {
            } catch (TimeoutException te) {
            }
        }
    });
    vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {

        public void run() {
            assertTrue("expected cache writer to be invoked", netWriteInvoked);
            assertTrue("expected originRemote to be true", originWasRemote);
            assertTrue("expected event to be create", operationWasCreate);
            assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
            // set flags for the next test - updating the same key
            netWriteInvoked = false;
            writerInvocationCount = 0;
        }
    });
    vm1.invoke(new SerializableRunnable("do an update that should be proxied in the other vm and invoke its cache writer") {

        public void run() {
            try {
                getRootRegion().getSubregion(name).put(objectName, updateValue);
            } catch (CacheWriterException cwe) {
            } catch (TimeoutException te) {
            }
        }
    });
    vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {

        public void run() {
            assertTrue("expected cache writer to be invoked", netWriteInvoked);
            assertTrue("expected originRemote to be true", originWasRemote);
            assertTrue("expected event to be create", operationWasCreate);
            assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
        }
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) CacheWriter(org.apache.geode.cache.CacheWriter) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) RegionEvent(org.apache.geode.cache.RegionEvent) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with EntryEvent

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

the class RegionTestCase method testRegionIdleInvalidate.

/**
   * Tests that a region that remains idle for a given amount of time is invalidated. Also tests
   * that accessing an entry of a region or a subregion counts as an access.
   */
@Test
public void testRegionIdleInvalidate() throws InterruptedException, CacheException {
    if (getRegionAttributes().getPartitionAttributes() != null) {
        // PR does not support INVALID ExpirationAction
        return;
    }
    final String name = this.getUniqueName();
    final String subname = this.getUniqueName() + "-SUB";
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    vm0.invoke(new CacheSerializableRunnable("testRegionIdleInvalidate") {

        public void run2() throws CacheException {
            TestCacheListener list = new TestCacheListener() {

                private int createCount = 0;

                public void afterInvalidate2(EntryEvent e) {
                    e.getRegion().getCache().getLogger().info("invalidate2 key=" + e.getKey());
                }

                public void afterRegionInvalidate2(RegionEvent e) {
                }

                public void afterUpdate2(EntryEvent e) {
                    // Clear the flag
                    this.wasInvoked();
                }

                public void afterCreate2(EntryEvent e) {
                    this.createCount++;
                    // we only expect one create; all the rest should be updates
                    assertEquals(1, this.createCount);
                    // Clear the flag
                    this.wasInvoked();
                }
            };
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
            factory.setRegionIdleTimeout(expire);
            factory.setStatisticsEnabled(true);
            RegionAttributes subRegAttrs = factory.create();
            factory.setCacheListener(list);
            RegionAttributes attrs = factory.create();
            Region region = null;
            Region sub = null;
            Region.Entry entry = null;
            long tilt;
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            ExpiryTask.suspendExpiration();
            try {
                region = createRegion(name, attrs);
                region.put(key, value);
                tilt = System.currentTimeMillis() + timeout;
                entry = region.getEntry(key);
                assertEquals(value, entry.getValue());
                sub = region.createSubregion(subname, subRegAttrs);
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
                ExpiryTask.permitExpiration();
            }
            waitForInvalidate(entry, tilt, 10);
            assertTrue(list.waitForInvocation(333));
            // The next phase of the test verifies that a get will cause the
            // expiration time to be extended.
            // For this phase we don't worry about actually expiring but just
            // making sure the expiration time gets extended.
            final int EXPIRATION_MS = 9000;
            region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(EXPIRATION_MS, ExpirationAction.INVALIDATE));
            LocalRegion lr = (LocalRegion) region;
            {
                ExpiryTask expiryTask = lr.getRegionIdleExpiryTask();
                region.put(key, value);
                long createExpiry = expiryTask.getExpirationTime();
                long changeTime = Wait.waitForExpiryClockToChange(lr, createExpiry - EXPIRATION_MS);
                region.put(key, "VALUE2");
                long putExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected putBaseExpiry=" + (putExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (putExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected putExpiry=" + putExpiry + " to be > than createExpiry=" + createExpiry, (putExpiry - createExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, putExpiry - EXPIRATION_MS);
                region.get(key);
                long getExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected getBaseExpiry=" + (getExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (getExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected getExpiry=" + getExpiry + " to be > than putExpiry=" + putExpiry, (getExpiry - putExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, getExpiry - EXPIRATION_MS);
                sub.put(key, value);
                long subPutExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subPutBaseExpiry=" + (subPutExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subPutExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subPutExpiry=" + subPutExpiry + " to be > than getExpiry=" + getExpiry, (subPutExpiry - getExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, subPutExpiry - EXPIRATION_MS);
                sub.get(key);
                long subGetExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subGetBaseExpiry=" + (subGetExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subGetExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subGetExpiry=" + subGetExpiry + " to be > than subPutExpiry=" + subPutExpiry, (subGetExpiry - subPutExpiry) > 0);
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) RegionEvent(org.apache.geode.cache.RegionEvent) Entry(org.apache.geode.cache.Region.Entry) ExpiryTask(org.apache.geode.internal.cache.ExpiryTask) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

EntryEvent (org.apache.geode.cache.EntryEvent)111 AttributesFactory (org.apache.geode.cache.AttributesFactory)75 Region (org.apache.geode.cache.Region)69 Test (org.junit.Test)66 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)55 RegionAttributes (org.apache.geode.cache.RegionAttributes)37 LocalRegion (org.apache.geode.internal.cache.LocalRegion)32 CacheException (org.apache.geode.cache.CacheException)30 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)30 VM (org.apache.geode.test.dunit.VM)29 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)26 CacheWriterException (org.apache.geode.cache.CacheWriterException)22 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)22 Host (org.apache.geode.test.dunit.Host)21 Properties (java.util.Properties)20 RegionEvent (org.apache.geode.cache.RegionEvent)18 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)17 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)15 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)15 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)15