raftserver

package module
v0.0.0-...-864489a Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2016 License: Apache-2.0 Imports: 9 Imported by: 0

README

RaftServer: A RPC Server implement base on Raft Paper in Golang

GoDoc Build Status

What is Raft

Raft is a consensus algorithm that is designed to be easy to understand. It's equivalent to Paxos in fault-tolerance and performance. The difference is that it's decomposed into relatively independent subproblems, and it cleanly addresses all major pieces needed for practical systems. We hope Raft will make consensus available to a wider audience, and that this wider audience will be able to develop a variety of higher quality consensus-based systems than are available today. (quote from here)

Installation and Usage

Install

go get github.com/kkdai/raftserver

Usage

	//Start three raft servers
	srv1 := StartClusterServers("127.0.0.1:1230", 1, []string{"127.0.0.1:1231", "127.0.0.1:1232"})
	srv2 := StartClusterServers("127.0.0.1:1231", 2, []string{"127.0.0.1:1230", "127.0.0.1:1232"})
	srv3 := StartClusterServers("127.0.0.1:1232", 3, []string{"127.0.0.1:1231", "127.0.0.1:1230"})

	//Need sleep a while for leader election.
	time.Sleep(time.Second * 2)		
	

Inspired By

Project52

It is one of my project 52.

License

etcd is under the Apache 2.0 license. See the LICENSE file for details.

Documentation

Overview

Package raftserver provides simple rpc server with K/V value.

Index

Constants

View Source
const (
	OK           = "OK"
	ErrNoKey     = "ErrNoKey"
	InvalidParam = "Invalid Parameter"
)
View Source
const (
	ElectionTimeOut time.Duration = 150 * time.Millisecond
)

Variables

This section is empty.

Functions

func DPrintf

func DPrintf(format string, a ...interface{}) (n int, err error)

Types

type AEParam

type AEParam struct {
	Term         int
	LeaderID     int
	PrevLogIndex int
	PrevLogTerm  int
	Entries      []string
	LeaderCommit int
}

AEParam : For AppendEntries

type AEReply

type AEReply struct {
	Term    int
	Success bool
}

AEReply :

type Clerk

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

func MakeClerk

func MakeClerk(server string) *Clerk

func (*Clerk) Get

func (ck *Clerk) Get(key string) string

Get :

func (*Clerk) Put

func (ck *Clerk) Put(key string, value string)

Put :

type Err

type Err string

type GetArgs

type GetArgs struct {
	Key string
}

type GetReply

type GetReply struct {
	Err   Err
	Value string
}

type KVRaft

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

func NewKVRaft

func NewKVRaft(id int, srvList []string) *KVRaft

NewKVRaft :

func StartClusterServers

func StartClusterServers(rpcPort string, myID int, cluster []string) *KVRaft

StartClusterServers :

func StartServer

func StartServer(rpcPort string, myID int) *KVRaft

StartServer :

func (*KVRaft) AppendEntries

func (kv *KVRaft) AppendEntries(args *AEParam, reply *AEReply) error

AppendEntries :RPC call to server to update Log Entry

func (*KVRaft) RequestVote

func (kv *KVRaft) RequestVote(args *RVParam, reply *RVReply) error

RequestVote :

type Log

type Log struct {
	Term int
	Data string
}

type LogData

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

func NewLogData

func NewLogData() *LogData

func (*LogData) Append

func (l *LogData) Append(in []Log) error

Append :Append data to last if not exist

func (*LogData) ContainIndex

func (l *LogData) ContainIndex(term int, index int) (bool, int)

ContainIndex :Reply true if term and index exist in current log, return index if exist

func (*LogData) Get

func (l *LogData) Get(index int) (Log, error)

Get :Get data by index

func (*LogData) Length

func (l *LogData) Length() int

Length :Return the data length

func (*LogData) TrimRight

func (l *LogData) TrimRight(index int)

Get :Get data by index TrimRight :Trim data after specfic index

type PutArgs

type PutArgs struct {
	Key   string
	Value string
}

type PutReply

type PutReply struct {
	Err           Err
	PreviousValue string
}

type RVParam

type RVParam struct {
	Term         int
	CandidateId  int
	LastLogIndex int
	LastLogTerm  int
}

RVParam

type RVReply

type RVReply struct {
	Term        int
	VoteGranted bool
}

RVReply

type Role

type Role int
const (
	Follower Role = iota + 1
	Candidate
	Leader
)

Jump to

Keyboard shortcuts

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