Class LeaderElector
- All Implemented Interfaces:
AutoCloseable
-
Constructor Summary
ConstructorsConstructorDescriptionLeaderElector
(LeaderElectionConfig config) LeaderElector
(LeaderElectionConfig config, Consumer<Throwable> exceptionHandler) -
Method Summary
-
Constructor Details
-
LeaderElector
-
LeaderElector
-
-
Method Details
-
run
Runs the leader election in foreground. The process will enter an acquisition loop trying to get a lease of the lock object set in configuration. The acquisition loop stops in either of these scenarios:1) An error occurs that prevents us from aquiring a lease.
2) The LeaderElector successfully acquires leadership. At this point, we will enter a renewal loop where we will continuously renew the lease following the provided configuration.
Note that in both cases the LeaderElector will NOT return to the acquisition loop. This is most relevant when a leader instance loses leadership as the LeaderElector will not try to re-acquire leadership. To do this, the caller is responsible for explicitly invoking the "run" method again.
- Parameters:
startLeadingHook
- called when a LeaderElector client starts leadingstopLeadingHook
- called when a LeaderElector client stops leading
-
run
public void run(Runnable startLeadingHook, Runnable stopLeadingHook, Consumer<String> onNewLeaderHook) Runs the leader election in foreground. The process will enter an acquisition loop trying to get a lease of the lock object set in configuration. The acquisition loop stops in either of these scenarios:1) An error occurs that prevents us from aquiring a lease.
2) The LeaderElector successfully acquires leadership. At this point, we will enter a renewal loop where we will continuously renew the lease following the provided configuration.
Note that in both cases the LeaderElector will NOT return to the acquisition loop. This is most relevant when a leader instance loses leadership as the LeaderElector will not try to re-acquire leadership. To do this, the caller is responsible for explicitly invoking the "run" method again.
- Parameters:
startLeadingHook
- called when a LeaderElector client starts leadingstopLeadingHook
- called when a LeaderElector client stops leadingonNewLeaderHook
- called when the client observes a leader that is not the previously observed leader. This includes the first observed leader when the client starts.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-