Search in sources :

Example 1 with Node

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

the class RaftPartitionGroup method buildPartitions.

private Collection<PartitionMetadata> buildPartitions(ClusterService clusterService) {
    int partitionSize = this.partitionSize;
    if (partitionSize == 0) {
        partitionSize = clusterService.getNodes().size();
    }
    List<NodeId> sorted = new ArrayList<>(clusterService.getNodes().stream().filter(node -> node.type() == Node.Type.CORE).map(Node::id).collect(Collectors.toSet()));
    Collections.sort(sorted);
    int length = sorted.size();
    int count = Math.min(partitionSize, length);
    Set<PartitionMetadata> metadata = Sets.newHashSet();
    for (int i = 0; i < partitions.size(); i++) {
        PartitionId partitionId = sortedPartitionIds.get(i);
        Set<NodeId> set = new HashSet<>(count);
        for (int j = 0; j < count; j++) {
            set.add(sorted.get((i + j) % length));
        }
        metadata.add(new PartitionMetadata(partitionId, set));
    }
    return metadata;
}
Also used : Node(io.atomix.cluster.Node) NodeId(io.atomix.cluster.NodeId) RaftProtocol(io.atomix.protocols.raft.RaftProtocol) ClusterEvent(io.atomix.cluster.ClusterEvent) LoggerFactory(org.slf4j.LoggerFactory) PartitionMetadata(io.atomix.primitive.partition.PartitionMetadata) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionId(io.atomix.primitive.partition.PartitionId) ArrayList(java.util.ArrayList) StorageLevel(io.atomix.storage.StorageLevel) HashSet(java.util.HashSet) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) Partition(io.atomix.primitive.partition.Partition) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Map(java.util.Map) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) Futures(io.atomix.utils.concurrent.Futures) Logger(org.slf4j.Logger) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Recovery(io.atomix.primitive.Recovery) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) File(java.io.File) ClusterEventListener(io.atomix.cluster.ClusterEventListener) List(java.util.List) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) Type(io.atomix.primitive.PrimitiveProtocol.Type) ClusterService(io.atomix.cluster.ClusterService) Collections(java.util.Collections) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Node(io.atomix.cluster.Node) NodeId(io.atomix.cluster.NodeId) ArrayList(java.util.ArrayList) PartitionMetadata(io.atomix.primitive.partition.PartitionMetadata) PartitionId(io.atomix.primitive.partition.PartitionId) HashSet(java.util.HashSet)

Example 2 with Node

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

the class DefaultClusterService method handleMetadataEvent.

/**
 * Handles a cluster metadata change event.
 */
private void handleMetadataEvent(ClusterMetadataEvent event) {
    // Iterate through all bootstrap nodes and add any missing data nodes, triggering NODE_ADDED events.
    // Collect the bootstrap node IDs into a set.
    Set<NodeId> bootstrapNodes = event.subject().bootstrapNodes().stream().map(node -> {
        StatefulNode existingNode = nodes.get(node.id());
        if (existingNode == null) {
            StatefulNode newNode = new StatefulNode(node.id(), node.type(), node.endpoint(), node.zone(), node.rack(), node.host());
            nodes.put(newNode.id(), newNode);
            post(new ClusterEvent(ClusterEvent.Type.NODE_ADDED, newNode));
        }
        return node.id();
    }).collect(Collectors.toSet());
    // Filter the set of data node IDs from the local node information.
    Set<NodeId> dataNodes = nodes.entrySet().stream().filter(entry -> entry.getValue().type() == Node.Type.CORE).map(entry -> entry.getKey()).collect(Collectors.toSet());
    // Compute the set of local data nodes missing in the set of bootstrap nodes.
    Set<NodeId> missingNodes = Sets.difference(dataNodes, bootstrapNodes);
    // For each missing data node, remove the node and trigger a NODE_REMOVED event.
    for (NodeId nodeId : missingNodes) {
        StatefulNode existingNode = nodes.remove(nodeId);
        if (existingNode != null) {
            post(new ClusterEvent(ClusterEvent.Type.NODE_REMOVED, existingNode));
        }
    }
}
Also used : ClusterMetadataEventListener(io.atomix.cluster.ClusterMetadataEventListener) Node(io.atomix.cluster.Node) NodeId(io.atomix.cluster.NodeId) ScheduledFuture(java.util.concurrent.ScheduledFuture) AbstractListenerManager(io.atomix.utils.event.AbstractListenerManager) ClusterEvent(io.atomix.cluster.ClusterEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedClusterService(io.atomix.cluster.ManagedClusterService) ClusterMetadataEvent(io.atomix.cluster.ClusterMetadataEvent) Threads.namedThreads(io.atomix.utils.concurrent.Threads.namedThreads) State(io.atomix.cluster.Node.State) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KryoNamespace(io.atomix.utils.serializer.KryoNamespace) ExecutorService(java.util.concurrent.ExecutorService) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Endpoint(io.atomix.messaging.Endpoint) ClusterMetadataService(io.atomix.cluster.ClusterMetadataService) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) ClusterEventListener(io.atomix.cluster.ClusterEventListener) MessagingService(io.atomix.messaging.MessagingService) KryoNamespaces(io.atomix.utils.serializer.KryoNamespaces) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ClusterService(io.atomix.cluster.ClusterService) Serializer(io.atomix.utils.serializer.Serializer) ClusterEvent(io.atomix.cluster.ClusterEvent) NodeId(io.atomix.cluster.NodeId)

Example 3 with Node

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

the class DefaultClusterEventingService method broadcast.

@Override
public <M> void broadcast(String topic, M message, Function<M, byte[]> encoder) {
    byte[] payload = SERIALIZER.encode(new InternalMessage(InternalMessage.Type.ALL, encoder.apply(message)));
    getSubscriberNodes(topic).forEach(nodeId -> {
        Node node = clusterService.getNode(nodeId);
        if (node != null && node.getState() == Node.State.ACTIVE) {
            messagingService.sendAsync(node.endpoint(), topic, payload);
        }
    });
}
Also used : Node(io.atomix.cluster.Node)

Example 4 with Node

use of io.atomix.cluster.Node 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 5 with Node

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

the class RaftPerformanceTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    Node node = nextNode(Node.Type.CLIENT);
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        MessagingService messagingService = NettyMessagingService.builder().withEndpoint(node.endpoint()).build().start().join();
        protocol = new RaftClientMessagingProtocol(messagingService, protocolSerializer, endpointMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(node.id());
    }
    RaftClient client = RaftClient.builder().withNodeId(node.id()).withProtocol(protocol).withThreadModel(ThreadModel.THREAD_PER_SERVICE).build();
    client.connect(members.stream().map(Node::id).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Node(io.atomix.cluster.Node) RaftClientMessagingProtocol(io.atomix.protocols.raft.protocol.RaftClientMessagingProtocol) NettyMessagingService(io.atomix.messaging.impl.NettyMessagingService) ManagedMessagingService(io.atomix.messaging.ManagedMessagingService) MessagingService(io.atomix.messaging.MessagingService)

Aggregations

Node (io.atomix.cluster.Node)13 Endpoint (io.atomix.messaging.Endpoint)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 ClusterMetadata (io.atomix.cluster.ClusterMetadata)5 MessagingService (io.atomix.messaging.MessagingService)5 Collectors (java.util.stream.Collectors)5 Maps (com.google.common.collect.Maps)4 ClusterService (io.atomix.cluster.ClusterService)4 ManagedClusterMetadataService (io.atomix.cluster.ManagedClusterMetadataService)4 NodeId (io.atomix.cluster.NodeId)4 Collection (java.util.Collection)4 Map (java.util.Map)4 Set (java.util.Set)4 Logger (org.slf4j.Logger)4 Sets (com.google.common.collect.Sets)3 TestMessagingServiceFactory (io.atomix.cluster.messaging.impl.TestMessagingServiceFactory)3 KryoNamespaces (io.atomix.utils.serializer.KryoNamespaces)3 Serializer (io.atomix.utils.serializer.Serializer)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3