use of io.kubernetes.client.extended.leaderelection.Lock in project java by kubernetes-client.
the class LeaderElectorTest method makeAndRunLeaderElectorAsync.
private LeaderElector makeAndRunLeaderElectorAsync(String candidateId, CyclicBarrier startBarrier, Runnable startBeingLeader, Runnable stopBeingLeader, ApiClient apiClient) {
Lock lock = makeLock(candidateId, NAMESPACE, LOCK_RESOURCE_NAME, apiClient);
LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(lock, Duration.ofSeconds(30), Duration.ofSeconds(23), Duration.ofSeconds(3));
LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
Thread thread = new Thread(() -> {
if (startBarrier != null) {
try {
startBarrier.await();
} catch (InterruptedException | BrokenBarrierException ex) {
LOGGER.error("startBarrier.await() failed", ex);
return;
}
}
leaderElector.run(startBeingLeader, stopBeingLeader);
}, String.format("%s-leader-elector-main", candidateId));
thread.setDaemon(true);
thread.start();
return leaderElector;
}
use of io.kubernetes.client.extended.leaderelection.Lock in project druid by druid-io.
the class DefaultK8sLeaderElectorFactory method create.
@Override
public K8sLeaderElector create(String candidateId, String namespace, String lockResourceName) {
Lock lock = createLock(candidateId, namespace, lockResourceName, realK8sClient);
LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig(lock, Duration.ofMillis(discoveryConfig.getLeaseDuration().getMillis()), Duration.ofMillis(discoveryConfig.getRenewDeadline().getMillis()), Duration.ofMillis(discoveryConfig.getRetryPeriod().getMillis()));
LeaderElector leaderElector = new LeaderElector(leaderElectionConfig);
return new K8sLeaderElector() {
@Override
public String getCurrentLeader() {
try {
return lock.get().getHolderIdentity();
} catch (ApiException ex) {
throw new RE(ex, "Failed to get current leader for [%s]", lockResourceName);
}
}
@Override
public void run(Runnable startLeadingHook, Runnable stopLeadingHook) {
leaderElector.run(startLeadingHook, stopLeadingHook);
}
};
}
Aggregations