Search in sources :

Example 1 with Cluster

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());
}
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 2 with Cluster

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();
}
Also used : AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) HazelcastCommandLine.getHazelcastClientInstanceImpl(com.hazelcast.client.console.HazelcastCommandLine.getHazelcastClientInstanceImpl) Cluster(com.hazelcast.cluster.Cluster) Member(com.hazelcast.cluster.Member) ClientClusterService(com.hazelcast.client.impl.spi.ClientClusterService) MCClusterMetadata(com.hazelcast.client.impl.management.MCClusterMetadata)

Example 3 with Cluster

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()));
    });
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) Cluster(com.hazelcast.cluster.Cluster) Util.idToString(com.hazelcast.jet.Util.idToString) ClientClusterService(com.hazelcast.client.impl.spi.ClientClusterService) MCClusterMetadata(com.hazelcast.client.impl.management.MCClusterMetadata) HelpCommand(picocli.CommandLine.HelpCommand) Command(picocli.CommandLine.Command)

Example 4 with Cluster

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;
    }
}
Also used : InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) Cluster(com.hazelcast.cluster.Cluster) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent) Nonnull(javax.annotation.Nonnull)

Example 5 with Cluster

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.cluster.Address) Cluster(com.hazelcast.cluster.Cluster) Member(com.hazelcast.cluster.Member) Test(org.junit.Test)

Aggregations

Cluster (com.hazelcast.cluster.Cluster)26 Member (com.hazelcast.cluster.Member)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 Test (org.junit.Test)12 QuickTest (com.hazelcast.test.annotation.QuickTest)11 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)10 ClientConfig (com.hazelcast.client.config.ClientConfig)5 Config (com.hazelcast.config.Config)5 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)4 MCClusterMetadata (com.hazelcast.client.impl.management.MCClusterMetadata)4 ClientClusterService (com.hazelcast.client.impl.spi.ClientClusterService)4 IPartition (com.hazelcast.internal.partition.IPartition)3 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)3 Partition (com.hazelcast.partition.Partition)3 PartitionService (com.hazelcast.partition.PartitionService)3 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 ExecutionException (java.util.concurrent.ExecutionException)3 HazelcastCommandLine.getHazelcastClientInstanceImpl (com.hazelcast.client.console.HazelcastCommandLine.getHazelcastClientInstanceImpl)2 RandomLB (com.hazelcast.client.util.RandomLB)2 RoundRobinLB (com.hazelcast.client.util.RoundRobinLB)2