Search in sources :

Example 1 with LeaderElector

use of io.kubernetes.client.extended.leaderelection.LeaderElector in project java by kubernetes-client.

the class LeaderElectorTest method testLeaderGracefulShutdown.

@Test(timeout = 45000L)
public void testLeaderGracefulShutdown() throws Exception {
    CountDownLatch startBeingLeader1 = new CountDownLatch(1);
    CountDownLatch stopBeingLeader1 = new CountDownLatch(1);
    LeaderElector leaderElector1 = makeAndRunLeaderElectorAsync("candidate1", null, () -> startBeingLeader1.countDown(), () -> stopBeingLeader1.countDown(), apiClient);
    // wait for candidate1 to become leader
    startBeingLeader1.await();
    CountDownLatch startBeingLeader2 = new CountDownLatch(1);
    CountDownLatch stopBeingLeader2 = new CountDownLatch(1);
    LeaderElector leaderElector2 = makeAndRunLeaderElectorAsync("candidate2", null, () -> startBeingLeader2.countDown(), () -> stopBeingLeader2.countDown(), apiClient);
    leaderElector1.close();
    // ensure stopBeingLeader hook is called
    stopBeingLeader1.await();
    // wait for candidate2 to become leader
    startBeingLeader2.await();
    leaderElector2.close();
}
Also used : LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with LeaderElector

use of io.kubernetes.client.extended.leaderelection.LeaderElector in project java by kubernetes-client.

the class LeaderElectorTest method testSingleCandidateLeaderElection.

@Test(timeout = 30000L)
public void testSingleCandidateLeaderElection() throws Exception {
    CountDownLatch startLeadershipLatch = new CountDownLatch(1);
    CountDownLatch stopLeadershipLatch = new CountDownLatch(1);
    LeaderElector leaderElector = makeAndRunLeaderElectorAsync("candidate", null, () -> startLeadershipLatch.countDown(), () -> stopLeadershipLatch.countDown(), apiClient);
    startLeadershipLatch.await();
    leaderElector.close();
    stopLeadershipLatch.await();
}
Also used : LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with LeaderElector

use of io.kubernetes.client.extended.leaderelection.LeaderElector 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 4 with LeaderElector

use of io.kubernetes.client.extended.leaderelection.LeaderElector in project java by kubernetes-client.

the class LeaderElectorTest method testMultiCandidateLeaderElection.

@Test(timeout = 30000L)
public void testMultiCandidateLeaderElection() throws Exception {
    CyclicBarrier startBarrier = new CyclicBarrier(2);
    CountDownLatch startBeingLeader = new CountDownLatch(1);
    CountDownLatch stopBeingLeader = new CountDownLatch(1);
    AtomicInteger startBeingLeaderCount = new AtomicInteger();
    AtomicInteger stopBeingLeaderCount = new AtomicInteger();
    AtomicReference<String> leaderRef = new AtomicReference<>();
    String candidate1 = "candidate1";
    String candidate2 = "candidate2";
    LeaderElector leaderElector1 = makeAndRunLeaderElectorAsync(candidate1, startBarrier, () -> {
        startBeingLeaderCount.incrementAndGet();
        leaderRef.set(candidate1);
        startBeingLeader.countDown();
    }, () -> {
        stopBeingLeaderCount.incrementAndGet();
        stopBeingLeader.countDown();
    }, apiClient);
    LeaderElector leaderElector2 = makeAndRunLeaderElectorAsync(candidate2, startBarrier, () -> {
        startBeingLeaderCount.incrementAndGet();
        leaderRef.set(candidate2);
        startBeingLeader.countDown();
    }, () -> {
        stopBeingLeaderCount.incrementAndGet();
        stopBeingLeader.countDown();
    }, apiClient);
    // wait till someone becomes leader
    startBeingLeader.await();
    Assert.assertNotNull(leaderRef.get());
    Assert.assertTrue(candidate1.equals(leaderRef.get()) || candidate2.equals(leaderRef.get()));
    // non-leader doesn't get to become leader
    if (candidate1.equals(leaderRef.get())) {
        leaderElector2.close();
        leaderElector1.close();
    } else {
        leaderElector1.close();
        leaderElector2.close();
    }
    stopBeingLeader.await();
    // make sure that only one candidate became leader
    Assert.assertEquals(1, startBeingLeaderCount.get());
    Assert.assertEquals(1, stopBeingLeaderCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 5 with LeaderElector

use of io.kubernetes.client.extended.leaderelection.LeaderElector in project java by kubernetes-client.

the class LeaderElectingControllerTest method testLeaderElectingController.

@Test(timeout = 90000)
public void testLeaderElectingController() throws ApiException, InterruptedException {
    AtomicReference<LeaderElectionRecord> record = new AtomicReference<>();
    record.set(new LeaderElectionRecord());
    Semaphore latch = new Semaphore(2);
    Semaphore controllerLatch = new Semaphore(2);
    when(mockLock.identity()).thenReturn("foo");
    when(mockLock.get()).thenThrow(new ApiException("Record Not Found", HttpURLConnection.HTTP_NOT_FOUND, null, null)).thenReturn(record.get());
    doAnswer(invocationOnMock -> {
        record.set(invocationOnMock.getArgument(0));
        latch.release();
        return true;
    }).when(mockLock).create(any());
    doAnswer(invocationOnMock -> {
        latch.release();
        return false;
    }).when(mockLock).update(any());
    doAnswer(invocationOnMock -> {
        controllerLatch.release();
        return null;
    }).when(mockController).run();
    doAnswer(invocationOnMock -> {
        controllerLatch.release();
        return null;
    }).when(mockController).shutdown();
    LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(mockLock, Duration.ofMillis(300), Duration.ofMillis(200), Duration.ofMillis(100))), mockController);
    latch.acquire(2);
    controllerLatch.acquire(2);
    Thread controllerThread = new Thread(leaderElectingController::run);
    controllerThread.start();
    latch.acquire(2);
    controllerThread.interrupt();
    verify(mockLock, times(1)).create(any());
    verify(mockLock, atLeastOnce()).update(any());
    controllerLatch.acquire(2);
    verify(mockController, times(1)).run();
    verify(mockController, times(1)).shutdown();
}
Also used : LeaderElectionRecord(io.kubernetes.client.extended.leaderelection.LeaderElectionRecord) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Aggregations

LeaderElector (io.kubernetes.client.extended.leaderelection.LeaderElector)8 LeaderElectionConfig (io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)5 Test (org.junit.Test)4 EndpointsLock (io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Lock (io.kubernetes.client.extended.leaderelection.Lock)2 ConfigMapLock (io.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock)2 ApiClient (io.kubernetes.client.openapi.ApiClient)2 ApiException (io.kubernetes.client.openapi.ApiException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Controller (io.kubernetes.client.extended.controller.Controller)1 ControllerManager (io.kubernetes.client.extended.controller.ControllerManager)1 LeaderElectingController (io.kubernetes.client.extended.controller.LeaderElectingController)1 ControllerBuilder (io.kubernetes.client.extended.controller.builder.ControllerBuilder)1 Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)1 Request (io.kubernetes.client.extended.controller.reconciler.Request)1 Result (io.kubernetes.client.extended.controller.reconciler.Result)1 EventType (io.kubernetes.client.extended.event.EventType)1 EventBroadcaster (io.kubernetes.client.extended.event.legacy.EventBroadcaster)1 EventRecorder (io.kubernetes.client.extended.event.legacy.EventRecorder)1