• Stars
    star
    852
  • Rank 51,309 (Top 2 %)
  • Language
    C
  • License
    Other
  • Created over 5 years ago
  • Updated 10 months 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,116
star
2

multipass

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

lxd

Powerful system container and virtual machine manager
Go
4,213
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,591
star
6

snapcraft

Package, distribute, and update any app for Linux and IoT.
Python
1,128
star
7

lightdm

Display Manager
C
791
star
8

vanilla-framework

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

netplan

Declarative network configuration for various backends
Python
594
star
10

ubuntu-desktop-installer

Ubuntu Desktop Installer
Dart
537
star
11

subiquity

Ubuntu Server Installer, and backend for Ubuntu Desktop Installer
Python
451
star
12

go-dqlite

Go bindings for libdqlite
Go
358
star
13

pylxd

Python module for LXD
Python
252
star
14

microcloud

Automated private cloud based on LXD, Ceph and OVN
Go
241
star
15

operator

Pure Python framework for writing Juju charms
Python
238
star
16

lxd-ui

Easy and accessible container and virtual machine management. A browser interface for LXD
TypeScript
208
star
17

chisel

Go
193
star
18

packer-maas

Packer templates to create MAAS deployable images
Python
172
star
19

microceph

Ceph for a one-rack cluster and appliances
Go
170
star
20

ubuntu.com

The official website for the Ubuntu operating system
HTML
164
star
21

pebble

Take control of your internal daemons!
Go
131
star
22

snapcraft.io

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

prometheus-openstack-exporter

OpenStack exporter for the prometheus monitoring system
Python
115
star
24

firmware-updater

An experimental firmware updater UI based on fwupd
Dart
104
star
25

bundle-kubeflow

Charmed Kubeflow
Python
92
star
26

dbus.dart

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

ubuntu-core-desktop

Makefile
88
star
28

ubuntu-image-legacy

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

sec-cvescan

Analyzes an Ubuntu system and checks for unpatched vulnerabilities.
Python
84
star
30

cloud-utils

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

ubuntu-image

Official tool to build Ubuntu Images.
Go
77
star
32

lxd-demo-server

The LXD demo server
JavaScript
74
star
33

steam-snap

Steam as a snap
Python
66
star
34

matter-pi-gpio-commander

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

design-vanilla-framework

Design components for Vanilla Framework.
60
star
36

charmcraft

Collaborate, build and publish charmed operators for Kubernetes, Linux and Windows.
Python
58
star
37

etrace

Utility for tracing execution of apps
Go
55
star
38

multipass-blueprints

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

microk8s-community-addons

The add-ons shipped with MicroK8s
Shell
54
star
40

Ubuntu-Sans-Mono-fonts

Python
51
star
41

bluez.dart

Dart
51
star
42

react-components

A set of components based on Vanilla Framework
TypeScript
50
star
43

curtin

Official mirror of curtin project
Python
50
star
44

jhack

Chock-full of Juju hackery.
Python
48
star
45

ubuntu-pro-client

Ubuntu Pro Client for offerings from Canonical
Python
47
star
46

wizard_router

A classic linear wizard router for Flutter
Dart
47
star
47

desktop_notifications.dart

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

landscape-client

The Landscape Client is the agent which communicates with the Landscape service.
Python
42
star
49

Ubuntu-Sans-fonts

Python
42
star
50

nrpe_exporter

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

candid

Identity Manager Service
Go
39
star
52

ubuntu-flutter-plugins

A collection of Flutter plugins and packages for Ubuntu applications.
Dart
38
star
53

workshops

Workshops
Dart
34
star
54

microk8s-core-addons

Core MicroK8s addons
Python
33
star
55

open-documentation-academy

Learn open-source software documentation skills with Canonical
33
star
56

maas-ui

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

ubuntu-cooker

Ubuntu WSL UWP Builder
PowerShell
31
star
58

autoinstall-desktop

Python
31
star
59

nm.dart

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

hotsos

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

autoinstall-generator

Utility to convert Debian Installer preseed to Subiquity answers
Python
29
star
62

ubuntu-desktop-provision

Ubuntu Desktop Provision
Dart
29
star
63

k8s-dqlite

Dqlite for Kubernetes
Go
27
star
64

landscape-scripts

A collection of scripts to make Landscape more powerful
Shell
27
star
65

snapd.dart

Native Dart client to access snapd
Dart
26
star
66

microovn

Snap based deployment of OVN
Go
25
star
67

kubeflow-examples

Charmed Kubeflow examples
Jupyter Notebook
25
star
68

checkbox

Checkbox
Python
24
star
69

go-tpm2

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

iot-agent

IoT Device Management Agent
Go
21
star
71

dotrun

Python
21
star
72

probert

Prober tool - Hardware discovery library used in Subiquity
Python
21
star
73

sync-issues-github-jira

Automation to sync issues from Github (using Github actions) to Jira (via Jira webhooks)
20
star
74

prometheus-k8s-operator

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

microcluster

dqlite cluster management using go-dqlite
Go
19
star
76

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
19
star
77

colcon-in-container

Colcon extension to build a colcon workspace in a container
Python
18
star
78

pycloudlib

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

desktop-design

HTML
18
star
80

canonical.com

Repository for the new version of canonical.com
HTML
18
star
81

flutter-snap

CMake
18
star
82

udisks.dart

Native Dart client library to access UDisks service on Linux.
Dart
18
star
83

gsettings.dart

Native Dart client library to access GSettings.
Dart
17
star
84

anbox-cloud.io

Anbox cloud demo site
HTML
17
star
85

iot-management

IoT Management Service
Go
17
star
86

microk8s-addons-repo-template

Template repository for MIcroK8s addons
Python
17
star
87

rockcraft

Tool to create OCI Images using the language from Snapcraft and Charmcraft.
Python
17
star
88

chisel-releases

17
star
89

maas-commissioning-scripts

A repository of example MAAS commissioning scripts
Shell
16
star
90

eks-snap

Single-package EKS Distro
Python
16
star
91

upower.dart

Native Dart client library to access UPower service on Linux.
Dart
16
star
92

ubuntu-desktop-hyper-v

Ubuntu Desktop Hyper-V Quick Create Gallery
Shell
16
star
93

maas.io

Site for maas.io
HTML
15
star
94

setup-lxd

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

libssh

mulitplatform C library implementing the SSHv2 and SSHv1 protocol on client and server side http://libssh.org
C
15
star
96

packagekit.dart

Native Dart client library to access PackageKit.
Dart
15
star
97

charm-relation-interfaces

Opinionated and standardized interface specifications for charmed operator relations.
Python
15
star
98

ubuntu-wsl-integration

Ubuntu WSL Integrations
Python
14
star
99

microk8s.io

Code for the microk8s.io website by Canonical
HTML
14
star
100

sqlair

Friendly type mapping for SQL databases
Go
14
star