• Stars
    star
    852
  • Rank 53,494 (Top 2 %)
  • Language
    C
  • License
    Other
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C implementation of the Raft consensus protocol

CI Tests codecov Documentation Status

English|简体中文

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.

License

This raft C library is released under a slightly modified version of LGPLv3, that includes a copyright exception letting users to statically link the library code in their project and release the final work under their own terms. See the full license text.

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
  • Leadership transfer extension
  • Pre-vote protocol

Install

If you are on a Debian-based system, you can get the latest development release from dqlite's dev PPA:

sudo add-apt-repository ppa:dqlite/dev
sudo apt-get update
sudo apt-get install libraft-dev

Building

To build libraft from source you'll need:

  • A reasonably recent version of libuv (v1.18.0 or beyond).
  • Optionally, but recommended, a reasonably recent version of liblz4 (v1.7.1 or beyond).
sudo apt-get install libuv1-dev liblz4-dev libtool pkg-config build-essential
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);
}

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, 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 apply_callback(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.

Usage Notes

The default libuv based raft_io implementation compresses the raft snapshots using the liblz4 library. Next to saving disk space, the lz4 compressed snapshots offer additional data integrity checks in the form of a Content Checksum, this allows raft to detect corruptions that occurred during storage. It is therefore recommended to not disable lz4 compression by means of the --disable-lz4 configure flag.

Detailed tracing will be enabled when the environment variable LIBRAFT_TRACE is set upon startup.

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:

More Repositories

1

microk8s

MicroK8s is a small, fast, single-package Kubernetes for datacenters and the edge.
Python
8,492
star
2

multipass

Multipass orchestrates virtual Ubuntu instances
C++
7,204
star
3

lxd

Powerful system container and virtual machine manager
Go
4,335
star
4

dqlite

Embeddable, replicated and fault tolerant SQL engine.
C
3,472
star
5

cloud-init

Official upstream for the cloud-init: cloud instance initialization
Python
2,963
star
6

snapd

The snapd and snap tools enable systems to work with .snap files.
Go
1,894
star
7

snapcraft

Package, distribute, and update any app for Linux and IoT.
Python
1,178
star
8

lightdm

Display Manager
C
825
star
9

vanilla-framework

From community websites to web applications, this CSS framework will help you achieve a consistent look and feel.
HTML
723
star
10

netplan

Declarative network configuration for various backends
Python
671
star
11

ubuntu-desktop-installer

Ubuntu Desktop Installer
Dart
537
star
12

subiquity

Ubuntu Server Installer, and backend for Ubuntu Desktop Installer
Python
481
star
13

go-dqlite

Go bindings for libdqlite
Go
358
star
14

maas

Official MAAS repository mirror (may be out of date). Development happens in Launchpad (https://git.launchpad.net/maas/).
Python
350
star
15

microcloud

Automated private cloud based on LXD, Ceph and OVN
Go
267
star
16

chisel

Go
263
star
17

lxd-ui

Easy and accessible container and virtual machine management. A browser interface for LXD
TypeScript
260
star
18

pylxd

Python module for LXD
Python
251
star
19

operator

Pure Python framework for writing Juju charms
Python
245
star
20

microceph

MicroCeph is snap-deployed Ceph with built-in clustering
Go
211
star
21

ubuntu.com

The official website for the Ubuntu operating system
HTML
189
star
22

packer-maas

Packer templates to create MAAS deployable images
Python
172
star
23

pebble

Take control of your internal daemons!
Go
139
star
24

snapcraft.io

The official website's repository for the Snap store
JavaScript
129
star
25

prometheus-openstack-exporter

OpenStack exporter for the prometheus monitoring system
Python
115
star
26

firmware-updater

An experimental firmware updater UI based on fwupd
Dart
109
star
27

ubuntu-core-desktop

Makefile
102
star
28

ubuntu-image

Official tool to build Ubuntu Images.
Go
100
star
29

bundle-kubeflow

Charmed Kubeflow
Python
98
star
30

cloud-utils

This package provides a useful set of utilities for interacting with a cloud.
Shell
91
star
31

dbus.dart

Native Dart client library to use DBus.
Dart
90
star
32

react-components

A set of components based on Vanilla Framework
TypeScript
88
star
33

sec-cvescan

Analyzes an Ubuntu system and checks for unpatched vulnerabilities.
Python
87
star
34

ubuntu-image-legacy

Legacy Python-based Ubuntu image building scripts. Please see Canonical/ubuntu-image instead. Report bugs at Launchpad.
Python
86
star
35

lxd-demo-server

The LXD demo server
JavaScript
75
star
36

Ubuntu-Sans-Mono-fonts

Python
71
star
37

tdx

Intel confidential computing - TDX
Python
69
star
38

open-documentation-academy

Learn open-source software documentation skills with Canonical
Shell
69
star
39

microk8s-community-addons

The add-ons shipped with MicroK8s
Python
67
star
40

charmcraft

Collaborate, build and publish charmed operators for Kubernetes, Linux and Windows.
Python
66
star
41

steam-snap

Steam as a snap
Python
66
star
42

ubuntu-desktop-provision

Ubuntu Desktop Provision
Dart
64
star
43

matter-pi-gpio-commander

Matter Raspberry Pi GPIO Commander - Turn your Pi into a Matter lighting device!
C++
63
star
44

python-libmaas

Unofficial python client library for MAAS
Python
62
star
45

terraform-provider-maas

Terraform MAAS provider
Go
60
star
46

design-vanilla-framework

Design components for Vanilla Framework.
60
star
47

autoinstall-desktop

Python
59
star
48

curtin

Official mirror of curtin project. Please direct merge proposals to https://code.launchpad.net/curtin
Python
58
star
49

Ubuntu-Sans-fonts

Python
56
star
50

etrace

Utility for tracing execution of apps
Go
55
star
51

multipass-blueprints

Blueprint definitions for [`multipass launch`](https://multipass.run)
54
star
52

ubuntu-pro-client

Ubuntu Pro Client for offerings from Canonical
Python
52
star
53

bluez.dart

Dart
51
star
54

jhack

Chock-full of Juju hackery.
Python
51
star
55

ubuntu-flutter-plugins

A collection of Flutter plugins and packages for Ubuntu applications.
Dart
48
star
56

landscape-client

The Landscape Client is the agent which communicates with the Landscape service.
Python
47
star
57

wizard_router

A classic linear wizard router for Flutter
Dart
47
star
58

desktop_notifications.dart

Native Dart client library to send Linux desktop notifications.
Dart
44
star
59

maas-ansible-playbook

An Ansible playbook for installing and configuring MAAS
Jinja
44
star
60

microk8s-core-addons

Core MicroK8s addons
Python
43
star
61

k8s-snap

Canonical Kubernetes is an opinionated and CNCF conformant Kubernetes operated by Snaps and Charms, which come together to bring simplified operations and an enhanced security posture on any infrastructure.
Go
42
star
62

nrpe_exporter

Prometheus NRPE exporter. This exporter exposes metrics on commands sent to a running NRPE daemon.
Go
42
star
63

candid

Identity Manager Service
Go
39
star
64

ubuntu-drivers-common

Python
38
star
65

workshops

Workshops
Dart
34
star
66

k8s-dqlite

Dqlite for Kubernetes
Go
34
star
67

rockcraft

Tool to create OCI Images using the language from Snapcraft and Charmcraft.
Python
34
star
68

chisel-releases

34
star
69

pi-gadget

Universal pi (pi2,pi3,pi4,cm3,cm4 and future) gadget snap for Ubuntu Core and classic systems.
Makefile
33
star
70

maas-ui

The UI for MAAS (metal-as-a-service)
TypeScript
32
star
71

ubuntu-cooker

Ubuntu WSL UWP Builder
PowerShell
31
star
72

pc-gadget

The gadget snap for Personal Computers using 64bit Intel or AMD processors
31
star
73

kubeflow-examples

Charmed Kubeflow examples
Jupyter Notebook
31
star
74

snapd.dart

Native Dart client to access snapd
Dart
31
star
75

hotsos

Software analysis toolkit. Define checks in high-level language and leverage library to perform analysis of common Cloud applications.
Python
31
star
76

autoinstall-generator

Utility to convert Debian Installer preseed to Subiquity answers
Python
31
star
77

checkbox

Checkbox
Python
30
star
78

nm.dart

Native Dart client library to access NetworkManager on Linux.
Dart
29
star
79

landscape-scripts

A collection of scripts to make Landscape more powerful
Shell
29
star
80

microovn

Snap based deployment of OVN
Go
26
star
81

colcon-in-container

Colcon extension to build a colcon workspace in a container
Python
25
star
82

sync-issues-github-jira

Automation to sync issues from Github (using Github actions) to Jira (via Jira webhooks)
25
star
83

models

Reference Models
Python
24
star
84

core-base

The main repository for ubuntu-core base snaps. It contains snaps from core22+.
Shell
24
star
85

charms.reactive

Framework for developing charms and relations using the reactive pattern
Python
22
star
86

probert

Prober tool - Hardware discovery library used in Subiquity
Python
22
star
87

apport

Apport intercepts Program crashes, collects debugging information about the crash and the operating system environment, and sends it to bug trackers in a standardized form. It also offers the user to report a bug about a package, with again collecting as much information about it as possible.
Python
22
star
88

go-tpm2

Native go library for interacting with TPM 2.0 devices
Go
22
star
89

iot-agent

IoT Device Management Agent
Go
21
star
90

prometheus-k8s-operator

This charmed operator automates the operational procedures of running Prometheus, an open-source metrics backend.
Python
21
star
91

dotrun

Python
21
star
92

microcluster

dqlite cluster management using go-dqlite
Go
21
star
93

maas-commissioning-scripts

A repository of example MAAS commissioning scripts
Shell
20
star
94

setup-lxd

A GitHub Action to install & configure LXD on a runner.
20
star
95

desktop-design

HTML
19
star
96

gomaasclient

Go MAAS client
Go
19
star
97

microk8s-addons-repo-template

Template repository for MIcroK8s addons
Python
19
star
98

imagecraft

Customize and build bootable Ubuntu images.
Python
18
star
99

pycloudlib

Python library to launch, interact and, snapshot cloud instances
Python
18
star
100

serial-vault

Go
18
star