use of org.apache.flink.runtime.leaderelection.LeaderInformation in project flink by apache.
the class KubernetesLeaderElectionDriverTest method testWriteLeaderInformation.
@Test
public void testWriteLeaderInformation() throws Exception {
new Context() {
{
runTest(() -> {
leaderCallbackGrantLeadership();
final LeaderInformation leader = LeaderInformation.known(UUID.randomUUID(), LEADER_ADDRESS);
leaderElectionDriver.writeLeaderInformation(leader);
assertThat(getLeaderConfigMap().getData().get(LEADER_ADDRESS_KEY), is(leader.getLeaderAddress()));
assertThat(getLeaderConfigMap().getData().get(LEADER_SESSION_ID_KEY), is(leader.getLeaderSessionID().toString()));
});
}
};
}
use of org.apache.flink.runtime.leaderelection.LeaderInformation in project flink by apache.
the class KubernetesMultipleComponentLeaderElectionDriverTest method testLeaderInformationChangeNotifiesListener.
@Test
public void testLeaderInformationChangeNotifiesListener() throws Exception {
new TestFixture() {
{
runTest(() -> {
leaderCallbackGrantLeadership();
final String componentA = "componentA";
final LeaderInformation leaderInformationA = LeaderInformation.known(UUID.randomUUID(), "localhost");
final String componentB = "componentB";
final LeaderInformation leaderInformationB = LeaderInformation.known(UUID.randomUUID(), "localhost");
leaderElectionDriver.publishLeaderInformation(componentA, leaderInformationA);
leaderElectionDriver.publishLeaderInformation(componentB, leaderInformationB);
notifyLeaderElectionWatchOnModifiedConfigMap();
final LeaderElectionEvent.AllKnownLeaderInformationEvent allKnownLeaderInformationEvent = leaderElectionListener.await(LeaderElectionEvent.AllKnownLeaderInformationEvent.class);
assertThat(allKnownLeaderInformationEvent.getLeaderInformationWithComponentIds()).containsExactlyInAnyOrder(LeaderInformationWithComponentId.create(componentA, leaderInformationA), LeaderInformationWithComponentId.create(componentB, leaderInformationB));
});
}
};
}
use of org.apache.flink.runtime.leaderelection.LeaderInformation in project flink by apache.
the class KubernetesMultipleComponentLeaderElectionDriverTest method testPublishLeaderInformation.
@Test
public void testPublishLeaderInformation() throws Exception {
new TestFixture() {
{
runTest(() -> {
leaderCallbackGrantLeadership();
leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
final LeaderInformation leaderInformation = LeaderInformation.known(UUID.randomUUID(), "localhost");
final String componentId = "componentId";
final DefaultLeaderRetrievalService leaderRetrievalService = new DefaultLeaderRetrievalService(new KubernetesMultipleComponentLeaderRetrievalDriverFactory(getFlinkKubeClient(), getConfigMapSharedWatcher(), testExecutorExtension.getExecutor(), LEADER_CONFIGMAP_NAME, componentId));
final TestingListener leaderRetrievalListener = new TestingListener();
leaderRetrievalService.start(leaderRetrievalListener);
leaderElectionDriver.publishLeaderInformation(componentId, leaderInformation);
notifyLeaderRetrievalWatchOnModifiedConfigMap();
leaderRetrievalListener.waitForNewLeader(10_000L);
assertThat(leaderRetrievalListener.getLeader()).isEqualTo(leaderInformation);
});
}
};
}
use of org.apache.flink.runtime.leaderelection.LeaderInformation in project flink by apache.
the class KubernetesLeaderElectionDriver method writeLeaderInformation.
@Override
public void writeLeaderInformation(LeaderInformation leaderInformation) {
assert (running);
final UUID confirmedLeaderSessionID = leaderInformation.getLeaderSessionID();
final String confirmedLeaderAddress = leaderInformation.getLeaderAddress();
try {
kubeClient.checkAndUpdateConfigMap(configMapName, configMap -> {
if (KubernetesLeaderElector.hasLeadership(configMap, lockIdentity)) {
// Get the updated ConfigMap with new leader information
if (confirmedLeaderAddress == null) {
configMap.getData().remove(LEADER_ADDRESS_KEY);
} else {
configMap.getData().put(LEADER_ADDRESS_KEY, confirmedLeaderAddress);
}
if (confirmedLeaderSessionID == null) {
configMap.getData().remove(LEADER_SESSION_ID_KEY);
} else {
configMap.getData().put(LEADER_SESSION_ID_KEY, confirmedLeaderSessionID.toString());
}
configMap.getLabels().putAll(configMapLabels);
return Optional.of(configMap);
}
return Optional.empty();
}).get();
if (LOG.isDebugEnabled()) {
LOG.debug("Successfully wrote leader information: Leader={}, session ID={}.", confirmedLeaderAddress, confirmedLeaderSessionID);
}
} catch (Exception e) {
fatalErrorHandler.onFatalError(new KubernetesException("Could not write leader information since ConfigMap " + configMapName + " does not exist.", e));
}
}
use of org.apache.flink.runtime.leaderelection.LeaderInformation in project flink by apache.
the class KubernetesMultipleComponentLeaderElectionDriver method extractLeaderInformation.
private static Collection<LeaderInformationWithComponentId> extractLeaderInformation(KubernetesConfigMap configMap) {
final Map<String, String> data = configMap.getData();
final Collection<LeaderInformationWithComponentId> leaderInformationWithLeaderNames = new ArrayList<>();
for (Map.Entry<String, String> keyValuePair : data.entrySet()) {
final String key = keyValuePair.getKey();
if (KubernetesUtils.isSingleLeaderKey(key)) {
final String leaderName = KubernetesUtils.extractLeaderName(key);
final LeaderInformation leaderInformation = KubernetesUtils.parseLeaderInformationSafely(keyValuePair.getValue()).orElse(LeaderInformation.empty());
leaderInformationWithLeaderNames.add(LeaderInformationWithComponentId.create(leaderName, leaderInformation));
}
}
return leaderInformationWithLeaderNames;
}
Aggregations