1 Star 0 Fork 3

孙宇 / jraft

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

jraft

项目地址

https://gitee.com/XDsun-yu/jraft

语雀文档

https://www.yuque.com/yuqueyonghuofhbn2/okk7bs

具体功能

  • Leader选举
  • 日志复制
  • 成员变更:已实现单节点变更,正在完善ing,等待同步到Git
  • 日志压缩:未实现
  • 分布式KV存储:采用RocksDB作为状态机,正在完善ing,等待同步到Git

日志压缩更加依赖于具体状态机的实现,可能会在以后的版本中完善

运行方式

运行jraft-stack-example的ClientTest

技术选型

日志存储

由于需要将日志进行持久化,如果我们直接对磁盘IO,效率会十分低下,因此选用RocksDB来实现日志存储的功能。RocksDB是基于LSM的存储引擎,LSM是一种数据结构,能够将写入操作顺序化,大大提高了写数据的性能。

RPC

由于节点之间的通信较为频繁,因此我们选用长连接。对于长连接,一般都使用netty或者其他基于netty的框架。在此我选用SOFAbolt,蚂蚁金融开源的一套基于netty的网络通信框架。

模块划分

  • LogModule:负责日志条目的读写
  • ConsensusModule:处理来自其他节点的追加日志以及投票请求
  • StateMachine:状态机的接口,不同状态机可以有不同的实现
  • RPC:实现不同节点之间的通信
  • Node:上述功能的聚合体,作为系统中的单个节点
  • LifeCycle:管理各个模块的生命周期

Model

img

  • DTO

    • Request

      • AppendEntriesRequest: 追加日志请求的参数的封装
      • VoteRequest: 投票请求的参数的封装
      • ClientRequest: 客户端请求的参数的封装
    • Response

      • AppendEntriesResponse: 追加日志响应的结果的封装
      • VoteReponse: 请求投票响应的结果的封装
      • ClientReponse: 对客户端响应的结果的封装
  • State: 对应原论文中的State

    • PersistentState

    • VolatileState

    • LeaderState

  • LogEntry: 日志条目的封装

  • Peer: 单个节点的配置

  • PeerGroup: 节点的集群配置

基本逻辑

处理client请求

img

  1. 判断Node状态,如果是Leader,继续进行处理,否则直接将请求转发给Leader(这里与论文中稍有区别,论文中应是直接返回false以及leader的地址,由client再次请求leader节点,这里为了减少一轮RPC,直接在节点出重定向)
  2. 根据请求生成日志条目并写入到日志模块
  3. 向其他节点发送追加日志请求,这里需要利用线程池并发调用RPC,同时,还需要给线程池分配获取RPC结果的任务,除此之外,main线程中还需要设置一定的等待时间来等待线程池中的任务执行完成
  4. 为了保证整个系统重启之后能够恢复原来的状态,因此在获取到结果之后,先对commitIndex进行一次更新,对应论文中的5.3和5.4,规则为:假设存在N满足N>commitIndex,使得大多数的matchIndex[i]≥N以及log[N].term == currentTerm成立,则令commitIndex == N。更新完毕之后,将commitIndex提交到commitChannel中,应用到状态机。
  5. 判断是否满足了超过半数的节点确认,如果是,再次更新commitIndex为当前logEntry的index,并进行提交,否则直接返回false
  6. 等待状态机应用后返回结果

Leader的追加日志实现

img

  1. 判断运行时间是否超出0.5s,未超时则继续
  2. 初始化任期,leaderId,leaderCommit
  3. 从日志模块读取从nextIndex到curLogEntry' s Index的日志
  4. 初始化prevLogIndex和prevLogTerm
  5. 调用RPC模块的send,向目标节点发送请求
  6. 同步等待RPC结果,如果成功直接返回true,如果失败还需判断目标节点的任期是否高于当前节点的任期,如果比当前节点高,则当前节点转变为follower,否则递减nextIndex回到步骤1继续重试

接收者追加日志的实现

img

  1. 尝试获取锁
  2. 比较leader的任期和当前节点的任期,如果当前节点的任期较高,则直接返回false
  3. 更新收到心跳的时间,当前的任期,节点的状态以及leaderId
  4. 如果日志为空,则为心跳包,直接返回即可
  5. 判断即将写入的位置是否有日志,并且是否和需要写入的日志发生冲突,如果发生冲突,则删除冲突位置及其之后的所有日志
  6. 追加日志
  7. 根据请求参数中的leaderCommit重设接收者的commitIndex
  8. 根据commitIndex进行提交
  9. 最终返回true并解锁

Follower超时选举的实现

img

  1. 线程池定时执行该任务
  2. 当前节点是否为leader,如果是,直接返回
  3. 当前时间-上一次收到心跳的时间是否超时(超时时间随机生成),如果不超时,直接返回
  4. 任期号自增,转变为candidate,投票给自己
  5. 调用RPC,向其他节点请求投票
  6. 收到结果后,再次判断当前节点是否处于candidate,如果不是,直接返回
  7. 如果大多数节点确认了投票请求,当前节点转变为leader,清除自己的投票状态,更新matchIndex和nextIndex,否则,清除自己的投票状态并返回,等待下一次选举

接收者请求投票的实现

img

  1. 尝试获取锁,这里是为了保证先来先服务,如果当前节点的锁已经被获取,则证明当前节点已经为别人投过票了
  2. 比较candidate的任期和当前节点的任期,如果当前节点的任期较高,则直接返回false
  3. 如果votedFor为空或者为candidateId,并且candidate的日志比当前节点的日志新,那么当前节点就把票投给该candidate
  4. 最终释放锁

多线程

为什么程序中很少使用锁,而基本都使用volatile?

Raft具有很强的容错机制,由于Quorum和term的设定,就算出现了leader在追加日志时变为了follower的情况,接收者也能通过比较任期来拒绝追加,所以Raft对线程安全的要求性不高,只需要保证共享资源在多个线程中的可见性即可。

异常处理

在多线程环境下,无法对单个线程进行try catch处理,正确的做法是定义异常处理器,即实现UncaughtExceptionHandler接口,重写其中的uncaughtException方法

栈计算器

栈计算器在原框架的基础上实现了状态机,在状态机内部维护一个Stack,通过来自client的请求进行字符串解析,从而实现不同的操作。

支持的指令:

  • create:创建一个栈计算器实例(即分布式系统中的一个节点)
  • delete:删除对应id的节点
  • push:向对应节点的栈状态机中压入值
  • pop:对应节点的栈状态机弹栈,并返回该值
  • get:返回栈顶元素
  • add:将对应节点的栈状态机的两个栈顶元素出栈并作为操作数相加,结果再次入栈
  • sub:将对应节点的栈状态机的两个栈顶元素出栈并作为操作数相减,结果再次入栈
  • mul:将对应节点的栈状态机的两个栈顶元素出栈并作为操作数相乘,结果再次入栈
  • div:将对应节点的栈状态机的两个栈顶元素出栈并作为操作数相除,结果再次入栈
  • inc:将对应节点的栈状态机的栈顶整数弹栈,自增后再次入栈
  • dec:将对应节点的栈状态机的栈顶整数弹栈,自减后再次入栈

需要改进的地方

  • RPC的等待时间,超时时间都是硬编码,用户无法自定义
  • 心跳包无法更新follower的commitIndex,只能等待下一次追加日志时才能把上一次的追加的日志进行提交
  • 当candidate竞选成功时,无法保证立马发送心跳包,只能等待下一次定时心跳任务的触发
  • 每次只能处理单个用户的一条请求,当QPS到达一定数量级时,可以先将用户请求放置于队列中,每隔一定时间再取出,进行批量追加

引用

在刚开始实现Raft协议时,查找了许多开源Raft框架的底层实现,本框架借鉴了其中的一些内容,推荐几个比较完整的框架:

SOFAJRaft:

蚂蚁金融开源的一套Raft框架,其中还实现了一套分布式存储框架

官网:https://www.sofastack.tech/projects/sofa-jraft/overview/

github地址:https://github.com/sofastack/sofa-jraft

TiDB:

PingCAP的一款分布式关系型数据库,底层同样采用Raft协议

官网:https://pingcap.com/zh/product/

github地址:https://github.com/pingcap/tidb

lu-raft-kv:

GitHub上开源的一套基于Raft的分布式存储实现,比较贴合论文

github地址:https://github.com/stateIs0/lu-raft-kv

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

java实现raft协议 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/XDsun-yu/jraft.git
git@gitee.com:XDsun-yu/jraft.git
XDsun-yu
jraft
jraft
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891