Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft: return non-nil Inflights in raft status #10903

Merged
merged 3 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions raft/rawnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"reflect"
"testing"

"go.etcd.io/etcd/raft/quorum"
"go.etcd.io/etcd/raft/raftpb"
"go.etcd.io/etcd/raft/tracker"
)
Expand Down Expand Up @@ -439,14 +440,33 @@ func TestRawNodeRestartFromSnapshot(t *testing.T) {
// no dependency check between Ready() and Advance()

func TestRawNodeStatus(t *testing.T) {
storage := NewMemoryStorage()
rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
s := NewMemoryStorage()
rn, err := NewRawNode(newTestConfig(1, []uint64{1}, 10, 1, s), nil)
if err != nil {
t.Fatal(err)
}
status := rawNode.Status()
if status == nil {
t.Errorf("expected status struct, got nil")
if status := rn.Status(); status.Progress != nil {
t.Fatalf("expected no Progress because not leader: %+v", status.Progress)
}
if err := rn.Campaign(); err != nil {
t.Fatal(err)
}
status := rn.Status()
if status.Lead != 1 {
t.Fatal("not lead")
}
if status.RaftState != StateLeader {
t.Fatal("not leader")
}
if exp, act := *rn.raft.prs.Progress[1], status.Progress[1]; !reflect.DeepEqual(exp, act) {
t.Fatalf("want: %+v\ngot: %+v", exp, act)
}
expCfg := tracker.Config{Voters: quorum.JointConfig{
quorum.MajorityConfig{1: {}},
nil,
}}
if !reflect.DeepEqual(expCfg, status.Config) {
t.Fatalf("want: %+v\ngot: %+v", expCfg, status.Config)
}
}

Expand Down
10 changes: 5 additions & 5 deletions raft/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Status struct {
SoftState

Applied uint64
Config tracker.Config
Progress map[uint64]tracker.Progress

LeadTransferee uint64
Expand All @@ -37,11 +38,9 @@ func getProgressCopy(r *raft) map[uint64]tracker.Progress {
m := make(map[uint64]tracker.Progress)
r.prs.Visit(func(id uint64, pr *tracker.Progress) {
var p tracker.Progress
p, pr = *pr, nil /* avoid accidental reuse below */

// The inflight buffer is tricky to copy and besides, it isn't exposed
// to the client, so pretend it's nil.
p.Inflights = nil
p = *pr
p.Inflights = pr.Inflights.Clone()
pr = nil

m[id] = p
})
Expand All @@ -56,6 +55,7 @@ func getStatusWithoutProgress(r *raft) Status {
s.HardState = r.hardState()
s.SoftState = *r.softState()
s.Applied = r.raftLog.applied
s.Config = r.prs.Config.Clone()
return s
}

Expand Down
8 changes: 8 additions & 0 deletions raft/tracker/inflights.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func NewInflights(size int) *Inflights {
}
}

// Clone returns an *Inflights that is identical to but shares no memory with
// the receiver.
func (in *Inflights) Clone() *Inflights {
ins := *in
ins.buffer = append([]uint64(nil), in.buffer...)
return &ins
}

// Add notifies the Inflights that a new message with the given index is being
// dispatched. Full() must be called prior to Add() to verify that there is room
// for one more message, and consecutive calls to add Add() must provide a
Expand Down