项目作者: feross

项目描述 :
An understandable consensus algorithm
高级语言: C++
项目地址: git://github.com/feross/raft.git
创建时间: 2019-03-28T07:15:47Z
项目社区:https://github.com/feross/raft

开源协议:

下载





Raft logo


Raft - An understandable consensus algorithm




What is Raft?

Raft is a consensus algorithm that is designed to be easy to understand. It’s
equivalent to Paxos in fault-tolerance and performance. The difference is that
it’s decomposed into relatively independent subproblems, and it cleanly
addresses all major pieces needed for practical systems. We hope Raft will make
consensus available to a wider audience, and that this wider audience will be
able to develop a variety of higher quality consensus-based systems than are
available today.

Quick Start

  1. $ make install-deps # Quick install system dependencies (macOS only)
  2. $ make
  3. $ ./raft --help

🥳 Congrats, you just got the Raft binary running! Now, let’s start a basic Raft server.

Starting the Raft server

The basic command line format is as follows:

  1. make
  2. ./raft --id <server_id>

The first option --id <server_id> gives the server the specified name or
“server id”. This is a friendly name that the server uses to identify itself to
other servers in the cluster, as well as to name thee persistent storage file.

This command looks for a config file at ./config and reads in information
about the Raft cluster from there. Here’s what a simple config file for a single
server cluster looks like:

  1. 127.0.0.1 4000

This defines a server that listens for client requests on 127.0.0.1 and port
4000.

Now start the server like this:

  1. ./raft --id 0 --reset
  2. ./raft --id 0

The --reset option must be included the first time a server is run to
establish the persistent storage and log files on disk. Read more about
--reset and the other command line options below.

🌟 That’s it. Now that you know how to start a single Raft server, you’re ready
to start an entire Raft cluster. We’ll do that in the next section.

Start a three server Raft cluster

To start a real Raft cluster, you need to run three copies of the program in
parallel and ensure that each command is given a unique server id.

Here is a sample config file for a three server cluster:

  1. 127.0.0.1 4000 4001 4002
  2. 127.0.0.1 5000 5001 5002
  3. 127.0.0.1 6000 6001 6002

Each line represents a separate server. Since there are three servers in the
cluster, each server needs to open two additional ports to receive connections
from the other servers in the cluster.

Here are some commands you can copy-paste into three separate terminals to start
up the cluster:

  1. ./raft --id 0 --reset
  2. ./raft --id 0
  1. ./raft --id 1 --reset
  2. ./raft --id 1
  1. ./raft --id 2 --reset
  2. ./raft --id 2

✨ And just like that, you’re running a Raft cluster!

Sending commands to the cluster

The purpose of Raft is to make a bunch of servers work together to reliably
update a state machine. So, we’ll now discuss how to actually update the state
in the state machine.

Note: In this implementation of Raft, we include a sample state machine that
takes terminal commands and runs them in bash, returning the output as a
string. The bash state machine conforms to the StateMachine interface defined
in state-machine.h and you can use it as an example when writing your own
state machine implementation, if you so desire.

First, start the ./client program which creates a REPL that sends commands to
the Raft cluster. The client automatically handles finding the leader server,
retrying the request with a different server if the leader becomes unavailable,
and displaying the output from running each command.

Here’s what a sample run of the client looks like:

  1. $ ./client
  2. > echo hello
  3. hello
  4. > touch myfile.txt
  5. > ls
  6. myfile.txt
  7. > quit

A few odds and ends…

Reset persistent storage

To reset the persistent storage of a server, use the --reset boolean argument.
This option is required the first time you start the server.

  1. ./raft --id <server_id> --reset

Note: Do not use --reset when a server is rejoining the cluster after a
crash or shutdown as this will wipe away the persistent state and log which will
cause consistency issues. Adding/removing servers from the cluster as described
in the Raft paper is currently not supported in this implementation.

Use a custom configuration file location

  1. ./raft --id <server_id> --config ./my_cool_config_file

To use a custom configuration file location, use the --config string argument.

Show debug logs

To show extremely verbose debug logs, use the --debug boolean argument.

  1. ./raft --id <server_id> --debug

Quiet mode

To show only warnings and errors and hide almost every other log message, use
the --quiet boolean argument.

  1. ./raft --id <server_id> --quiet

Command Line Help

Get help from the command line by using the --help boolean argument:

  1. $ ./raft --help
  2. Raft - An understandable consensus algorithm
  3. Usage:
  4. ./raft [options] [peers ...]
  5. Minimal Example:
  6. Start a server that connects to one other server.
  7. ./raft --id <server_id> --reset
  8. Cluster Example:
  9. Start a three server Raft cluster.
  10. Use the config file:
  11. 127.0.0.1 4000 4001 4002
  12. 127.0.0.1 5000 5001 5002
  13. 127.0.0.1 6000 6001 6002
  14. Run:
  15. ./raft --id 0 --reset
  16. ./raft --id 1 --reset
  17. ./raft --id 2 --reset
  18. Usage:
  19. --config Path to configuration file (default = ./config) [string]
  20. --debug Show all logs [bool]
  21. --help Print help message [bool]
  22. --id Server identifier [int]
  23. --quiet Show only errors [bool]
  24. --reset Delete server storage [bool]

Install Dependencies

Quick Install (macOS only)

macOS users with the Homebrew package manager installed can
install all system dependencies in one step.

  1. make install-deps

Protocol Buffers

Protocol buffers are a language-neutral, platform-neutral, extensible way of
serializing structured data for use in communications protocols and data storage.

See the official
Protocol Buffers website to
learn more.

pkg-config

pkg-config is a helper tool used when compiling applications and libraries. It
helps you insert the correct compiler options on the command line.

See the official
pkg-config website
to learn more.

Additional Raft Resources

License

Copyright (c) Feross Aboukhadijeh and Jake McKinnon