Search in sources :

Example 46 with EntryEvent

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

the class EventIDVerificationDUnitTest method createClientCache.

public static void createClientCache(String host, Integer port1, Integer port2) throws Exception {
    PORT1 = port1.intValue();
    PORT2 = port2.intValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new EventIDVerificationDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setMirrorType(MirrorType.NONE);
    ClientServerTestCase.configureConnectionPool(factory, host, new int[] { PORT1, PORT2 }, true, -1, 2, null, -1, -1, false, -2);
    CacheWriter writer = new CacheWriterAdapter() {

        public void beforeCreate(EntryEvent event) {
            EventID eventId = ((EntryEventImpl) event).getEventId();
            vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            try {
                super.beforeCreate(event);
            } catch (CacheWriterException e) {
                e.printStackTrace();
                fail("Test failed bcoz of exception =" + e);
            }
        }

        public void beforeUpdate(EntryEvent event) {
            EventID eventId = ((EntryEventImpl) event).getEventId();
            vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            try {
                super.beforeUpdate(event);
            } catch (CacheWriterException e) {
                e.printStackTrace();
                fail("Test failed bcoz of exception =" + e);
            }
        }

        public void beforeDestroy(EntryEvent event) {
            EventID eventId = ((EntryEventImpl) event).getEventId();
            vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            try {
                super.beforeDestroy(event);
            } catch (CacheWriterException e) {
                e.printStackTrace();
                fail("Test failed bcoz of exception =" + e);
            }
        }

        public void beforeRegionDestroy(RegionEvent event) {
            EventID eventId = ((RegionEventImpl) event).getEventId();
            vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            try {
                super.beforeRegionDestroy(event);
            } catch (CacheWriterException e) {
                e.printStackTrace();
                fail("Test failed bcoz of exception =" + e);
            }
        }

        public void beforeRegionClear(RegionEvent event) {
            EventID eventId = ((RegionEventImpl) event).getEventId();
            vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
            try {
                super.beforeRegionClear(event);
            } catch (CacheWriterException e) {
                e.printStackTrace();
                fail("Test failed bcoz of exception =" + e);
            }
        }
    };
    factory.setCacheWriter(writer);
    /*
     * factory.setCacheListener(new CacheListenerAdapter() { public void afterCreate(EntryEvent
     * event) { synchronized (this) { threadId = ((EntryEventImpl)event).getEventId().getThreadID();
     * membershipId = ((EntryEventImpl)event).getEventId().getMembershipID(); } }
     *
     * public void afterUpdate(EntryEvent event) { synchronized (this) { verifyEventIDs(event); } }
     *
     * public void afterDestroy(EntryEvent event) { synchronized (this) { verifyEventIDs(event); } }
     * public void afterRegionDestroy(RegionEvent event) { synchronized (this) { threadId =
     * ((RegionEventImpl)event).getEventId().getThreadID(); membershipId =
     * ((RegionEventImpl)event).getEventId().getMembershipID(); } } });
     */
    RegionAttributes attrs = factory.create();
    Region r = cache.createRegion(REGION_NAME, attrs);
    r.registerInterest("ALL_KEYS");
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) RegionAttributes(org.apache.geode.cache.RegionAttributes) RegionEventImpl(org.apache.geode.internal.cache.RegionEventImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheWriter(org.apache.geode.cache.CacheWriter) EntryEvent(org.apache.geode.cache.EntryEvent) EventID(org.apache.geode.internal.cache.EventID) Region(org.apache.geode.cache.Region) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 47 with EntryEvent

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

the class PartitionedRegionHelper method getPRRoot.

/**
   * Return a region that is the root for all PartitionedRegion meta data on this Node. The main
   * administrative Regions contained within are <code>allPartitionedRegion</code> (Scope
   * DISTRIBUTED_ACK) and <code>bucket2Node</code> (Scope DISTRIBUTED_ACK) and dataStore regions.
   * 
   * @return a GLOBLAL scoped root region used for PartitionedRegion administration
   */
public static LocalRegion getPRRoot(final InternalCache cache, boolean createIfAbsent) {
    DistributedRegion root = (DistributedRegion) cache.getRegion(PR_ROOT_REGION_NAME, true);
    if (root == null) {
        if (!createIfAbsent) {
            return null;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Creating root Partitioned Admin Region {}", PartitionedRegionHelper.PR_ROOT_REGION_NAME);
        }
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.DISTRIBUTED_ACK);
        factory.setDataPolicy(DataPolicy.REPLICATE);
        factory.addCacheListener(new FixedPartitionAttributesListener());
        if (Boolean.getBoolean(DistributionConfig.GEMFIRE_PREFIX + "PRDebug")) {
            factory.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterCreate(EntryEvent event) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Create Event for allPR: key = {} oldVal = {} newVal = {} Op = {} origin = {} isNetSearch = {}", event.getKey(), event.getOldValue(), event.getNewValue(), event.getOperation(), event.getDistributedMember(), event.getOperation().isNetSearch());
                    }
                }

                @Override
                public void afterUpdate(EntryEvent event) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Update Event for allPR: key = {} oldVal = {} newVal = {} Op = {} origin = {} isNetSearch = {}", event.getKey(), event.getOldValue(), event.getNewValue(), event.getOperation(), event.getDistributedMember(), event.getOperation().isNetSearch());
                    }
                }

                @Override
                public void afterDestroy(EntryEvent event) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Destroy Event for allPR: key = {} oldVal = {} newVal = {} Op = {} origin = {} isNetSearch = {}", event.getKey(), event.getOldValue(), event.getNewValue(), event.getOperation(), event.getDistributedMember(), event.getOperation().isNetSearch());
                    }
                }
            });
            factory.setCacheWriter(new CacheWriterAdapter() {

                @Override
                public void beforeUpdate(EntryEvent event) throws CacheWriterException {
                    // the prConfig node list must advance (otherwise meta data becomes out of sync)
                    final PartitionRegionConfig newConf = (PartitionRegionConfig) event.getNewValue();
                    final PartitionRegionConfig oldConf = (PartitionRegionConfig) event.getOldValue();
                    if (newConf != oldConf && !newConf.isGreaterNodeListVersion(oldConf)) {
                        throw new CacheWriterException(LocalizedStrings.PartitionedRegionHelper_NEW_PARTITIONEDREGIONCONFIG_0_DOES_NOT_HAVE_NEWER_VERSION_THAN_PREVIOUS_1.toLocalizedString(new Object[] { newConf, oldConf }));
                    }
                }
            });
        }
        RegionAttributes ra = factory.create();
        // Create anonymous stats holder for Partitioned Region meta data
        final HasCachePerfStats prMetaStatsHolder = new HasCachePerfStats() {

            public CachePerfStats getCachePerfStats() {
                return new CachePerfStats(cache.getDistributedSystem(), "partitionMetaData");
            }
        };
        try {
            root = (DistributedRegion) cache.createVMRegion(PR_ROOT_REGION_NAME, ra, new InternalRegionArguments().setIsUsedForPartitionedRegionAdmin(true).setInternalRegion(true).setCachePerfStatsHolder(prMetaStatsHolder));
            root.getDistributionAdvisor().addMembershipListener(new MemberFailureListener());
        } catch (RegionExistsException ignore) {
            // we avoid this before hand, but yet we have to catch it
            root = (DistributedRegion) cache.getRegion(PR_ROOT_REGION_NAME, true);
        } catch (IOException ieo) {
            Assert.assertTrue(false, "IOException creating Partitioned Region root: " + ieo);
        } catch (ClassNotFoundException cne) {
            Assert.assertTrue(false, "ClassNotFoundExcpetion creating Partitioned Region root: " + cne);
        }
    }
    Assert.assertTrue(root != null, "Can not obtain internal Partitioned Region configuration root");
    return root;
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) RegionExistsException(org.apache.geode.cache.RegionExistsException) IOException(java.io.IOException) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 48 with EntryEvent

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

the class ClientServerRegisterInterestsDUnitTest method testClientRegisterInterests.

@Test
public void testClientRegisterInterests() {
    ClientCache clientCache = setupGemFireClientCache();
    try {
        Region<String, String> example = clientCache.getRegion("/Example");
        assertNotNull("'Example' Region in Client Cache was not found!", example);
        assertEquals(1, example.size());
        assertTrue(example.containsKey("1"));
        assertEquals("ONE", example.get("1"));
        assertTrue(entryEvents.empty());
        String value = put("/Example", "2", "TWO");
        assertEquals("TWO", value);
        waitOnEvent(WAIT_TIME_MILLISECONDS);
        assertFalse(entryEvents.empty());
        EntryEvent entryEvent = (EntryEvent) entryEvents.pop();
        assertEquals("2", entryEvent.getKey());
        assertEquals("TWO", entryEvent.getNewValue());
        assertNull(entryEvent.getOldValue());
        assertEquals(2, example.size());
        assertTrue(example.containsKey("2"));
        assertEquals("TWO", example.get("2"));
    } finally {
        clientCache.close();
    }
}
Also used : EntryEvent(org.apache.geode.cache.EntryEvent) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 49 with EntryEvent

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

the class CacheListenerTestCase method testCacheListenerAfterInvalidate.

/**
   * Tests that the <code>CacheListener</code> is called after an entry is
   * {@linkplain CacheListener#afterInvalidate invalidated}.
   */
@Test
public void testCacheListenerAfterInvalidate() throws CacheException {
    String name = this.getUniqueName();
    final Object key = this.getUniqueName();
    final Object value = new Integer(42);
    // Object arg = "ARG";
    TestCacheListener listener = new TestCacheListener() {

        public void afterCreate2(EntryEvent event) {
        // This method will get invoked when the region is populated
        }

        public void afterInvalidate2(EntryEvent event) {
            assertEquals(key, event.getKey());
            assertEquals(value, event.getOldValue());
            assertNull(event.getNewValue());
            verifyEventProps(event);
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheListener(listener);
    Region region = createRegion(name, factory.create());
    // Does not exist so should not invoke listener
    try {
        region.invalidate(key);
        fail("expected EntryNotFoundException");
    } catch (EntryNotFoundException expected) {
    }
    assertFalse(listener.wasInvoked());
    region.create(key, value);
    assertTrue(listener.wasInvoked());
    region.invalidate(key);
    assertTrue(listener.wasInvoked());
    // already invalid so should not invoke listener
    region.invalidate(key);
    assertFalse(listener.wasInvoked());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 50 with EntryEvent

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

the class CacheListenerTestCase method testCacheListenerAfterInvalidateWithForce.

@Test
public void testCacheListenerAfterInvalidateWithForce() throws CacheException {
    AbstractRegionMap.FORCE_INVALIDATE_EVENT = true;
    try {
        String name = this.getUniqueName();
        final Object key = this.getUniqueName();
        final Object value = new Integer(42);
        TestCacheListener listener = new TestCacheListener() {

            int invalidateCount = 0;

            public void afterCreate2(EntryEvent event) {
            // This method will get invoked when the region is populated
            }

            public void afterInvalidate2(EntryEvent event) {
                invalidateCount++;
                assertEquals(key, event.getKey());
                if (invalidateCount == 2) {
                    assertEquals(value, event.getOldValue());
                } else {
                    assertNull(event.getOldValue());
                }
                assertNull(event.getNewValue());
                verifyEventProps(event);
            }
        };
        AttributesFactory factory = new AttributesFactory(getRegionAttributes());
        factory.setCacheListener(listener);
        Region region = createRegion(name, factory.create());
        // Does not exist but should still invoke listener
        try {
            region.invalidate(key);
            fail("expected EntryNotFoundException");
        } catch (EntryNotFoundException expected) {
        }
        assertTrue(listener.wasInvoked());
        region.create(key, value);
        assertTrue(listener.wasInvoked());
        region.invalidate(key);
        assertTrue(listener.wasInvoked());
        // already invalid but should still invoke listener
        region.invalidate(key);
        assertTrue(listener.wasInvoked());
    } finally {
        AbstractRegionMap.FORCE_INVALIDATE_EVENT = false;
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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