Search in sources :

Example 1 with KubernetesConfigMapSharedWatcher

use of org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher in project flink by apache.

the class KubernetesSharedInformerITCase method testWatchWithBlockHandler.

@Test
public void testWatchWithBlockHandler() throws Exception {
    try (KubernetesConfigMapSharedWatcher watcher = client.createConfigMapSharedWatcher(labels)) {
        final String configMapName = getConfigMapName(System.currentTimeMillis());
        final long block = 500;
        final long maxUpdateVal = 30;
        final TestingBlockCallbackHandler handler = new TestingBlockCallbackHandler(block, maxUpdateVal);
        final Watch watch = watcher.watch(configMapName, handler, watchCallbackExecutorService);
        createConfigMap(configMapName);
        for (int i = 1; i <= maxUpdateVal; i++) {
            updateConfigMap(configMapName, ImmutableMap.of("val", String.valueOf(i)));
        }
        try {
            handler.expectedFuture.get(120, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            fail("expected value: " + maxUpdateVal + ", actual: " + handler.val);
        }
        try {
            handler.assertFuture.get(2 * block, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
        // expected
        }
        watch.close();
    }
}
Also used : KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) Watch(org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 2 with KubernetesConfigMapSharedWatcher

use of org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher 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);
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderElectionEventHandler(org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) ExecutorService(java.util.concurrent.ExecutorService) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.Test)

Example 3 with KubernetesConfigMapSharedWatcher

use of org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher in project flink by apache.

the class KubernetesSharedInformerITCase method testWatch.

@Test(timeout = 120000)
public void testWatch() throws Exception {
    try (KubernetesConfigMapSharedWatcher watcher = client.createConfigMapSharedWatcher(labels)) {
        for (int i = 0; i < 10; i++) {
            List<TestingCallbackHandler> callbackHandlers = new ArrayList<>();
            List<Watch> watchers = new ArrayList<>();
            watchInRange(watcher, callbackHandlers, watchers, 0, 20);
            createConfigMapsInRange(0, 5);
            watchInRange(watcher, callbackHandlers, watchers, 20, 40);
            createConfigMapsInRange(5, 10);
            updateConfigMapInRange(0, 10, ImmutableMap.of("foo", "bar"));
            for (TestingCallbackHandler handler : callbackHandlers) {
                handler.addFuture.get();
                handler.addOrUpdateFuture.get();
                assertFalse(handler.assertFuture.isCompletedExceptionally());
            }
            watchers.forEach(Watch::close);
            callbackHandlers.clear();
            watchers.clear();
            watchInRange(watcher, callbackHandlers, watchers, 40, 60);
            for (TestingCallbackHandler handler : callbackHandlers) {
                handler.addFuture.get();
                handler.addOrUpdateFuture.get();
            }
            client.deleteConfigMapsByLabels(labels).get();
            for (TestingCallbackHandler handler : callbackHandlers) {
                handler.deleteFuture.get();
                if (handler.assertFuture.isCompletedExceptionally()) {
                    handler.assertFuture.get();
                }
            }
            watchers.forEach(Watch::close);
        }
    }
}
Also used : KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) ArrayList(java.util.ArrayList) Watch(org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch) Test(org.junit.Test)

Aggregations

KubernetesConfigMapSharedWatcher (org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher)3 Test (org.junit.Test)3 Watch (org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch)2 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 Configuration (org.apache.flink.configuration.Configuration)1 KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)1 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)1 LeaderInformation (org.apache.flink.runtime.leaderelection.LeaderInformation)1 TestingLeaderElectionEventHandler (org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler)1 TestingLeaderRetrievalEventHandler (org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler)1