use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.
the class KubernetesStateHandleStoreITCase method testMultipleKubernetesStateHandleStores.
@Test
public void testMultipleKubernetesStateHandleStores() throws Exception {
final Configuration configuration = kubernetesResource.getConfiguration();
final String leaderConfigMapName = LEADER_CONFIGMAP_NAME + System.currentTimeMillis();
final int leaderNum = 3;
final KubernetesLeaderElector[] leaderElectors = new KubernetesLeaderElector[leaderNum];
final FlinkKubeClient[] kubeClients = new FlinkKubeClient[leaderNum];
final TestingLeaderCallbackHandler[] leaderCallbackHandlers = new TestingLeaderCallbackHandler[leaderNum];
@SuppressWarnings("unchecked") final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle>[] stateHandleStores = new KubernetesStateHandleStore[leaderNum];
try {
for (int i = 0; i < leaderNum; i++) {
final String lockIdentity = UUID.randomUUID().toString();
kubeClients[i] = kubeClientFactory.fromConfiguration(configuration, "testing");
leaderCallbackHandlers[i] = new TestingLeaderCallbackHandler(lockIdentity);
leaderElectors[i] = kubeClients[i].createLeaderElector(new KubernetesLeaderElectionConfiguration(leaderConfigMapName, lockIdentity, configuration), leaderCallbackHandlers[i]);
stateHandleStores[i] = new KubernetesStateHandleStore<>(kubeClients[i], leaderConfigMapName, new TestingLongStateHandleHelper(), (ignore) -> true, lockIdentity);
leaderElectors[i].run();
}
// Wait for the leader
final String lockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT);
Long expectedState = null;
for (int i = 0; i < leaderNum; i++) {
// leader
if (leaderCallbackHandlers[i].getLockIdentity().equals(lockIdentity)) {
expectedState = (long) i;
}
stateHandleStores[i].addAndLock(KEY, new TestingLongStateHandleHelper.LongStateHandle(i));
}
// Only the leader could add successfully
assertThat(expectedState, is(notNullValue()));
assertThat(stateHandleStores[0].getAllAndLock().size(), is(1));
assertThat(stateHandleStores[0].getAllAndLock().get(0).f0.retrieveState().getValue(), is(expectedState));
assertThat(stateHandleStores[0].getAllAndLock().get(0).f1, is(KEY));
} finally {
TestingLongStateHandleHelper.clearGlobalState();
// Cleanup the resources
for (int i = 0; i < leaderNum; i++) {
if (leaderElectors[i] != null) {
leaderElectors[i].stop();
}
if (kubeClients[i] != null) {
kubeClients[i].close();
}
}
kubernetesResource.getFlinkKubeClient().deleteConfigMap(leaderConfigMapName).get();
}
}
use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.
the class KubernetesLeaderElectorITCase method testMultipleKubernetesLeaderElectors.
@Test
public void testMultipleKubernetesLeaderElectors() throws Exception {
final Configuration configuration = kubernetesResource.getConfiguration();
final String leaderConfigMapName = LEADER_CONFIGMAP_NAME_PREFIX + System.currentTimeMillis();
final int leaderNum = 3;
final KubernetesLeaderElector[] leaderElectors = new KubernetesLeaderElector[leaderNum];
// We use different Kubernetes clients for different leader electors.
final FlinkKubeClient[] kubeClients = new FlinkKubeClient[leaderNum];
final TestingLeaderCallbackHandler[] leaderCallbackHandlers = new TestingLeaderCallbackHandler[leaderNum];
try {
for (int i = 0; i < leaderNum; i++) {
kubeClients[i] = kubeClientFactory.fromConfiguration(configuration, "testing");
leaderCallbackHandlers[i] = new TestingLeaderCallbackHandler(UUID.randomUUID().toString());
final KubernetesLeaderElectionConfiguration leaderConfig = new KubernetesLeaderElectionConfiguration(leaderConfigMapName, leaderCallbackHandlers[i].getLockIdentity(), configuration);
leaderElectors[i] = kubeClients[i].createLeaderElector(leaderConfig, leaderCallbackHandlers[i]);
// Start the leader electors to contend the leader
leaderElectors[i].run();
}
// Wait for the first leader
final String firstLockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT);
for (int i = 0; i < leaderNum; i++) {
if (leaderCallbackHandlers[i].getLockIdentity().equals(firstLockIdentity)) {
// Check the callback isLeader is called.
leaderCallbackHandlers[i].waitForNewLeader(TIMEOUT);
assertThat(leaderCallbackHandlers[i].hasLeadership(), is(true));
// Current leader died
leaderElectors[i].stop();
// Check the callback notLeader is called.
leaderCallbackHandlers[i].waitForRevokeLeader(TIMEOUT);
assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false));
} else {
assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false));
}
}
// Another leader should be elected successfully and update the lock identity
final String anotherLockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT);
assertThat(anotherLockIdentity, is(not(firstLockIdentity)));
} finally {
// Cleanup the resources
for (int i = 0; i < leaderNum; i++) {
if (leaderElectors[i] != null) {
leaderElectors[i].stop();
}
if (kubeClients[i] != null) {
kubeClients[i].close();
}
}
kubernetesResource.getFlinkKubeClient().deleteConfigMap(leaderConfigMapName).get();
}
}
use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.
the class KubernetesMultipleComponentLeaderElectionHaServices method getOrInitializeSingleLeaderElectionService.
private DefaultMultipleComponentLeaderElectionService getOrInitializeSingleLeaderElectionService() {
synchronized (lock) {
if (multipleComponentLeaderElectionService == null) {
try {
final KubernetesLeaderElectionConfiguration leaderElectionConfiguration = new KubernetesLeaderElectionConfiguration(getClusterConfigMap(), lockIdentity, configuration);
multipleComponentLeaderElectionService = new DefaultMultipleComponentLeaderElectionService(fatalErrorHandler, new KubernetesMultipleComponentLeaderElectionDriverFactory(kubeClient, leaderElectionConfiguration, configMapSharedWatcher, watchExecutorService, fatalErrorHandler));
} catch (Exception e) {
throw new FlinkRuntimeException("Could not initialize the default single leader election service.", e);
}
}
return multipleComponentLeaderElectionService;
}
}
use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.
the class KubernetesLeaderElectionAndRetrievalITCase method testLeaderElectionAndRetrieval.
@Test
public void testLeaderElectionAndRetrieval() throws Exception {
final String configMapName = LEADER_CONFIGMAP_NAME + System.currentTimeMillis();
KubernetesLeaderElectionDriver leaderElectionDriver = null;
KubernetesLeaderRetrievalDriver leaderRetrievalDriver = null;
final FlinkKubeClient flinkKubeClient = kubernetesResource.getFlinkKubeClient();
final Configuration configuration = kubernetesResource.getConfiguration();
final String clusterId = configuration.getString(KubernetesConfigOptions.CLUSTER_ID);
final KubernetesConfigMapSharedWatcher configMapSharedWatcher = flinkKubeClient.createConfigMapSharedWatcher(KubernetesUtils.getConfigMapLabels(clusterId, LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY));
final ExecutorService watchExecutorService = Executors.newCachedThreadPool();
final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
try {
leaderElectionDriver = new KubernetesLeaderElectionDriver(flinkKubeClient, configMapSharedWatcher, watchExecutorService, new KubernetesLeaderElectionConfiguration(configMapName, UUID.randomUUID().toString(), configuration), electionEventHandler, electionEventHandler::handleError);
electionEventHandler.init(leaderElectionDriver);
final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
leaderRetrievalDriver = new KubernetesLeaderRetrievalDriver(flinkKubeClient, configMapSharedWatcher, watchExecutorService, configMapName, retrievalEventHandler, KubernetesUtils::getLeaderInformationFromConfigMap, retrievalEventHandler::handleError);
electionEventHandler.waitForLeader(TIMEOUT);
// Check the new leader is confirmed
final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
// Check the leader retrieval driver should be notified the leader address
retrievalEventHandler.waitForNewLeader(TIMEOUT);
assertThat(retrievalEventHandler.getLeaderSessionID(), is(confirmedLeaderInformation.getLeaderSessionID()));
assertThat(retrievalEventHandler.getAddress(), is(confirmedLeaderInformation.getLeaderAddress()));
} finally {
electionEventHandler.close();
if (leaderElectionDriver != null) {
leaderElectionDriver.close();
}
if (leaderRetrievalDriver != null) {
leaderRetrievalDriver.close();
}
flinkKubeClient.deleteConfigMap(configMapName).get();
configMapSharedWatcher.close();
ExecutorUtils.gracefulShutdown(5, TimeUnit.SECONDS, watchExecutorService);
}
}
Aggregations