Search in sources :

Example 1 with ClusterMetadata

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

the class DefaultClusterServiceTest method testClusterService.

@Test
public void testClusterService() throws Exception {
    TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
    ClusterMetadata clusterMetadata = buildClusterMetadata(1, 2, 3);
    Node localNode1 = buildNode(1, Node.Type.CORE);
    ManagedClusterService clusterService1 = new DefaultClusterService(localNode1, new TestClusterMetadataService(clusterMetadata), messagingServiceFactory.newMessagingService(localNode1.endpoint()).start().join());
    Node localNode2 = buildNode(2, Node.Type.CORE);
    ManagedClusterService clusterService2 = new DefaultClusterService(localNode2, new TestClusterMetadataService(clusterMetadata), messagingServiceFactory.newMessagingService(localNode2.endpoint()).start().join());
    Node localNode3 = buildNode(3, Node.Type.CORE);
    ManagedClusterService clusterService3 = new DefaultClusterService(localNode3, new TestClusterMetadataService(clusterMetadata), messagingServiceFactory.newMessagingService(localNode3.endpoint()).start().join());
    assertNull(clusterService1.getNode(NodeId.from("1")));
    assertNull(clusterService1.getNode(NodeId.from("2")));
    assertNull(clusterService1.getNode(NodeId.from("3")));
    CompletableFuture<ClusterService>[] futures = new CompletableFuture[3];
    futures[0] = clusterService1.start();
    futures[1] = clusterService2.start();
    futures[2] = clusterService3.start();
    CompletableFuture.allOf(futures).join();
    Thread.sleep(1000);
    assertEquals(3, clusterService1.getNodes().size());
    assertEquals(3, clusterService2.getNodes().size());
    assertEquals(3, clusterService3.getNodes().size());
    assertEquals(Node.Type.CORE, clusterService1.getLocalNode().type());
    assertEquals(Node.Type.CORE, clusterService1.getNode(NodeId.from("1")).type());
    assertEquals(Node.Type.CORE, clusterService1.getNode(NodeId.from("2")).type());
    assertEquals(Node.Type.CORE, clusterService1.getNode(NodeId.from("3")).type());
    assertEquals(State.ACTIVE, clusterService1.getLocalNode().getState());
    assertEquals(State.ACTIVE, clusterService1.getNode(NodeId.from("1")).getState());
    assertEquals(State.ACTIVE, clusterService1.getNode(NodeId.from("2")).getState());
    assertEquals(State.ACTIVE, clusterService1.getNode(NodeId.from("3")).getState());
    Node dataNode = buildNode(4, Node.Type.DATA);
    ManagedClusterService dataClusterService = new DefaultClusterService(dataNode, new TestClusterMetadataService(clusterMetadata), messagingServiceFactory.newMessagingService(dataNode.endpoint()).start().join());
    assertEquals(State.INACTIVE, dataClusterService.getLocalNode().getState());
    assertNull(dataClusterService.getNode(NodeId.from("1")));
    assertNull(dataClusterService.getNode(NodeId.from("2")));
    assertNull(dataClusterService.getNode(NodeId.from("3")));
    assertNull(dataClusterService.getNode(NodeId.from("4")));
    assertNull(dataClusterService.getNode(NodeId.from("5")));
    dataClusterService.start().join();
    Thread.sleep(1000);
    assertEquals(4, clusterService1.getNodes().size());
    assertEquals(4, clusterService2.getNodes().size());
    assertEquals(4, clusterService3.getNodes().size());
    assertEquals(4, dataClusterService.getNodes().size());
    Node clientNode = buildNode(5, Node.Type.CLIENT);
    ManagedClusterService clientClusterService = new DefaultClusterService(clientNode, new TestClusterMetadataService(clusterMetadata), messagingServiceFactory.newMessagingService(clientNode.endpoint()).start().join());
    assertEquals(State.INACTIVE, clientClusterService.getLocalNode().getState());
    assertNull(clientClusterService.getNode(NodeId.from("1")));
    assertNull(clientClusterService.getNode(NodeId.from("2")));
    assertNull(clientClusterService.getNode(NodeId.from("3")));
    assertNull(clientClusterService.getNode(NodeId.from("4")));
    assertNull(clientClusterService.getNode(NodeId.from("5")));
    clientClusterService.start().join();
    Thread.sleep(1000);
    assertEquals(5, clusterService1.getNodes().size());
    assertEquals(5, clusterService2.getNodes().size());
    assertEquals(5, clusterService3.getNodes().size());
    assertEquals(5, dataClusterService.getNodes().size());
    assertEquals(5, clientClusterService.getNodes().size());
    assertEquals(Node.Type.CLIENT, clientClusterService.getLocalNode().type());
    assertEquals(Node.Type.CORE, clientClusterService.getNode(NodeId.from("1")).type());
    assertEquals(Node.Type.CORE, clientClusterService.getNode(NodeId.from("2")).type());
    assertEquals(Node.Type.CORE, clientClusterService.getNode(NodeId.from("3")).type());
    assertEquals(Node.Type.DATA, clientClusterService.getNode(NodeId.from("4")).type());
    assertEquals(Node.Type.CLIENT, clientClusterService.getNode(NodeId.from("5")).type());
    assertEquals(State.ACTIVE, clientClusterService.getLocalNode().getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("1")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("2")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("3")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("4")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("5")).getState());
    Thread.sleep(2500);
    clusterService1.stop().join();
    Thread.sleep(2500);
    assertEquals(5, clusterService2.getNodes().size());
    assertEquals(Node.Type.CORE, clusterService2.getNode(NodeId.from("1")).type());
    assertEquals(State.INACTIVE, clusterService2.getNode(NodeId.from("1")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("2")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("3")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("4")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("5")).getState());
    assertEquals(State.INACTIVE, clientClusterService.getNode(NodeId.from("1")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("2")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("3")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("4")).getState());
    assertEquals(State.ACTIVE, clientClusterService.getNode(NodeId.from("5")).getState());
    dataClusterService.stop().join();
    Thread.sleep(2500);
    assertEquals(4, clusterService2.getNodes().size());
    assertEquals(State.INACTIVE, clusterService2.getNode(NodeId.from("1")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("2")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("3")).getState());
    assertNull(clusterService2.getNode(NodeId.from("4")));
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("5")).getState());
    clientClusterService.stop().join();
    Thread.sleep(2500);
    assertEquals(3, clusterService2.getNodes().size());
    assertEquals(State.INACTIVE, clusterService2.getNode(NodeId.from("1")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("2")).getState());
    assertEquals(State.ACTIVE, clusterService2.getNode(NodeId.from("3")).getState());
    assertNull(clusterService2.getNode(NodeId.from("4")));
    assertNull(clusterService2.getNode(NodeId.from("5")));
}
Also used : ClusterMetadata(io.atomix.cluster.ClusterMetadata) CompletableFuture(java.util.concurrent.CompletableFuture) Node(io.atomix.cluster.Node) ManagedClusterService(io.atomix.cluster.ManagedClusterService) TestMessagingServiceFactory(io.atomix.cluster.messaging.impl.TestMessagingServiceFactory) Test(org.junit.Test)

Example 2 with ClusterMetadata

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

the class DefaultClusterMetadataServiceTest method testSingleNodeBootstrap.

@Test
public void testSingleNodeBootstrap() throws Exception {
    TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
    ClusterMetadata clusterMetadata = buildClusterMetadata(1);
    Node localNode1 = buildNode(1, Node.Type.CORE);
    ManagedClusterMetadataService metadataService1 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode1.endpoint()).start().join());
    metadataService1.start().join();
    assertEquals(1, metadataService1.getMetadata().bootstrapNodes().size());
    Node localNode2 = buildNode(2, Node.Type.CORE);
    ManagedClusterMetadataService metadataService2 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode2.endpoint()).start().join());
    metadataService2.start().join();
    metadataService2.addNode(localNode2);
    assertEquals(2, metadataService2.getMetadata().bootstrapNodes().size());
}
Also used : ClusterMetadata(io.atomix.cluster.ClusterMetadata) ManagedClusterMetadataService(io.atomix.cluster.ManagedClusterMetadataService) Node(io.atomix.cluster.Node) TestMessagingServiceFactory(io.atomix.cluster.messaging.impl.TestMessagingServiceFactory) Test(org.junit.Test)

Example 3 with ClusterMetadata

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

the class DefaultClusterMetadataServiceTest method testClusterMetadataService.

@Test
public void testClusterMetadataService() throws Exception {
    TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
    ClusterMetadata clusterMetadata = buildClusterMetadata(1, 2, 3);
    Node localNode1 = buildNode(1, Node.Type.CORE);
    ManagedClusterMetadataService metadataService1 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode1.endpoint()).start().join());
    Node localNode2 = buildNode(2, Node.Type.CORE);
    ManagedClusterMetadataService metadataService2 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode2.endpoint()).start().join());
    Node localNode3 = buildNode(3, Node.Type.CORE);
    ManagedClusterMetadataService metadataService3 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode3.endpoint()).start().join());
    List<CompletableFuture<ClusterMetadataService>> futures = new ArrayList<>();
    futures.add(metadataService1.start());
    futures.add(metadataService2.start());
    futures.add(metadataService3.start());
    Futures.allOf(futures).join();
    assertEquals(3, metadataService1.getMetadata().bootstrapNodes().size());
    assertEquals(3, metadataService2.getMetadata().bootstrapNodes().size());
    assertEquals(3, metadataService3.getMetadata().bootstrapNodes().size());
    Node localNode4 = buildNode(4, Node.Type.CORE);
    ManagedClusterMetadataService metadataService4 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode4.endpoint()).start().join());
    metadataService4.start().join();
    assertEquals(3, metadataService4.getMetadata().bootstrapNodes().size());
    TestClusterMetadataEventListener localEventListener = new TestClusterMetadataEventListener();
    metadataService4.addListener(localEventListener);
    TestClusterMetadataEventListener remoteEventListener1 = new TestClusterMetadataEventListener();
    metadataService1.addListener(remoteEventListener1);
    TestClusterMetadataEventListener remoteEventListener2 = new TestClusterMetadataEventListener();
    metadataService2.addListener(remoteEventListener2);
    TestClusterMetadataEventListener remoteEventListener3 = new TestClusterMetadataEventListener();
    metadataService3.addListener(remoteEventListener3);
    metadataService4.addNode(localNode4);
    assertEquals(4, metadataService4.getMetadata().bootstrapNodes().size());
    assertEquals(4, localEventListener.event().subject().bootstrapNodes().size());
    assertEquals(4, remoteEventListener1.event().subject().bootstrapNodes().size());
    assertEquals(4, metadataService1.getMetadata().bootstrapNodes().size());
    assertEquals(4, remoteEventListener2.event().subject().bootstrapNodes().size());
    assertEquals(4, metadataService2.getMetadata().bootstrapNodes().size());
    assertEquals(4, remoteEventListener3.event().subject().bootstrapNodes().size());
    assertEquals(4, metadataService3.getMetadata().bootstrapNodes().size());
    Node localNode5 = buildNode(5, Node.Type.CORE);
    ManagedClusterMetadataService metadataService5 = new DefaultClusterMetadataService(clusterMetadata, messagingServiceFactory.newMessagingService(localNode5.endpoint()).start().join());
    metadataService5.start().join();
    assertEquals(4, metadataService5.getMetadata().bootstrapNodes().size());
}
Also used : ClusterMetadata(io.atomix.cluster.ClusterMetadata) ManagedClusterMetadataService(io.atomix.cluster.ManagedClusterMetadataService) CompletableFuture(java.util.concurrent.CompletableFuture) Node(io.atomix.cluster.Node) ArrayList(java.util.ArrayList) TestMessagingServiceFactory(io.atomix.cluster.messaging.impl.TestMessagingServiceFactory) Test(org.junit.Test)

Example 4 with ClusterMetadata

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

the class DefaultClusterEventingServiceTest method testClusterEventService.

@Test
public void testClusterEventService() throws Exception {
    TestMessagingServiceFactory factory = new TestMessagingServiceFactory();
    ClusterMetadata clusterMetadata = buildClusterMetadata(1, 1, 2, 3);
    Node localNode1 = buildNode(1, Node.Type.CORE);
    MessagingService messagingService1 = factory.newMessagingService(localNode1.endpoint()).start().join();
    ClusterService clusterService1 = new DefaultClusterService(localNode1, new TestClusterMetadataService(clusterMetadata), messagingService1).start().join();
    ClusterEventingService eventService1 = new DefaultClusterEventingService(clusterService1, messagingService1).start().join();
    Node localNode2 = buildNode(2, Node.Type.CORE);
    MessagingService messagingService2 = factory.newMessagingService(localNode2.endpoint()).start().join();
    ClusterService clusterService2 = new DefaultClusterService(localNode2, new TestClusterMetadataService(clusterMetadata), messagingService2).start().join();
    ClusterEventingService eventService2 = new DefaultClusterEventingService(clusterService2, messagingService2).start().join();
    Node localNode3 = buildNode(3, Node.Type.CORE);
    MessagingService messagingService3 = factory.newMessagingService(localNode3.endpoint()).start().join();
    ClusterService clusterService3 = new DefaultClusterService(localNode3, new TestClusterMetadataService(clusterMetadata), messagingService3).start().join();
    ClusterEventingService eventService3 = new DefaultClusterEventingService(clusterService3, messagingService3).start().join();
    Thread.sleep(100);
    Set<Integer> events = new CopyOnWriteArraySet<>();
    eventService1.<String>subscribe("test1", SERIALIZER::decode, message -> {
        assertEquals(message, "Hello world!");
        events.add(1);
    }, MoreExecutors.directExecutor()).join();
    eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
        assertEquals(message, "Hello world!");
        events.add(2);
    }, MoreExecutors.directExecutor()).join();
    eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
        assertEquals(message, "Hello world!");
        events.add(3);
    }, MoreExecutors.directExecutor()).join();
    eventService3.broadcast("test1", "Hello world!", SERIALIZER::encode);
    Thread.sleep(100);
    assertEquals(3, events.size());
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(3));
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(1));
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(2));
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(3));
    events.clear();
    eventService1.<String, String>subscribe("test2", SERIALIZER::decode, message -> {
        events.add(1);
        return message;
    }, SERIALIZER::encode, MoreExecutors.directExecutor()).join();
    eventService2.<String, String>subscribe("test2", SERIALIZER::decode, message -> {
        events.add(2);
        return message;
    }, SERIALIZER::encode, MoreExecutors.directExecutor()).join();
    assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
    assertEquals(1, events.size());
    assertTrue(events.contains(1));
    events.clear();
    assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
    assertEquals(1, events.size());
    assertTrue(events.contains(2));
    events.clear();
    assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
    assertEquals(1, events.size());
    assertTrue(events.contains(1));
}
Also used : ClusterMetadata(io.atomix.cluster.ClusterMetadata) Node(io.atomix.cluster.Node) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ClusterMetadata(io.atomix.cluster.ClusterMetadata) ClusterEventingService(io.atomix.cluster.messaging.ClusterEventingService) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Endpoint(io.atomix.messaging.Endpoint) Test(org.junit.Test) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) List(java.util.List) MessagingService(io.atomix.messaging.MessagingService) KryoNamespaces(io.atomix.utils.serializer.KryoNamespaces) ClusterService(io.atomix.cluster.ClusterService) Serializer(io.atomix.utils.serializer.Serializer) TestClusterMetadataService(io.atomix.cluster.impl.TestClusterMetadataService) Assert.assertEquals(org.junit.Assert.assertEquals) Node(io.atomix.cluster.Node) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ClusterEventingService(io.atomix.cluster.messaging.ClusterEventingService) MessagingService(io.atomix.messaging.MessagingService) TestClusterMetadataService(io.atomix.cluster.impl.TestClusterMetadataService) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) ClusterService(io.atomix.cluster.ClusterService) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) Test(org.junit.Test)

Aggregations

ClusterMetadata (io.atomix.cluster.ClusterMetadata)4 Node (io.atomix.cluster.Node)4 Test (org.junit.Test)4 TestMessagingServiceFactory (io.atomix.cluster.messaging.impl.TestMessagingServiceFactory)3 ManagedClusterMetadataService (io.atomix.cluster.ManagedClusterMetadataService)2 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 ClusterService (io.atomix.cluster.ClusterService)1 ManagedClusterService (io.atomix.cluster.ManagedClusterService)1 DefaultClusterService (io.atomix.cluster.impl.DefaultClusterService)1 TestClusterMetadataService (io.atomix.cluster.impl.TestClusterMetadataService)1 ClusterEventingService (io.atomix.cluster.messaging.ClusterEventingService)1 Endpoint (io.atomix.messaging.Endpoint)1 MessagingService (io.atomix.messaging.MessagingService)1 KryoNamespaces (io.atomix.utils.serializer.KryoNamespaces)1 Serializer (io.atomix.utils.serializer.Serializer)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 List (java.util.List)1