use of com.hazelcast.cluster.Cluster 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.Cluster in project hazelcast by hazelcast.
the class ClientConsoleApp method startPrompt.
private static String startPrompt(HazelcastInstance hz) {
HazelcastClientInstanceImpl hazelcastClientImpl = getHazelcastClientInstanceImpl(hz);
ClientClusterService clientClusterService = hazelcastClientImpl.getClientClusterService();
MCClusterMetadata clusterMetadata = FutureUtil.getValue(getClusterMetadata(hazelcastClientImpl, clientClusterService.getMasterMember()));
Cluster cluster = hazelcastClientImpl.getCluster();
Set<Member> members = cluster.getMembers();
String versionString = "Hazelcast " + clusterMetadata.getMemberVersion();
return new StringBuilder().append("Hazelcast Console Application has started.\n").append("Connected to ").append(versionString).append(" at ").append(members.iterator().next().getAddress().toString()).append(" (+").append(members.size() - 1).append(" more)\n").append("Type 'help' for instructions").toString();
}
use of com.hazelcast.cluster.Cluster in project hazelcast by hazelcast.
the class HazelcastCommandLine method cluster.
@Command(description = "Shows current cluster state and information about members")
public void cluster(@Mixin(name = "verbosity") Verbosity verbosity, @Mixin(name = "targets") TargetsMixin targets) {
runWithHazelcast(targets, verbosity, false, hz -> {
HazelcastClientInstanceImpl hazelcastClientImpl = getHazelcastClientInstanceImpl(hz);
ClientClusterService clientClusterService = hazelcastClientImpl.getClientClusterService();
MCClusterMetadata clusterMetadata = FutureUtil.getValue(getClusterMetadata(hazelcastClientImpl, clientClusterService.getMasterMember()));
Cluster cluster = hazelcastClientImpl.getCluster();
println("State: " + clusterMetadata.getCurrentState());
println("Version: " + clusterMetadata.getMemberVersion());
println("Size: " + cluster.getMembers().size());
println("");
String format = "%-24s %-19s";
printf(format, "ADDRESS", "UUID");
cluster.getMembers().forEach(member -> printf(format, member.getAddress(), member.getUuid()));
});
}
use of com.hazelcast.cluster.Cluster in project hazelcast by hazelcast.
the class ClientClusterServiceImpl method addMembershipListener.
@Nonnull
@Override
public UUID addMembershipListener(@Nonnull MembershipListener listener) {
checkNotNull(listener, "Listener can't be null");
synchronized (clusterViewLock) {
UUID id = addMembershipListenerWithoutInit(listener);
if (listener instanceof InitialMembershipListener) {
Cluster cluster = client.getCluster();
Collection<Member> members = memberListSnapshot.get().members.values();
// it will be redirected to listeners when it arrives see #handleInitialMembershipEvent
if (!members.isEmpty()) {
InitialMembershipEvent event = new InitialMembershipEvent(cluster, toUnmodifiableHasSet(members));
((InitialMembershipListener) listener).init(event);
}
}
return id;
}
}
use of com.hazelcast.cluster.Cluster in project hazelcast by hazelcast.
the class TestProcessorContextTest method test.
@Test
public void test() throws Exception {
Address a0 = new Address("1.2.3.4", 0);
TestProcessorContext c = new TestProcessorContext().setPartitionAssignment(ImmutableMap.of(a0, new int[] { 0, 3, 6 }));
HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
Cluster mockCluster = mock(Cluster.class);
when(mockHazelcastInstance.getCluster()).thenReturn(mockCluster);
Member mockMember = mock(Member.class);
when(mockCluster.getLocalMember()).thenReturn(mockMember);
when(mockMember.getAddress()).thenReturn(a0);
c.setHazelcastInstance(mockHazelcastInstance);
assertArrayEquals(new int[] { 0, 3, 6 }, c.memberPartitions());
c.setLocalParallelism(2);
c.setLocalProcessorIndex(0);
assertArrayEquals(new int[] { 0, 6 }, c.processorPartitions());
c.setLocalProcessorIndex(1);
assertArrayEquals(new int[] { 3 }, c.processorPartitions());
c.setLocalParallelism(3);
c.setLocalProcessorIndex(2);
assertArrayEquals(new int[] { 6 }, c.processorPartitions());
c.setLocalParallelism(4);
c.setLocalProcessorIndex(3);
assertArrayEquals(new int[] {}, c.processorPartitions());
}
Aggregations