jraft-core

Java implementation of Raft Consensus, this module contains only the core algorithm, and does not have any dependency except Java SE 8, for interface implementations that required by this module, please checkout jraft-ext module

License

License

Categories

Categories

Data
GroupId

GroupId

com.github.datatechnology.jraft.core
ArtifactId

ArtifactId

jraft-core
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

jraft-core
Java implementation of Raft Consensus, this module contains only the core algorithm, and does not have any dependency except Java SE 8, for interface implementations that required by this module, please checkout jraft-ext module
Project URL

Project URL

https://github.com/datatechnology/jraft
Source Code Management

Source Code Management

https://github.com/datatechnology/jraft.git

Download jraft-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.datatechnology.jraft.core/jraft-core/ -->
<dependency>
    <groupId>com.github.datatechnology.jraft.core</groupId>
    <artifactId>jraft-core</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.datatechnology.jraft.core/jraft-core/
implementation 'com.github.datatechnology.jraft.core:jraft-core:1.0.1'
// https://jarcasting.com/artifacts/com.github.datatechnology.jraft.core/jraft-core/
implementation ("com.github.datatechnology.jraft.core:jraft-core:1.0.1")
'com.github.datatechnology.jraft.core:jraft-core:jar:1.0.1'
<dependency org="com.github.datatechnology.jraft.core" name="jraft-core" rev="1.0.1">
  <artifact name="jraft-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.datatechnology.jraft.core', module='jraft-core', version='1.0.1')
)
libraryDependencies += "com.github.datatechnology.jraft.core" % "jraft-core" % "1.0.1"
[com.github.datatechnology.jraft.core/jraft-core "1.0.1"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar

Project Modules

There are no modules declared in this project.

This project will be proactively monitored and updated

jraft

Raft consensus implementation in java

The core algorithm is implemented based on the TLA+ spec, whose safety is proven, and liveness is highly dependent on the pseudo-random number sequence, which could be fine if different servers in the same cluster are generating random numbers with different seeds.

Supported Features

  • Core Algorithm, safety is proven
  • Configuration Change Support, add or remove servers one by one without limitation
  • Client Request Support
  • Urgent commit, see below
  • log compaction

Urgent Commit

Urgent Commit is a new feature introduced by this implementation, which enables the leader to ask all other servers to commit one or more logs if the commit index is advanced. With Urgent Commit, the system's performance is highly improved and the heartbeat interval could be increased to seconds, depending on how long your application can abide when a leader goes down, usually, one or two seconds is fine.

About this implementation

It's always safer to implement such kind of algorithm based on Math description other than natural language description. there should be an auto-conversion from TLA+ to programming languages, even they are taking things in different ways, but they are identical.

In the example of dmprinter (Distributed Message Printer), it takes about 4ms to commit a message, while in Active-Active scenario (sending messages to all three instances of dmprinter), it takes about 9ms to commit a message, the data is collected by CLT (Central Limitation Theory) with 95% of confidence level.

Code Structure

This project contains not that much code, as it's well abstracted, here is the project structure

  • core, the core algorithm implementation, you can go only with this, however, you need to implement the following interfaces,
    1. Logger and LoggerFactory
    2. RpcClient and RpcClientFactory
    3. RpcListener
    4. ServerStateManager and SequentialLogStore
    5. StateMachine
  • exts, some implementations for the interfaces mentioned above, it provides TCP based CompletableFuture enabled RPC client and server as well as FileBasedSequentialLogStore, with this, you will be able to implement your own system by only implement StateMachine interface
  • dmprinter, a sample application, as it's name, it's distributed message printer, for sample and testing.
  • setup, some scripts for Windows(R) platform to run dmprinter

Run dmprinter

dmprinter is a distributed message printer which is used to verify the core implementation. A set of Windows(R) based setup scripts is shipped with this project, under setup folder.

  1. Export three projects into one executable jar file, called jraft.jar
  2. Start a command prompt, change directory to setup folder
  3. Run setup.cmd, it will start three instances of jraft
  4. Write a simple client by using Python or whatever language you would love to connect to any port between 8001 and 8003, which dmprinter is listening on. message format see below
  5. You can call addsrv.cmd \<server-id-in-int\> to start new instance of raft and call addsrv:\<server-id-in-int\>,tcp://localhost:900\<server-id-in-int\> to join the server to cluster, e.g. addsrv:4,tcp://localhost:9004 through the client, or remove a server from cluster rmsrv,4, check out more details about the message format as below,

Message format <header><message>

<header> := four bytes, which is encoded from an integer in little-endian format, the integer is the bytes of the <message>

<message> := <id|command>:<content>

<id> := uuid

<command> := addsrv|rmsrv

<content> := srvid,uri|srvid|any-string

Versions

Version
1.0.1
1.0.0