Skip to content

Commit 2b0032e

Browse files
authored
Support retrieving current term (#620)
Simple addition to make this internal variable accessible (without relying on `stats` object, which is a brittle approach). _Term_ is a fundamental concept in Raft consensus, so making it easily available to clients of this library seems like an obvious thing to do. There are also specific use cases supported by knowing the Term: - Allow clients of this library to retrieve and display diagnostic information about the state of the Raft system. - Support certain types of reads of a Raft-managed store. For example, if a client can check before and after a Read that an election has not taken place during the read, it allows those clients to make certain guarantees about the data read from the Raft-managed Store. One way to do this is to simply check that the Term has not changed (see [this discussion](https://groups.google.com/g/raft-dev/c/4QlyV0aptEQ/m/1JxcmSgRAwAJ) for more details).
1 parent dd1f3da commit 2b0032e

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,11 @@ func (r *Raft) Stats() map[string]string {
12081208
return s
12091209
}
12101210

1211+
// CurrentTerm returns the current term.
1212+
func (r *Raft) CurrentTerm() uint64 {
1213+
return r.getCurrentTerm()
1214+
}
1215+
12111216
// LastIndex returns the last index in stable storage,
12121217
// either from the last log or from the last snapshot.
12131218
func (r *Raft) LastIndex() uint64 {

0 commit comments

Comments
 (0)