Skip to content

Commit

Permalink
Adding some skeletons
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Nov 5, 2013
1 parent fe13829 commit a614d49
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
18 changes: 18 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package raft

import (
"time"
)

// Config provides any necessary configuraiton to
// the Raft server
type Config struct {
// Time without a leader before we attempt an election
ElectionTimeout time.Duration
}

func DefaultConfig() *Config {
return &Config{
ElectionTimeout: 150 * time.Millisecond,
}
}
15 changes: 15 additions & 0 deletions leader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package raft

// LeaderState is used to track the additional state
// needed as a leader
type LeaderState struct {
followers map[string]*FollowerState
}

type FollowerState struct {
// This is the next index to send
nextIndex uint64

// This is the last known replicated index
replicatedIndex uint64
}
25 changes: 25 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package raft

// Log entries are replicated to all members of the Raft cluster
// and form the heart of the replicated state machine.
type Log struct {
Index uint64
Term uint64
Data []byte
}

// LogStore is used to provide an interface for storing
// and retrieving logs in a durable fashion
type LogStore interface {
// Returns the last index written. 0 for no entries.
LastIndex() (uint64, error)

// Gets a log entry at a given index
GetLog(index uint64, log *Log) error

// Stores a log entry
StoreLog(log *Log) error

// Deletes a range of log entries. The range is inclusive.
DeleteRange(min, max uint64) error
}
32 changes: 32 additions & 0 deletions raft.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package raft

type RaftState uint8

const (
Follower RaftState = iota
Candidate
Leader
)

type Raft struct {
// Configuration
conf *Config

// Current state
state RaftState

// stable is a StableStore implementation for durable state
stable StableStore

// logs is a LogStore implementation to keep our logs
logs LogStore

// Highest commited log entry
commitIndex uint64

// Last applied log to the FSM
lastApplied uint64

// If we are the leader, we have extra state
leader *LeaderState
}
21 changes: 21 additions & 0 deletions stable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package raft

// StableStore is used to provide stable storage
// of key configurations to ensure safety.
type StableStore interface {
// Returns the current term
CurrentTerm() (uint64, error)

// Returns the candidate we voted for this term
VotedFor() (string, error)

// Sets the current term. Clears the current vote.
SetCurrentTerm(uint64) error

// Sets a candidate vote for the current term
SetVote(string) error

// Returns our candidate ID. This should be unique
// and constant across runs
CandidateID() (string, error)
}

0 comments on commit a614d49

Please sign in to comment.