shipwreck

package module
v0.0.0-...-ce3a8ff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 16 Imported by: 0

README

Shipwreck, Raft that crashed

This repository implements the Raft consensus algorithm. It's in no way production ready implementation, more so implementation to better understand how the algorithm works. You can read more about this here.

TODO

  • gRPC Communication
  • Error handling
  • Follower request proxy
  • File based storage
  • Unit tests
  • Integration tests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGrpcConn

func NewGrpcConn[T nodeMessage](addr string) (conn[T], error)

Types

type GrpcNodeServer

type GrpcNodeServer[T nodeMessage] struct {
	proto.UnsafeNodeServer
	// contains filtered or unexported fields
}

func NewGrpcNodeServer

func NewGrpcNodeServer[T nodeMessage](n RaftNode[T], addr string) *GrpcNodeServer[T]

func (*GrpcNodeServer[T]) AppendEntries

func (gns *GrpcNodeServer[T]) AppendEntries(ctx context.Context, req *proto.LogRequest) (*proto.LogReply, error)

AppendEntries implements proto.NodeServer.

func (*GrpcNodeServer[T]) GetMetadata

GetMetadata implements proto.NodeServer.

func (*GrpcNodeServer[T]) ProxyPush

func (gns *GrpcNodeServer[T]) ProxyPush(ctx context.Context, req *proto.ProxyPushRequest) (*proto.ProxyPushReply, error)

ProxyPush implements proto.NodeServer.

func (*GrpcNodeServer[T]) RequestVote

func (gns *GrpcNodeServer[T]) RequestVote(ctx context.Context, req *proto.VoteRequest) (*proto.VoteReply, error)

RequestVote implements proto.NodeServer.

func (*GrpcNodeServer[T]) Start

func (gns *GrpcNodeServer[T]) Start(ctx context.Context) error

type LogReply

type LogReply struct {
	CommitOffset int64
	Success      bool
}

type LogRequest

type LogRequest[T nodeMessage] struct {
	CommitOffset int64
	StartOffset  int64
	Entries      []T
}

type MemoryStorage

type MemoryStorage[T any] struct {
	// contains filtered or unexported fields
}

func (*MemoryStorage[T]) Append

func (ms *MemoryStorage[T]) Append(command ...T) (int64, error)

func (*MemoryStorage[T]) Commit

func (ms *MemoryStorage[T]) Commit(offset int64) ([]T, error)

func (*MemoryStorage[T]) Commited

func (ms *MemoryStorage[T]) Commited() int64

func (*MemoryStorage[T]) Discard

func (ms *MemoryStorage[T]) Discard(start int64, end int64) error

func (*MemoryStorage[T]) Get

func (ms *MemoryStorage[T]) Get(start int64, end int64) ([]T, error)

func (*MemoryStorage[T]) Length

func (ms *MemoryStorage[T]) Length() int64

type Message

type Message[T msg] struct {
	SourceID string
	TargetID string
	Msg      T
}

type NodeMode

type NodeMode int64
const (
	NodeModeFollower NodeMode = iota
	NodeModeCandidate
	NodeModeLeader
)

type ProxyPush

type ProxyPush[T nodeMessage] struct {
	Value T
}

type ProxyPushReply

type ProxyPushReply struct {
	Ok bool
}

type RaftNode

type RaftNode[T nodeMessage] interface {
	ID() string
	String() string
	Mode() NodeMode
	Push(v T) error
	AddPeer(conn conn[T])
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
	// contains filtered or unexported methods
}

func NewNode

func NewNode[T nodeMessage](storage Storage[T], commitCallback commitCallbackFunc[T]) RaftNode[T]

TODO handle commitCallback

type Storage

type Storage[T any] interface {
	Append(command ...T) (int64, error)
	Get(start int64, end int64) ([]T, error)
	// Commits the logs to the offset and returns the commited logs
	Commit(offset int64) ([]T, error)
	Discard(start int64, end int64) error

	Length() int64
	Commited() int64
}

type VoteReply

type VoteReply struct {
	Granted bool
}

type VoteRequest

type VoteRequest struct {
	Term         int64
	CommitOffset int64
}

Directories

Path Synopsis
protos
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL