Search in sources :

Example 26 with CacheWriter

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

the class DistributedRegion method cacheWriteBeforeDestroy.

/**
   * @return true if cacheWrite was performed
   */
@Override
boolean cacheWriteBeforeDestroy(EntryEventImpl event, Object expectedOldValue) throws CacheWriterException, EntryNotFoundException, TimeoutException {
    boolean result = false;
    if (event.isDistributed()) {
        CacheWriter localWriter = basicGetWriter();
        Set netWriteRecipients = localWriter == null ? this.distAdvisor.adviseNetWrite() : null;
        if ((localWriter != null || (netWriteRecipients != null && !netWriteRecipients.isEmpty())) && !event.inhibitAllNotifications()) {
            final long start = getCachePerfStats().startCacheWriterCall();
            try {
                event.setOldValueFromRegion();
                SearchLoadAndWriteProcessor processor = SearchLoadAndWriteProcessor.getProcessor();
                try {
                    processor.initialize(this, event.getKey(), null);
                    processor.doNetWrite(event, netWriteRecipients, localWriter, SearchLoadAndWriteProcessor.BEFOREDESTROY);
                    result = true;
                } finally {
                    processor.release();
                }
            } finally {
                getCachePerfStats().endCacheWriterCall(start);
            }
        }
        serverDestroy(event, expectedOldValue);
    }
    return result;
}
Also used : Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) CacheWriter(org.apache.geode.cache.CacheWriter)

Example 27 with CacheWriter

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

the class SearchAndLoadDUnitTest method testOneHopNetWrite.

@Test
public void testOneHopNetWrite() throws CacheException, InterruptedException {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    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 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.PERSISTENT_REPLICATE);
                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("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);
            }
        }
    });
    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) {
            }
        }
    });
    vm0.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) {
            }
        }
    });
    vm0.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 28 with CacheWriter

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

the class SearchAndLoadDUnitTest method testNetWrite.

@Test
public void testNetWrite() throws CacheException, InterruptedException {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName() + "-ACK";
    final String objectName = "Gemfire7";
    final Integer value = new Integer(483);
    vm0.invoke(new SerializableRunnable("Create ACK Region with cacheWriter") {

        public void run() {
            netWriteInvoked = false;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setCacheWriter(new CacheWriter() {

                    public void beforeCreate(EntryEvent e) throws CacheWriterException {
                        netWriteInvoked = true;
                        return;
                    }

                    public void beforeUpdate(EntryEvent e) throws CacheWriterException {
                        netWriteInvoked = true;
                        return;
                    }

                    public void beforeDestroy(EntryEvent e) throws CacheWriterException {
                        return;
                    }

                    public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
                        return;
                    }

                    public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
                        return;
                    }

                    public void close() {
                    }
                });
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            loaderInvoked = false;
            remoteLoaderInvoked = false;
            netWriteInvoked = false;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Do a put operation resulting in cache writer notification in other vm") {

        public void run() {
            try {
                getRootRegion().getSubregion(name).put(objectName, value);
                try {
                    Object result = getRootRegion().getSubregion(name).get(objectName);
                    assertEquals(result, value);
                } catch (CacheLoaderException cle) {
                } catch (TimeoutException te) {
                }
            } catch (CacheWriterException cwe) {
            } catch (TimeoutException te) {
            }
        }
    });
    vm0.invoke(new SerializableRunnable("ensure that cache writer was invoked") {

        public void run() {
            assertTrue("expected cache writer to be invoked", netWriteInvoked);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) RegionEvent(org.apache.geode.cache.RegionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) CacheWriter(org.apache.geode.cache.CacheWriter) EntryEvent(org.apache.geode.cache.EntryEvent) TimeoutException(org.apache.geode.cache.TimeoutException) CacheWriterException(org.apache.geode.cache.CacheWriterException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 29 with CacheWriter

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

the class LocalRegion method cacheWriteBeforeDestroy.

/**
   * @return true if cacheWrite was performed
   * @see DistributedRegion#cacheWriteBeforeDestroy(EntryEventImpl, Object)
   */
boolean cacheWriteBeforeDestroy(EntryEventImpl event, Object expectedOldValue) throws CacheWriterException, EntryNotFoundException, TimeoutException {
    boolean result = false;
    // copy into local var to prevent race condition
    CacheWriter writer = basicGetWriter();
    if (writer != null && event.getOperation() != Operation.REMOVE && !event.inhibitAllNotifications()) {
        final long start = getCachePerfStats().startCacheWriterCall();
        try {
            writer.beforeDestroy(event);
        } finally {
            getCachePerfStats().endCacheWriterCall(start);
        }
        result = true;
    }
    serverDestroy(event, expectedOldValue);
    return result;
}
Also used : CacheWriter(org.apache.geode.cache.CacheWriter)

Example 30 with CacheWriter

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

the class ProxyDUnitTest method distributedOps.

////////////////////// Test Methods //////////////////////
/**
   * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
   */
private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    Region r = createRootRegion("ProxyDUnitTest", af.create());
    doCreateOtherVm();
    r.put("putkey", "putvalue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check put") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals("putvalue1", r.getEntry("putkey").getValue());
            r.put("putkey", "putvalue2");
        }
    });
    assertEquals(false, r.containsKey("putkey"));
    // netsearch
    assertEquals("putvalue2", r.get("putkey"));
    r.invalidate("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check invalidate") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals(null, r.getEntry("putkey").getValue());
        }
    });
    // invalid so total miss
    assertEquals(null, r.get("putkey"));
    r.destroy("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check destroy") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(false, r.containsKey("putkey"));
        }
    });
    // total miss
    assertEquals(null, r.get("putkey"));
    r.create("createKey", "createValue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check create") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("createKey"));
            assertEquals("createValue1", r.getEntry("createKey").getValue());
        }
    });
    {
        Map m = new HashMap();
        m.put("putAllKey1", "putAllValue1");
        m.put("putAllKey2", "putAllValue2");
        r.putAll(m, "putAllCallback");
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check putAll") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putAllKey1"));
            assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
            assertEquals(true, r.containsKey("putAllKey2"));
            assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
        }
    });
    r.clear();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheWriter") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            CacheWriter cw = new CacheWriterAdapter() {

                public void beforeCreate(EntryEvent event) throws CacheWriterException {
                    throw new CacheWriterException("expected");
                }
            };
            am.setCacheWriter(cw);
        }
    });
    try {
        r.put("putkey", "putvalue");
        fail("expected CacheWriterException");
    } catch (CacheWriterException expected) {
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    // total miss
    assertEquals(null, r.get("loadkey"));
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheLoader") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            // clear csche writer
            am.setCacheWriter(null);
            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);
        }
    });
    // net load
    assertEquals("loadvalue", r.get("loadkey"));
    // total miss
    assertEquals(null, r.get("foobar"));
    try {
        r.get("loadexception");
        fail("expected CacheLoaderException");
    } catch (CacheLoaderException expected) {
    }
    r.destroyRegion();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(null, r);
        }
    });
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) 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)

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