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();
}
}
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);
}
}
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);
}
}
}
Aggregations