How do I get notified of the change of leader?
I want to implement a distributed KV system with data sharding based on consistent hash , which involves the need to re-shard and transfer the cluster data after the cluster leader changes.I see that etcd does not provide a notification mechanism, but hashiCorp/raft does provide LeaderCh and NotifyCh to actively notify the application layer that the leader has changed.
In the implementation of this system, in addition to the above functions, a preVote function is required, but this function is not implemented in hashicorp/raft. So eventually I have to resort to etcd/raft. So I'd like to ask if you have that notification mechanism in your implementation of raft, or if there are other better suggestions
@linkypi
The entire asynchronous Raft pattern and signaling are kept internal to this library and are not exposed publicly for specific reasons. Nevertheless, you can obtain leader transfer signals by implementing a simple function that ticks every X interval and compares the leader's IDs.
func monit(n *raft.node) {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
var leader uint64
for {
select {
case <-ticker.C:
if id := node.Leader(); id > leader {
fmt.Println("leader changed")
leader = id
if node.Whoami() == leader {
fmt.Println("this member promoted to be the raft leader")
}
}
}
}
}