Categorygithub.com/borfi/dragonboat
modulepackage
2.1.1+incompatible
Repository: https://github.com/borfi/dragonboat.git
Documentation: pkg.go.dev

# README

dragonboat

Dragonboat - Go多组Raft库

license Build status travis-ci Go Report Card Godoc Join the chat at https://gitter.im/lni/dragonboat

关于

Dragonboat是一个高性能Go实现的多组Raft 共识算法库,它同时提供C++11支持。

Raft这样的共识算法使得只要系统中的多数成员在线便可使得系统持续运行。比如,一个拥有5台服务器的Raft集群中即使有两台服务器故障依旧可以工作。它同时向客户端展现一个单机节点,始终提供强一致保证的数据访存。同时,所有在线的成员节点都可用来提供读操作,从而提供更高的读吞吐总和。

所有Raft相关的技术难点都会由Dragonboat来承担,用户从而可以只关注他们各自的应用领域。Dragonboats使用十分简便,详细的例程可使新用户在半小时内完全掌握它。

功能

  • 便于使用的可构建单组与多组Raft应用的Go和C++ API
  • 功能完备的多组Raft协议的实现,同机支持数千Raft组
  • 完备的测试确保正确性,这包括Jepsen所带的Knossos强一致性检查,部分测试日志在此
  • 全流水线设计、TLS支持,适合被部署于跨地域的高网络延时公网环境
  • 在中档硬件上即可获得约300万/秒的写或1000万/秒的强一致读的性能
  • 支持定制的Raft log存储与Raft RPC模块,可方便整合最新IO类技术产品
  • 多Raft组由自带的Drummer服务器组件管理以获得良好的高可用

性能

Dragonboat是目前Github网站上最快的开源多组Raft实现。

在三节点系统上,使用中端硬件,如22核2.8Ghz的Intel志强处理器和NVME固态硬盘(具体信息在此),在16字节的荷载下,Dragonboat可持续每秒900万次写或在9:1的高读写比场景下提供每秒1100万次的混合读写操作。高吞吐在跨地域分布环境依旧被保持,在使用更多的clients的情况下,在RTT为30ms时依旧能实现200万次每秒的IO操作。 throughput

每个服务器上可轻易承载数千Raft组。并发的活跃Raft组数量对吞吐有直接影响,而大量的闲置Raft组对系统性能并无巨大影响。 nodes

下表是毫秒为单位的写延迟数据。Dragonboat可以在处理每秒800万次写(16字节荷载)的时候做到P99的写延迟小于5ms。读延迟低于写延迟,因为用于linearizable读的ReadIndex协议无需对每个读请求做落盘写。

每秒请求数荷载大小99.9% percentile99% percentile平均
100万162.241.190.79
100万12811.111.370.92
100万102471.6125.913.75
500万164.641.951.16
500万12836.616.551.96
800万1612.014.652.13

当测试单组性能时,Dragonboat可以在16字节负载下持续每秒完成125万次写,此时平均写延迟是1.3毫秒,P99写延迟为2.6毫秒。上述性能是在平均单机占用三个2.8Ghz的核心的情况下实现的。

即使在很高的系统负载下,Go的GC所带来的Stop-the-World停顿也显著低于1毫秒。在即将发布的Go 1.12版中,GC的Stop-the-World停顿时间将进一步大幅减低。Golang的runtime.ReadMemStats显示即使在很高的系统负载下,GC也仅占用了少于1%的可利用CPU时间。 stw

系统需求

  • x86_64 Linux或MacOS, Go 1.10, 1.11,支持C++11的近期版本GCC或Clang
  • RocksDB 5.13.4或更新的版本

开始使用

上列步骤使用的是Master branch的代码。Master是用于开发的非稳定branch。生产环境请使用已发布版本。

下载Dragonboat库至您的Go工作环境:

$ go get -u -d github.com/lni/dragonboat

请选择使用RocksDB还是LevelDB来存储Raft日志数据,建议使用RocksDB。

RocksDB

如果RocksDB 5.13.4或者更新版本尚未安装:

$ cd $GOPATH/src/github.com/lni/dragonboat
$ make install-rocksdb-ull

上述命令将把RocksDB 5.13.4安装到/usr/local/lib和/usr/local/include/rocksdb。如果RocksDB已经被安装则可跳过此步。

运行内建测试以检查安装是否完成:

$ cd $GOPATH/src/github.com/lni/dragonboat
$ make dragonboat-test

编译您自己的基于Dragonboat的应用:

CGO_CFLAGS="-I/path/to/rocksdb/include" CGO_LDFLAGS="-L/path/to/rocksdb/lib -lrocksdb" go build -v pkgname

LevelDB

使用LevelDB无额外安装步骤,运行上述同样的测试:

$ cd $GOPATH/src/github.com/lni/dragonboat
$ DRAGONBOAT_LOGDB=leveldb make dragonboat-test

或者编译同样该应用:

go build -v -tags="dragonboat_leveldb" pkgname

可选 - 装C++ Binding

C++ Binding的安装仅在您希望在C++项目中使用Dragonboat时才需要:

$ cd $GOPATH/src/github.com/lni/dragonboat
$ make binding
$ sudo make install-binding

运行C++ binding测试(需gtest库):

$ cd $GOPATH/src/github.com/lni/dragonboat
$ make clean
$ make test-cppwrapper

欢迎阅读或使用godoc文档中文例程常见问题和在线讨论组

中文例程

中文例程在这里

项目状态

Dragonboat适用于生产环境。

参与

报告bugs, 请提交一个issue。参与贡献改进及新功能, 请提交pull request并创建一个issue以便讨论与进度追踪。

开源许可协议

本项目以Apache License Version 2.0授权开源,请参考LICENSE文件。

本项目所使用的第三方代码与它们的开源许可证信息的列表在此

# Packages

Package client implements the client Session struct for identifying proposal clients and their progress.
Package config contains functions and types used for managing dragonboat's configurations.
Package drummer implements a reference Master server and client.
Package logger manages loggers used in dragonboat.
No description provided by the author
Package raftio contains structs, interfaces and function definitions required to build customized persistent Raft log storage and Raft RPC modules.
Package raftpb contains definitions of Raft internal structs, states and messages that are not visible to typical dragonboat applications.
Package statemachine contains the definition of the IStateMachine interface required to be implemented by dragonboat based applications.

# Functions

IsTempError returns a boolean value indicating whether the specified error is a temporary error that worth to be retried later with the exact same input, potentially on a more suitable NodeHost instance.
NewNodeHost creates a new NodeHost instance.
NewNodeHostWithMasterClientFactory creates a new NodeHost instance and uses the specified MasterClientFactoryFunc function to create the optional master client.

# Constants

DEVVersion is a boolean flag indicating whether this is a dev version.
DragonboatMajor is the major version number.
DragonboatMinor is the minor version number.
DragonboatPatch is the patch version number.

# Variables

ErrBadKey indicates that the key is bad, retry the request is recommended.
ErrCanceled indicates that the request has been canceled.
ErrClusterAlreadyExist indicates that the specified cluster already exist.
ErrClusterClosed indicates that the requested cluster is being shut down.
ErrClusterNotFound indicates that the specified cluster is not found.
ErrDeadlineNotSet indicates that the context parameter provided does not carry a deadline.
ErrInvalidClusterSettings indicates that cluster settings specified for the StartCluster method are invalid.
ErrInvalidDeadline indicates that the specified deadline is invalid, e.g.
ErrInvalidSession indicates that the specified client session is invalid.
ErrNoSnapshot is the error used to indicate that there is no snapshot available.
ErrPayloadTooBig indicates that the payload is too big.
ErrPendingConfigChangeExist indicates that there is already a pending membership change exist in the system.
ErrRejected indicates that the request has been rejected.
ErrSystemBusy indicates that the system is too busy to handle the request.
ErrSystemStopped indicates that the system is being shut down.
ErrTimeout indicates that the operation timed out.
ErrTimeoutTooSmall indicates that the specified timeout value is too small.
NodeHostInfoReportSecond defines how often NodeHost reports its info to optional Master servers.

# Structs

ClusterInfo provides Raft cluster details.
Membership is the struct used to describe Raft cluster membership query results.
NodeHost manages Raft clusters and enables them to share resources such as transport and persistent storage etc.
NodeHostInfo provides info about the NodeHost, including its managed Raft clusters and Raft logs saved in its local persistent storage.
RequestResult is the result struct returned for the request.
RequestState is the object used to provide request result to users.

# Interfaces

ICompleteHandler is a handler interface that will be invoked when the request in completed.
IMasterClient is the interface to be implemented for connecting NodeHosts with Master servers.
INodeUser is the interface implemented by a Raft node user type.

# Type aliases

MasterClientFactoryFunc is the factory function for creating a new IMasterClient instance.
RequestResultCode is the result code returned to the client to indicate the outcome of the request.