use of org.apache.flink.kubernetes.kubeclient.FlinkKubeClient 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.kubeclient.FlinkKubeClient in project flink by apache.
the class KubernetesStateHandleStoreTest method testReplaceFailedWithPossiblyInconsistentState.
@Test
public void testReplaceFailedWithPossiblyInconsistentState() throws Exception {
final PossibleInconsistentStateException updateException = new PossibleInconsistentStateException();
new Context() {
{
runTest(() -> {
leaderCallbackGrantLeadership();
final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new KubernetesStateHandleStore<>(flinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
store.addAndLock(key, state);
final FlinkKubeClient anotherFlinkKubeClient = createFlinkKubeClientBuilder().setCheckAndUpdateConfigMapFunction((configMapName, function) -> FutureUtils.completedExceptionally(updateException)).build();
final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> anotherStore = new KubernetesStateHandleStore<>(anotherFlinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
final StringResourceVersion resourceVersion = anotherStore.exists(key);
assertThat(resourceVersion.isExisting(), is(true));
try {
anotherStore.replace(key, resourceVersion, new TestingLongStateHandleHelper.LongStateHandle(23456L));
fail("An exception having a PossibleInconsistentStateException as its cause should have been thrown.");
} catch (Exception ex) {
assertThat(ex, is(updateException));
}
assertThat(anotherStore.getAllAndLock().size(), is(1));
// The state does not change
assertThat(anotherStore.getAndLock(key).retrieveState(), is(state));
assertThat(TestingLongStateHandleHelper.getGlobalStorageSize(), is(2));
// no state was discarded
assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0), is(0));
assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(1), is(0));
});
}
};
}
use of org.apache.flink.kubernetes.kubeclient.FlinkKubeClient 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.kubeclient.FlinkKubeClient 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.FlinkKubeClient in project flink by apache.
the class KubernetesStateHandleStoreTest method testReplaceFailedAndDiscardState.
@Test
public void testReplaceFailedAndDiscardState() throws Exception {
final FlinkRuntimeException updateException = new FlinkRuntimeException("Failed to update");
new Context() {
{
runTest(() -> {
leaderCallbackGrantLeadership();
final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new KubernetesStateHandleStore<>(flinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
store.addAndLock(key, state);
final FlinkKubeClient anotherFlinkKubeClient = createFlinkKubeClientBuilder().setCheckAndUpdateConfigMapFunction((configMapName, function) -> {
throw updateException;
}).build();
final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> anotherStore = new KubernetesStateHandleStore<>(anotherFlinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
final TestingLongStateHandleHelper.LongStateHandle newState = new TestingLongStateHandleHelper.LongStateHandle(23456L);
final StringResourceVersion resourceVersion = anotherStore.exists(key);
assertThat(resourceVersion.isExisting(), is(true));
try {
anotherStore.replace(key, resourceVersion, newState);
fail("We should get an exception when kube client failed to update.");
} catch (Exception ex) {
assertThat(ex, FlinkMatchers.containsCause(updateException));
}
assertThat(anotherStore.getAllAndLock().size(), is(1));
// The state do not change
assertThat(anotherStore.getAndLock(key).retrieveState(), is(state));
assertThat(TestingLongStateHandleHelper.getGlobalStorageSize(), is(2));
assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0), is(0));
assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(1), is(1));
});
}
};
}
Aggregations