Unregistering Entities in cluster mode
halber opened this issue · 0 comments
Problem description
NeonBee uses the shared map to manage the registration fo Entities to EntityVerticles e.g:
("Products" -> "ProductsVerticle")
("OrderTypes" -> "C4SOrderTypesVerticle")
During the boot up of an EntityVerticle "announceEntityVerticle" is used to register:
"Entity Products" is handled by "me" (EntityProductsVerticle)
Problem: No "stop()" method was implemented, and no fallback handling was implemented (so the map may contain entries of verticles that no longer exist)
Target state
When a node is shut down properly, the entities registered by that node should be removed. If a node is not shut down
properly, the entities that were registered by that node should be purged through a cleanup method.
Solution
The solution consists of two methods. The gracefully shutdown and the cleanup method. The gracefully shutdown method
deletes all entries from the shared data map that the node has added. After cleaning up the shared data, the node can be
shutdown normally.
gracefully shutdown
When a node is shut down, the unregister method should be called. To call the unregister method, a hook must be
registered in NeonBee. The unregistering method cleans up the shared map and removes all entities that were registered
by that node. After the shared map is cleaned up, an event to EVENT_BUS_MODELS_LOADED_ADDRESS is fired to update the
ODataV4Endpoint routes. The gracefully shutdown method is intended to shorten the time frame until the cluster detects
that a node has left the cluster.
cleanup method
If a node cannot perform the gracefully shutdown method, the other nodes that are still alive must clean up the
registered entities. To detect that a node has left the cluster, a io.vertx.core.spi.cluster.NodeListener must be
implemented and registered by the io.vertx.core.spi.cluster.ClusterManager.nodeListener method. The
NodeListener#nodeLeft method is called when the cluster detects that a node has left the cluster. This method is also
called when a node leaves the cluster and executes the Gracefully Shutdown method. To avoid each cluster node tries to
change the shared map, the nodes decide which node will perform the cleanup. To decide which node executes the cleanup, a
simple implementation could use the hazelcast leader.
public boolean getLeader() {
HazelcastClusterManager instance = ...
Member oldestMember = instance.getCluster().getMembers().iterator().next();
return oldestMember.localMember();
}
}The cleanup method is basically the same as the gracefully shutdown method. The only difference is that it cleans up the
shared map for another node. After the cleanup is complete, an event is fired to the EVENT_BUS_MODELS_LOADED_ADDRESS.