Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft: implement Raft consensus protocol #121

Draft
wants to merge 145 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
7fda008
adds basic structs for raft operation
SUMUKHA-PK May 7, 2020
24bc5af
Merge branch 'master' into raft
SUMUKHA-PK May 8, 2020
e4f1ec5
implements basic logic of leader election
SUMUKHA-PK May 8, 2020
a1f247b
implements cluster interface to get config data from `internal/node`
SUMUKHA-PK May 8, 2020
94f5727
minor changes
SUMUKHA-PK May 8, 2020
82829c3
Add a package dedicated to messages
tsatke May 8, 2020
f8b6522
Add godoc
tsatke May 8, 2020
d19e9a5
Add cluster interface
tsatke May 8, 2020
a98870c
Implement a tcp cluster
tsatke May 8, 2020
45ee8e8
Export another cluster function
tsatke May 8, 2020
9bdaaab
Change Id to ID
tsatke May 9, 2020
7881b36
Change Id to ID
tsatke May 9, 2020
c4979bd
Add tcp cluster tests
tsatke May 9, 2020
b37962e
minor updates
SUMUKHA-PK May 9, 2020
8bc664f
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 9, 2020
f58bd74
Fix warning in generated code and remove obsolete whitespace
tsatke May 9, 2020
4f33c89
Fix warnings
tsatke May 9, 2020
7083008
adds a raft.Server interface, fixes static check errors
SUMUKHA-PK May 9, 2020
1f10dd0
adds a raft.Server interface, fixes static check errors
SUMUKHA-PK May 9, 2020
0e964c4
Fix race condition
tsatke May 9, 2020
80c6997
adds a raft.Server interface
SUMUKHA-PK May 9, 2020
c4f4b7d
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 9, 2020
8a9ccac
fixes error
SUMUKHA-PK May 9, 2020
46d2156
fixes error
SUMUKHA-PK May 9, 2020
78be72f
minor changes in raft
SUMUKHA-PK May 10, 2020
568ea23
Fix race condition
tsatke May 11, 2020
3b3760e
patches some problems, updates protoc version, changes []bytes to id
SUMUKHA-PK May 11, 2020
7196c33
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 11, 2020
e3f9a10
patches some problems, updates protoc version, changes []bytes to id
SUMUKHA-PK May 11, 2020
25c095f
Add io.Closer to raft server
tsatke May 11, 2020
8ea1bad
Implement node
tsatke May 11, 2020
4e84f59
working with getting a struct implement the server interface
SUMUKHA-PK May 11, 2020
17ce69d
functions set-up to begin implementation
SUMUKHA-PK May 11, 2020
fb359cf
Pass logger to raft
tsatke May 11, 2020
763f0c8
adds a logger
SUMUKHA-PK May 11, 2020
f88e16d
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 11, 2020
6d180b9
Fix lint godoc errors
tsatke May 11, 2020
216165b
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 11, 2020
0e20741
Fix unused warning
tsatke May 11, 2020
83128ea
Merge branch 'raft' of github.com:tomarrell/lbadd into raft
tsatke May 11, 2020
1e51e24
Add missing lock guard
tsatke May 12, 2020
240aec9
re-thinks design of raft
SUMUKHA-PK May 12, 2020
2822759
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 12, 2020
c47c6fa
Make node ID global
tsatke May 12, 2020
3c94193
Add OwnID to raft cluster
tsatke May 12, 2020
dc96aa9
Add a test to ensure that the ID exchange works
tsatke May 12, 2020
7016b63
Fix lock race
tsatke May 12, 2020
f9bef8d
Satisfy errcheck
tsatke May 12, 2020
ca1a4b5
initial raft operations setup
SUMUKHA-PK May 14, 2020
665b4a0
moved from ticker to timer, simplified goroutines, added node to simp…
SUMUKHA-PK May 15, 2020
6dc016b
partially working tests, basic leader's working phase complete
SUMUKHA-PK May 15, 2020
783dae8
Make ctxfunc ignore generated files
tsatke May 16, 2020
313f61f
Add mocks to network and raft for easy testing
tsatke May 16, 2020
d0a199e
Fix lock copy
tsatke May 16, 2020
e055158
Add doc files to mock packages
tsatke May 16, 2020
ca4b0d8
Fix lint errors
tsatke May 16, 2020
64bad62
implements basic leader election test
SUMUKHA-PK May 17, 2020
6b78a27
implements basic leader election test
SUMUKHA-PK May 17, 2020
77d2a9a
implemented appendEntriesResponse
Abby3017 May 19, 2020
ac235c0
added well defined logs, tweaked leader election, added some corner c…
SUMUKHA-PK May 19, 2020
26f35d4
added go.sum
SUMUKHA-PK May 19, 2020
5e32c22
adds some logging
SUMUKHA-PK May 20, 2020
45a887f
corrected logs and adds more logs
SUMUKHA-PK May 21, 2020
b70b170
merge master
SUMUKHA-PK May 21, 2020
57ec13f
Merge branch 'raft' of https://github.com/tomarrell/lbadd into append…
Abby3017 May 21, 2020
cb1aa34
added comment and removed typecast
Abby3017 May 21, 2020
f9d6dcf
adds follower code, some logs and logic in append entries
SUMUKHA-PK May 21, 2020
0212e8e
more comment added & fixed copy lock bug
Abby3017 May 22, 2020
14ef5e9
test cases for appendEntries has been added
Abby3017 May 22, 2020
89d77a5
TODO added for lastApplied
Abby3017 May 22, 2020
3264ff1
minor changes
SUMUKHA-PK May 22, 2020
1f9d83c
fixed race condition and err checks
SUMUKHA-PK May 22, 2020
6557909
updated format of comment
Abby3017 May 22, 2020
1df1687
added issue to TODO task
Abby3017 May 22, 2020
5915d8e
merge raft changes
Abby3017 May 22, 2020
08a9bb7
replaced loop with append function
Abby3017 May 22, 2020
3fc4d8e
started a small testing framework
SUMUKHA-PK May 23, 2020
5afc486
code formatted for better readability
Abby3017 May 23, 2020
f9f6921
moved mu above Log to show Mutex should be used when modifying Logs
Abby3017 May 23, 2020
7d1ec65
comment added and assert message updated
Abby3017 May 24, 2020
932309f
Merge branch 'raft' of https://github.com/tomarrell/lbadd into append…
Abby3017 May 24, 2020
bfda2d8
added description about the Test function
Abby3017 May 24, 2020
ff63d6a
reverted mu location change
Abby3017 May 25, 2020
e2da784
formatted comments for better readability
Abby3017 May 25, 2020
f94f4ea
push to merge other branch, fixed failing test
SUMUKHA-PK May 26, 2020
f4312d3
Merge branch 'master' into raft
tsatke May 26, 2020
e51b2e3
merge master
SUMUKHA-PK May 26, 2020
5656861
merge master
SUMUKHA-PK May 26, 2020
be65942
merge master
SUMUKHA-PK May 26, 2020
59d7c8a
Merge branch 'raft' of https://github.com/tomarrell/lbadd into append…
Abby3017 May 26, 2020
f04fb20
Merge pull request #129 from Abby3017/appendEntriesResponse
SUMUKHA-PK May 26, 2020
d90b5e0
Add mock example
tsatke May 27, 2020
1e01aae
some testing code
SUMUKHA-PK May 28, 2020
5a97108
Merge branch 'master' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 28, 2020
8325f93
Merge branch 'master' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK May 28, 2020
e49c183
basic raft testing framework setup
SUMUKHA-PK May 28, 2020
cba4a77
basic raft testing framework setup
SUMUKHA-PK May 28, 2020
136614d
basic raft testing framework setup
SUMUKHA-PK May 28, 2020
7e60042
adds a viable raft test
SUMUKHA-PK May 29, 2020
959f1c6
Merge branch 'master' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK Jun 5, 2020
7c68a2e
Merge branch 'master' into raft
SUMUKHA-PK Jun 7, 2020
38d9f47
a basic complete cluster mock raft test
SUMUKHA-PK Jun 12, 2020
9d7b000
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK Jun 12, 2020
bf4312c
this commit adds a working raft test to base future tests off of
SUMUKHA-PK Jun 12, 2020
972f849
raft tests in progress
SUMUKHA-PK Jun 13, 2020
81005b3
raft tests in progress
SUMUKHA-PK Jun 13, 2020
55ecf62
progress in adding complete testing of raft
SUMUKHA-PK Jun 13, 2020
aac650f
triage on tests and lower layers
SUMUKHA-PK Jun 14, 2020
5b0d140
triage on tests and lower layers
SUMUKHA-PK Jun 14, 2020
3457be5
a well rounded base for raft test is created
SUMUKHA-PK Jun 17, 2020
e9d5bf1
this commit adds a new method to relay data from a follower to the le…
SUMUKHA-PK Jun 21, 2020
cc9ca03
this commit adds a new method to relay data from a follower to the le…
SUMUKHA-PK Jun 21, 2020
d388f37
this commit adds a new method to relay data from a follower to the le…
SUMUKHA-PK Jun 21, 2020
9a932cc
this commit cleans up some comments
SUMUKHA-PK Jun 21, 2020
df33de3
this commit ensures that the server closes in the test
SUMUKHA-PK Jun 21, 2020
de16058
moved LogData's variable from string to a compiled code; has some TOD…
SUMUKHA-PK Jun 24, 2020
8792b6c
moved LogData's variable from string to a compiled code; has some TOD…
SUMUKHA-PK Jun 24, 2020
e9e0afc
Trigger CI
SUMUKHA-PK Jun 24, 2020
c286fe2
Trigger CI
SUMUKHA-PK Jun 24, 2020
e025b90
this commits adds implementation of converting message to command typ…
SUMUKHA-PK Jun 30, 2020
79cd3ae
this commits adds implementation of converting message to command typ…
SUMUKHA-PK Jun 30, 2020
22600e1
this commits adds implementation of converting message to command typ…
SUMUKHA-PK Jun 30, 2020
cb1db20
this commits adds implementation of converting message to command typ…
SUMUKHA-PK Jun 30, 2020
04a6c6e
this commit implements and tests converting message from command
SUMUKHA-PK Jul 2, 2020
44a90fb
this commit completes implementation and testing of conversion of com…
SUMUKHA-PK Jul 2, 2020
464719f
this commit completes implementation and testing of conversion of com…
SUMUKHA-PK Jul 2, 2020
a91559b
moved the old stop-gap arrangement of replication to the new conversi…
SUMUKHA-PK Jul 2, 2020
fe86835
this commit implements corrections suggested
SUMUKHA-PK Jul 3, 2020
e145c85
a basic idea for #174
SUMUKHA-PK Jul 5, 2020
7a8e61b
this commit adds a basic test framework for integration of raft modul…
SUMUKHA-PK Jul 8, 2020
b884065
fixed staticcheck errors
SUMUKHA-PK Jul 8, 2020
43f75ba
fixed errors
SUMUKHA-PK Jul 8, 2020
189fec9
Merge branch 'raft' of https://github.com/tomarrell/lbadd into raft
SUMUKHA-PK Jul 10, 2020
ddeda0a
this commit adds a self sufficient skeleton for a raft test framework
SUMUKHA-PK Jul 11, 2020
67aabc0
fixed staticcheck errors
SUMUKHA-PK Jul 11, 2020
0bed533
fixed staticcheck errors
SUMUKHA-PK Jul 11, 2020
ba0982f
this commit refreshes the idea of the framework and adds error checki…
SUMUKHA-PK Jul 13, 2020
bc74c82
merge master
SUMUKHA-PK Jul 13, 2020
e6abf6a
this commit adds engine execution to the node package, tidies go mod …
SUMUKHA-PK Jul 13, 2020
42e3f30
fix staticcheck errors
SUMUKHA-PK Jul 13, 2020
80a150d
this commit adds a network test framework and implements starting up …
SUMUKHA-PK Jul 20, 2020
127e675
merge master
SUMUKHA-PK Jul 20, 2020
1c640a1
merge master
SUMUKHA-PK Jul 31, 2020
91d71aa
this commit mends some issues in conversion of command to message
SUMUKHA-PK Jul 31, 2020
57c03a1
this commit mends some parts of message to command and vice-versa con…
SUMUKHA-PK Aug 1, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# global code owners
* @TimSatke @tomarrell
* @TimSatke @tomarrell

internal/database/storage/btree @tomarrell
internal/parser @TimSatke @SUMUKHA-PK
internal/raft @SUMUKHA-PK
internal/raft/cluster @TimSatke
internal/raft/message @TimSatke
internal/compiler @TimSatke
internal/database/storage/btree @tomarrell
internal/network @TimSatke
internal/parser @TimSatke @SUMUKHA-PK
internal/parser @TimSatke @SUMUKHA-PK
2 changes: 1 addition & 1 deletion cmd/lbadd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func startNode(cmd *cobra.Command, args []string) {
if err := node.ListenAndServe(cmd.Context(), addr); err != nil {
log.Error().
Err(err).
Msg("listen and serve")
Msg("open")
os.Exit(ExitAbnormal)
}
}
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ module github.com/tomarrell/lbadd
go 1.13

require (
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.5.1
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/oklog/ulid v1.3.1
github.com/rs/zerolog v1.19.0
github.com/spf13/afero v1.3.2
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/text v0.3.3
golang.org/x/tools v0.0.0-20200521211927-2b542361a4fc
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
golang.org/x/tools v0.0.0-20200713011307-fd294ab11aed
google.golang.org/protobuf v1.25.0
gotest.tools v2.2.0+incompatible
)
91 changes: 56 additions & 35 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/compiler/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var _ Command = (*DropIndex)(nil)
var _ Command = (*DropTrigger)(nil)
var _ Command = (*DropView)(nil)
var _ Command = (*Update)(nil)
var _ Command = (*Insert)(nil)
var _ Command = (*Join)(nil)
var _ Command = (*Limit)(nil)
var _ Command = (*Insert)(nil)

// Command describes a structure that can be executed by the database executor.
// Instead of using bytecode, we use a hierarchical structure for the executor.
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion internal/network/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"

"github.com/rs/zerolog"
"github.com/tomarrell/lbadd/internal/id"
"github.com/tomarrell/lbadd/internal/network"
)

Expand All @@ -26,7 +27,8 @@ func ExampleServer() {

<-srv.Listening() // wait for the server to come up

client, _ := network.DialTCP(ctx, ":59513")
clientID := id.Create()
client, _ := network.DialTCP(ctx, clientID, ":59513")
defer func() {
_ = client.Close()
}()
Expand Down
54 changes: 0 additions & 54 deletions internal/network/id.go

This file was deleted.

13 changes: 0 additions & 13 deletions internal/network/id_test.go

This file was deleted.

82 changes: 82 additions & 0 deletions internal/network/mocks/conn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/network/mocks/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package mocks provides generated mock implementations for easy testing.
package mocks
20 changes: 9 additions & 11 deletions internal/network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package network

import (
"context"
"fmt"
"io"
"net"

"github.com/tomarrell/lbadd/internal/id"
)

// ConnHandler is a handler function for handling new connections. It will be
Expand All @@ -29,11 +30,16 @@ type Server interface {
// Addr returns the address that this server is listening to.
Addr() net.Addr

// OwnID returns the ID of this server. The remote ID of any connection is
// the own ID of another server.
OwnID() id.ID
// OnConnect sets a callback that will be executed whenever a new connection
// connects to this server.
OnConnect(ConnHandler)
}

//go:generate mockery -case=snake -name=Conn

// Conn describes a network connection. One can send a message with Conn.Send,
// and receive one with Conn.Receive. Unlike an io.Writer, the data that is
// passed into Send is guaranteed to be returned in a single Receive call on the
Expand All @@ -42,9 +48,8 @@ type Server interface {
type Conn interface {
io.Closer

// ID returns the ID of this connection. It can be used to uniquely identify
// this connection globally.
ID() ID
// RemoteID returns the own ID of the server that this connection points to.
RemoteID() id.ID
// Send sends the given payload to the remote part of this connection. The
// message will not be chunked, and can be read with a single call to
// Conn.Receive.
Expand All @@ -53,10 +58,3 @@ type Conn interface {
// is a byte slice that was sent with a single call to Conn.Send.
Receive(context.Context) ([]byte, error)
}

// ID describes an identifier that is used for connections. An ID has to be
// unique application-wide. IDs must not be re-used.
type ID interface {
fmt.Stringer
Bytes() []byte
}
46 changes: 30 additions & 16 deletions internal/network/tcp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"

"github.com/tomarrell/lbadd/internal/id"
"golang.org/x/net/context"
"golang.org/x/sync/errgroup"
)
Expand All @@ -22,8 +24,8 @@ var (
var _ Conn = (*tcpConn)(nil)

type tcpConn struct {
id ID
closed bool
remoteID id.ID
closed int32

readLock sync.Mutex
writeLock sync.Mutex
Expand All @@ -32,7 +34,7 @@ type tcpConn struct {

// DialTCP dials to the given address, assuming a TCP network. The returned Conn
// is ready to use.
func DialTCP(ctx context.Context, addr string) (Conn, error) {
func DialTCP(ctx context.Context, ownID id.ID, addr string) (Conn, error) {
// dial the remote endpoint
var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", addr)
Expand All @@ -43,38 +45,50 @@ func DialTCP(ctx context.Context, addr string) (Conn, error) {
// create a new connection object
tcpConn := newTCPConn(conn)

// receive the connection ID from the remote endpoint and apply it
myID, err := tcpConn.Receive(ctx)
// receive the remote ID from the remote endpoint and apply it
remoteID, err := tcpConn.Receive(ctx)
if err != nil {
_ = tcpConn.Close()
return nil, fmt.Errorf("receive ID: %w", err)
return nil, fmt.Errorf("receive remote ID: %w", err)
}
parsedID, err := parseID(myID)
parsedID, err := id.Parse(remoteID)
if err != nil {
_ = tcpConn.Close()
return nil, fmt.Errorf("parse ID: %w", err)
return nil, fmt.Errorf("parse remote ID: %w", err)
}
tcpConn.remoteID = parsedID

// send own ID to remote endpoint
err = tcpConn.Send(ctx, ownID.Bytes())
if err != nil {
_ = tcpConn.Close()
return nil, fmt.Errorf("send own ID: %w", err)
}
tcpConn.id = parsedID

// return the connection object
return tcpConn, nil
}

// NewTCPConn wraps the underlying connection into a tcpConn.
func NewTCPConn(underlying net.Conn) Conn {
return newTCPConn(underlying)
}

func newTCPConn(underlying net.Conn) *tcpConn {
id := createID()
id := id.Create()
conn := &tcpConn{
id: id,
remoteID: id,
underlying: underlying,
}
return conn
}

func (c *tcpConn) ID() ID {
return c.id
func (c *tcpConn) RemoteID() id.ID {
return c.remoteID
}

func (c *tcpConn) Send(ctx context.Context, payload []byte) error {
if c.closed {
if atomic.LoadInt32(&c.closed) == 1 {
return ErrClosed
}

Expand Down Expand Up @@ -137,7 +151,7 @@ func (c *tcpConn) sendAsync(payload []byte) chan error {
}

func (c *tcpConn) Receive(ctx context.Context) ([]byte, error) {
if c.closed {
if atomic.LoadInt32(&c.closed) == 1 {
return nil, ErrClosed
}

Expand Down Expand Up @@ -202,7 +216,7 @@ func (c *tcpConn) receiveAsync() chan interface{} {
}

func (c *tcpConn) Close() error {
c.closed = true
atomic.StoreInt32(&c.closed, 1)

// release all resources
ctx := context.Background()
Expand Down
Loading