-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
86 lines (70 loc) · 2.33 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package raft
import (
"errors"
"fmt"
)
// Raft server states.
type ServerState uint32
const (
FOLLOWER ServerState = iota
CANDIDATE
LEADER
)
var ErrStopped = errors.New("ConsensusModule is stopped")
var ErrNotLeader = errors.New("Not currently in LEADER state")
// FIXME: this needs actual values for debugging
var ErrIndexCompacted = errors.New("Given index is less than or equal to lastCompacted")
// Raft election term.
// Initialized to 0 on first boot, increases monotonically.
type TermNo uint64
// A state machine command (in serialized form).
// The contents of the byte slice are opaque to the ConsensusModule.
type Command []byte
// CommandResult is the result of applying a command to the state machine.
// The contents of the byte slice are opaque to the ConsensusModule.
type CommandResult interface{}
// An entry in the Raft Log
type LogEntry struct {
TermNo
Command
}
// Log entry index. First index is 1.
type LogIndex uint64
type IndexChangeListener func(new LogIndex)
// A WatchableIndex is a LogIndex that notifies listeners when the value changes.
//
// When the underlying LogIndex value is changed, all registered listeners are
// called in registered order.
// The listener is guaranteed that another change will not occur until it
// has returned to this method.
//
// This is implemented by logindex.WatchedIndex.
type WatchableIndex interface {
// Get the current value.
Get() LogIndex
// Add the given callback as a listener for changes.
// This is NOT safe to call from a listener.
AddListener(didChangeListener IndexChangeListener)
}
// An integer that uniquely identifies a server in a Raft cluster.
//
// Zero is reserved for "unknown" and should not be used as a server id.
//
// See config.ClusterInfo for how this is used in this package.
// The number value does not have a meaning to this package.
// This package also does not know about the network details - e.g. protocol/host/port -
// since the RPC is not part of the package but is delegated to the user.
type ServerId uint64
// ServerStateToString returns a string representation of a ServerState value.
func ServerStateToString(serverState ServerState) string {
switch serverState {
case FOLLOWER:
return "FOLLOWER"
case CANDIDATE:
return "CANDIDATE"
case LEADER:
return "LEADER"
default:
return fmt.Sprintf("Unknown ServerState: %v", serverState)
}
}