Search in sources :

Example 21 with CacheWriter

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

the class LocalRegion method cacheWriteBeforeRegionDestroy.

/**
   * @return true if cacheWrite was performed
   * @see DistributedRegion#cacheWriteBeforeRegionDestroy(RegionEventImpl)
   */
boolean cacheWriteBeforeRegionDestroy(RegionEventImpl event) throws CacheWriterException, TimeoutException {
    boolean result = false;
    // copy into local var to prevent race condition
    CacheWriter writer = basicGetWriter();
    if (writer != null) {
        final long start = getCachePerfStats().startCacheWriterCall();
        try {
            writer.beforeRegionDestroy(event);
        } finally {
            getCachePerfStats().endCacheWriterCall(start);
        }
        result = true;
    }
    serverRegionDestroy(event);
    return result;
}
Also used : CacheWriter(org.apache.geode.cache.CacheWriter)

Example 22 with CacheWriter

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

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

the class PutAllCallBkRemoteVMDUnitTest method createCacheForVM1.

public static void createCacheForVM1() {
    try {
        CacheListener aListener = new AfterCreateCallback();
        CacheWriter aWriter = new BeforeCreateCallback();
        ds = (new PutAllCallBkRemoteVMDUnitTest()).getSystem(props);
        cache = CacheFactory.create(ds);
        AttributesFactory factory = new AttributesFactory();
        factory.setDataPolicy(DataPolicy.REPLICATE);
        factory.setScope(Scope.DISTRIBUTED_ACK);
        RegionAttributes attr1 = factory.create();
        paperRegion = cache.createRegion("paper", attr1);
        factory.setCacheWriter(aWriter);
        factory.addCacheListener(aListener);
        RegionAttributes attr = factory.create();
        region = cache.createRegion("map", attr);
    } catch (CacheException ex) {
        throw new RuntimeException("vm1 cache creation exception", ex);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) CacheWriter(org.apache.geode.cache.CacheWriter) CacheListener(org.apache.geode.cache.CacheListener)

Example 24 with CacheWriter

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

the class PutAllCallBkSingleVMDUnitTest method testputAllSingleVM.

// test methods
@Test
public void testputAllSingleVM() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    // Object obj2;
    Object[] objArr = new Object[1];
    for (int i = 0; i < 5; i++) {
        objArr[0] = "" + i;
        vm0.invoke(PutAllCallBkSingleVMDUnitTest.class, "putMethod", objArr);
    }
    vm0.invoke(() -> PutAllCallBkSingleVMDUnitTest.putAllMethod());
    vm0.invoke(new CacheSerializableRunnable("temp1") {

        public void run2() throws CacheException {
            if (!afterCreate) {
                fail("FAILED in aftercreate call back");
            }
            assertEquals(region.size(), putAllcounter);
            assertEquals(region.size(), beforeCreateputAllcounter);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("abc") {

        public void run2() throws CacheException {
            CacheListener bListener = new AfterUpdateCallback();
            CacheWriter bWriter = new BeforeUpdateCallback();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setCacheWriter(bWriter);
            factory.setCacheListener(bListener);
            RegionAttributes attr = factory.create();
            Region tempRegion = cache.createRegion("temp", attr);
            // to invoke afterUpdate we should make sure that entries are already present
            for (int i = 0; i < 5; i++) {
                tempRegion.put(new Integer(i), new String("region" + i));
            }
            Map m = new HashMap();
            for (int i = 0; i < 5; i++) {
                m.put(new Integer(i), new String("map" + i));
            }
            tempRegion.putAll(m, "putAllAfterUpdateCallback");
            // now, verifying callbacks
            if (!afterUpdate) {
                fail("FAILED in afterupdate call back");
            }
            assertEquals(tempRegion.size(), afterUpdateputAllcounter);
            assertEquals(tempRegion.size(), beforeUpdateputAllcounter);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) HashMap(java.util.HashMap) Host(org.apache.geode.test.dunit.Host) CacheListener(org.apache.geode.cache.CacheListener) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) CacheWriter(org.apache.geode.cache.CacheWriter) Region(org.apache.geode.cache.Region) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 25 with CacheWriter

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

the class DistributedRegion method cacheWriteBeforeRegionDestroy.

@Override
boolean cacheWriteBeforeRegionDestroy(RegionEventImpl event) throws CacheWriterException, TimeoutException {
    boolean result = false;
    if (event.getOperation().isDistributed()) {
        CacheWriter localWriter = basicGetWriter();
        Set netWriteRecipients = localWriter == null ? this.distAdvisor.adviseNetWrite() : null;
        if (localWriter != null || netWriteRecipients != null && !netWriteRecipients.isEmpty()) {
            final long start = getCachePerfStats().startCacheWriterCall();
            try {
                SearchLoadAndWriteProcessor processor = SearchLoadAndWriteProcessor.getProcessor();
                try {
                    processor.initialize(this, "preDestroyRegion", null);
                    processor.doNetWrite(event, netWriteRecipients, localWriter, SearchLoadAndWriteProcessor.BEFOREREGIONDESTROY);
                    result = true;
                } finally {
                    processor.release();
                }
            } finally {
                getCachePerfStats().endCacheWriterCall(start);
            }
        }
        serverRegionDestroy(event);
    }
    return result;
}
Also used : Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) CacheWriter(org.apache.geode.cache.CacheWriter)

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