Search in sources :

Example 1 with LeaderInformation

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()));
            });
        }
    };
}
Also used : LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.Test)

Example 2 with LeaderInformation

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));
            });
        }
    };
}
Also used : LeaderElectionEvent(org.apache.flink.runtime.leaderelection.LeaderElectionEvent) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.jupiter.api.Test)

Example 3 with LeaderInformation

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);
            });
        }
    };
}
Also used : TestingListener(org.apache.flink.runtime.leaderelection.TestingListener) LeaderElectionEvent(org.apache.flink.runtime.leaderelection.LeaderElectionEvent) DefaultLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.jupiter.api.Test)

Example 4 with 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));
    }
}
Also used : LEADER_ADDRESS_KEY(org.apache.flink.kubernetes.utils.Constants.LEADER_ADDRESS_KEY) LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY(org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) LoggerFactory(org.slf4j.LoggerFactory) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) Map(java.util.Map) FatalErrorHandler(org.apache.flink.runtime.rpc.FatalErrorHandler) KubernetesUtils.checkConfigMaps(org.apache.flink.kubernetes.utils.KubernetesUtils.checkConfigMaps) LeaderElectionEventHandler(org.apache.flink.runtime.leaderelection.LeaderElectionEventHandler) LeaderElectionException(org.apache.flink.runtime.leaderelection.LeaderElectionException) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) ExecutorService(java.util.concurrent.ExecutorService) KubernetesUtils(org.apache.flink.kubernetes.utils.KubernetesUtils) Logger(org.slf4j.Logger) KubernetesUtils.getLeaderInformationFromConfigMap(org.apache.flink.kubernetes.utils.KubernetesUtils.getLeaderInformationFromConfigMap) LeaderElectionDriver(org.apache.flink.runtime.leaderelection.LeaderElectionDriver) UUID(java.util.UUID) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Watch(org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch) List(java.util.List) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Optional(java.util.Optional) LEADER_SESSION_ID_KEY(org.apache.flink.kubernetes.utils.Constants.LEADER_SESSION_ID_KEY) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) UUID(java.util.UUID) LeaderElectionException(org.apache.flink.runtime.leaderelection.LeaderElectionException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 5 with LeaderInformation

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;
}
Also used : LeaderInformationWithComponentId(org.apache.flink.runtime.leaderelection.LeaderInformationWithComponentId) ArrayList(java.util.ArrayList) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Map(java.util.Map) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation)

Aggregations

LeaderInformation (org.apache.flink.runtime.leaderelection.LeaderInformation)11 Test (org.junit.Test)6 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)3 KubernetesConfigMap (org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap)3 Map (java.util.Map)2 UUID (java.util.UUID)2 ExecutorService (java.util.concurrent.ExecutorService)2 KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)2 KubernetesConfigMapSharedWatcher (org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher)2 LeaderElectionEvent (org.apache.flink.runtime.leaderelection.LeaderElectionEvent)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Configuration (org.apache.flink.configuration.Configuration)1 Watch (org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch)1 KubernetesException (org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)1 KubernetesLeaderElector (org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector)1 LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY (org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY)1 LEADER_ADDRESS_KEY (org.apache.flink.kubernetes.utils.Constants.LEADER_ADDRESS_KEY)1