Skip to content

Commit 09ad89e

Browse files
authored
Merge pull request #485 from hashicorp/dnephin/godoc-1
Miscellaneous godoc and comment improvements
2 parents ba08869 + ba61084 commit 09ad89e

File tree

6 files changed

+68
-67
lines changed

6 files changed

+68
-67
lines changed

api.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import (
1515
)
1616

1717
const (
18-
// This is the current suggested max size of the data in a raft log entry.
19-
// This is based on current architecture, default timing, etc. Clients can
18+
// SuggestedMaxDataSize of the data in a raft log entry, in bytes.
19+
//
20+
// The value is based on current architecture, default timing, etc. Clients can
2021
// ignore this value if they want as there is no actual hard checking
2122
// within the library. As the library is enhanced this value may change
2223
// over time to reflect current suggested maximums.
2324
//
24-
// Increasing beyond this risks RPC IO taking too long and preventing
25-
// timely heartbeat signals which are sent in serial in current transports,
26-
// potentially causing leadership instability.
25+
// Applying log entries with data greater than this size risks RPC IO taking
26+
// too long and preventing timely heartbeat signals. These signals are sent in serial
27+
// in current transports, potentially causing leadership instability.
2728
SuggestedMaxDataSize = 512 * 1024
2829
)
2930

@@ -146,8 +147,8 @@ type Raft struct {
146147
// the log/snapshot.
147148
configurations configurations
148149

149-
// Holds a copy of the latest configuration which can be read
150-
// independently from main loop.
150+
// Holds a copy of the latest configuration which can be read independently
151+
// of the main loop.
151152
latestConfiguration atomic.Value
152153

153154
// RPC chan comes from the transport layer
@@ -762,11 +763,11 @@ func (r *Raft) ApplyLog(log Log, timeout time.Duration) ApplyFuture {
762763
}
763764
}
764765

765-
// Barrier is used to issue a command that blocks until all preceeding
766+
// Barrier is used to issue a command that blocks until all preceding
766767
// operations have been applied to the FSM. It can be used to ensure the
767768
// FSM reflects all queued writes. An optional timeout can be provided to
768769
// limit the amount of time we wait for the command to be started. This
769-
// must be run on the leader or it will fail.
770+
// must be run on the leader, or it will fail.
770771
func (r *Raft) Barrier(timeout time.Duration) Future {
771772
metrics.IncrCounter([]string{"raft", "barrier"}, 1)
772773
var timer <-chan time.Time
@@ -775,11 +776,7 @@ func (r *Raft) Barrier(timeout time.Duration) Future {
775776
}
776777

777778
// Create a log future, no index or term yet
778-
logFuture := &logFuture{
779-
log: Log{
780-
Type: LogBarrier,
781-
},
782-
}
779+
logFuture := &logFuture{log: Log{Type: LogBarrier}}
783780
logFuture.init()
784781

785782
select {
@@ -792,9 +789,9 @@ func (r *Raft) Barrier(timeout time.Duration) Future {
792789
}
793790
}
794791

795-
// VerifyLeader is used to ensure the current node is still
796-
// the leader. This can be done to prevent stale reads when a
797-
// new leader has potentially been elected.
792+
// VerifyLeader is used to ensure this peer is still the leader. It may be used
793+
// to prevent returning stale data from the FSM after the peer has lost
794+
// leadership.
798795
func (r *Raft) VerifyLeader() Future {
799796
metrics.IncrCounter([]string{"raft", "verify_leader"}, 1)
800797
verifyFuture := &verifyFuture{}
@@ -817,8 +814,9 @@ func (r *Raft) GetConfiguration() ConfigurationFuture {
817814
return configReq
818815
}
819816

820-
// AddPeer (deprecated) is used to add a new peer into the cluster. This must be
821-
// run on the leader or it will fail. Use AddVoter/AddNonvoter instead.
817+
// AddPeer to the cluster configuration. Must be run on the leader, or it will fail.
818+
//
819+
// Deprecated: Use AddVoter/AddNonvoter instead.
822820
func (r *Raft) AddPeer(peer ServerAddress) Future {
823821
if r.protocolVersion > 2 {
824822
return errorFuture{ErrUnsupportedProtocol}
@@ -832,10 +830,11 @@ func (r *Raft) AddPeer(peer ServerAddress) Future {
832830
}, 0)
833831
}
834832

835-
// RemovePeer (deprecated) is used to remove a peer from the cluster. If the
836-
// current leader is being removed, it will cause a new election
837-
// to occur. This must be run on the leader or it will fail.
838-
// Use RemoveServer instead.
833+
// RemovePeer from the cluster configuration. If the current leader is being
834+
// removed, it will cause a new election to occur. Must be run on the leader,
835+
// or it will fail.
836+
837+
// Deprecated: Use RemoveServer instead.
839838
func (r *Raft) RemovePeer(peer ServerAddress) Future {
840839
if r.protocolVersion > 2 {
841840
return errorFuture{ErrUnsupportedProtocol}
@@ -955,7 +954,7 @@ func (r *Raft) Snapshot() SnapshotFuture {
955954
// Restore is used to manually force Raft to consume an external snapshot, such
956955
// as if restoring from a backup. We will use the current Raft configuration,
957956
// not the one from the snapshot, so that we can restore into a new cluster. We
958-
// will also use the higher of the index of the snapshot, or the current index,
957+
// will also use the max of the index of the snapshot, or the current index,
959958
// and then add 1 to that, so we force a new state with a hole in the Raft log,
960959
// so that the snapshot will be sent to followers and used for any new joiners.
961960
// This can only be run on the leader, and blocks until the restore is complete
@@ -1011,7 +1010,7 @@ func (r *Raft) Restore(meta *SnapshotMeta, reader io.Reader, timeout time.Durati
10111010
}
10121011
}
10131012

1014-
// State is used to return the current raft state.
1013+
// State returns the state of this raft peer.
10151014
func (r *Raft) State() RaftState {
10161015
return r.getState()
10171016
}
@@ -1151,7 +1150,7 @@ func (r *Raft) AppliedIndex() uint64 {
11511150
// This can only be called from the leader, or it will fail. The leader will
11521151
// stop accepting client requests, make sure the target server is up to date
11531152
// and starts the transfer with a TimeoutNow message. This message has the same
1154-
// effect as if the election timeout on the on the target server fires. Since
1153+
// effect as if the election timeout on the target server fires. Since
11551154
// it is unlikely that another server is starting an election, it is very
11561155
// likely that the target server is able to win the election. Note that raft
11571156
// protocol version 3 is not sufficient to use LeadershipTransfer. A recent

config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,18 @@ type Config struct {
132132
// can _understand_.
133133
ProtocolVersion ProtocolVersion
134134

135-
// HeartbeatTimeout specifies the time in follower state without
136-
// a leader before we attempt an election.
135+
// HeartbeatTimeout specifies the time in follower state without contact
136+
// from a leader before we attempt an election.
137137
HeartbeatTimeout time.Duration
138138

139-
// ElectionTimeout specifies the time in candidate state without
140-
// a leader before we attempt an election.
139+
// ElectionTimeout specifies the time in candidate state without contact
140+
// from a leader before we attempt an election.
141141
ElectionTimeout time.Duration
142142

143-
// CommitTimeout controls the time without an Apply() operation
144-
// before we heartbeat to ensure a timely commit. Due to random
145-
// staggering, may be delayed as much as 2x this value.
143+
// CommitTimeout specifies the time without an Apply operation before the
144+
// leader sends an AppendEntry RPC to followers, to ensure a timely commit of
145+
// log entries.
146+
// Due to random staggering, may be delayed as much as 2x this value.
146147
CommitTimeout time.Duration
147148

148149
// MaxAppendEntries controls the maximum number of append entries

fsm.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,34 @@ import (
88
"github.com/armon/go-metrics"
99
)
1010

11-
// FSM provides an interface that can be implemented by
12-
// clients to make use of the replicated log.
11+
// FSM is implemented by clients to make use of the replicated log.
1312
type FSM interface {
14-
// Apply log is invoked once a log entry is committed.
15-
// It returns a value which will be made available in the
16-
// ApplyFuture returned by Raft.Apply method if that
17-
// method was called on the same Raft node as the FSM.
13+
// Apply is called once a log entry is committed by a majority of the cluster.
14+
//
15+
// Apply should apply the log to the FSM. Apply must be deterministic and
16+
// produce the same result on all peers in the cluster.
17+
//
18+
// The returned value is returned to the client as the ApplyFuture.Response.
1819
Apply(*Log) interface{}
1920

20-
// Snapshot is used to support log compaction. This call should
21-
// return an FSMSnapshot which can be used to save a point-in-time
22-
// snapshot of the FSM. Apply and Snapshot are not called in multiple
23-
// threads, but Apply will be called concurrently with Persist. This means
24-
// the FSM should be implemented in a fashion that allows for concurrent
25-
// updates while a snapshot is happening.
21+
// Snapshot returns an FSMSnapshot used to: support log compaction, to
22+
// restore the FSM to a previous state, or to bring out-of-date followers up
23+
// to a recent log index.
24+
//
25+
// The Snapshot implementation should return quickly, because Apply can not
26+
// be called while Snapshot is running. Generally this means Snapshot should
27+
// only capture a pointer to the state, and any expensive IO should happen
28+
// as part of FSMSnapshot.Persist.
29+
//
30+
// Apply and Snapshot are always called from the same thread, but Apply will
31+
// be called concurrently with FSMSnapshot.Persist. This means the FSM should
32+
// be implemented to allow for concurrent updates while a snapshot is happening.
2633
Snapshot() (FSMSnapshot, error)
2734

2835
// Restore is used to restore an FSM from a snapshot. It is not called
2936
// concurrently with any other command. The FSM must discard all previous
30-
// state.
31-
Restore(io.ReadCloser) error
37+
// state before restoring the snapshot.
38+
Restore(snapshot io.ReadCloser) error
3239
}
3340

3441
// BatchingFSM extends the FSM interface to add an ApplyBatch function. This can
@@ -72,7 +79,7 @@ func (r *Raft) runFSM() {
7279
batchingFSM, batchingEnabled := r.fsm.(BatchingFSM)
7380
configStore, configStoreEnabled := r.fsm.(ConfigurationStore)
7481

75-
commitSingle := func(req *commitTuple) {
82+
applySingle := func(req *commitTuple) {
7683
// Apply the log if a command or config change
7784
var resp interface{}
7885
// Make sure we send a response
@@ -107,10 +114,10 @@ func (r *Raft) runFSM() {
107114
lastTerm = req.log.Term
108115
}
109116

110-
commitBatch := func(reqs []*commitTuple) {
117+
applyBatch := func(reqs []*commitTuple) {
111118
if !batchingEnabled {
112119
for _, ct := range reqs {
113-
commitSingle(ct)
120+
applySingle(ct)
114121
}
115122
return
116123
}
@@ -213,7 +220,7 @@ func (r *Raft) runFSM() {
213220
case ptr := <-r.fsmMutateCh:
214221
switch req := ptr.(type) {
215222
case []*commitTuple:
216-
commitBatch(req)
223+
applyBatch(req)
217224

218225
case *restoreFuture:
219226
restore(req)

log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
// LogBarrier is used to ensure all preceding operations have been
3131
// applied to the FSM. It is similar to LogNoop, but instead of returning
32-
// once committed, it only returns once the FSM manager acks it. Otherwise
32+
// once committed, it only returns once the FSM manager acks it. Otherwise,
3333
// it is possible there are operations committed but not yet applied to
3434
// the FSM.
3535
LogBarrier

raft.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (r *Raft) requestConfigChange(req configurationChangeRequest, timeout time.
123123
}
124124
}
125125

126-
// run is a long running goroutine that runs the Raft FSM.
126+
// run the main thread that handles leadership and RPC requests.
127127
func (r *Raft) run() {
128128
for {
129129
// Check if we are doing a shutdown
@@ -135,7 +135,6 @@ func (r *Raft) run() {
135135
default:
136136
}
137137

138-
// Enter into a sub-FSM
139138
switch r.getState() {
140139
case Follower:
141140
r.runFollower()
@@ -147,7 +146,7 @@ func (r *Raft) run() {
147146
}
148147
}
149148

150-
// runFollower runs the FSM for a follower.
149+
// runFollower runs the main loop while in the follower state.
151150
func (r *Raft) runFollower() {
152151
didWarn := false
153152
r.logger.Info("entering follower state", "follower", r, "leader", r.Leader())
@@ -236,8 +235,7 @@ func (r *Raft) runFollower() {
236235
func (r *Raft) liveBootstrap(configuration Configuration) error {
237236
// Use the pre-init API to make the static updates.
238237
cfg := r.config()
239-
err := BootstrapCluster(&cfg, r.logs, r.stable, r.snapshots,
240-
r.trans, configuration)
238+
err := BootstrapCluster(&cfg, r.logs, r.stable, r.snapshots, r.trans, configuration)
241239
if err != nil {
242240
return err
243241
}
@@ -252,7 +250,7 @@ func (r *Raft) liveBootstrap(configuration Configuration) error {
252250
return r.processConfigurationLogEntry(&entry)
253251
}
254252

255-
// runCandidate runs the FSM for a candidate.
253+
// runCandidate runs the main loop while in the candidate state.
256254
func (r *Raft) runCandidate() {
257255
r.logger.Info("entering candidate state", "node", r, "term", r.getCurrentTerm()+1)
258256
metrics.IncrCounter([]string{"raft", "state", "candidate"}, 1)
@@ -365,7 +363,7 @@ func (r *Raft) setupLeaderState() {
365363
r.leaderState.stepDown = make(chan struct{}, 1)
366364
}
367365

368-
// runLeader runs the FSM for a leader. Do the setup here and drop into
366+
// runLeader runs the main loop while in leader state. Do the setup here and drop into
369367
// the leaderLoop for the hot loop.
370368
func (r *Raft) runLeader() {
371369
r.logger.Info("entering leader state", "leader", r)
@@ -464,11 +462,7 @@ func (r *Raft) runLeader() {
464462
// an unbounded number of uncommitted configurations in the log. We now
465463
// maintain that there exists at most one uncommitted configuration entry in
466464
// any log, so we have to do proper no-ops here.
467-
noop := &logFuture{
468-
log: Log{
469-
Type: LogNoop,
470-
},
471-
}
465+
noop := &logFuture{log: Log{Type: LogNoop}}
472466
r.dispatchLogs([]*logFuture{noop})
473467

474468
// Sit in the leader loop until we step down
@@ -659,7 +653,7 @@ func (r *Raft) leaderLoop() {
659653
commitIndex := r.leaderState.commitment.getCommitIndex()
660654
r.setCommitIndex(commitIndex)
661655

662-
// New configration has been committed, set it as the committed
656+
// New configuration has been committed, set it as the committed
663657
// value.
664658
if r.configurations.latestIndex > oldCommitIndex &&
665659
r.configurations.latestIndex <= commitIndex {
@@ -1449,7 +1443,7 @@ func (r *Raft) processConfigurationLogEntry(entry *Log) error {
14491443
return nil
14501444
}
14511445

1452-
// requestVote is invoked when we get an request vote RPC call.
1446+
// requestVote is invoked when we get a request vote RPC call.
14531447
func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) {
14541448
defer metrics.MeasureSince([]string{"raft", "rpc", "requestVote"}, time.Now())
14551449
r.observe(*req)

util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func randomTimeout(minVal time.Duration) <-chan time.Time {
3232
if minVal == 0 {
3333
return nil
3434
}
35-
extra := (time.Duration(rand.Int63()) % minVal)
35+
extra := time.Duration(rand.Int63()) % minVal
3636
return time.After(minVal + extra)
3737
}
3838

0 commit comments

Comments
 (0)