1. Quickly understand Raft algorithm

Raft applies to a protocol for managing log consistency and is easier to understand and implement than Paxos. To improve understanding, Raft divides the consistency algorithm into several parts, including Leader Selection, log replication, and safety, and uses greater consistency to reduce the number of states that must be considered.

Raft algorithm is much more intuitive to understand than Paxos.

The Raft algorithm divides the Server into three states, also known as roles:

  • The Leader is responsible for Client interaction and log replication. A maximum of one Leader exists in the system at a time.
  • Followers passively respond to request RPCS but never initiate request RPCS.
  • Candidate

A temporary role exists only in the election phase of leader. If a node wants to become leader, it initiates a vote request and becomes a candidate at the same time. If the election is successful, it becomes a candidate; otherwise, it is returned to follower

The flow of states, or characters, is as follows:

In Raft, the problem is broken down into: leader selection, log replication, security, and member changes.

Replication state machines are implemented by copying logs:

Logs: Each machine saves a log that is generated from client requests and contains a series of command state machines. The state machines execute these commands in sequence. Consistency model: In a distributed environment, logs of multiple machines are guaranteed to be consistent, so that the state machines are played back to the same state

Raft algorithm selects the main flow

Term is similar to the succession of dynasties in Chinese history. Raft algorithm divides time into arbitrary terms of different lengths.

The election process

1. Follower adds the current term and becomes a candidate. Candidate votes himself and sends RequestVote RPC to other servers in the cluster. A server that receives a RequestVote will vote for no more than one candidate in a term on a first-come, first-served basis. And only vote for log who is at least as new as themselves.

For a more detailed description of Raft, see here, from distributed Consistency to consensus mechanisms (II) Raft algorithm

CP consistency in Nacos

Spring Cloud Alibaba Nacos officially supports AP and CP consistency protocols in 1.0.0. The CP consistency protocol implementation is based on simplified Raft CP consistency.

How to implement Raft algorithm

Nacos server at startup, through RunningConfig onApplicationEvent () method call RaftCore. The init () method.

Start the election

public static void init() throws Exception { Loggers.RAFT.info("initializing Raft sub-system"); // Start Notifier, poll Datums, notify RaftListener executor.submit(Notifier); Add (namingproxy.getServers ()); long start = System.currentTimeMillis(); // Load Datum and term data from disk for data recovery raftStore.load (); Loggers.RAFT.info("cache loaded, peer count: {}, datum count: {}, current term: {}", peers.size(), datums.size(), peers.getTerm()); while (true) { if (notifier.tasks.size() <= 0) { break; } Thread.sleep(1000L); System.out.println(notifier.tasks.size()); } Loggers.RAFT.info("finish to load data from disk, cost: {} ms.", (System.currentTimeMillis() - start)); GlobalExecutor.register(new MasterElection()); // The Leader election globalExecutor.register1 (new HeartBeat()); // Raft heartbeat Globalexecutor.register (new AddressServerUpdater(), globalExecutor.address_server_update_interval_ms); // Raft heartbeat Globalexecutor.register (new AddressServerUpdater(), GlobalExecutor. if (peers.size() > 0) { if (lock.tryLock(INIT_LOCK_TIME_SECONDS, TimeUnit.SECONDS)) { initialized = true; lock.unlock(); } } else { throw new Exception("peers is empty."); } Loggers.RAFT.info("timer started: leader timeout ms: {}, heart-beat timeout ms: {}", GlobalExecutor.LEADER_TIMEOUT_MS, GlobalExecutor.HEARTBEAT_INTERVAL_MS); }Copy the code

The init method does the following:

  1. Get Raft cluster node select.add (namingproxy.getServers ());
  2. Raft cluster data recovery raftStore.load ();
  3. Raft election GlobalExecutor.register(new MasterElection());
  4. Raft GlobalExecutor.register(new HeartBeat());
  5. Raft Publishing Content
  6. Raft ensures consistency

The election process

Where the raft cluster is internode through exposed Restful interfaces, the code is in RaftController. RaftController The RaftController is used for communication between nodes in the RAFT cluster

POST HTTP://{ip:port}/v1/ns/raft/vote: To request a vote POST HTTP://{ip:port}/v1/ns/raft/beat: The Leader sends heartbeat messages to the Follower GET HTTP://{ip:port}/v1/ns/raft/peer: Obtain RaftPeer information about the node. PUT HTTP://{ip:port}/v1/ns/raft/datum/reload: reloads a log. POST HTTP://{ip:port}/v1/ns/raft/datum: The Leader receives the sent data and stores it in DELETE HTTP://{ip:port}/v1/ns/raft/datum: The Leader receives the sent data and deletes the operation GET HTTP://{ip:port}/v1/ns/raft/datum: Obtain the data stored on the node. GET HTTP://{ip:port}/v1/ns/raft/state: Obtain the status of the node {UP or DOWN} POST HTTP://{ip:port}/v1/ns/raft/datum/commit: The Follower node receives data from the Leader and stores the data. DELETE HTTP://{ip:port}/v1/ns/raft/datum: The Follower node receives data from the Leader to delete data. GET HTTP://{ip:port}/v1/ns/raft/leader: GET HTTP://{ip:port}/v1/ns/raft/listeners: GET all the event listeners of the Raft cluster RaftPeerSetCopy the code

heartbeat

A heartbeat mechanism is used in Raft to trigger the leader election. Globalexecutor.register (new HeartBeat()) is used to register HeartBeat scheduled tasks in GlobalExecutor. The operations include:

  • Reset the heart Timeout and Election Timeout of the Leader node.
  • SendBeat () Sends heartbeat packets
public class HeartBeat implements Runnable { @Override public void run() { try { if (! peers.isReady()) { return; } RaftPeer local = peers.local(); local.heartbeatDueMs -= GlobalExecutor.TICK_PERIOD_MS; if (local.heartbeatDueMs > 0) { return; } local.resetHeartbeatDue(); sendBeat(); } catch (Exception e) { Loggers.RAFT.warn("[RAFT] error while sending beat {}", e); }}}Copy the code

The Raft consistency implementation in Nacos is described in detail. You can download the source code and see RaftCore for more details. Source code can be checked out with the following address:

git clone https://github.com/alibaba/nacos.git
Copy the code

Scan our public account: Architecture evolution for first-hand technical information and original articles