raft

package module
v0.0.0-...-21e2581 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: MIT Imports: 18 Imported by: 0

README

Go实现Raft分布式共识算法

使用

raft 网络中的节点使用 Server 结构体表示的,节点创建一个 Server,然后通过 peer 接口暴露给其他节点

node 之间的通信可以通过 http transport 和 http Peer。

增加和删除节点

TODO

  • Leader election
  • Log Replication
  • Basic Unit Test
  • Http transport
  • net/rpc transport
  • Configuration changes (joint consensus mode)
  • Log compaction
  • Robost demo application
  • complex unit test

reference

https://github.com/peterbourgon/raft

https://en.wikipedia.org/wiki/Raft_(algorithm)

Usage

分布式存储

https://github.com/siddontang/rust-raftkv

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MinimumElectionTimeoutMS int32 = 250
	MaximumElectionTimeoutMS       = 2 * MinimumElectionTimeoutMS
)

论文里写的好象是 150ms ~ 300ms

View Source
var (
	// IDPath is where the ID handler (GET) will be installed by the
	// HTTPTransport.
	IDPath = "/raft/id"

	// AppendEntriesPath is where the AppendEntries RPC handler (POST) will be
	// installed by the HTTPTransport.
	AppendEntriesPath = "/raft/appendentries"

	// RequestVotePath is where the requestVote RPC handler (POST) will be
	// installed by the HTTPTransport.
	RequestVotePath = "/raft/requestvote"

	// CommandPath is where the Command RPC handler (POST) will be installed by
	// the HTTPTransport.
	CommandPath = "/raft/command"

	// SetConfigurationPath is where the SetConfiguration RPC handler (POST)
	// will be installed by the HTTPTransport.
	SetConfigurationPath = "/raft/setconfiguration"
)

Functions

func HTTPTransport

func HTTPTransport(mux *http.ServeMux, s *Server)

HTTPTransport creates an ingress bridge from the outside world to the passed server, by installing handlers for all the necessary RPCs to the passed mux.

func MaximumElectionTimeout

func MaximumElectionTimeout() time.Duration

Types

type ApplyFunc

type ApplyFunc func(commitIndex uint64, cmd []byte) []byte

整个函数到底干嘛的???

type Peer

type Peer interface {
	// contains filtered or unexported methods
}

Peer is the local representation of a remote node. It's an interface that may be backed by any concrete transport: local, HTTP, net/rpc, etc. Peers must be encoding/gob encodable.

func NewHTTPPeer

func NewHTTPPeer(url *url.URL) (Peer, error)

NewHTTPPeer constructs a new HTTP peer. Part of construction involves making a HTTP GET request against the passed URL at IDPath, to resolve the remote server's ID.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the agent that perform all raft logic

func NewServer

func NewServer(id uint64, store io.ReadWriter, a ApplyFunc) *Server

func (*Server) Command

func (s *Server) Command(cmd []byte, response chan<- []byte) error

func (*Server) SetConfiguration

func (s *Server) SetConfiguration(peers ...Peer) error

func (*Server) Start

func (s *Server) Start()

func (*Server) Stop

func (s *Server) Stop()

给服务器的读等待chan放入数据,然后读出,触发通道关闭

Jump to

Keyboard shortcuts

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