Skip to content

Commit

Permalink
Support retrieving current term (hashicorp#620)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
otoolep authored Oct 10, 2024
1 parent dd1f3da commit 2b0032e
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,11 @@ func (r *Raft) Stats() map[string]string {
return s
}

// CurrentTerm returns the current term.
func (r *Raft) CurrentTerm() uint64 {
return r.getCurrentTerm()
}

// LastIndex returns the last index in stable storage,
// either from the last log or from the last snapshot.
func (r *Raft) LastIndex() uint64 {
Expand Down

0 comments on commit 2b0032e

Please sign in to comment.