Search in sources :

Example 1 with NodeId

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

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

the class DefaultClusterMetadataService method sendAdvertisement.

/**
 * Sends an anti-entropy advertisement to the given node.
 */
private void sendAdvertisement(Endpoint endpoint) {
    clock.increment();
    ClusterMetadataAdvertisement advertisement = new ClusterMetadataAdvertisement(Maps.newHashMap(Maps.transformValues(nodes, node -> new NodeDigest(node.timestamp(), node.tombstone()))));
    messagingService.sendAndReceive(endpoint, ADVERTISEMENT_MESSAGE, SERIALIZER.encode(advertisement)).whenComplete((response, error) -> {
        if (error == null) {
            Set<NodeId> nodes = SERIALIZER.decode(response);
            for (NodeId nodeId : nodes) {
                ReplicatedNode node = this.nodes.get(nodeId);
                if (node != null) {
                    sendUpdate(endpoint, new NodeUpdate(node, clock.increment()));
                }
            }
        } else {
            log.warn("Anti-entropy advertisement to {} failed!", endpoint);
        }
    });
}
Also used : NodeId(io.atomix.cluster.NodeId)

Example 3 with NodeId

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

the class DefaultClusterMetadataService method handleAdvertisement.

/**
 * Handles an anti-entropy advertisement.
 */
private byte[] handleAdvertisement(Endpoint endpoint, byte[] payload) {
    LogicalTimestamp timestamp = clock.increment();
    ClusterMetadataAdvertisement advertisement = SERIALIZER.decode(payload);
    Set<NodeId> staleNodes = nodes.values().stream().map(node -> {
        NodeDigest digest = advertisement.digest(node.id());
        if (digest == null || node.isNewerThan(digest.timestamp())) {
            sendUpdate(endpoint, new NodeUpdate(node, timestamp));
        } else if (digest.isNewerThan(node.timestamp())) {
            if (digest.tombstone()) {
                if (!node.tombstone()) {
                    nodes.put(node.id(), new ReplicatedNode(node.id(), node.type(), node.endpoint(), node.zone(), node.rack(), node.host(), digest.timestamp(), true));
                    post(new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, getMetadata()));
                }
            } else {
                return node.id();
            }
        }
        return null;
    }).filter(Objects::nonNull).collect(Collectors.toSet());
    return SERIALIZER.encode(Sets.newHashSet(Sets.union(Sets.difference(advertisement.digests(), nodes.keySet()), staleNodes)));
}
Also used : LogicalTimestamp(io.atomix.utils.time.LogicalTimestamp) ClusterMetadataEvent(io.atomix.cluster.ClusterMetadataEvent) NodeId(io.atomix.cluster.NodeId)

Example 4 with NodeId

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

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

the class PrimaryBackupTest method createClient.

/**
 * Creates a Raft client.
 */
private PrimaryBackupClient createClient() throws Throwable {
    NodeId nodeId = nextNodeId();
    PrimaryBackupClient client = PrimaryBackupClient.builder().withClientName("test").withClusterService(new TestClusterService(nodeId, nodes)).withSessionIdProvider(() -> CompletableFuture.completedFuture(nextSessionId())).withPrimaryElection(election).withProtocol(protocolFactory.newClientProtocol(nodeId)).build();
    clients.add(client);
    return client;
}
Also used : TestClusterService(io.atomix.cluster.TestClusterService) NodeId(io.atomix.cluster.NodeId)

Aggregations

NodeId (io.atomix.cluster.NodeId)17 CompletableFuture (java.util.concurrent.CompletableFuture)7 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)5 Collection (java.util.Collection)5 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)4 Futures (io.atomix.utils.concurrent.Futures)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 RaftError (io.atomix.protocols.raft.RaftError)3 RaftServer (io.atomix.protocols.raft.RaftServer)3 RaftCluster (io.atomix.protocols.raft.cluster.RaftCluster)3 RaftClusterEvent (io.atomix.protocols.raft.cluster.RaftClusterEvent)3 RaftClusterEventListener (io.atomix.protocols.raft.cluster.RaftClusterEventListener)3 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)3 RaftContext (io.atomix.protocols.raft.impl.RaftContext)3 JoinRequest (io.atomix.protocols.raft.protocol.JoinRequest)3