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