Search in sources :

Example 1 with ClusterMembershipEvent

use of io.atomix.cluster.ClusterMembershipEvent 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 ClusterMembershipEvent

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

the class AtomixTest method testClientProperties.

/**
 * Tests a client properties.
 */
@Test
public void testClientProperties() throws Exception {
    List<CompletableFuture<Atomix>> futures = new ArrayList<>();
    futures.add(startAtomix(1, Arrays.asList(1, 2, 3), ConsensusProfile.builder().withMembers("1", "2", "3").withDataPath(new File(new File(DATA_DIR, "client-properties"), "1")).build()));
    futures.add(startAtomix(2, Arrays.asList(1, 2, 3), ConsensusProfile.builder().withMembers("1", "2", "3").withDataPath(new File(new File(DATA_DIR, "client-properties"), "2")).build()));
    futures.add(startAtomix(3, Arrays.asList(1, 2, 3), ConsensusProfile.builder().withMembers("1", "2", "3").withDataPath(new File(new File(DATA_DIR, "client-properties"), "3")).build()));
    Futures.allOf(futures).get(30, TimeUnit.SECONDS);
    TestClusterMembershipEventListener dataListener = new TestClusterMembershipEventListener();
    instances.get(0).getMembershipService().addListener(dataListener);
    Properties properties = new Properties();
    properties.setProperty("a-key", "a-value");
    Atomix client1 = startAtomix(4, Arrays.asList(1, 2, 3), properties, Profile.client()).get(30, TimeUnit.SECONDS);
    assertEquals(1, client1.getPartitionService().getPartitionGroups().size());
    // client1 added to data node
    ClusterMembershipEvent event1 = dataListener.event();
    assertEquals(ClusterMembershipEvent.Type.MEMBER_ADDED, event1.type());
    Member member = event1.subject();
    assertNotNull(member.properties());
    assertEquals(1, member.properties().size());
    assertEquals("a-value", member.properties().get("a-key"));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ClusterMembershipEvent(io.atomix.cluster.ClusterMembershipEvent) ArrayList(java.util.ArrayList) Properties(java.util.Properties) File(java.io.File) Member(io.atomix.cluster.Member) Test(org.junit.Test)

Example 3 with ClusterMembershipEvent

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

the class AtomixTest method testClientJoinLeave.

private void testClientJoinLeave(Profile... profiles) throws Exception {
    List<CompletableFuture<Atomix>> futures = new ArrayList<>();
    futures.add(startAtomix(1, Arrays.asList(1, 2, 3), profiles[0]));
    futures.add(startAtomix(2, Arrays.asList(1, 2, 3), profiles[1]));
    futures.add(startAtomix(3, Arrays.asList(1, 2, 3), profiles[2]));
    Futures.allOf(futures).get(30, TimeUnit.SECONDS);
    TestClusterMembershipEventListener dataListener = new TestClusterMembershipEventListener();
    instances.get(0).getMembershipService().addListener(dataListener);
    Atomix client1 = startAtomix(4, Arrays.asList(1, 2, 3), Profile.client()).get(30, TimeUnit.SECONDS);
    assertEquals(1, client1.getPartitionService().getPartitionGroups().size());
    // client1 added to data node
    ClusterMembershipEvent event1 = dataListener.event();
    assertEquals(ClusterMembershipEvent.Type.MEMBER_ADDED, event1.type());
    Thread.sleep(1000);
    TestClusterMembershipEventListener clientListener = new TestClusterMembershipEventListener();
    client1.getMembershipService().addListener(clientListener);
    Atomix client2 = startAtomix(5, Arrays.asList(1, 2, 3), Profile.client()).get(30, TimeUnit.SECONDS);
    assertEquals(1, client2.getPartitionService().getPartitionGroups().size());
    // client2 added to data node
    assertEquals(ClusterMembershipEvent.Type.MEMBER_ADDED, dataListener.event().type());
    // client2 added to client node
    assertEquals(ClusterMembershipEvent.Type.MEMBER_ADDED, clientListener.event().type());
    client2.stop().get(30, TimeUnit.SECONDS);
    // client2 removed from data node
    assertEquals(ClusterMembershipEvent.Type.REACHABILITY_CHANGED, dataListener.event().type());
    assertEquals(ClusterMembershipEvent.Type.MEMBER_REMOVED, dataListener.event().type());
    // client2 removed from client node
    assertEquals(ClusterMembershipEvent.Type.REACHABILITY_CHANGED, clientListener.event().type());
    assertEquals(ClusterMembershipEvent.Type.MEMBER_REMOVED, clientListener.event().type());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ClusterMembershipEvent(io.atomix.cluster.ClusterMembershipEvent) ArrayList(java.util.ArrayList)

Aggregations

ClusterMembershipEvent (io.atomix.cluster.ClusterMembershipEvent)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Member (io.atomix.cluster.Member)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 BootstrapService (io.atomix.cluster.BootstrapService)1 ManagedClusterMembershipService (io.atomix.cluster.ManagedClusterMembershipService)1 Node (io.atomix.cluster.Node)1 TestBootstrapService (io.atomix.cluster.TestBootstrapService)1 BootstrapDiscoveryProvider (io.atomix.cluster.discovery.BootstrapDiscoveryProvider)1 TestBroadcastServiceFactory (io.atomix.cluster.messaging.impl.TestBroadcastServiceFactory)1 TestMessagingServiceFactory (io.atomix.cluster.messaging.impl.TestMessagingServiceFactory)1 TestUnicastServiceFactory (io.atomix.cluster.messaging.impl.TestUnicastServiceFactory)1 HeartbeatMembershipProtocol (io.atomix.cluster.protocol.HeartbeatMembershipProtocol)1 HeartbeatMembershipProtocolConfig (io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig)1 File (java.io.File)1 Properties (java.util.Properties)1