• Stars
    star
    966
  • Rank 45,647 (Top 1.0 %)
  • Language
    Go
  • License
    The Unlicense
  • Created over 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🚣 Raft implementation in Go

🚣 Raft

This is an instructional implementation of the Raft distributed consensus algorithm in Go. It's accompanied by a series of blog posts:

Each of the partN directories in this repository is the complete source code for Part N of the blog post series (except Part 0, which is introductory and has no code). There is a lot of duplicated code between the different partN directories - this is a conscious design decision. Rather than abstracting and reusing parts of the implementation, I opted for keeping the code as simple as possible. Each directory is completely self contained and can be read and understood in isolation. Using a graphical diff tool to see the deltas between the parts can be instructional.

How to use this repository

You can read the code, but I'd also encourage you to run tests and observe the logs they print out. The repository contains a useful tool for visualizing output. Here's a complete usage example:

$ cd part1
$ go test -v -race -run TestElectionFollowerComesBack |& tee /tmp/raftlog
... logging output
... test should PASS
$ go run ../tools/raft-testlog-viz/main.go < /tmp/raftlog
PASS TestElectionFollowerComesBack map[0:true 1:true 2:true TEST:true] ; entries: 150
... Emitted file:///tmp/TestElectionFollowerComesBack.html

PASS

Now open file:///tmp/TestElectionFollowerComesBack.html in your browser. You should see something like this:

Image of log browser

Scroll and read the logs from the servers, noticing state changes (highlighted with colors). Feel free to add your own cm.dlog(...) calls to the code to experiment and print out more details.

Changing and testing the code

Each partN directory is completely independent of the others, and is its own Go module. The Raft code itself has no external dependencies; the only require in its go.mod is for a package that enables goroutine leak testing - it's only used in tests.

To work on part2, for example:

$ cd part2
... make code changes
$ go test -race ./...

Depending on the part and your machine, the tests can take up to a minute to run. Feel free to enable verbose logging with -v, and/or used the provided dotest.sh script to run specific tests with log visualization.

Contributing

I'm interested in hearing your opinion or suggestions for the code in this repository. Feel free to open an issue if something is unclear, or if you think you found a bug. Code contributions through PRs are welcome as well.

More Repositories

1

pycparser

🐍 Complete C99 parser in pure Python
Python
3,124
star
2

pyelftools

Parsing ELF and DWARF in Python
Python
1,883
star
3

code-for-blog

Code samples from my blog
Python
1,511
star
4

llvm-clang-samples

UNMAINTAINED: Examples of using the LLVM and Clang compilation libraries and tools
C++
1,181
star
5

pss

pss is a power-tool for searching inside source code files.
Python
323
star
6

pykaleidoscope

Implementation of the LLVM tutorial in Python
Python
290
star
7

modlib

Go project layout with exported packages and command-line tools, using modules
Go
176
star
8

static-server

A simple, zero-configuration HTTP server CLI for serving static files
Go
175
star
9

deep-learning-samples

Sample code for deep learning & neural networks
Python
166
star
10

bobscheme

An implementation of Scheme in Python and C++
C++
162
star
11

luz-cpu

Educational open-source CPU suite (with assembler, linker and simulator)
Python
154
star
12

js-8080-sim

Intel 8080 assembler and simulator in JS, optimized for simplicity.
JavaScript
151
star
13

wasm-wat-samples

Samples of WebAssembly Text programs
WebAssembly
114
star
14

libjit-samples

Code samples for using libjit
C
90
star
15

wcx64

Clone of wc in x64 assembly
Assembly
81
star
16

go-sudoku

Toolkit for solving and generating Sudoku puzzles in Go
Go
80
star
17

c-unleashed-book-souce-code

Source code for the "C Unleashed" book by Richard Heathfield, Lawrence Kirby, et al.
C
69
star
18

asdl_parser

Standalone ASDL parser for upstream CPython 3.x
Python
61
star
19

gemini-cli

Access Gemini LLMs from the command-line
Go
57
star
20

gosax

gosax is a basic wrapper for stream parsing of XML (SAX) Go
Go
56
star
21

go-ungrammar

Ungrammar implementation and API in Go
Go
39
star
22

paip-in-clojure

🌅 Code from "Paradigms of Artificial Intelligence Programming" in Clojure
Clojure
39
star
23

cs344

Introduction to Parallel Programming class code
C++
31
star
24

go-websocket-sample

Sample Go websocket server and JS client with tracing and JSON data interchange
Go
19
star
25

esms

Legacy - ESMS (Electronic Soccer Management Simulator)
C++
18
star
26

go-quines

Some quines in the Go programming language
Go
15
star
27

xmlgen

C
14
star
28

python3-samples

Samples of Python 3 code, libraries, etc.
Python
12
star
29

tupperformula

JS demo of Tupper's formula
JavaScript
9
star
30

arm_asm_hacks

ARM assembly hacks
Assembly
8
star
31

hackutils

Various hacky utils for Go
Go
8
star
32

gocloud-samples

Samples/hacks with gocloud
Go
7
star
33

lsystem

JavaScript
6
star
34

jscool2asm

JavaScript
5
star
35

cl-in-clj

Common Lisp --> Clojure snippets/idioms translation
Clojure
3
star
36

sample-go-cli

Sample Go CLI project structure
Go
3
star
37

gocdkx

Fork of google/go-cloud for experiments.
Go
3
star
38

colorful-lines

My JS clone of the Colorful Lines game
JavaScript
3
star
39

so-tag-sentiment-analysis

Go
2
star
40

line-plotting

JavaScript
2
star
41

go-travis-test

sandbox
Shell
2
star
42

cljmin

Minimal project template for Clojure
Clojure
2
star
43

sample-go-project-vscode

Go
1
star
44

diffuse-sim

JavaScript
1
star
45

onlisp-in-clojure

Clojure
1
star
46

4clojure-solutions

My solutions to https://www.4clojure.com/problems
Clojure
1
star