[
https://issues.apache.org/jira/browse/FLINK-2354?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14941227#comment-14941227
]
ASF GitHub Bot commented on FLINK-2354:
---------------------------------------
Github user tillrohrmann commented on a diff in the pull request:
https://github.com/apache/flink/pull/1153#discussion_r41030614
--- Diff:
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/ZooKeeperCheckpointIDCounter.java
---
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.checkpoint;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.recipes.shared.SharedCount;
+import org.apache.curator.framework.recipes.shared.VersionedValue;
+import org.apache.curator.framework.state.ConnectionState;
+import org.apache.curator.framework.state.ConnectionStateListener;
+import org.apache.flink.runtime.jobmanager.RecoveryMode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * {@link CheckpointIDCounter} instances for JobManagers running in {@link
RecoveryMode#ZOOKEEPER}.
+ *
+ * <p>Each counter creates a ZNode:
+ * <pre>
+ * +----O /flink/checkpoint-counter/<job-id> 1 [persistent]
+ * .
+ * .
+ * .
+ * +----O /flink/checkpoint-counter/<job-id> N [persistent]
+ * </pre>
+ *
+ * <p>The checkpoints IDs are required to be ascending (per job). In order
to guarantee this in case
+ * of job manager failures we use ZooKeeper to have a shared counter
across job manager instances.
+ */
+public class ZooKeeperCheckpointIDCounter implements CheckpointIDCounter {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(ZooKeeperCheckpointIDCounter.class);
+
+ /** Curator ZooKeeper client */
+ private final CuratorFramework client;
+
+ /** Path of the shared count */
+ private final String counterPath;
+
+ /** Curator recipe for shared counts */
+ private final SharedCount sharedCount;
+
+ /** Connection state listener to monitor the client connection */
+ private final SharedCountConnectionStateListener connStateListener =
+ new SharedCountConnectionStateListener();
+
+ /**
+ * Creates a {@link ZooKeeperCheckpointIDCounter} instance.
+ *
+ * @param client Curator ZooKeeper client
+ * @param counterPath ZooKeeper path for the counter. It's sufficient
to have a path per-job.
+ * @throws Exception
+ */
+ public ZooKeeperCheckpointIDCounter(CuratorFramework client, String
counterPath) throws Exception {
+ this.client = checkNotNull(client, "Curator client");
+ this.counterPath = checkNotNull(counterPath, "Counter path");
+ this.sharedCount = new SharedCount(client, counterPath, 1);
+ }
+
+ @Override
+ public void start() throws Exception {
+ sharedCount.start();
+
client.getConnectionStateListenable().addListener(connStateListener);
+ }
+
+ @Override
+ public void stop() throws Exception {
+ sharedCount.close();
+
client.getConnectionStateListenable().removeListener(connStateListener);
+
+ LOG.info("Removing {} from ZooKeeper", counterPath);
+
client.delete().deletingChildrenIfNeeded().inBackground().forPath(counterPath);
+ }
+
+ @Override
+ public long getAndIncrement() throws Exception {
+ while (true) {
+ ConnectionState connState =
connStateListener.getLastState();
+
+ if (connState != null) {
+ throw new IllegalStateException("Connection
state: " + connState);
+ }
+
+ VersionedValue<Integer> current =
sharedCount.getVersionedValue();
+
+ Integer newCount = current.getValue() + 1;
+
+ if (sharedCount.trySetCount(current, newCount)) {
+ return current.getValue();
+ }
+ }
+ }
+
+ /**
+ * Connection state listener. In case of {@link
ConnectionState#SUSPENDED} or {@link
+ * ConnectionState#LOST} we are not guaranteed to read a current count
from ZooKeeper.
+ */
+ private class SharedCountConnectionStateListener implements
ConnectionStateListener {
+
+ private volatile ConnectionState lastState;
+
+ @Override
+ public void stateChanged(CuratorFramework client,
ConnectionState newState) {
+ if (newState == ConnectionState.SUSPENDED || newState
== ConnectionState.LOST) {
--- End diff --
Can the state change back from `SUSPENDED` to `CONNECTED`? If so, then we
should clear the `lastState` variable.
> Recover running jobs on JobManager failure
> ------------------------------------------
>
> Key: FLINK-2354
> URL: https://issues.apache.org/jira/browse/FLINK-2354
> Project: Flink
> Issue Type: Sub-task
> Components: JobManager
> Affects Versions: master
> Reporter: Ufuk Celebi
> Assignee: Ufuk Celebi
> Fix For: 0.10
>
>
> tl;dr Persist JobGraphs in state backend and coordinate reference to state
> handle via ZooKeeper.
> Problem: When running multiple JobManagers in high availability mode, the
> leading job manager looses all running jobs when it fails. After a new
> leading job manager is elected, it is not possible to recover any previously
> running jobs.
> Solution: The leading job manager, which receives the job graph writes 1) the
> job graph to a state backend, and 2) a reference to the respective state
> handle to ZooKeeper. In general, job graphs can become large (multiple MBs,
> because they include closures etc.). ZooKeeper is not designed for data of
> this size. The level of indirection via the reference to the state backend
> keeps the data in ZooKeeper small.
> Proposed ZooKeeper layout:
> /flink (default)
> +- currentJobs
> +- job id i
> +- state handle reference of job graph i
> The 'currentJobs' node needs to be persistent to allow recovery of jobs
> between job managers. The currentJobs node needs to satisfy the following
> invariant: There is a reference to a job graph with id i IFF the respective
> job graph needs to be recovered by a newly elected job manager leader.
> With this in place, jobs will be recovered from their initial state (as if
> resubmitted). The next step is to backup the runtime state handles of
> checkpoints in a similar manner.
> ---
> This work will be based on [[email protected]]'s implementation of
> FLINK-2291. The leader election service notifies the job manager about
> granted/revoked leadership. This notification happens via Akka and thus
> serially *per* job manager, but results in eventually consistent state
> between job managers. For some snapshots of time it is possible to have a new
> leader granted leadership, before the old one has been revoked its leadership.
> [[email protected]], can you confirm that leadership does not guarantee
> mutually exclusive access to the shared 'currentJobs' state?
> For example, the following can happen:
> - JM 1 is leader, JM 2 is standby
> - JOB i is running (and hence /flink/currentJobs/i exists)
> - ZK notifies leader election service (LES) of JM 1 and JM 2
> - LES 2 immediately notifies JM 2 about granted leadership, but LES 1
> notification revoking leadership takes longer
> - JOB i finishes (TMs don't notice leadership change yet) and JM 1 receives
> final JobStatusChange
> - JM 2 resubmits the job /flink/currentJobs/i
> - JM 1 removes /flink/currentJobs/i, because it is now finished
> => inconsistent state (wrt the specified invariant above)
> If it is indeed a problem, we can circumvent this with a Curator recipe for
> [shared locks|http://curator.apache.org/curator-recipes/shared-lock.html] to
> coordinate the access to currentJobs. The lock needs to be acquired on
> leadership.
> ---
> Minimum required tests:
> - Unit tests for job graph serialization and writing to state backend and
> ZooKeeper with expected nodes
> - Unit tests for job submission to job manager in leader/non-leader state
> - Unit tests for leadership granting/revoking and job submission/restarting
> interleavings
> - Process failure integration tests with single and multiple running jobs
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)