Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get notified of the change of leader? #23

Open
linkypi opened this issue Jan 22, 2024 · 1 comment
Open

How do I get notified of the change of leader? #23

linkypi opened this issue Jan 22, 2024 · 1 comment

Comments

@linkypi
Copy link

linkypi commented Jan 22, 2024

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

@shaj13
Copy link
Owner

shaj13 commented Apr 23, 2024

@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")
                            }
                     }
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants