Class LeaderElector
- java.lang.Object
-
- io.fabric8.kubernetes.client.extended.leaderelection.LeaderElector
-
public class LeaderElector extends Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static DoubleJITTER_FACTOR
-
Constructor Summary
Constructors Constructor Description LeaderElector(KubernetesClient kubernetesClient, LeaderElectionConfig leaderElectionConfig, Executor executor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancanBecomeLeader(LeaderElectionRecord leaderElectionRecord)protected booleanisLeader(LeaderElectionRecord leaderElectionRecord)protected static Durationjitter(Duration duration, double maxFactor)Returns aDurationbetween the provided duration and (duration + maxFactor*duration)protected static CompletableFuture<Void>loop(Consumer<CompletableFuture<?>> consumer, LongSupplier delaySupplier, Executor executor)Periodically (every provided period) runs the providedConsumerin a separate thread until the suppliedCompletableFutureis completed.protected static ZonedDateTimenow()booleanrelease()Release the leadership if currently held.voidrun()Starts the leader election loop.CompletableFuture<?>start()Start a leader elector.
-
-
-
Field Detail
-
JITTER_FACTOR
protected static final Double JITTER_FACTOR
-
-
Constructor Detail
-
LeaderElector
public LeaderElector(KubernetesClient kubernetesClient, LeaderElectionConfig leaderElectionConfig, Executor executor)
-
-
Method Detail
-
run
public void run()
Starts the leader election loop. May only be run once.start()is preferred as it does not hold a thread.
-
start
public CompletableFuture<?> start()
Start a leader elector. The future may be cancelled to stop the leader elector. May only be run once.- Returns:
- the future
-
release
public boolean release()
Release the leadership if currently held. If not cancelled, the elector will continue to try and re-acquire the lock.- Returns:
- true if the lock was successfully released. false if there is no lock, or this is not the leader
-
isLeader
protected final boolean isLeader(LeaderElectionRecord leaderElectionRecord)
-
canBecomeLeader
protected final boolean canBecomeLeader(LeaderElectionRecord leaderElectionRecord)
-
loop
protected static CompletableFuture<Void> loop(Consumer<CompletableFuture<?>> consumer, LongSupplier delaySupplier, Executor executor)
Periodically (every provided period) runs the providedConsumerin a separate thread until the suppliedCompletableFutureis completed.- Parameters:
consumer- function to run in a separate threaddelaySupplier- to schedule the run of the provided consumer- Returns:
- the future to be completed
-
now
protected static ZonedDateTime now()
-
-