Search in sources :

Example 1 with ClusterMetadataEvent

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

the class DefaultClusterMetadataService method handleUpdate.

/**
 * Handles an update from another node.
 */
private void handleUpdate(Endpoint endpoint, byte[] payload) {
    NodeUpdate update = SERIALIZER.decode(payload);
    clock.incrementAndUpdate(update.timestamp());
    ReplicatedNode node = nodes.get(update.node().id());
    if (node == null || node.timestamp().isOlderThan(update.timestamp())) {
        nodes.put(update.node().id(), update.node());
        post(new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, getMetadata()));
    }
}
Also used : ClusterMetadataEvent(io.atomix.cluster.ClusterMetadataEvent)

Example 2 with ClusterMetadataEvent

use of io.atomix.cluster.ClusterMetadataEvent 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 3 with ClusterMetadataEvent

use of io.atomix.cluster.ClusterMetadataEvent 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 4 with ClusterMetadataEvent

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

the class DefaultClusterMetadataService method addNode.

@Override
public void addNode(Node node) {
    if (node.type() != Node.Type.CLIENT) {
        ReplicatedNode replicatedNode = nodes.get(node.id());
        if (replicatedNode == null) {
            LogicalTimestamp timestamp = clock.increment();
            replicatedNode = new ReplicatedNode(node.id(), node.type(), node.endpoint(), node.zone(), node.rack(), node.host(), timestamp, false);
            nodes.put(replicatedNode.id(), replicatedNode);
            broadcastUpdate(new NodeUpdate(replicatedNode, timestamp));
            post(new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, getMetadata()));
        }
    }
}
Also used : LogicalTimestamp(io.atomix.utils.time.LogicalTimestamp) ClusterMetadataEvent(io.atomix.cluster.ClusterMetadataEvent)

Example 5 with ClusterMetadataEvent

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

the class DefaultClusterMetadataService method removeNode.

@Override
public void removeNode(Node node) {
    ReplicatedNode replicatedNode = nodes.get(node.id());
    if (replicatedNode != null) {
        LogicalTimestamp timestamp = clock.increment();
        replicatedNode = new ReplicatedNode(node.id(), node.type(), node.endpoint(), node.zone(), node.rack(), node.host(), timestamp, true);
        nodes.put(replicatedNode.id(), replicatedNode);
        broadcastUpdate(new NodeUpdate(replicatedNode, timestamp));
        post(new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, getMetadata()));
    }
}
Also used : LogicalTimestamp(io.atomix.utils.time.LogicalTimestamp) ClusterMetadataEvent(io.atomix.cluster.ClusterMetadataEvent)

Aggregations

ClusterMetadataEvent (io.atomix.cluster.ClusterMetadataEvent)5 LogicalTimestamp (io.atomix.utils.time.LogicalTimestamp)3 NodeId (io.atomix.cluster.NodeId)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 ClusterEvent (io.atomix.cluster.ClusterEvent)1 ClusterEventListener (io.atomix.cluster.ClusterEventListener)1 ClusterMetadataEventListener (io.atomix.cluster.ClusterMetadataEventListener)1 ClusterMetadataService (io.atomix.cluster.ClusterMetadataService)1 ClusterService (io.atomix.cluster.ClusterService)1 ManagedClusterService (io.atomix.cluster.ManagedClusterService)1 Node (io.atomix.cluster.Node)1 State (io.atomix.cluster.Node.State)1 Endpoint (io.atomix.messaging.Endpoint)1 MessagingService (io.atomix.messaging.MessagingService)1 Threads.namedThreads (io.atomix.utils.concurrent.Threads.namedThreads)1 AbstractListenerManager (io.atomix.utils.event.AbstractListenerManager)1 KryoNamespace (io.atomix.utils.serializer.KryoNamespace)1