Search in sources :

Example 1 with Member

use of io.atomix.cluster.Member in project atomix by atomix.

the class DefaultClusterMembershipServiceTest method testClusterService.

@Test
public void testClusterService() throws Exception {
    TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
    TestUnicastServiceFactory unicastServiceFactory = new TestUnicastServiceFactory();
    TestBroadcastServiceFactory broadcastServiceFactory = new TestBroadcastServiceFactory();
    Collection<Node> bootstrapLocations = buildBootstrapNodes(3);
    Member localMember1 = buildMember(1);
    BootstrapService bootstrapService1 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember1.address()).start().join(), unicastServiceFactory.newUnicastService(localMember1.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService1 = new DefaultClusterMembershipService(localMember1, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService1, localMember1, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService1, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
    Member localMember2 = buildMember(2);
    BootstrapService bootstrapService2 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember2.address()).start().join(), unicastServiceFactory.newUnicastService(localMember2.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService2 = new DefaultClusterMembershipService(localMember2, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService2, localMember2, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService2, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
    Member localMember3 = buildMember(3);
    BootstrapService bootstrapService3 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember3.address()).start().join(), unicastServiceFactory.newUnicastService(localMember3.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService3 = new DefaultClusterMembershipService(localMember3, Version.from("1.0.1"), new DefaultNodeDiscoveryService(bootstrapService3, localMember3, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService3, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
    assertNull(clusterService1.getMember(MemberId.from("1")));
    assertNull(clusterService1.getMember(MemberId.from("2")));
    assertNull(clusterService1.getMember(MemberId.from("3")));
    CompletableFuture.allOf(new CompletableFuture[] { clusterService1.start(), clusterService2.start(), clusterService3.start() }).join();
    Thread.sleep(5000);
    assertEquals(3, clusterService1.getMembers().size());
    assertEquals(3, clusterService2.getMembers().size());
    assertEquals(3, clusterService3.getMembers().size());
    assertTrue(clusterService1.getLocalMember().isActive());
    assertTrue(clusterService1.getMember(MemberId.from("1")).isActive());
    assertTrue(clusterService1.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService1.getMember(MemberId.from("3")).isActive());
    assertEquals("1.0.0", clusterService1.getMember("1").version().toString());
    assertEquals("1.0.0", clusterService1.getMember("2").version().toString());
    assertEquals("1.0.1", clusterService1.getMember("3").version().toString());
    Member anonymousMember = buildMember(4);
    BootstrapService ephemeralBootstrapService = new TestBootstrapService(messagingServiceFactory.newMessagingService(anonymousMember.address()).start().join(), unicastServiceFactory.newUnicastService(anonymousMember.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService ephemeralClusterService = new DefaultClusterMembershipService(anonymousMember, Version.from("1.1.0"), new DefaultNodeDiscoveryService(ephemeralBootstrapService, anonymousMember, new BootstrapDiscoveryProvider(bootstrapLocations)), ephemeralBootstrapService, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
    assertFalse(ephemeralClusterService.getLocalMember().isActive());
    assertNull(ephemeralClusterService.getMember(MemberId.from("1")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("2")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("3")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("4")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("5")));
    ephemeralClusterService.start().join();
    Thread.sleep(1000);
    assertEquals(4, clusterService1.getMembers().size());
    assertEquals(4, clusterService2.getMembers().size());
    assertEquals(4, clusterService3.getMembers().size());
    assertEquals(4, ephemeralClusterService.getMembers().size());
    assertEquals("1.0.0", clusterService1.getMember("1").version().toString());
    assertEquals("1.0.0", clusterService1.getMember("2").version().toString());
    assertEquals("1.0.1", clusterService1.getMember("3").version().toString());
    assertEquals("1.1.0", clusterService1.getMember("4").version().toString());
    clusterService1.stop().join();
    Thread.sleep(5000);
    assertEquals(3, clusterService2.getMembers().size());
    assertNull(clusterService2.getMember(MemberId.from("1")));
    assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("4")).isActive());
    ephemeralClusterService.stop().join();
    Thread.sleep(5000);
    assertEquals(2, clusterService2.getMembers().size());
    assertNull(clusterService2.getMember(MemberId.from("1")));
    assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
    assertNull(clusterService2.getMember(MemberId.from("4")));
    Thread.sleep(2500);
    assertEquals(2, clusterService2.getMembers().size());
    assertNull(clusterService2.getMember(MemberId.from("1")));
    assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
    assertNull(clusterService2.getMember(MemberId.from("4")));
    TestClusterMembershipEventListener eventListener = new TestClusterMembershipEventListener();
    clusterService2.addListener(eventListener);
    ClusterMembershipEvent event;
    clusterService3.getLocalMember().properties().put("foo", "bar");
    event = eventListener.nextEvent();
    assertEquals(ClusterMembershipEvent.Type.METADATA_CHANGED, event.type());
    assertEquals("bar", event.subject().properties().get("foo"));
    clusterService3.getLocalMember().properties().put("foo", "baz");
    event = eventListener.nextEvent();
    assertEquals(ClusterMembershipEvent.Type.METADATA_CHANGED, event.type());
    assertEquals("baz", event.subject().properties().get("foo"));
    CompletableFuture.allOf(new CompletableFuture[] { clusterService1.stop(), clusterService2.stop(), clusterService3.stop() }).join();
}
Also used : TestUnicastServiceFactory(io.atomix.cluster.messaging.impl.TestUnicastServiceFactory) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) Node(io.atomix.cluster.Node) ManagedClusterMembershipService(io.atomix.cluster.ManagedClusterMembershipService) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) TestMessagingServiceFactory(io.atomix.cluster.messaging.impl.TestMessagingServiceFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterMembershipEvent(io.atomix.cluster.ClusterMembershipEvent) TestBootstrapService(io.atomix.cluster.TestBootstrapService) TestBroadcastServiceFactory(io.atomix.cluster.messaging.impl.TestBroadcastServiceFactory) Member(io.atomix.cluster.Member) HeartbeatMembershipProtocol(io.atomix.cluster.protocol.HeartbeatMembershipProtocol) TestBootstrapService(io.atomix.cluster.TestBootstrapService) BootstrapService(io.atomix.cluster.BootstrapService) Test(org.junit.Test)

Example 2 with Member

use of io.atomix.cluster.Member in project atomix by atomix.

the class HeartbeatMembershipProtocol method sendHeartbeat.

/**
 * Sends a heartbeat to the given peer.
 */
private CompletableFuture<Void> sendHeartbeat(GossipMember member) {
    return bootstrapService.getMessagingService().sendAndReceive(member.address(), HEARTBEAT_MESSAGE, SERIALIZER.encode(localMember)).whenCompleteAsync((response, error) -> {
        if (error == null) {
            Collection<GossipMember> remoteMembers = SERIALIZER.decode(response);
            for (GossipMember remoteMember : remoteMembers) {
                if (!remoteMember.id().equals(localMember.id())) {
                    updateMember(remoteMember, remoteMember.id().equals(member.id()));
                }
            }
        } else {
            LOGGER.debug("{} - Sending heartbeat to {} failed", localMember.id(), member, error);
            if (member.isReachable()) {
                member.setReachable(false);
                post(new GroupMembershipEvent(GroupMembershipEvent.Type.REACHABILITY_CHANGED, member));
            }
            PhiAccrualFailureDetector failureDetector = failureDetectors.computeIfAbsent(member.id(), n -> new PhiAccrualFailureDetector());
            double phi = failureDetector.phi();
            if (phi >= config.getPhiFailureThreshold() || (phi == 0.0 && System.currentTimeMillis() - failureDetector.lastUpdated() > config.getFailureTimeout().toMillis())) {
                if (members.remove(member.id()) != null) {
                    failureDetectors.remove(member.id());
                    post(new GroupMembershipEvent(GroupMembershipEvent.Type.MEMBER_REMOVED, member));
                }
            }
        }
    }, heartbeatScheduler).exceptionally(e -> null).thenApply(v -> null);
}
Also used : Node(io.atomix.cluster.Node) ScheduledFuture(java.util.concurrent.ScheduledFuture) AbstractListenerManager(io.atomix.utils.event.AbstractListenerManager) Address(io.atomix.utils.net.Address) NodeDiscoveryEvent(io.atomix.cluster.discovery.NodeDiscoveryEvent) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) NodeDiscoveryService(io.atomix.cluster.discovery.NodeDiscoveryService) Threads.namedThreads(io.atomix.utils.concurrent.Threads.namedThreads) Lists(com.google.common.collect.Lists) Namespaces(io.atomix.utils.serializer.Namespaces) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MemberId(io.atomix.cluster.MemberId) Futures(io.atomix.utils.concurrent.Futures) ExecutorService(java.util.concurrent.ExecutorService) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Properties(java.util.Properties) Collection(java.util.Collection) NodeDiscoveryEventListener(io.atomix.cluster.discovery.NodeDiscoveryEventListener) Set(java.util.Set) Version(io.atomix.utils.Version) Maps(com.google.common.collect.Maps) Member(io.atomix.cluster.Member) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) PhiAccrualFailureDetector(io.atomix.cluster.impl.PhiAccrualFailureDetector) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Stream(java.util.stream.Stream) Namespace(io.atomix.utils.serializer.Namespace) AddressSerializer(io.atomix.cluster.impl.AddressSerializer) Serializer(io.atomix.utils.serializer.Serializer) BootstrapService(io.atomix.cluster.BootstrapService) PhiAccrualFailureDetector(io.atomix.cluster.impl.PhiAccrualFailureDetector)

Example 3 with Member

use of io.atomix.cluster.Member in project atomix by atomix.

the class AtomicMapPerformanceTest method createClient.

/**
 * Creates an Atomix client.
 */
private Atomix createClient() {
    Member member = nextNode();
    Atomix atomix = Atomix.builder().withMemberId(member.id()).withAddress(member.address()).withMembershipProvider(BootstrapDiscoveryProvider.builder().withNodes((Collection) members).build()).withProfiles(Profile.client()).build();
    atomix.start().join();
    clients.add(atomix);
    return atomix;
}
Also used : Atomix(io.atomix.core.Atomix) Member(io.atomix.cluster.Member)

Example 4 with Member

use of io.atomix.cluster.Member in project atomix by atomix.

the class RaftPerformanceTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    Member member = nextNode();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        MessagingService messagingService = new NettyMessagingService("test", member.address(), new MessagingConfig()).start().join();
        protocol = new RaftClientMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(member.id());
    }
    RaftClient client = RaftClient.builder().withMemberId(member.id()).withPartitionId(PartitionId.from("test", 1)).withProtocol(protocol).withThreadModel(ThreadModel.SHARED_THREAD_POOL).build();
    client.connect(members.stream().map(Member::id).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) Member(io.atomix.cluster.Member) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) RaftClient(io.atomix.protocols.raft.RaftClient) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) ManagedMessagingService(io.atomix.cluster.messaging.ManagedMessagingService)

Example 5 with Member

use of io.atomix.cluster.Member in project atomix by atomix.

the class RaftPerformanceTest method nextNode.

/**
 * Returns the next unique member identifier.
 *
 * @return The next unique member identifier.
 */
private Member nextNode() {
    Address address = Address.from("localhost", ++port);
    Member member = Member.builder(MemberId.from(String.valueOf(++nextId))).withAddress(address).build();
    addressMap.put(member.id(), address);
    return member;
}
Also used : Address(io.atomix.utils.net.Address) Member(io.atomix.cluster.Member) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember)

Aggregations

Member (io.atomix.cluster.Member)11 CompletableFuture (java.util.concurrent.CompletableFuture)5 Address (io.atomix.utils.net.Address)4 BootstrapService (io.atomix.cluster.BootstrapService)3 MemberId (io.atomix.cluster.MemberId)3 Node (io.atomix.cluster.Node)3 MessagingService (io.atomix.cluster.messaging.MessagingService)3 Test (org.junit.Test)3 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 ClusterMembershipEvent (io.atomix.cluster.ClusterMembershipEvent)2 ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)2 ManagedClusterMembershipService (io.atomix.cluster.ManagedClusterMembershipService)2 TestBootstrapService (io.atomix.cluster.TestBootstrapService)2 BootstrapDiscoveryProvider (io.atomix.cluster.discovery.BootstrapDiscoveryProvider)2 ClusterEventService (io.atomix.cluster.messaging.ClusterEventService)2 ManagedClusterEventService (io.atomix.cluster.messaging.ManagedClusterEventService)2 HeartbeatMembershipProtocol (io.atomix.cluster.protocol.HeartbeatMembershipProtocol)2 HeartbeatMembershipProtocolConfig (io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig)2 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)2