Coder Social home page Coder Social logo

b-xiang / raft Goto Github PK

View Code? Open in Web Editor NEW

This project forked from canonical/raft

1.0 1.0 1.0 1.7 MB

C implementation of the Raft consensus protocol

License: Apache License 2.0

Emacs Lisp 0.02% Makefile 0.48% M4 3.27% C 94.16% C++ 1.90% Shell 0.17%

raft's Introduction

Build Status codecov

Fully asynchronous C implementation of the Raft consensus protocol.

The library has modular design: its core part implements only the core Raft algorithm logic, in a fully platform independent way. On top of that, a pluggable interface defines the I/O implementation for networking (send/receive RPC messages) and disk persistence (store log entries and snapshots).

A stock implementation of the I/O interface is provided when building the library with default options. It is based on libuv and should fit the vast majority of use cases. The only catch is that it currently requires Linux, since it uses the Linux AIO API for disk I/O. Patches are welcome to add support for more platforms.

See raft.h for full documentation.

Features

This implementation includes all the basic features described in the Raft dissertation:

  • Leader election
  • Log replication
  • Log compaction
  • Membership changes

It also includes a few optional enhancements:

  • Optimistic pipelining to reduce log replication latency
  • Writing to leader's disk in parallel
  • Automatic stepping down when the leader loses quorum

Building

autoreconf -i
./configure --enable-example
make

Example

The best way to understand how to use the library is probably reading the code of the example server included in the source code.

You can also see the example server in action by running:

./example/cluster

which spawns a little cluster of 3 servers, runs a sample workload, and randomly stops and restarts a server from time to time.

Quick guide

It is recommended that you read raft.h for documentation details, but here's a quick high-level guide of what you'll need to do (error handling is omitted for brevity).

Create an instance of the stock raft_io interface implementation (or implement your own one if the one that comes with the library really does not fit):

const char *dir = "/your/raft/data";
struct uv_loop_s loop;
struct raft_uv_transport transport;
struct raft_io io;
uv_loop_init(&loop);
raft_uv_tcp_init(&transport, &loop);
raft_uv_init(&io, &loop, dir, &transport);

Define your application Raft FSM, implementing the raft_fsm interface:

struct raft_fsm
{
  void *data;
  int (*apply)(struct raft_fsm *fsm, const struct raft_buffer *buf, void **result);
  int (*snapshot)(struct raft_fsm *fsm, struct raft_buffer *bufs[], unsigned *n_bufs);
  int (*restore)(struct raft_fsm *fsm, struct raft_buffer *buf);
}

Create an instance of the stock raft_logger interface implementation:

struct raft_logger logger;
raft_ring_logger_init(&logger, stdio);

Pick a unique ID and address for each server and initialize the raft object:

unsigned id = 1;
const char *address = "192.168.1.1:9999";
struct raft raft;
raft_init(&raft, &io, &fsm, &logger, id, address);

If it's the first time you start the cluster, create a configuration object containing each server that should be present in the cluster (typically just one, since you can grow your cluster at a later point using raft_add and raft_promote) and bootstrap:

struct raft_configuration configuration;
raft_configuration_init(&configuration);
raft_configuration_add(&configuration, 1, "192.168.1.1:9999", true);
raft_bootstrap(&raft, &configuration);

Start the raft server:

raft_start(&raft);
uv_run(&loop, UV_RUN_DEFAULT);

Asynchronously submit requests to apply new commands to your application FSM:

static void applyCb(struct raft_apply *req, int status, void *result) {
  /* ... */
}

struct raft_apply req;
struct raft_buffer buf;
buf.len = ...; /* The length of your FSM entry data */
buf.base = ...; /* Your FSM entry data */
raft_apply(&raft, &req, &buf, 1, apply_callback);

To add more servers to the cluster use the raft_add() and raft_promote APIs.

Notable users

Credits

Of course the biggest thanks goes to Diego Ongaro :) (the original author of the Raft dissertation)

A lot of ideas and inspiration was taken from other Raft implementations such as:

raft's People

Contributors

freeekanayaka avatar andreasstieger avatar

Stargazers

zhaoyao avatar

Watchers

James Cloos avatar

Forkers

zhaoyaogit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.