use of org.apache.flink.runtime.leaderelection.TestingLeaderElectionService in project flink by apache.
the class ResourceManagerHATest method testGrantAndRevokeLeadership.
@Test
public void testGrantAndRevokeLeadership() throws Exception {
final TestingLeaderElectionService leaderElectionService = new TestingLeaderElectionService();
final TestingResourceManagerService resourceManagerService = TestingResourceManagerService.newBuilder().setRmLeaderElectionService(leaderElectionService).build();
try {
resourceManagerService.start();
final UUID leaderId = UUID.randomUUID();
resourceManagerService.isLeader(leaderId);
// after grant leadership, verify resource manager is started with the fencing token
assertEquals(leaderId, leaderElectionService.getConfirmationFuture().get().getLeaderSessionId());
assertTrue(resourceManagerService.getResourceManagerFencingToken().isPresent());
assertEquals(leaderId, resourceManagerService.getResourceManagerFencingToken().get().toUUID());
// then revoke leadership, verify resource manager is closed
final Optional<CompletableFuture<Void>> rmTerminationFutureOpt = resourceManagerService.getResourceManagerTerminationFuture();
assertTrue(rmTerminationFutureOpt.isPresent());
resourceManagerService.notLeader();
rmTerminationFutureOpt.get().get();
resourceManagerService.rethrowFatalErrorIfAny();
} finally {
resourceManagerService.cleanUp();
}
}
use of org.apache.flink.runtime.leaderelection.TestingLeaderElectionService in project flink by apache.
the class ResourceManagerTaskExecutorTest method createAndStartResourceManager.
private void createAndStartResourceManager() throws Exception {
final TestingLeaderElectionService leaderElectionService = new TestingLeaderElectionService();
rmService = TestingResourceManagerService.newBuilder().setRpcService(rpcService).setRmLeaderElectionService(leaderElectionService).build();
rmService.start();
rmService.isLeader(UUID.randomUUID());
leaderElectionService.getConfirmationFuture().thenRun(() -> {
rmGateway = rmService.getResourceManagerGateway().orElseThrow(() -> new AssertionError("RM not available after confirming leadership."));
}).get(TIMEOUT.getSize(), TIMEOUT.getUnit());
}
use of org.apache.flink.runtime.leaderelection.TestingLeaderElectionService in project flink by apache.
the class ResourceManagerTest method setup.
@Before
public void setup() throws Exception {
highAvailabilityServices = new TestingHighAvailabilityServices();
highAvailabilityServices.setResourceManagerLeaderElectionService(new TestingLeaderElectionService());
testingFatalErrorHandler = new TestingFatalErrorHandler();
resourceManagerResourceId = ResourceID.generate();
}
use of org.apache.flink.runtime.leaderelection.TestingLeaderElectionService in project flink by apache.
the class DispatcherCleanupITCase method testCleanupThroughRetries.
@Test
public void testCleanupThroughRetries() throws Exception {
final JobGraph jobGraph = createJobGraph();
final JobID jobId = jobGraph.getJobID();
// JobGraphStore
final AtomicInteger actualGlobalCleanupCallCount = new AtomicInteger();
final OneShotLatch successfulCleanupLatch = new OneShotLatch();
final int numberOfErrors = 5;
final RuntimeException temporaryError = new RuntimeException("Expected RuntimeException: Unable to remove job graph.");
final JobGraphStore jobGraphStore = createAndStartJobGraphStoreWithCleanupFailures(numberOfErrors, temporaryError, actualGlobalCleanupCallCount, successfulCleanupLatch);
haServices.setJobGraphStore(jobGraphStore);
// Construct leader election service.
final TestingLeaderElectionService leaderElectionService = new TestingLeaderElectionService();
haServices.setJobMasterLeaderElectionService(jobId, leaderElectionService);
// start the dispatcher with enough retries on cleanup
final JobManagerRunnerRegistry jobManagerRunnerRegistry = new DefaultJobManagerRunnerRegistry(2);
final Dispatcher dispatcher = createTestingDispatcherBuilder().setResourceCleanerFactory(new DispatcherResourceCleanerFactory(ForkJoinPool.commonPool(), TestingRetryStrategies.createWithNumberOfRetries(numberOfErrors), jobManagerRunnerRegistry, haServices.getJobGraphStore(), blobServer, haServices, UnregisteredMetricGroups.createUnregisteredJobManagerMetricGroup())).build();
dispatcher.start();
toTerminate.add(dispatcher);
leaderElectionService.isLeader(UUID.randomUUID());
final DispatcherGateway dispatcherGateway = dispatcher.getSelfGateway(DispatcherGateway.class);
dispatcherGateway.submitJob(jobGraph, TIMEOUT).get();
waitForJobToFinish(leaderElectionService, dispatcherGateway, jobId);
successfulCleanupLatch.await();
assertThat(actualGlobalCleanupCallCount.get(), equalTo(numberOfErrors + 1));
assertThat("The JobGraph should be removed from JobGraphStore.", haServices.getJobGraphStore().getJobIds(), IsEmptyCollection.empty());
CommonTestUtils.waitUntilCondition(() -> haServices.getJobResultStore().hasJobResultEntry(jobId), Deadline.fromNow(Duration.ofMinutes(5)), "The JobResultStore should have this job marked as clean.");
}
use of org.apache.flink.runtime.leaderelection.TestingLeaderElectionService in project flink by apache.
the class DispatcherCleanupITCase method testCleanupAfterLeadershipChange.
@Test
public void testCleanupAfterLeadershipChange() throws Exception {
final JobGraph jobGraph = createJobGraph();
final JobID jobId = jobGraph.getJobID();
// Construct job graph store.
final AtomicInteger actualGlobalCleanupCallCount = new AtomicInteger();
final OneShotLatch successfulCleanupLatch = new OneShotLatch();
final RuntimeException temporaryError = new RuntimeException("Unable to remove job graph.");
final JobGraphStore jobGraphStore = createAndStartJobGraphStoreWithCleanupFailures(1, temporaryError, actualGlobalCleanupCallCount, successfulCleanupLatch);
haServices.setJobGraphStore(jobGraphStore);
// Construct leader election service.
final TestingLeaderElectionService leaderElectionService = new TestingLeaderElectionService();
haServices.setJobMasterLeaderElectionService(jobId, leaderElectionService);
// start the dispatcher with no retries on cleanup
final CountDownLatch jobGraphRemovalErrorReceived = new CountDownLatch(1);
final Dispatcher dispatcher = createTestingDispatcherBuilder().setFatalErrorHandler(throwable -> {
final Optional<Throwable> maybeError = ExceptionUtils.findThrowable(throwable, temporaryError::equals);
if (maybeError.isPresent()) {
jobGraphRemovalErrorReceived.countDown();
} else {
testingFatalErrorHandlerResource.getFatalErrorHandler().onFatalError(throwable);
}
}).build();
dispatcher.start();
toTerminate.add(dispatcher);
leaderElectionService.isLeader(UUID.randomUUID());
final DispatcherGateway dispatcherGateway = dispatcher.getSelfGateway(DispatcherGateway.class);
dispatcherGateway.submitJob(jobGraph, TIMEOUT).get();
waitForJobToFinish(leaderElectionService, dispatcherGateway, jobId);
jobGraphRemovalErrorReceived.await();
// Remove job master leadership.
leaderElectionService.notLeader();
// This will clear internal state of election service, so a new contender can register.
leaderElectionService.stop();
assertThat(successfulCleanupLatch.isTriggered(), CoreMatchers.is(false));
assertThat("The JobGraph is still stored in the JobGraphStore.", haServices.getJobGraphStore().getJobIds(), CoreMatchers.is(Collections.singleton(jobId)));
assertThat("The JobResultStore has this job marked as dirty.", haServices.getJobResultStore().getDirtyResults().stream().map(JobResult::getJobId).collect(Collectors.toSet()), CoreMatchers.is(Collections.singleton(jobId)));
// Run a second dispatcher, that restores our finished job.
final Dispatcher secondDispatcher = createTestingDispatcherBuilder().setRecoveredDirtyJobs(haServices.getJobResultStore().getDirtyResults()).build();
secondDispatcher.start();
toTerminate.add(secondDispatcher);
leaderElectionService.isLeader(UUID.randomUUID());
CommonTestUtils.waitUntilCondition(() -> haServices.getJobResultStore().getDirtyResults().isEmpty(), Deadline.fromNow(TimeUtils.toDuration(TIMEOUT)));
assertThat("The JobGraph is not stored in the JobGraphStore.", haServices.getJobGraphStore().getJobIds(), IsEmptyCollection.empty());
assertTrue("The JobResultStore has the job listed as clean.", haServices.getJobResultStore().hasJobResultEntry(jobId));
// wait for the successful cleanup to be triggered
successfulCleanupLatch.await();
assertThat(actualGlobalCleanupCallCount.get(), equalTo(2));
}
Aggregations