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();
}
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();
}
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;
}
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());
}
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();
}
Aggregations