A cluster consists of several members, and to manage these members there must be a list of all members that can be used to accurately locate the address of a node and then send an action message to that node. How to maintain this list of all members is the subject of this sectionto discussion.
Member maintenance is the basic function of a cluster, usually divided into a separate module or layer to complete this function, it provides member list query, member maintenance, member list Change event notification and other capabilities. Since the tribes is located in communication based on the same node, there is no problem of the main node election, it has the function of automatically discovering the node, that is, the new node joins to notify the other members of the cluster to update the member list, so that each node can update the member list in time. Each node maintains a list of cluster members. , node 1, node 2, node 3 use multicast through the switch each has maintained a list of members, and they have a time interval to the switch group broadcast their own node messages, That is heartbeat operation. When the fourth node joins the cluster group, the node four-to-one switch multicast its own node message, the principle three nodes received after each node four joined to the respective member list, and the original three nodes also constantly send node messages to the switch, node four received and then update the member list information, Finally, up to four nodes have four node member information.
Look down.tribescluster is how to design the implementation of the above functions, the creation and maintenance of its member list is based on the classic multicast implementation, each node creates a node information emitter and node information receiver, let them run in a separate thread. The emitter is used to send messages to the group within its own node, while the receiver is used to receive and process node messages sent by other nodes. In order for communication between nodes to be recognized, it is necessary to define a semantics, that is, the structure of the agreed message protocol ,tribesthe member message is defined as two fixed values that indicate the start and end of the message and begin to identifyTribes_mbr_beginthe value is a byte array- -------1, 0, end identificationTribes_mbr_endthe value is a byte array- -------1, 0, the entire protocol package structure is: Start identity (10bytes)+Package Length (4bytes)+Survival Time (8bytes)+tcpPorts (4bytes)+Secure Port (4bytes)+udpPorts (4bytes)+hostLength (1byte)+host(nbytes)+Command Length (4bytes)+Command (nbytes)+Domain Length (4bytes)+Domain Name (nbytes)+Unique SessionID(16bytes)+Payload Length (4bytes)+Payload (nbytes)+End Identification (10bytes). The member emitter is organized into a packet structure and multicast according to the protocol, the receiver receives the packet and follows the protocol to unpack, maintaining the member table according to the package information.
The following section of the code is a simple demonstration of the implementation process, due to the space Problem packet processing omitted:
public class Mcastservice {
Private MulticastSocket socket;
Private String address = "228.0.0.4";
private int port = 8000;
Private InetAddress addr;
Private byte[] buffer = new byte[2048];
Private Datagrampacket Receivepacket;
Private Final Object Sendlock = new Object ();
public void Start () {
try {
Addr = Inetaddress.getbyname (address);
Receivepacket = new Datagrampacket (buffer, buffer.length, addr,port);
Socket.joingroup (addr);
New Receiverthread (). Start ();
New Senderthread (). Start ();
} catch (IOException e) {
}
}
public class Receiverthread extends Thread {
public void Run () {
while (true) {
try {
Receive ();
} catch (ArrayIndexOutOfBoundsException ax) {
}
}
}
}
public class Senderthread extends Thread {
public void Run () {
while (true) {
try {
Send ();
} catch (Exception x) {
}
try {
Thread.Sleep (1000);
} catch (Exception ignore) {
}
}
}
}
public void Send () {
byte[] data = organization of the package structure according to the membership agreement ;
Datagrampacket packet = new Datagrampacket (data, data.length, addr, port);
try {
Socket.send (packet);
} catch (IOException e) {
}
}
public void receive () {
try {
Socket.receive (Receivepacket);
Parse the processing member message.
} catch (IOException e) {
}
}
}
The first step is to join the multicast member operations, and then start the receiver thread, the emitter thread, the general receiver to start first. Every 1 seconds, the transmitter organizes protocol packets to send the heartbeat, the receivers of the members in the multicast group parse the received protocol messages, update the local member list of the respective nodes according to certain logic, and if the member table already contains members of the protocol package, only updates the message such as survival time.
Tribes uses the above principles to maintain cluster members, and to provide services related to members by the standalone module Membershipservice, such as obtaining information about all members of the cluster.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to maintain cluster member information