what algorithm hazelcast uses to find the master node

巧了我就是萌 提交于 2020-02-04 09:34:29

问题


I am researching the algorithm that hazelcast uses to find the master node in multicast way.First I find the function to find master node: com.hazelcast.cluster.MulticastJoiner.findMasterWithMulticast()

private Address findMasterWithMulticast() {
    try {
        if (logger.isFinestEnabled()) {
            logger.finest("Searching for master node. Max tries: " + maxTryCount.get());
        }
        JoinRequest joinRequest = node.createJoinRequest();
        while (node.isActive() && currentTryCount.incrementAndGet() <= maxTryCount.get()) {
            joinRequest.setTryCount(currentTryCount.get());
            node.multicastService.send(joinRequest);
            if (node.getMasterAddress() == null) {
                //noinspection BusyWait
                Thread.sleep(PUBLISH_INTERVAL);
            } else {
                return node.getMasterAddress();
            }
        }
    } catch (final Exception e) {
        if (logger != null) {
            logger.warning(e);
        }
    } finally {
        currentTryCount.set(0);
    }
    return null;
}

what the function does is just sendding a joinRequest to other nodes in the same clusters. The function com.hazelcast.cluster.MulticastJoiner.onReceivedJoinRequest(JoinRequest) deals with the joinRequest.

public void onReceivedJoinRequest(JoinRequest joinRequest) {
    if (joinRequest.getUuid().compareTo(node.localMember.getUuid()) < 0) {
        maxTryCount.incrementAndGet();
    }
}

It only increases the trycount.What does this mean?How the master node is selected?Wish your help.


回答1:


In multicast discovery, each node calculates its own maxTryCount before join process is initiated. maxTryCount is the maximum number of join messages those will be published by that specific node, before declaring itself as master.

It's calculated using a few parameters such as multicast timeout, IP address and port of node etc. See MulticastJoiner.calculateTryCount(). So, main purpose here is each node (most probably) will have a different maximum try count.

Additionally, when a node receives join request, maxTryCount is incremented by 1 if the pure function defined inside MulticastJoiner.onReceivedJoinRequest() returns true:

joinRequest.getUuid().compareTo(node.localMember.getUuid()) < 0

This is to diverge maxTryCount number on both sides.

Once a node publishes maxTryCount join messages, then it declares itself as master (oldest member) and starts to accept/respond join requests.




回答2:


One of the main features of Hazelcast is that it does not have a master member. Each cluster member is configured to be the same in terms of functionality. The oldest member (the first member created in the cluster) automatically performs the data assignment to cluster members.

When you start a member, a partition table is created within it. This table stores the partition IDs and the cluster members to which they belong. The purpose of this table is to make all members (including lite members) in the cluster aware of this information, making sure that each member knows where the data is.

The oldest member in the cluster (the one that started first) periodically sends the partition table to all members. In this way, each member of the cluster is informed about any changes to partition ownership. The ownerships may be changed when, for example, a new member joins the cluster, or when a member leaves the cluster.

NOTE: If the oldest member of the cluster goes down, the next oldest member sends the partition table information to the other ones.

Thank you



来源:https://stackoverflow.com/questions/41776429/what-algorithm-hazelcast-uses-to-find-the-master-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!