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());
}
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());
}
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());
}
});
}
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());
}
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;
}
Aggregations