GithubHelp home page GithubHelp logo

doytsujin / ra Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rabbitmq/ra

0.0 1.0 0.0 18.22 MB

A Raft implementation for Erlang and Elixir that strives to be efficient and make it easier to use multiple Raft clusters in a single system.

License: Other

Makefile 0.14% Erlang 99.86%

ra's Introduction

A Raft Implementation for Erlang and Elixir

Ra is a Raft implementation by Team RabbitMQ. It is not tied to RabbitMQ and can be used in any Erlang or Elixir project. It is, however, heavily inspired by and geared towards RabbitMQ needs.

Ra (by virtue of being a Raft implementation) is a library that allows users to implement persistent, fault-tolerant and replicated state machines.

Project Maturity

This library has been extensively tested and is suitable for production use. This means the primary APIs (ra, ra_machine modules) and on disk formats will be backwards-compatible going forwards in line with Semantic Versioning. Care has been taken to version all on-disk data formats to enable frictionless future upgrades.

Status

The following Raft features are implemented:

  • Leader election
  • Log replication
  • Cluster membership changes: one server (member) at a time
  • Log compaction (with limitations and RabbitMQ-specific extensions)
  • Snapshot installation

Build Status

Build Status

Supported Erlang/OTP Versions

Ra supports the following Erlang/OTP versions:

  • 23.x
  • 22.x
  • 21.3.x (testing will be discontinued in September 2020)

22.x and later versions are highly recommended because of distribution traffic fragmentation.

Design Goals

  • Low footprint: use as few resources as possible, avoid process tree explosion
  • Able to run thousands of ra clusters within an Erlang node
  • Provide adequate performance for use as a basis for a distributed data service

Use Cases

This library is primarily developed as the foundation for replication layer for replicated queues in a future version of RabbitMQ. The design it aims to replace uses a variant of Chain Based Replication which has two major shortcomings:

  • Replication algorithm is linear
  • Failure recovery procedure requires expensive topology changes

Smallest Possible Usage Example

The example below assumes a few things:

  • You are familiar with the basics of distributed Erlang
  • Three Erlang nodes are started on the local machine or reachable resolvable hosts. Their names are [email protected], [email protected], and [email protected] in the example below but your actual hostname will be different. Therefore the naming scheme is ra{N}@{hostname}. This is not a Ra requirement so you are welcome to use different node names and update the code accordingly.

Erlang nodes can be started using rebar3 shell --name {node name}. They will have Ra modules on code path:

# replace hostname.local with your actual hostname
rebar3 shell --name [email protected]
# replace hostname.local with your actual hostname
rebar3 shell --name [email protected]
# replace hostname.local with your actual hostname
rebar3 shell --name [email protected]

After Ra nodes form a cluster, state machine commands can be performed.

Here's what a small example looks like:

%% The Ra application has to be started before it can be used.
ra:start(),

%% All servers in a Ra cluster are named processes on Erlang nodes.
%% The Erlang nodes must have distribution enabled and be able to
%% communicate with each other.
%% See https://learnyousomeerlang.com/distribunomicon if you are new to Erlang/OTP.

%% These Erlang nodes will host Ra nodes. They are the "seed" and assumed to
%% be running or come online shortly after Ra cluster formation is started with ra:start_cluster/3.
ErlangNodes = ['[email protected]', '[email protected]', '[email protected]'],

%% This will check for Erlang distribution connectivity. If Erlang nodes
%% cannot communicate with each other, Ra nodes would not be able to cluster or communicate
%% either.
[io:format("Attempting to communicate with node ~s, response: ~s~n", [N, net_adm:ping(N)]) || N <- ErlangNodes],

%% Create some Ra server IDs to pass to the configuration. These IDs will be
%% used to address Ra nodes in Ra API functions.
ServerIds = [{quick_start, N} || N <- ErlangNodes],

ClusterName = quick_start,
%% State machine that implements the logic
Machine = {simple, fun erlang:'+'/2, 0},

%% Start a Ra cluster  with an addition state machine that has an initial state of 0.
%% It's sufficient to invoke this function only on one Erlang node. For example, this
%% can be a "designated seed" node or the node that was first to start and did not discover
%% any peers after a few retries.
%%
%% Repeated startup attempts will fail even if the cluster is formed, has elected a leader
%% and is fully functional.
{ok, ServersStarted, _ServersNotStarted} = ra:start_cluster(ClusterName, Machine, ServerIds),

%% Add a number to the state machine.
%% Simple state machines always return the full state after each operation.
{ok, StateMachineResult, LeaderId} = ra:process_command(hd(ServersStarted), 5),

%% Use the leader id from the last command result for the next one
{ok, 12, LeaderId1} = ra:process_command(LeaderId, 7).

See Ra state machine tutorial for how to write more sophisiticated state machines by implementing the ra_machine behaviour.

A Ra-based key/value store example is available in a separate repository.

Documentation

Examples

Configuration Reference

Key Description Data Type
data_dir A directory name where Ra node will store its data Local directory path
wal_data_dir A directory name where Ra will store it's WAL (Write Ahead Log) data. If unspecified, `data_dir` is used. Local directory path
wal_max_size_bytes The maximum size of the WAL in bytes. Default: 512 MB Positive integer
wal_compute_checksums Indicate whether the wal should compute and validate checksums. Default: `true` Boolean
wal_write_strategy
  • default: used by default. write(2) system calls are delayed until a buffer is due to be flushed. Then it writes all the data in a single call then fsyncs. Fastest option but incurs some additional memory use.
  • o_sync: Like default but will try to open the file with O_SYNC and thus wont need the additional fsync(2) system call. If it fails to open the file with this flag this mode falls back to default.
Enumeration: default | o_sync
wal_sync_method
  • datasync: used by default. Uses the fdatasync(2) system call after each batch. This avoids flushing file meta-data after each write batch and thus may be slightly faster than sync on some system. When datasync is configured the wal will try to pre-allocate the entire WAL file. Not all systems support fdatasync. Please consult system documentation and configure it to use sync instead if it is not supported.
  • sync: uses the fsync system call after each batch.
Enumeration: datasync | sync
logger_module Allows the configuration of a custom logger module. The default is logger. The module must implement a function of the same signature as logger:log/4 (the variant that takes a format not the variant that takes a function). Atom
wal_max_batch_size Controls the internal max batch size that the WAL will accept. Higher numbers may result in higher memory use. Default: 32768. Positive integer
metrics_key Metrics key. The key used to write metrics into the ra_metrics table. Atom
low_priority_commands_flush_size When commands are pipelined using the low priority mode Ra tries to hold them back in favour of normal priority commands. This setting determines the number of low priority commands that are added to the log each flush cycle. Default: 25 Positive integer

Logging

Ra will use default OTP logger by default, unless logger_module configuration key is used to override.

To change log level to debug for all applications, use

logger:set_primary_config(level, debug).

Copyright and License

(c) 2017-2020, VMware Inc or its affiliates.

Double licensed under the ASL2 and MPL2.0. See LICENSE for details.

ra's People

Contributors

0xflotus avatar acogoluegnes avatar benoitc avatar dcorbacho avatar dumbbell avatar gerhard avatar hairyhum avatar kjnilsson avatar lbragaglia avatar lukebakken avatar mbj4668 avatar michaelklishin avatar mryawe avatar philipcristiano avatar spring-operator avatar timbuchwaldt avatar vanlightly avatar zambal avatar

Watchers

 avatar

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.