Search in sources :

Example 1 with Lock

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;
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) LeaseLock(io.kubernetes.client.extended.leaderelection.resourcelock.LeaseLock) ConfigMapLock(io.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) Lock(io.kubernetes.client.extended.leaderelection.Lock)

Example 2 with Lock

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);
        }
    };
}
Also used : RE(org.apache.druid.java.util.common.RE) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) ConfigMapLock(io.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock) Lock(io.kubernetes.client.extended.leaderelection.Lock) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

LeaderElectionConfig (io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)2 LeaderElector (io.kubernetes.client.extended.leaderelection.LeaderElector)2 Lock (io.kubernetes.client.extended.leaderelection.Lock)2 ConfigMapLock (io.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock)2 EndpointsLock (io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock)1 LeaseLock (io.kubernetes.client.extended.leaderelection.resourcelock.LeaseLock)1 ApiException (io.kubernetes.client.openapi.ApiException)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 RE (org.apache.druid.java.util.common.RE)1