Search in sources :

Example 16 with MembershipEvent

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

the class ClientMembershipListener method detectMembershipEvents.

private List<MembershipEvent> detectMembershipEvents(Map<String, Member> prevMembers) {
    List<MembershipEvent> events = new LinkedList<MembershipEvent>();
    Set<Member> eventMembers = unmodifiableSet(members);
    List<Member> newMembers = new LinkedList<Member>();
    for (Member member : members) {
        Member former = prevMembers.remove(member.getUuid());
        if (former == null) {
            newMembers.add(member);
        }
    }
    // removal events should be added before added events
    for (Member member : prevMembers.values()) {
        events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, eventMembers));
        Address address = member.getAddress();
        if (clusterService.getMember(address) == null) {
            Connection connection = connectionManager.getConnection(address);
            if (connection != null) {
                connection.close(null, newTargetDisconnectedExceptionCausedByMemberLeftEvent(connection));
            }
        }
    }
    for (Member member : newMembers) {
        events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_ADDED, eventMembers));
    }
    return events;
}
Also used : Address(com.hazelcast.nio.Address) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) Connection(com.hazelcast.nio.Connection) AbstractMember(com.hazelcast.instance.AbstractMember) Member(com.hazelcast.core.Member) LinkedList(java.util.LinkedList)

Example 17 with MembershipEvent

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

the class ClientReconnectTest method testReconnectToNewInstanceAtSameAddress.

@Test
public void testReconnectToNewInstanceAtSameAddress() throws InterruptedException {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    Address localAddress = instance.getCluster().getLocalMember().getAddress();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    instance.shutdown();
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance(localAddress);
    assertOpenEventually(memberRemovedLatch);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, client.getCluster().getMembers().size());
            Iterator<Member> iterator = client.getCluster().getMembers().iterator();
            Member member = iterator.next();
            assertEquals(instance2.getCluster().getLocalMember(), member);
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.nio.Address) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter) Iterator(java.util.Iterator) AssertTask(com.hazelcast.test.AssertTask) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 18 with MembershipEvent

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

the class ClientMembershipListener method handle.

@Override
public void handle(Collection<Member> initialMembers) {
    Map<String, Member> prevMembers = Collections.emptyMap();
    if (!members.isEmpty()) {
        prevMembers = new HashMap<String, Member>(members.size());
        for (Member member : members) {
            prevMembers.put(member.getUuid(), member);
        }
        members.clear();
    }
    for (Member initialMember : initialMembers) {
        members.add(initialMember);
    }
    if (prevMembers.isEmpty()) {
        //this means this is the first time client connected to server
        logger.info(membersString());
        clusterService.handleInitialMembershipEvent(new InitialMembershipEvent(client.getCluster(), unmodifiableSet(members)));
        initialListFetchedLatch.countDown();
        return;
    }
    List<MembershipEvent> events = detectMembershipEvents(prevMembers);
    logger.info(membersString());
    fireMembershipEvent(events);
    initialListFetchedLatch.countDown();
}
Also used : InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) AbstractMember(com.hazelcast.instance.AbstractMember) Member(com.hazelcast.core.Member) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent)

Example 19 with MembershipEvent

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

the class ClientMembershipListener method memberAdded.

private void memberAdded(Member member) {
    members.add(member);
    logger.info(membersString());
    MembershipEvent event = new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_ADDED, unmodifiableSet(members));
    clusterService.handleMembershipEvent(event);
}
Also used : InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent)

Example 20 with MembershipEvent

use of com.hazelcast.core.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 (Exception 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.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

MembershipEvent (com.hazelcast.core.MembershipEvent)22 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 InitialMembershipEvent (com.hazelcast.core.InitialMembershipEvent)9 MembershipAdapter (com.hazelcast.core.MembershipAdapter)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 Member (com.hazelcast.core.Member)6 ParallelTest (com.hazelcast.test.annotation.ParallelTest)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Config (com.hazelcast.config.Config)5 ListenerConfig (com.hazelcast.config.ListenerConfig)5 JoinConfig (com.hazelcast.config.JoinConfig)4 NetworkConfig (com.hazelcast.config.NetworkConfig)4 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)4 AssertTask (com.hazelcast.test.AssertTask)4 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)3 MembershipListener (com.hazelcast.core.MembershipListener)3 Node (com.hazelcast.instance.Node)3 FirewallingTcpIpConnectionManager (com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager)3 TransactionalQueue (com.hazelcast.core.TransactionalQueue)2