public interface Election
DistributedGroup.
Elections are performed automatically within each DistributedGroup. As members
are added to or removed from the group, the group will automatically elect group leaders. Each unique
leader election is represented by a Term. The Term.term() is guaranteed to be unique
and monotonically increasing throughout the lifetime of a group. When a leader disconnects from the
cluster or leaves the group, the term will be increased and a new leader elected. Leaders are elected
using a semi-random algorithm. Each instance of a group is guaranteed to see the same leader and term
at the same logical time (not real time).
To access the current group term, use the term()} getter.
Term currentTerm = group.election().term();
Clients can listen for changes in group leadership by registering an election listener via
onElection(Consumer).
group.election().onElection(term -> {
GroupMember leader = term.leader();
});
Term term()
If the group is undergoing a new election, the returned Term can potentially have a
null Term.leader(). To ensure the term has a leader, either check the value or use
an election listener to listen for new leaders.
Listener<Term> onElection(Consumer<Term> callback)
The provided callback will be called each time the election is changed to a new term and a leader
is elected. The Term provided to the election callback is guaranteed to have a monotonically
increasing term number and a non-null Term.leader().
group.election().onElection(term -> {
GroupMember leader = term.leader();
});
If a leader was already elected for the current term when the callback is registered, the callback will
be immediately called before the completion of the registration. Aside from this initial notification,
all election listeners are guaranteed to see elections at the same logical time and in the same order on all
nodes.callback - The callback to be called when a new leader is elected.Copyright © 2013–2017. All rights reserved.