Search in sources :

Example 41 with CacheListener

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

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

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

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

the class DistributedRegion method postCreateRegion.

/**
   * In addition to inherited code this method also invokes RegionMembershipListeners
   */
@Override
protected void postCreateRegion() {
    super.postCreateRegion();
    // should we sync on this.distAdvisor first to prevent bug 44369?
    synchronized (this.advisorListener) {
        Set others = this.advisorListener.getInitialMembers();
        CacheListener[] listeners = fetchCacheListenersField();
        if (listeners != null) {
            for (CacheListener listener : listeners) {
                if (listener instanceof RegionMembershipListener) {
                    RegionMembershipListener regionMembershipListener = (RegionMembershipListener) listener;
                    try {
                        DistributedMember[] otherDms = new DistributedMember[others.size()];
                        others.toArray(otherDms);
                        regionMembershipListener.initialMembers(this, otherDms);
                    } catch (VirtualMachineError err) {
                        SystemFailure.initiateFailure(err);
                        // now, so don't let this thread continue.
                        throw err;
                    } catch (Throwable t) {
                        // Whenever you catch Error or Throwable, you must also
                        // catch VirtualMachineError (see above). However, there is
                        // _still_ a possibility that you are dealing with a cascading
                        // error condition, so you also need to check to see if the JVM
                        // is still usable:
                        SystemFailure.checkFailure();
                        logger.error(LocalizedMessage.create(LocalizedStrings.DistributedRegion_EXCEPTION_OCCURRED_IN_REGIONMEMBERSHIPLISTENER), t);
                    }
                }
            }
        }
        Set<String> allGatewaySenderIds = getAllGatewaySenderIds();
        if (!allGatewaySenderIds.isEmpty()) {
            for (GatewaySender sender : this.cache.getAllGatewaySenders()) {
                if (sender.isParallel() && allGatewaySenderIds.contains(sender.getId())) {
                    // addShadowPartitionedRegionForUserRR
                    if (sender.getId().contains(AsyncEventQueueImpl.ASYNC_EVENT_QUEUE_PREFIX)) {
                        throw new AsyncEventQueueConfigurationException(LocalizedStrings.ParallelAsyncEventQueue_0_CAN_NOT_BE_USED_WITH_REPLICATED_REGION_1.toLocalizedString(new Object[] { AsyncEventQueueImpl.getAsyncEventQueueIdFromSenderId(sender.getId()), this.getFullPath() }));
                    }
                    throw new GatewaySenderConfigurationException(LocalizedStrings.ParallelGatewaySender_0_CAN_NOT_BE_USED_WITH_REPLICATED_REGION_1.toLocalizedString(new Object[] { sender.getId(), this.getFullPath() }));
                }
            }
        }
    }
}
Also used : GatewaySender(org.apache.geode.cache.wan.GatewaySender) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) GatewaySenderConfigurationException(org.apache.geode.internal.cache.wan.GatewaySenderConfigurationException) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) CacheListener(org.apache.geode.cache.CacheListener) AsyncEventQueueConfigurationException(org.apache.geode.internal.cache.wan.AsyncEventQueueConfigurationException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember)

Example 45 with CacheListener

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

the class AbstractRegion method initCacheListeners.

@Override
public void initCacheListeners(CacheListener[] newListeners) {
    checkReadiness();
    CacheListener[] listenersToAdd = null;
    if (newListeners != null) {
        listenersToAdd = new CacheListener[newListeners.length];
        for (int i = 0; i < newListeners.length; i++) {
            listenersToAdd[i] = wrapRegionMembershipListener(newListeners[i]);
        }
    }
    CacheListener[] oldListeners;
    synchronized (this.clSync) {
        oldListeners = this.cacheListeners;
        if (listenersToAdd == null || listenersToAdd.length == 0) {
            this.cacheListeners = EMPTY_LISTENERS;
        } else {
            // we have some listeners to add
            if (Arrays.asList(listenersToAdd).contains(null)) {
                throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_INITCACHELISTENERS_PARAMETER_HAD_A_NULL_ELEMENT.toLocalizedString());
            }
            CacheListener[] newCacheListeners = new CacheListener[listenersToAdd.length];
            System.arraycopy(listenersToAdd, 0, newCacheListeners, 0, newCacheListeners.length);
            this.cacheListeners = newCacheListeners;
        }
    }
    // moved the following out of the sync for bug 34512
    if (listenersToAdd == null || listenersToAdd.length == 0) {
        if (oldListeners != null && oldListeners.length > 0) {
            for (CacheListener oldListener : oldListeners) {
                closeCacheCallback(oldListener);
            }
            cacheListenersChanged(false);
        }
    } else {
        // we had some listeners to add
        if (oldListeners != null && oldListeners.length > 0) {
            for (CacheListener oldListener : oldListeners) {
                closeCacheCallback(oldListener);
            }
        } else {
            cacheListenersChanged(true);
        }
    }
}
Also used : CacheListener(org.apache.geode.cache.CacheListener) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint)

Aggregations

CacheListener (org.apache.geode.cache.CacheListener)55 AttributesFactory (org.apache.geode.cache.AttributesFactory)28 Region (org.apache.geode.cache.Region)24 RegionAttributes (org.apache.geode.cache.RegionAttributes)17 EntryEvent (org.apache.geode.cache.EntryEvent)13 Test (org.junit.Test)13 CacheException (org.apache.geode.cache.CacheException)12 Properties (java.util.Properties)11 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)11 VM (org.apache.geode.test.dunit.VM)11 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)11 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)8 CacheWriter (org.apache.geode.cache.CacheWriter)7 AttributesMutator (org.apache.geode.cache.AttributesMutator)6 Host (org.apache.geode.test.dunit.Host)6 Iterator (java.util.Iterator)5 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)5 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)5 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)5 CacheLoader (org.apache.geode.cache.CacheLoader)4