Selecting a common node in given time interval in a multi-node cluster systems

Shubhmeet Kaur
2 min readFeb 18, 2021

Hashing is computational technique to convert data of arbitrary size to a fixed length value. Hashing can be achieved via Hash functions and Hash tables. Common application of hashing is in data storage and retriecal applications to access data in small and constant time per retreival. Amount of storgae space requried to store hashed value is very low, and is fractionally greater then storing the actual data. Hashing avoids non-linear acess time of complicated structured and non structured data structures such as trees and list.

Problem Definitation :

There are n nodes in the clustered system. The requirement is operation o needs to be performed every f hrs by any one node/server in the cluster. The node selected must be random to ensure evenly distribution of load balancing among the clustered system. The same node should be selected in the given interval irrespective of which node is running the selection process or how many time you select the node in the given interval which has to execute the actual operation. How will you ensure each node selects the same node in the given interval {time t1 + frequency f} to execute this operation o? All the nodes/servers in the cluster will use consistent hashing algorithm to generate the same node to the caller as depicted in below code snippet.

/*
*
* Choose a node to execute the operation, if the localnode is selected, then execute operation, otherwise log it.
*
/
public void executeOperation()
{
Node nodeSelected = selectServer(startTime, frequency, listofServers);
//am i the node to execute operation?
if (nodeSelected is localNode) {
System.out.println("Current Node is choosen to run operation in this iteration");
//exceute operation o
}
else
{
System.out.println("Node " + nodeSelected +" was choosen to run operation in this iteration");
}
}
/*
*
* startTime :when the operation o was started
* frequency :how frequent the operation o needs to be triggered
* listOfServers :list of all the servers in the system
*
/
public Node selectServer(long startTime, long frequency, List<Node> listOfServers)
{
long currentTime = System.currentTimeMillis();
long interval;
if (currentTime >= startTime)
{
interval = (currentTime - startTime) / (frequency * 1000 * 60 * 60);
}
else
{
interval = (startTime - currentTime) / (frequency * 1000 * 60 * 60); }
int index = (int) (interval % listofServers.size());
return listofServers.get(index);
}

Conclusion

Hence, one can use hashing to ensure same node/server can be selected to perform given operation for a defined interval of time in a large scale multi-node cluster system.

Thank you,

Shubhmeet

--

--

Shubhmeet Kaur

Software Engineer | Graduated MSCS,Fall 2018 | Code Enthusiastic