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()));
}
}
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)));
}
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));
}
}
}
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()));
}
}
}
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()));
}
}
Aggregations