oraft

Raft consensus algorithm implemented in OCaml
README

Library of Raft consensus algorithm implemented in OCaml

Current Status

TODO

  • Cluster membership changes

  • Log compaction

Requirement

  • opam

  • dune

Build

$ opam install --deps-only .
$ dune build

Install

$ opam install .

Test

Unit test

$ dune runtest

Smoke test

This repository has an example Raft application that is a simple KVS as described below. The following command runs 5 KVS servers and a verification tool that sequentially restarts at most 2 servers.

$ smoke_test/run.sh

Chaos test

This repository has an example Raft application that is a simple KVS as described below. The following command runs 5 KVS servers, a verification tool and chaos testing tool in docker containers. The 5 KVS servers will randomly experience pause and network delay.

$ chaos_test/run.sh

Usage

Create a config file for each Raft application

{
    "node_id": 4,
    "nodes": [
        {"id": 1, "host": "localhost", "port": 7891},
        {"id": 2, "host": "localhost", "port": 7892},
        {"id": 3, "host": "localhost", "port": 7893},
        {"id": 4, "host": "localhost", "port": 7894},
        {"id": 5, "host": "localhost", "port": 7895}
    ],
    "log_file": "oraft.log",
    "log_level": "INFO",
    "state_dir": "state",
    "election_timeout_millis": 300,
    "heartbeat_interval_millis": 50
}

node_id needs to be modified for each node.

Write an application using ORaft

The following code is a very simple application that uses ORaft.

open Lwt

let main =
  let oraft =
    Oraft.start ~conf_file:"/path/to/oraft-config.json" ~apply_log:(fun i s ->
      Printf.printf
        "Received %d th command. Maybe you'd better take care of '%s' instead of just printing\n" i s;
      flush stdout
    )
  in
  let rec loop () = Lwt_io.read_line Lwt_io.stdin
    >>= fun s -> oraft.post_command s
    >>= fun result -> Lwt_io.printl (if result then "OK" else "ERR")
    >>= fun () -> loop ()
  in
  Lwt.join [ loop (); oraft.process ] |> Lwt_main.run

Example

This repo contains example project that is a simple KVS.

Run a cluster on multi processes

You can execute the project like this:

$ ./example/run_all.sh

5 Raft application processes will start.

And then, you can send a request using curl command or something

$ curl -X POST --data-binary 'SET a hello' http://localhost:8181/command
$ curl -X POST --data-binary 'GET a' http://localhost:8182/command
hello
$ curl -X POST --data-binary 'SET b 42' http://localhost:8183/command
$ curl -X POST --data-binary 'GET b' http://localhost:8184/command
42
$ curl -X POST --data-binary 'INCR b' http://localhost:8185/command
$ curl -X POST --data-binary 'GET b' http://localhost:8181/command
43
Install
Published
15 Nov 2020
Sources
0.1.1.tar.gz
md5=e0b530b3b1553f729d7c58a764f116f4
sha512=c27aae4ca8a7c4506e11cb86cc10468086443433a9c410ef98ec3efccd0dec5c548aa858b8c07d0347a1c99a83781a0eb7239d0445457813b3bfdb00a404706c
Dependencies
Reverse Dependencies