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