Search in sources :

Example 11 with MembershipEvent

use of com.hazelcast.cluster.MembershipEvent 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)

Example 12 with MembershipEvent

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

the class ClientClusterRestartEventTest method testSingleMemberRestart.

@Test
public void testSingleMemberRestart() {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(newConfig());
    Member oldMember = instance.getCluster().getLocalMember();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(newClientConfig());
    final CountDownLatch memberAdded = new CountDownLatch(1);
    final CountDownLatch memberRemoved = new CountDownLatch(1);
    final AtomicReference<Member> addedMemberReference = new AtomicReference<Member>();
    final AtomicReference<Member> removedMemberReference = new AtomicReference<Member>();
    client.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            addedMemberReference.set(membershipEvent.getMember());
            memberAdded.countDown();
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            removedMemberReference.set(membershipEvent.getMember());
            memberRemoved.countDown();
        }
    });
    instance.shutdown();
    // Allow same addresses to be used to test hot restart correctly
    hazelcastFactory.cleanup();
    instance = hazelcastFactory.newHazelcastInstance(newConfig());
    Member newMember = instance.getCluster().getLocalMember();
    assertOpenEventually(memberRemoved);
    assertEquals(oldMember, removedMemberReference.get());
    assertOpenEventually(memberAdded);
    assertEquals(newMember, addedMemberReference.get());
    Set<Member> members = client.getCluster().getMembers();
    assertContains(members, newMember);
    assertEquals(1, members.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipEvent(com.hazelcast.cluster.MembershipEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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)

Example 13 with MembershipEvent

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

the class AdvancedNetworkClientIntegrationTest method testClientMembershipEvent.

@Test
public void testClientMembershipEvent() {
    client = HazelcastClient.newHazelcastClient(getClientConfig());
    AtomicReference<Member> memberAdded = new AtomicReference<>();
    AtomicReference<Member> memberRemoved = new AtomicReference<>();
    Address removedMemberAddress = instances[2].getCluster().getLocalMember().getAddressMap().get(CLIENT);
    client.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            memberAdded.set(membershipEvent.getMember());
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemoved.set(membershipEvent.getMember());
        }
    });
    instances[2].shutdown();
    assertClusterSizeEventually(2, instances[0]);
    assertTrueEventually(() -> assertNotNull(memberRemoved.get()));
    assertEquals(memberRemoved.get().getAddress(), removedMemberAddress);
    instances[2] = Hazelcast.newHazelcastInstance(getConfig());
    assertClusterSizeEventually(3, instances);
    assertTrueEventually(() -> assertNotNull(memberAdded.get()));
    assertEquals(memberAdded.get().getAddress(), instances[2].getCluster().getLocalMember().getAddressMap().get(CLIENT));
}
Also used : Address(com.hazelcast.cluster.Address) MembershipEvent(com.hazelcast.cluster.MembershipEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 14 with MembershipEvent

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

the class MembershipManager method sendMembershipEventNotifications.

private void sendMembershipEventNotifications(MemberImpl member, Set<Member> members, final boolean added) {
    int eventType = added ? MembershipEvent.MEMBER_ADDED : MembershipEvent.MEMBER_REMOVED;
    node.getNodeExtension().getAuditlogService().eventBuilder(added ? AuditlogTypeIds.CLUSTER_MEMBER_ADDED : AuditlogTypeIds.CLUSTER_MEMBER_REMOVED).message("Membership changed").addParameter("memberAddress", member.getAddress()).log();
    MembershipEvent membershipEvent = new MembershipEvent(clusterService, member, eventType, members);
    Collection<MembershipAwareService> membershipAwareServices = nodeEngine.getServices(MembershipAwareService.class);
    if (membershipAwareServices != null && !membershipAwareServices.isEmpty()) {
        final MembershipServiceEvent event = new MembershipServiceEvent(membershipEvent);
        for (final MembershipAwareService service : membershipAwareServices) {
            nodeEngine.getExecutionService().execute(MEMBERSHIP_EVENT_EXECUTOR_NAME, () -> {
                if (added) {
                    service.memberAdded(event);
                } else {
                    service.memberRemoved(event);
                }
            });
        }
    }
    EventService eventService = nodeEngine.getEventService();
    Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME);
    for (EventRegistration reg : registrations) {
        eventService.publishEvent(SERVICE_NAME, reg, membershipEvent, reg.getId().hashCode());
    }
}
Also used : EventRegistration(com.hazelcast.spi.impl.eventservice.EventRegistration) MembershipServiceEvent(com.hazelcast.internal.services.MembershipServiceEvent) MembershipEvent(com.hazelcast.cluster.MembershipEvent) MembershipAwareService(com.hazelcast.internal.services.MembershipAwareService) EventService(com.hazelcast.spi.impl.eventservice.EventService)

Example 15 with MembershipEvent

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

the class ClientTxnTest method testTxnRollback.

@Test
public void testTxnRollback() throws Exception {
    final String queueName = randomString();
    final TransactionContext context = client.newTransactionContext();
    CountDownLatch txnRollbackLatch = new CountDownLatch(1);
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    try {
        context.beginTransaction();
        assertNotNull(context.getTxnId());
        final TransactionalQueue queue = context.getQueue(queueName);
        queue.offer(randomString());
        server.shutdown();
        context.commitTransaction();
        fail("commit should throw exception!!!");
    } catch (TransactionException e) {
        context.rollbackTransaction();
        txnRollbackLatch.countDown();
    }
    assertOpenEventually(txnRollbackLatch);
    assertOpenEventually(memberRemovedLatch);
    final IQueue<Object> q = client.getQueue(queueName);
    assertNull(q.poll());
    assertEquals(0, q.size());
}
Also used : TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.cluster.MembershipEvent) MembershipAdapter(com.hazelcast.cluster.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MembershipEvent (com.hazelcast.cluster.MembershipEvent)15 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Test (org.junit.Test)10 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Member (com.hazelcast.cluster.Member)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 MembershipListener (com.hazelcast.cluster.MembershipListener)6 MembershipAdapter (com.hazelcast.cluster.MembershipAdapter)5 ClientConfig (com.hazelcast.client.config.ClientConfig)3 InitialMembershipEvent (com.hazelcast.cluster.InitialMembershipEvent)3 Address (com.hazelcast.cluster.Address)2 Config (com.hazelcast.config.Config)2 AssertTask (com.hazelcast.test.AssertTask)2 TransactionContext (com.hazelcast.transaction.TransactionContext)2 TransactionException (com.hazelcast.transaction.TransactionException)2 TransactionalQueue (com.hazelcast.transaction.TransactionalQueue)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)1 TcpClientConnection (com.hazelcast.client.impl.connection.tcp.TcpClientConnection)1