Search in sources :

Example 16 with MembershipManager

use of org.apache.geode.distributed.internal.membership.MembershipManager in project geode by apache.

the class ConcurrentMapOpsDUnitTest method doRetriedOperation.

private void doRetriedOperation(final Operation op, boolean usePR) {
    Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM client = host.getVM(2);
    final int port1 = createRegionsAndStartServer(server1, true);
    final int port2 = createRegionsAndStartServer(server2, true);
    final String regionName = usePR ? PR_REG_NAME : REP_REG_NAME;
    IgnoredException.addIgnoredException("java.net.SocketException");
    createClientRegion(client, port1, false, port2);
    SerializableCallable getID = new SerializableCallable("get DM ID") {

        public Object call() {
            return getSystem().getDistributedMember();
        }
    };
    final DistributedMember server1ID = (DistributedMember) server1.invoke(getID);
    final DistributedMember server2ID = (DistributedMember) server2.invoke(getID);
    Set<IgnoredException> exceptions = new HashSet<IgnoredException>();
    exceptions.add(IgnoredException.addIgnoredException("Membership: requesting removal", server1));
    exceptions.add(IgnoredException.addIgnoredException("Membership: requesting removal", server2));
    exceptions.add(IgnoredException.addIgnoredException("ForcedDisconnect", server1));
    exceptions.add(IgnoredException.addIgnoredException("ForcedDisconnect", server2));
    try {
        server1.invoke(new SerializableCallable("install crasher in server1") {

            public Object call() throws Exception {
                Region r = getCache().getRegion(regionName);
                r.put("key0", "value");
                if (op == Operation.PUT_IF_ABSENT) {
                    r.destroy("key0");
                }
                // force client to use server1 for now
                // getCache().getCacheServers().get(0).stop();
                r.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

                    private void killSender(EntryEvent event) {
                        if (event.isOriginRemote()) {
                            MembershipManager mgr = MembershipManagerHelper.getMembershipManager(getSystem());
                            mgr.requestMemberRemoval(server2ID, "removing for test");
                            try {
                                mgr.waitForDeparture(server2ID);
                            } catch (Exception e) {
                                fail("failed to stop the other server for this test:" + e.getMessage());
                            }
                        }
                    }

                    @Override
                    public void afterCreate(EntryEvent event) {
                        getCache().getLogger().info("afterCreate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterUpdate(EntryEvent event) {
                        getCache().getLogger().info("afterUpdate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterDestroy(EntryEvent event) {
                        getCache().getLogger().info("afterDestroy invoked with " + event);
                        killSender(event);
                    }
                });
                return null;
            }
        });
        server2.invoke(new SerializableCallable("install crasher in server2") {

            public Object call() throws Exception {
                Region r = getCache().getRegion(regionName);
                // force client to use server1 for now
                // getCache().getCacheServers().get(0).stop();
                r.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

                    private void killSender(EntryEvent event) {
                        if (event.isOriginRemote()) {
                            MembershipManager mgr = MembershipManagerHelper.getMembershipManager(getSystem());
                            mgr.requestMemberRemoval(server1ID, "removing for test");
                            try {
                                mgr.waitForDeparture(server1ID);
                            } catch (Exception e) {
                                fail("failed to stop the other server for this test:" + e.getMessage());
                            }
                        }
                    }

                    @Override
                    public void afterCreate(EntryEvent event) {
                        getCache().getLogger().info("afterCreate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterUpdate(EntryEvent event) {
                        getCache().getLogger().info("afterUpdate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterDestroy(EntryEvent event) {
                        getCache().getLogger().info("afterDestroy invoked with " + event);
                        killSender(event);
                    }
                });
                return null;
            }
        });
        client.invoke(new SerializableRunnable() {

            public void run() {
                GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
                Region r = cache.getRegion(regionName);
                if (op == Operation.PUT_IF_ABSENT) {
                    assertTrue("expected putIfAbsent to succeed and return null", r.putIfAbsent("key0", "newvalue") == null);
                } else if (op == Operation.REMOVE) {
                    assertTrue("expected remove operation to succeed and return true", r.remove("key0", "value"));
                } else if (op == Operation.REPLACE) {
                    assertTrue("expected replace operation to succeed and return true", r.replace("key0", "value", "newvalue"));
                }
            }
        });
    } finally {
        disconnectAllFromDS();
        for (IgnoredException ex : exceptions) {
            ex.remove();
        }
    }
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) DistributedMember(org.apache.geode.distributed.DistributedMember) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Region(org.apache.geode.cache.Region) MembershipManager(org.apache.geode.distributed.internal.membership.MembershipManager)

Aggregations

MembershipManager (org.apache.geode.distributed.internal.membership.MembershipManager)16 GMSMembershipManager (org.apache.geode.distributed.internal.membership.gms.mgr.GMSMembershipManager)6 MembershipTest (org.apache.geode.test.junit.categories.MembershipTest)6 Test (org.junit.Test)6 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)5 Properties (java.util.Properties)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 IOException (java.io.IOException)3 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)3 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)3 File (java.io.File)2 ForcedDisconnectException (org.apache.geode.ForcedDisconnectException)2 GemFireConfigException (org.apache.geode.GemFireConfigException)2 SystemConnectException (org.apache.geode.SystemConnectException)2 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)2 NetView (org.apache.geode.distributed.internal.membership.NetView)2 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)2 Host (org.apache.geode.test.dunit.Host)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2 VM (org.apache.geode.test.dunit.VM)2