Search in sources :

Example 1 with MembershipListener

use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.

the class QueueAdvancedTest method testDeadTaker.

/**
 * Test for issue 730 (Google).
 */
@Test
public void testDeadTaker() throws Exception {
    Config config = getConfig();
    CountDownLatch shutdownLatch = new CountDownLatch(1);
    config.addListenerConfig(new ListenerConfig().setImplementation(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            shutdownLatch.countDown();
        }
    }));
    HazelcastInstance[] instances = createHazelcastInstanceFactory(2).newInstances(config);
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    warmUpPartitions(h1, h2);
    IQueue<VersionedObject<String>> q1 = h1.getQueue("default");
    IQueue<VersionedObject<String>> q2 = h2.getQueue("default");
    CountDownLatch startLatch = new CountDownLatch(1);
    new Thread(() -> {
        try {
            assertTrue("Expected startLatch.await() to succeed within 10 seconds", startLatch.await(10, SECONDS));
            Thread.sleep(5000);
            h2.getLifecycleService().terminate();
        } catch (InterruptedException e) {
            LOG.info(e);
        }
    }).start();
    new Thread(() -> {
        try {
            startLatch.countDown();
            VersionedObject<String> value = q2.take();
            fail("Should not be able to take value from queue, but got: " + value);
        } catch (HazelcastInstanceNotActiveException e) {
            ignore(e);
        } catch (InterruptedException e) {
            LOG.info(e);
        }
    }).start();
    assertTrue("Expected shutdownLatch.await() to succeed within 1 minute", shutdownLatch.await(1, MINUTES));
    q1.offer(new VersionedObject<>("item"));
    assertEquals(1, q1.size());
    assertEquals(new VersionedObject<>("item"), q1.poll());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MembershipEvent(com.hazelcast.cluster.MembershipEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TestThread(com.hazelcast.test.TestThread) ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipListener(com.hazelcast.cluster.MembershipListener) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with MembershipListener

use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.

the class ClientClusterRestartEventTest method testMultiMemberRestart.

@Test
public void testMultiMemberRestart() throws ExecutionException, InterruptedException {
    HazelcastInstance instance1 = hazelcastFactory.newHazelcastInstance(newConfig());
    HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance(newConfig());
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(newClientConfig());
    Member oldMember1 = instance1.getCluster().getLocalMember();
    Member oldMember2 = instance2.getCluster().getLocalMember();
    final CountDownLatch memberAdded = new CountDownLatch(2);
    final Set<Member> addedMembers = Collections.newSetFromMap(new ConcurrentHashMap<Member, Boolean>());
    final CountDownLatch memberRemoved = new CountDownLatch(2);
    final Set<Member> removedMembers = Collections.newSetFromMap(new ConcurrentHashMap<Member, Boolean>());
    client.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            addedMembers.add(membershipEvent.getMember());
            memberAdded.countDown();
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            removedMembers.add(membershipEvent.getMember());
            memberRemoved.countDown();
        }
    });
    Cluster cluster = instance1.getCluster();
    assertTrueEventually(() -> {
        try {
            cluster.shutdown();
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    });
    // Allow same addresses to be used to test hot restart correctly
    hazelcastFactory.cleanup();
    Future<HazelcastInstance> f1 = spawn(() -> hazelcastFactory.newHazelcastInstance(newConfig()));
    Future<HazelcastInstance> f2 = spawn(() -> hazelcastFactory.newHazelcastInstance(newConfig()));
    instance1 = f1.get();
    instance2 = f2.get();
    Member newMember1 = instance1.getCluster().getLocalMember();
    Member newMember2 = instance2.getCluster().getLocalMember();
    assertOpenEventually(memberRemoved);
    assertEquals(2, removedMembers.size());
    assertContains(removedMembers, oldMember1);
    assertContains(removedMembers, oldMember2);
    assertOpenEventually(memberAdded);
    assertEquals(2, addedMembers.size());
    assertContains(addedMembers, newMember1);
    assertContains(addedMembers, newMember2);
    Set<Member> members = client.getCluster().getMembers();
    assertContains(members, newMember1);
    assertContains(members, newMember2);
    assertEquals(2, members.size());
}
Also used : MembershipEvent(com.hazelcast.cluster.MembershipEvent) Cluster(com.hazelcast.cluster.Cluster) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with MembershipListener

use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method applyInitialState.

private void applyInitialState(int version, Collection<MemberInfo> memberInfos) {
    MemberListSnapshot snapshot = createSnapshot(version, memberInfos);
    translateToPublicAddress.refresh(client.getClusterDiscoveryService().current().getAddressProvider(), memberInfos);
    memberListSnapshot.set(snapshot);
    logger.info(membersString(snapshot));
    Set<Member> members = toUnmodifiableHasSet(snapshot.members.values());
    InitialMembershipEvent event = new InitialMembershipEvent(client.getCluster(), members);
    for (MembershipListener listener : listeners.values()) {
        if (listener instanceof InitialMembershipListener) {
            ((InitialMembershipListener) listener).init(event);
        }
    }
}
Also used : InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent)

Example 4 with MembershipListener

use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.

the class AbstractListenersOnReconnectTest method testListenersTerminateRandomNode.

private void testListenersTerminateRandomNode(boolean isSmartClient) {
    factory.newInstances(null, 3);
    ClientConfig clientConfig = createClientConfig(isSmartClient);
    client = factory.newHazelcastClient(clientConfig);
    setupListener();
    terminateRandomNode();
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final CountDownLatch memberAddedLatch = new CountDownLatch(1);
    clientInstanceImpl.getClientClusterService().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            memberAddedLatch.countDown();
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
        }
    });
    factory.newHazelcastInstance();
    assertOpenEventually(memberAddedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : MembershipEvent(com.hazelcast.cluster.MembershipEvent) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.cluster.MembershipListener)

Example 5 with MembershipListener

use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testClientMembershipEvents_onSplitBrain.

@Test
public void testClientMembershipEvents_onSplitBrain() {
    Config config = new Config();
    HazelcastInstance instanceA = hazelcastFactory.newHazelcastInstance(config);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Integer.MAX_VALUE);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    HazelcastInstance instanceB = hazelcastFactory.newHazelcastInstance(config);
    HazelcastInstance instanceC = hazelcastFactory.newHazelcastInstance(config);
    Member memberA = instanceA.getCluster().getLocalMember();
    Member memberB = instanceB.getCluster().getLocalMember();
    Member memberC = instanceC.getCluster().getLocalMember();
    CountDownLatch splitLatch = new CountDownLatch(1);
    CountDownLatch bRemovedLatch = new CountDownLatch(1);
    CountDownLatch aRemovedLatch = new CountDownLatch(1);
    CountDownLatch switchedToCLatch = new CountDownLatch(1);
    client.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            if (memberC.equals(membershipEvent.getMember())) {
                switchedToCLatch.countDown();
            }
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            if (memberC.equals(membershipEvent.getMember())) {
                splitLatch.countDown();
            }
            if (memberB.equals(membershipEvent.getMember())) {
                bRemovedLatch.countDown();
            }
            if (memberA.equals(membershipEvent.getMember())) {
                aRemovedLatch.countDown();
            }
        }
    });
    blockCommunicationBetween(instanceA, instanceC);
    closeConnectionBetween(instanceA, instanceC);
    blockCommunicationBetween(instanceB, instanceC);
    closeConnectionBetween(instanceB, instanceC);
    assertOpenEventually(" A B | C", splitLatch);
    instanceB.shutdown();
    assertOpenEventually(" A | C", bRemovedLatch);
    instanceA.shutdown();
    assertOpenEventually("_ | C", aRemovedLatch);
    assertOpenEventually("Client should connect to C and see C as memberAdded", switchedToCLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) MembershipEvent(com.hazelcast.cluster.MembershipEvent) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MembershipListener (com.hazelcast.cluster.MembershipListener)8 MembershipEvent (com.hazelcast.cluster.MembershipEvent)6 Member (com.hazelcast.cluster.Member)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Test (org.junit.Test)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 ClientConfig (com.hazelcast.client.config.ClientConfig)2 Config (com.hazelcast.config.Config)2 ListenerConfig (com.hazelcast.config.ListenerConfig)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ClientListener (com.hazelcast.client.ClientListener)1 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)1 Address (com.hazelcast.cluster.Address)1 Cluster (com.hazelcast.cluster.Cluster)1 InitialMembershipEvent (com.hazelcast.cluster.InitialMembershipEvent)1 InitialMembershipListener (com.hazelcast.cluster.InitialMembershipListener)1 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)1 DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)1