当前位置:网站首页>KV database based on raft consensus protocol
KV database based on raft consensus protocol
2022-07-26 08:33:00 【biyezuopin】
be based on Raft Consensus agreement KV database
Project introduction
Distributed consistency is the basis of building fault-tolerant systems , It enables some machines to work in clusters , And allow some of these nodes to fail .Raft It is a common distributed consensus protocol ,Raft First, elect a server As Leader, Then give it full responsibility for managing logs .Leader Receive log entries from the client , Copy to other server, And tell them when they can safely apply log entries to their state machine . To have a Leader Can be simplified replicated log Management of . for example ,leader You can decide where to put new log entries , Without asking other nodes , Data is always simple from leader Flow to other nodes .Leader May fail or disconnect , In this case, a new one will be selected leader. Specific algorithm details can refer to the paper 《In search of an Understandable Consensus Algorithm》.
This project is based on Raft Distributed consensus protocol , Implement a simple KV database . This database supports 3 Operations , namely GET、PUT、APPEND.GET Operation can get a Key Corresponding Value.PUT Operation can make a Key Corresponding Value write in .APPEND The operation can set Value Append a written Key Corresponding Value On .
System architecture

every last server There is a log that stores a series of instructions ,state machine Will execute these instructions in sequence . Each log holds the same instructions in the same order , So every one state machine Process the same instructions ,state machine It's the same , Therefore, it will eventually reach the same state and output .
Guarantee replicated log Consistency is the task of consistency algorithm .server The consistency module in receives the instructions from the client and adds them to its own log , It can also be compared with other server The consistency module in the communication to ensure that every log Can have the same content and order , Even if some of them server Downtime . Once the instruction is copied correctly , It can be called committed. every last server The state machine in is processed in log order committed Instructions , And return the output to the client .
Source code analysis
Client code :

With GET Operation as an example . Because in a cluster , Only to be Leader Our server can handle user requests , So the client will cache when sending requests to the server Leader Information about . If the error returned is NotLeader, Will try again , Send the request to other servers .
Server code :


The same to GET Operation as an example , The server is receiving Get When asked , Will check whether you are Leader, If so, register a callback function . In another cycle , The server will listen successfully Commit Of Raft journal , And back to GET The result to the client .
Raft Module code :
Raft Modules are the most complex place , It implements the description in the reference paper :

stay Raft layer , There are three RPC call , namely sendRequestVote, sendAppendEntries, sendInstallSnapshot. among sendRequestVote For election Leader,sendAppendEntries Used to synchronize Raft Log and send heartbeat sendInstallSnapshot For fast recovery .
Test part
In order to verify Raft The correctness of the implementation of distributed consensus algorithm has enabled the server to correctly handle requests from clients , There are a lot of test cases in the project . These tests simulate various server outages , Network isolation . The KV The database can all pass these tests .
The test will simulate various server downtime , Network isolation . The KV The database can all pass these tests .
边栏推荐
- 22-07-16 personal training match 3 competition experience
- awk作业
- 有点牛逼,一个月13万+
- 基于C#实现的文件管理文件系统
- BGP routing principle
- Redis advanced
- Automation and disconnection monitoring of video addition
- Error handling response: Error: Syntax error, unrecognized expression: .c-container /deep/ .c-contai
- Summary of common skills
- 请问现在flinkcdc支持sqlserver实例名方式连接吗?
猜你喜欢

【时间复杂度空间复杂度】

Bee guitar score high octave and low octave

基于C语言设计的换乘指南打印系统

Flitter imitates wechat long press pop-up copy recall paste collection and other custom customization

【EndNote】文献模板编排语法详解

基于C语言的哈夫曼转化软件

22-07-12 personal training match 1 competition experience

Date and time function of MySQL function summary

吉他五线谱联系 茉莉花

QT note 1
随机推荐
Flex three column layout
[endnote] compilation of document types and abbreviations of document types
Guitar staff link Jasmine
memorandum...
Alphabetic string
23.10 Admin features
苹果强硬新规:用第三方支付也要抽成,开发者亏大了!
Write common API tools swagger and redoc
Lesson 3: gcc compiler
Fluent custom popupmenubutton
Dev gridcontrol captures key events
Redis advanced
Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)
BGP routing principle
The full name of flitter IDFA is identity for advertisers, that is, advertising identifiers. It is used to mark users. At present, it is most widely used for advertising, personalized recommendation,
matplotlib学习笔记
mysql函数汇总之日期和时间函数
Common Oracle functions
Day 4 homework
基于C语言的哈夫曼转化软件