raft-concencous算法的纯python实现
pyraftlog的Python项目详细描述
pyraftlog是一个raft共识算法实现,它提供对共识日志的直接访问。
共识节点可以实例化为定义的模式,但也可以通过rpc更改其模式。 节点可以有三种模式:
- 激活
- 一个成熟的共识节点,如果一个领导层 超时。
- 不情愿
- 一个共识节点,在没有资格的情况下,它将不情愿地领导集群 候选人。只要有一个合格的活动节点,不情愿的节点就会取消其领导地位。
- 被动
- 一个共识节点,它将响应所有投票请求,但不会将自身转换为 候选人。此模式在测试或开发环境中可能有用,但不应使用 正在生产中。
一致性节点可以启用日志缩减,这在完整历史记录的某些情况下非常有用 不需要或将需要大量存储以保留。如果log_reduction设置为 True在state中,集群将自动将日志减少到最新的 last_applied在集群中共享的索引。将日志缩减设置为True将使 很难向集群添加其他节点
模拟服务器
有一个模拟服务器可以通过调用pyraftlog-mock:
来运行usage: pyraftlog-mock [-h] [-t {active,reluctant,passive}] -n NODE -b NEIGHBOURS [NEIGHBOURS ...] [-p PORT] [-r] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-f FILE] Run a mock localhost pyraftlog server optional arguments: -h, --help show this help message and exit -t {active,reluctant,passive}, --type {active,reluctant,passive} Type of the node -n NODE, --node NODE (host:)?port of this node. e.g. 7001 or node:7001 -b NEIGHBOURS [NEIGHBOURS ...], --neighbours NEIGHBOURS [NEIGHBOURS ...] Port(s) of neighbour -p PORT, --port PORT Port for receiving commands -r, --log-reduction Set log reduction to True -l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL} Logging level -f FILE, --file FILE Storage filename
创建ssl证书(用于模拟)
以下命令集(在./certs/中执行)将创建一个用于在本地主机上运行模拟群集的ca和设备证书集:
# Only do once: generate the root CA key: > openssl genrsa -out transport-ca.key 4096 # Generate the root CA certificate: ## Country Name (2 letter code) []:GB ## State or Province Name (full name) []:. ## Locality Name (eg, city) []:. ## Organization Name (eg, company) []:. ## Organizational Unit Name (eg, section) []:. ## Common Name (eg, fully qualified host name) []:PyRaftLog ## Email Address []:. > openssl req -x509 -new -nodes -key transport-ca.key -sha256 -days 1024 -out transport-ca.pem # Generate device certificates # Only do once: generate device key: > openssl genrsa -out transport-consensus.key 4096 # Generate device certificate signing request: ## Country Name (2 letter code) []:GB ## State or Province Name (full name) []:. ## Locality Name (eg, city) []:. ## Organization Name (eg, company) []:. ## Organizational Unit Name (eg, section) []:. ## Common Name (eg, fully qualified host name) []:localhost ## Email Address []:. > openssl req -new -key transport-consensus.key -out transport-consensus.csr # Generate a signed device certificate: > openssl x509 -req -in transport-consensus.csr -CA transport-ca.pem -CAkey transport-ca.key -CAcreateserial -out transport-consensus.crt -days 500 -sha256