Search in sources :

Example 1 with MembershipEvent

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

the class ClientTxnTest method testTxnRollbackOnServerCrash.

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

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    server.getLifecycleService().terminate();
    try {
        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)

Example 2 with MembershipEvent

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

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

the class MembershipListenerTest method whenMemberAdded_thenMemberAddedEvent.

@Test
public void whenMemberAdded_thenMemberAddedEvent() throws Exception {
    final HazelcastInstance server1 = hazelcastFactory.newHazelcastInstance();
    final MemberShipEventLogger listener = new MemberShipEventLogger();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    client.getCluster().addMembershipListener(listener);
    // start a second server and verify that the listener receives it.
    final HazelcastInstance server2 = hazelcastFactory.newHazelcastInstance();
    // verify that the listener receives member added event.
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertNotEquals("Expecting one or more events", 0, listener.events.size());
            MembershipEvent event = (MembershipEvent) listener.events.getLast();
            assertEquals("Last event should be member added", MembershipEvent.MEMBER_ADDED, event.getEventType());
            assertEquals(server2.getCluster().getLocalMember(), event.getMember());
            assertEquals(getMembers(server1, server2), event.getMembers());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent) MembershipEvent(com.hazelcast.cluster.MembershipEvent) AssertTask(com.hazelcast.test.AssertTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with MembershipEvent

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

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

the class ClientClusterServiceImpl method detectMembershipEvents.

private List<MembershipEvent> detectMembershipEvents(Collection<Member> prevMembers, Set<Member> currentMembers) {
    List<Member> newMembers = new LinkedList<>();
    Set<Member> deadMembers = new HashSet<>(prevMembers);
    for (Member member : currentMembers) {
        if (!deadMembers.remove(member)) {
            newMembers.add(member);
        }
    }
    List<MembershipEvent> events = new LinkedList<>();
    for (Member member : deadMembers) {
        events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, currentMembers));
        Connection connection = connectionManager.getConnection(member.getUuid());
        if (connection != null) {
            connection.close(null, new TargetDisconnectedException("The client has closed the connection to this member," + " after receiving a member left event from the cluster. " + connection));
        }
    }
    for (Member member : newMembers) {
        events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_ADDED, currentMembers));
    }
    if (events.size() != 0) {
        MemberListSnapshot snapshot = memberListSnapshot.get();
        if (snapshot.members.values().size() != 0) {
            logger.info(membersString(snapshot));
        }
    }
    return events;
}
Also used : InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent) MembershipEvent(com.hazelcast.cluster.MembershipEvent) Connection(com.hazelcast.internal.nio.Connection) TcpClientConnection(com.hazelcast.client.impl.connection.tcp.TcpClientConnection) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) Member(com.hazelcast.cluster.Member) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

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