From 2b0032e390b888b0058d221989886d090b9d9609 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Thu, 10 Oct 2024 12:57:40 -0400 Subject: [PATCH] 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). --- api.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api.go b/api.go index cff2eaac..4ddeb36a 100644 --- a/api.go +++ b/api.go @@ -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 {