Search in sources :

Example 6 with CacheWriter

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

the class LocalRegion method cacheWriteBeforeRegionClear.

private void cacheWriteBeforeRegionClear(RegionEventImpl event) throws CacheWriterException, TimeoutException {
    // copy into local var to prevent race condition
    CacheWriter writer = basicGetWriter();
    if (writer != null) {
        final long start = getCachePerfStats().startCacheWriterCall();
        try {
            writer.beforeRegionClear(event);
        } finally {
            getCachePerfStats().endCacheWriterCall(start);
        }
    }
    serverRegionClear(event);
}
Also used : CacheWriter(org.apache.geode.cache.CacheWriter)

Example 7 with CacheWriter

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

the class PartitionedRegion method cacheWriteBeforeDestroy.

/**
   * Invoke the CacheWriter before the detroy operation occurs. Each BucketRegion delegates to the
   * CacheWriter on the PartitionedRegion meaning that CacheWriters on a BucketRegion should only be
   * used for internal purposes.
   * 
   * @see BucketRegion#cacheWriteBeforeDestroy(EntryEventImpl, Object)
   */
@Override
boolean cacheWriteBeforeDestroy(EntryEventImpl event, Object expectedOldValue) throws CacheWriterException, EntryNotFoundException, TimeoutException {
    // return if notifications are inhibited
    if (event.inhibitAllNotifications()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Notification inhibited for key {}", event.getKey());
        }
        return false;
    }
    if (event.isDistributed()) {
        serverDestroy(event, expectedOldValue);
        CacheWriter localWriter = basicGetWriter();
        Set netWriteRecipients = localWriter == null ? this.distAdvisor.adviseNetWrite() : null;
        if (localWriter == null && (netWriteRecipients == null || netWriteRecipients.isEmpty())) {
            return false;
        }
        final long start = getCachePerfStats().startCacheWriterCall();
        try {
            event.setOldValueFromRegion();
            SearchLoadAndWriteProcessor processor = SearchLoadAndWriteProcessor.getProcessor();
            processor.initialize(this, event.getKey(), null);
            processor.doNetWrite(event, netWriteRecipients, localWriter, SearchLoadAndWriteProcessor.BEFOREDESTROY);
            processor.release();
        } finally {
            getCachePerfStats().endCacheWriterCall(start);
        }
        return true;
    }
    return false;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) CacheWriter(org.apache.geode.cache.CacheWriter)

Example 8 with CacheWriter

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

the class CacheXml66DUnitTest method testCacheWriter.

/**
   * Tests a cache writer with no parameters
   */
@Test
public void testCacheWriter() throws Exception {
    CacheCreation cache = new CacheCreation();
    RegionAttributesCreation attrs = new RegionAttributesCreation(cache);
    CacheWriter writer = new MyTestCacheWriter();
    attrs.setCacheWriter(writer);
    cache.createRegion("root", attrs);
    testXml(cache);
}
Also used : CacheWriter(org.apache.geode.cache.CacheWriter) RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) ClientCacheCreation(org.apache.geode.internal.cache.xmlcache.ClientCacheCreation) Test(org.junit.Test)

Example 9 with CacheWriter

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

use of org.apache.geode.cache.CacheWriter 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)

Aggregations

CacheWriter (org.apache.geode.cache.CacheWriter)31 AttributesFactory (org.apache.geode.cache.AttributesFactory)16 CacheException (org.apache.geode.cache.CacheException)12 RegionAttributes (org.apache.geode.cache.RegionAttributes)9 CacheListener (org.apache.geode.cache.CacheListener)7 Test (org.junit.Test)7 Set (java.util.Set)6 CacheWriterException (org.apache.geode.cache.CacheWriterException)6 EntryEvent (org.apache.geode.cache.EntryEvent)6 TimeoutException (org.apache.geode.cache.TimeoutException)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 HashSet (java.util.HashSet)5 RegionEvent (org.apache.geode.cache.RegionEvent)5 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)5 CacheLoader (org.apache.geode.cache.CacheLoader)4 Region (org.apache.geode.cache.Region)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AttributesMutator (org.apache.geode.cache.AttributesMutator)3 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)3