Skip to content

Commit 059d8d5

Browse files
committed
Expose additional raft properties
Signed-off-by: Joshua Zhang <joshua.x.zhang@gmail.com>
1 parent f642e1b commit 059d8d5

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

node.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ func IsEmptySnap(sp pb.Snapshot) bool {
128128
return sp.Metadata.Index == 0
129129
}
130130

131+
type CustomCommand interface {
132+
Do(rn *RawNode)
133+
}
134+
131135
// Node represents a node in a raft cluster.
132136
type Node interface {
133137
// Tick increments the internal logical clock for the Node by a single tick. Election
@@ -240,6 +244,8 @@ type Node interface {
240244
ReportSnapshot(id uint64, status SnapshotStatus)
241245
// Stop performs any necessary termination of the Node.
242246
Stop()
247+
248+
DoCustomCommand(ctx context.Context, cmd CustomCommand) error
243249
}
244250

245251
type Peer struct {
@@ -305,6 +311,7 @@ type node struct {
305311
done chan struct{}
306312
stop chan struct{}
307313
status chan chan Status
314+
cmdc chan CustomCommand
308315

309316
rn *RawNode
310317
}
@@ -324,6 +331,7 @@ func newNode(rn *RawNode) node {
324331
done: make(chan struct{}),
325332
stop: make(chan struct{}),
326333
status: make(chan chan Status),
334+
cmdc: make(chan CustomCommand),
327335
rn: rn,
328336
}
329337
}
@@ -446,6 +454,8 @@ func (n *node) run() {
446454
advancec = nil
447455
case c := <-n.status:
448456
c <- getStatus(r)
457+
case cmd := <-n.cmdc:
458+
cmd.Do(n.rn)
449459
case <-n.stop:
450460
close(n.done)
451461
return
@@ -608,3 +618,14 @@ func (n *node) ForgetLeader(ctx context.Context) error {
608618
func (n *node) ReadIndex(ctx context.Context, rctx []byte) error {
609619
return n.step(ctx, pb.Message{Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: rctx}}})
610620
}
621+
622+
func (n *node) DoCustomCommand(ctx context.Context, cmd CustomCommand) error {
623+
select {
624+
case n.cmdc <- cmd:
625+
return nil
626+
case <-n.done:
627+
return ErrStopped
628+
case <-ctx.Done():
629+
return ctx.Err()
630+
}
631+
}

rawnode.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,11 @@ func (rn *RawNode) ForgetLeader() error {
560560
func (rn *RawNode) ReadIndex(rctx []byte) {
561561
_ = rn.raft.Step(pb.Message{Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: rctx}}})
562562
}
563+
564+
func (rn *RawNode) GetVotes() map[uint64]bool {
565+
votes := make(map[uint64]bool, len(rn.raft.trk.Votes))
566+
for k, v := range rn.raft.trk.Votes {
567+
votes[k] = v
568+
}
569+
return votes
570+
}

status.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type BasicStatus struct {
3939
Applied uint64
4040

4141
LeadTransferee uint64
42+
43+
LastLogIndex uint64
44+
LastLogTerm uint64
4245
}
4346

4447
func getProgressCopy(r *raft) map[uint64]tracker.Progress {
@@ -61,6 +64,9 @@ func getBasicStatus(r *raft) BasicStatus {
6164
s.HardState = r.hardState()
6265
s.SoftState = r.softState()
6366
s.Applied = r.raftLog.applied
67+
e := r.raftLog.lastEntryID()
68+
s.LastLogIndex = e.index
69+
s.LastLogTerm = e.term
6470
return s
6571
}
6672

0 commit comments

Comments
 (0)