Lab 3: Fault-tolerant Key-Value Store with Raft

Due: 11-29-2021 23:59 (UTC+8)

Introduction

In this lab, you will design a Raft library to implement a fault-tolerant distributed key-value storage system. The library decouples the consensus algorithm from the replicated state machine. The user of the library only needs to care about the implementation of a single node state machine, e.g. a single node key-value store. With this library, we can easily extend a single machine system to a distributed system with fault tolerance.

avatar

A replicated system achieves fault tolerance by replicating the state on multiple machines. For example, a replicated key-value store copies all the key-value pairs to multiple machines. The replication mechanism makes the system available even if some of the servers crash (or encounter a network failure). However, the key challenge for implementing a replicated system is to keep the replicas consistent, which can be solved by consensus algorithms such as Paxos or Raft.

Raft is a consensus algorithm that is famous for its understandability. It's equivalent to Paxos in fault tolerance for replicated systems. Raft decomposes the consensus problem into relatively independent subproblems, which are much easier to understand. The key data structure in Raft is the log, which organizes the clients' requests into a sequence. Raft guarantees all the servers will apply the same log commands in the same order, which means the servers will all be in a consistent state. If a server fails but later recovers, Raft takes care of bringing its log up to date. And Raft can work as long as at least a majority of the servers are alive and connected.

Raft implements consensus by first electing a leader among the servers (part 1), then giving the leader authority and responsibility for managing the log. The leader accepts log entries from clients, replicates them on other servers, and tells servers when it is safe to apply log entries to their state machines (part 2). The logs should be persisted on the non-volatile storage to tolerate machine crashes (part 3). And as the log grows longer, Raft will compact the log via snapshotting (part 4).

This lab will follow the description of the Raft paper. So, make sure you have read and understood the Raft paper of the extended version (especially section 5 and section 7) before coding. And you will find that Figure 2 and Figure 13 in the raft paper can cover most of your design in this lab.

There are 5 parts in this lab.

Each part relies on the implementation of the prior one. So you must implement these parts one by one.

If you have any questions about this lab, please feel free to let the TA know: Mingcong Han (mingconghan@sjtu.edu.cn).

IMPORTANT: You may take more than 12 hours to complete this lab. Start as early as possible!

Hope you can enjoy the lab!

Getting started

Please backup all of your prior labs' solutions before starting this lab.

Then, pull this lab from the repo:

Next, switch to the lab3 branch:

Merge with lab2, and solve the conflicts.

After merging the conflicts, you should be able to compile the new project successfully:

Overview of the code

You will mainly modify and complete the codes in raft.h(part 1 - part 4), raft_protocol.h, raft_protocol.cc (part 1 - part 4), raft_state_machine.h, raft_state_machine.cc (part 5) and raft_storage.h(part 3 - part 4).

There are 4 important C++ classes you need to pay attention to.

The first two classes (raft_command and raft_state_machine in raft_state_machine.h) are related to the state machine. We have already implemented these two classes for testing your Raft implementation in the first four parts. And you will implement your own in part 5. But you still need to check the interfaces provided by them in the early parts. For example, you will use the raft_state_machine::apply_log interface to apply a committed Raft log to the state machine.

And the raft class (in raft.h) is the core of your implementation, which represents a Raft node (or Raft server). raft is a class template with two template parameters, state_machine and command. Remember we're implementing a raft library that decouples the consensus algorithm from the replicated state machine. Therefore, the user can implement their own state machine (e.g. a kv store) and pass it to the Raft library via the two template parameters.

The user ensures the state_machine inherits from raft_state_machine and the command inherits from raft_command. So you can use the interfaces provided by the two base classes in your implementation.

And the last important class is raft_storage, which you will complete to persist the Raft log and metadata. raft_storage is also a class template with a template parameter named command, which is the same as the template parameter of raft class. And you can use the interface provided by raft_command, such as size, deserialize and serialize to implement the log persistency.

Notice: You must not change the constructor definition of these classes.

Understand the raft class

Now, let's first walk through how raft works.

Our raft algorithm is implemented asynchronously, which means the events (e.g. leader election or log replication) should all happen in the background. For example, when the user calls raft::new_command to append a new command to the leader's log, the leader should return the new_command function immediately. And the log should be replicated to the follower asynchronously in another background thread.

A raft node starts after calling raft::start(), and it will create 4 background threads.

The background threads will periodically do something in the background (e.g. send heartbeats in run_background_ping, or start an election in run_background_election). And you will implement the body of these background threads.

Besides the events, the RPCs also should be sent and handled asynchronously. If you have tried the RPC library in lab2, you may know that the RPC call provided by the lab is a synchronous version, which means the caller thread will be blocked until the RPC completes. To implement an asynchronous RPC call, this lab also provides a thread pool to handle asynchronous events. For example:

To test your implementation, you can type:

And you can change the partX to the part you want to test, e.g. part1 or part2.

Part 1 - Leader Election

In this part, you will implement the leader election protocol and heartbeat mechanism of the Raft consensus algorithm. And you can refer to Figure 2 in the raft paper to implement this part.

You'd better follow the steps:

  1. Complete the request_vote_args and request_vote_reply class in raft_protocol.h. Also, remember to complete the marshall and unmarshal function in raft_protocol.cc for RPCs.
  2. Complete the method raft::request_vote following Figure 2 in the Raft paper (you may also need to define some variables for the raft class, such as commit_idx).
  3. Complete the method raft::handle_request_vote_reply, which should handle the RPC reply.
  4. Complete the method raft::run_background_election, which should turn to candidate and start an election after a leader timeout by sending request_vote RPCs asynchronously.
  5. Now, the raft nodes should be able to elect a leader automatically. But to keep its leadership, the leader should send heartbeat (i.e. an empty AppendEntries RPC) to the followers periodically. You can implement the heartbeat by implementing the AppendEntries RPC (e.g. complete append_entries_args, append_entries_reply, raft::append_entries, raft::handle_append_entries_reply, raft::run_background_ping).

You should pass the 2 test cases of this part. (10 points + 10 points)

Hints:

Part 2 - Log Replication

In this part, you will implement the log replication protocol of the Raft consensus algorithm. Still, you can refer to Figure 2 in the raft paper.

Recommended steps:

  1. Complete raft::new_command to append new command to the leader's log.
  2. Complete the methods related to the AppendEntries RPC (e.g. raft::append_entries, raft::handle_append_entries_reply).
  3. Complete raft::run_background_commit to send logs to the followers asynchronously.
  4. Complete raft::run_background_apply to apply the committed logs to the state machine.

You should pass the 7 test cases of this part. (10 points * 2 + 5 points * 5)

Hints:

Part 3 - Log Persistency

In this part, you will persist the states of a Raft node. Check Figure 2 in the Raft paper again, to figure out what should be persisted.

Recommended steps:

  1. You should implement the class raft_storeage in raft_storage.h to persist the necessary states (e.g. logs). The test case will use the constructor raft_storage(const std::string &file_dir) to create a raft_storage object. Each raft node will have its own file_dir to persist the states. And after a failure, the node will restore its storage via this dir.
  2. You should use the raft::storage to persist the state, whenever they are changed.
  3. And you should use the storage to restore the state when a Raft node is created.

You should pass the 6 test cases of this part. (5 points + 5 points + 5 points + 2 points + 2 points + 1 point)

Hints:

Part 4 - Snapshot

In this part, you will implement the snapshot mechanism of the Raft algorithm. You can refer to Figure 13 in the Raft extended paper.

Notice: you don't need to partition the snapshot. You can send the whole snapshot in a single RPC.

Recommended steps:

  1. Complete the classes and methods related to raft::install_snapshot.
  2. Complete the method raft::save_snapshot.
  3. Modify all the codes related to the log you have implemented before. (E.g. number of logs)
  4. Restore the snapshot in the raft constructor.

You should pass the 3 test cases of this part. (2 points + 2 points + 1 points)

Hints:

Part 5 - Fault-tolerant Key-Value Store

In this part, you will use the library to build a fault-tolerant key-value store. You will implement a state machine that works as a single machine key-value store.

Recommended steps:

  1. Complete the class kv_state_machine, kv_command in raft_state_machine.h and raft_state_machine.cc.

Notice: The command is executed asynchronously when applied to the state machine. Therefore, to get the result of the command, we provide a struct named result in the kv_command. You should fill this struct when applying the command. The usage should be like this:

You should pass the 3 test cases of this part. (4 points + 4 points + 3 points)

Hints:

Grading

After you have implmented all the parts above, run the grading script:

IMPORTANT: The grade scrip will run each test case many times. Once a test case failes, you will not get the score of that case. So, please make sure there are no concurrent bugs.

Handin Procedure

After all the above done:

That should produce a file called lab3.tgz in the directory. Change the file name to your student id:

Then upload lab3_[your student id].tgz file to Canvas before the deadline.

You'll receive full credits if your code passes the same tests that we gave you, when we run your code on our machines.