Search in sources :

Example 1 with PossibleInconsistentStateException

use of org.apache.flink.runtime.persistence.PossibleInconsistentStateException in project flink by apache.

the class Fabric8FlinkKubeClientTest method testCheckAndUpdateConfigMapWhenGetConfigMapFailed.

@Test
public void testCheckAndUpdateConfigMapWhenGetConfigMapFailed() throws Exception {
    final int configuredRetries = flinkConfig.getInteger(KubernetesConfigOptions.KUBERNETES_TRANSACTIONAL_OPERATION_MAX_RETRIES);
    final KubernetesConfigMap configMap = buildTestingConfigMap();
    this.flinkKubeClient.createConfigMap(configMap).get();
    mockGetConfigMapFailed(configMap.getInternalResource());
    final int initialRequestCount = server.getRequestCount();
    try {
        this.flinkKubeClient.checkAndUpdateConfigMap(TESTING_CONFIG_MAP_NAME, c -> {
            throw new AssertionError("The replace operation should have never been triggered.");
        }).get();
        fail("checkAndUpdateConfigMap should fail without a PossibleInconsistentStateException being the cause when number of retries has been exhausted.");
    } catch (Exception ex) {
        assertThat(ex, FlinkMatchers.containsMessage("Could not complete the " + "operation. Number of retries has been exhausted."));
        final int actualRetryCount = server.getRequestCount() - initialRequestCount;
        assertThat(actualRetryCount, is(configuredRetries + 1));
        assertThat("An error while retrieving the ConfigMap should not cause a PossibleInconsistentStateException.", ExceptionUtils.findThrowable(ex, PossibleInconsistentStateException.class).isPresent(), is(false));
    }
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Arrays(java.util.Arrays) KubernetesTestUtils(org.apache.flink.kubernetes.KubernetesTestUtils) ExceptionUtils(org.apache.flink.util.ExceptionUtils) KubernetesService(org.apache.flink.kubernetes.kubeclient.resources.KubernetesService) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesPod(org.apache.flink.kubernetes.kubeclient.resources.KubernetesPod) CONFIG_FILE_LOG4J_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOG4J_NAME) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ExternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator) BlobServerOptions(org.apache.flink.configuration.BlobServerOptions) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) KubernetesClientTestBase(org.apache.flink.kubernetes.KubernetesClientTestBase) JobManagerOptions(org.apache.flink.configuration.JobManagerOptions) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Matchers.is(org.hamcrest.Matchers.is) Constants(org.apache.flink.kubernetes.utils.Constants) ClusterSpecification(org.apache.flink.client.deployment.ClusterSpecification) FlinkMatchers(org.apache.flink.core.testutils.FlinkMatchers) KubernetesConfigOptionsInternal(org.apache.flink.kubernetes.configuration.KubernetesConfigOptionsInternal) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) ArrayList(java.util.ArrayList) DeploymentOptions(org.apache.flink.configuration.DeploymentOptions) RestOptions(org.apache.flink.configuration.RestOptions) Service(io.fabric8.kubernetes.api.model.Service) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesJobManagerParameters(org.apache.flink.kubernetes.kubeclient.parameters.KubernetesJobManagerParameters) Matchers.isIn(org.hamcrest.Matchers.isIn) KubernetesDeploymentTarget(org.apache.flink.kubernetes.configuration.KubernetesDeploymentTarget) ExecutorService(java.util.concurrent.ExecutorService) Assert.assertNotNull(org.junit.Assert.assertNotNull) Pod(io.fabric8.kubernetes.api.model.Pod) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ExecutionException(java.util.concurrent.ExecutionException) Assert.assertNull(org.junit.Assert.assertNull) CONFIG_FILE_LOGBACK_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOGBACK_NAME) KubernetesJobManagerFactory(org.apache.flink.kubernetes.kubeclient.factory.KubernetesJobManagerFactory) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Assert.assertEquals(org.junit.Assert.assertEquals) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with PossibleInconsistentStateException

use of org.apache.flink.runtime.persistence.PossibleInconsistentStateException 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));
            });
        }
    };
}
Also used : Arrays(java.util.Arrays) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Predicate(java.util.function.Predicate) FlinkMatchers(org.apache.flink.core.testutils.FlinkMatchers) Matchers.not(org.hamcrest.Matchers.not) Test(org.junit.Test) Collectors(java.util.stream.Collectors) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) List(java.util.List) JobID(org.apache.flink.api.common.JobID) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) FunctionUtils(org.apache.flink.util.function.FunctionUtils) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) StateHandleStore(org.apache.flink.runtime.persistence.StateHandleStore) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) Comparator(java.util.Comparator) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) Before(org.junit.Before) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Test(org.junit.Test)

Example 3 with PossibleInconsistentStateException

use of org.apache.flink.runtime.persistence.PossibleInconsistentStateException in project flink by apache.

the class ZooKeeperStateHandleStore method addAndLock.

/**
 * Creates a state handle, stores it in ZooKeeper and locks it. A locked node cannot be removed
 * by another {@link ZooKeeperStateHandleStore} instance as long as this instance remains
 * connected to ZooKeeper.
 *
 * <p><strong>Important</strong>: This will <em>not</em> store the actual state in ZooKeeper,
 * but create a state handle and store it in ZooKeeper. This level of indirection makes sure
 * that data in ZooKeeper is small.
 *
 * <p>The operation will fail if there is already a node under the given path.
 *
 * @param pathInZooKeeper Destination path in ZooKeeper (expected to *not* exist yet)
 * @param state State to be added
 * @return The Created {@link RetrievableStateHandle}.
 * @throws PossibleInconsistentStateException if the write-to-ZooKeeper operation failed. This
 *     indicates that it's not clear whether the new state was successfully written to ZooKeeper
 *     or not. Proper error handling has to be applied on the caller's side.
 * @throws Exception If a ZooKeeper or state handle operation fails
 */
@Override
public RetrievableStateHandle<T> addAndLock(String pathInZooKeeper, T state) throws PossibleInconsistentStateException, Exception {
    checkNotNull(pathInZooKeeper, "Path in ZooKeeper");
    checkNotNull(state, "State");
    final String path = normalizePath(pathInZooKeeper);
    if (exists(path).isExisting()) {
        throw new AlreadyExistException(String.format("ZooKeeper node %s already exists.", path));
    }
    final RetrievableStateHandle<T> storeHandle = storage.store(state);
    final byte[] serializedStoreHandle = serializeOrDiscard(storeHandle);
    try {
        writeStoreHandleTransactionally(path, serializedStoreHandle);
        return storeHandle;
    } catch (KeeperException.NodeExistsException e) {
        // we can assume that this is a result of the retry mechanism.
        return storeHandle;
    } catch (Exception e) {
        if (indicatesPossiblyInconsistentState(e)) {
            throw new PossibleInconsistentStateException(e);
        }
        // In case of any other failure, discard the state and rethrow the exception.
        storeHandle.discardState();
        throw e;
    }
}
Also used : PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) IOException(java.io.IOException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException)

Example 4 with PossibleInconsistentStateException

use of org.apache.flink.runtime.persistence.PossibleInconsistentStateException in project flink by apache.

the class ZooKeeperStateHandleStore method replace.

/**
 * Replaces a state handle in ZooKeeper and discards the old state handle.
 *
 * @param pathInZooKeeper Destination path in ZooKeeper (expected to exist and start with a '/')
 * @param expectedVersion Expected version of the node to replace
 * @param state The new state to replace the old one
 * @throws Exception If a ZooKeeper or state handle operation fails
 */
@Override
public void replace(String pathInZooKeeper, IntegerResourceVersion expectedVersion, T state) throws Exception {
    checkNotNull(pathInZooKeeper, "Path in ZooKeeper");
    checkNotNull(state, "State");
    final String path = normalizePath(pathInZooKeeper);
    RetrievableStateHandle<T> oldStateHandle = get(path, false);
    RetrievableStateHandle<T> newStateHandle = storage.store(state);
    final byte[] serializedStateHandle = serializeOrDiscard(newStateHandle);
    // initialize flags to serve the failure case
    boolean discardOldState = false;
    boolean discardNewState = true;
    try {
        setStateHandle(path, serializedStateHandle, expectedVersion.getValue());
        // swap subject for deletion in case of success
        discardOldState = true;
        discardNewState = false;
    } catch (Exception e) {
        if (indicatesPossiblyInconsistentState(e)) {
            // it's unclear whether the state handle metadata was written to ZooKeeper -
            // hence, we don't discard any data
            discardNewState = false;
            throw new PossibleInconsistentStateException(e);
        }
        // We wrap the exception here so that it could be caught in DefaultJobGraphStore
        throw ExceptionUtils.findThrowable(e, KeeperException.NoNodeException.class).map(nnee -> new NotExistException("ZooKeeper node " + path + " does not exist.", nnee)).orElseThrow(() -> e);
    } finally {
        if (discardOldState) {
            oldStateHandle.discardState();
        }
        if (discardNewState) {
            newStateHandle.discardState();
        }
    }
}
Also used : PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) IOException(java.io.IOException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException)

Example 5 with PossibleInconsistentStateException

use of org.apache.flink.runtime.persistence.PossibleInconsistentStateException in project flink by apache.

the class Fabric8FlinkKubeClientTest method testCheckAndUpdateConfigMapWhenReplaceConfigMapFailed.

@Test
public void testCheckAndUpdateConfigMapWhenReplaceConfigMapFailed() throws Exception {
    final int configuredRetries = flinkConfig.getInteger(KubernetesConfigOptions.KUBERNETES_TRANSACTIONAL_OPERATION_MAX_RETRIES);
    final KubernetesConfigMap configMap = buildTestingConfigMap();
    this.flinkKubeClient.createConfigMap(configMap).get();
    mockReplaceConfigMapFailed(configMap.getInternalResource());
    final AtomicInteger retries = new AtomicInteger(0);
    try {
        this.flinkKubeClient.checkAndUpdateConfigMap(TESTING_CONFIG_MAP_NAME, c -> {
            retries.incrementAndGet();
            return Optional.of(configMap);
        }).get();
        fail("checkAndUpdateConfigMap should fail due to a PossibleInconsistentStateException when number of retries has been exhausted.");
    } catch (Exception ex) {
        assertThat(ex, FlinkMatchers.containsMessage("Could not complete the " + "operation. Number of retries has been exhausted."));
        assertThat(retries.get(), is(configuredRetries + 1));
        assertThat("An error while replacing the ConfigMap should cause an PossibleInconsistentStateException.", ExceptionUtils.findThrowable(ex, PossibleInconsistentStateException.class).isPresent(), is(true));
    }
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Arrays(java.util.Arrays) KubernetesTestUtils(org.apache.flink.kubernetes.KubernetesTestUtils) ExceptionUtils(org.apache.flink.util.ExceptionUtils) KubernetesService(org.apache.flink.kubernetes.kubeclient.resources.KubernetesService) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesPod(org.apache.flink.kubernetes.kubeclient.resources.KubernetesPod) CONFIG_FILE_LOG4J_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOG4J_NAME) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ExternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator) BlobServerOptions(org.apache.flink.configuration.BlobServerOptions) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) KubernetesClientTestBase(org.apache.flink.kubernetes.KubernetesClientTestBase) JobManagerOptions(org.apache.flink.configuration.JobManagerOptions) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Matchers.is(org.hamcrest.Matchers.is) Constants(org.apache.flink.kubernetes.utils.Constants) ClusterSpecification(org.apache.flink.client.deployment.ClusterSpecification) FlinkMatchers(org.apache.flink.core.testutils.FlinkMatchers) KubernetesConfigOptionsInternal(org.apache.flink.kubernetes.configuration.KubernetesConfigOptionsInternal) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) ArrayList(java.util.ArrayList) DeploymentOptions(org.apache.flink.configuration.DeploymentOptions) RestOptions(org.apache.flink.configuration.RestOptions) Service(io.fabric8.kubernetes.api.model.Service) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesJobManagerParameters(org.apache.flink.kubernetes.kubeclient.parameters.KubernetesJobManagerParameters) Matchers.isIn(org.hamcrest.Matchers.isIn) KubernetesDeploymentTarget(org.apache.flink.kubernetes.configuration.KubernetesDeploymentTarget) ExecutorService(java.util.concurrent.ExecutorService) Assert.assertNotNull(org.junit.Assert.assertNotNull) Pod(io.fabric8.kubernetes.api.model.Pod) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ExecutionException(java.util.concurrent.ExecutionException) Assert.assertNull(org.junit.Assert.assertNull) CONFIG_FILE_LOGBACK_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOGBACK_NAME) KubernetesJobManagerFactory(org.apache.flink.kubernetes.kubeclient.factory.KubernetesJobManagerFactory) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

PossibleInconsistentStateException (org.apache.flink.runtime.persistence.PossibleInconsistentStateException)10 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Test (org.junit.Test)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Predicate (java.util.function.Predicate)4 FlinkMatchers (org.apache.flink.core.testutils.FlinkMatchers)4 KubernetesConfigMap (org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.is (org.hamcrest.Matchers.is)4 Assert.fail (org.junit.Assert.fail)4 CompletionException (java.util.concurrent.CompletionException)3 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)3 KubernetesException (org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)3 KubernetesLeaderElector (org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector)3