You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/12/24 03:45:52 UTC

[GitHub] [flink] wangyang0918 opened a new pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

wangyang0918 opened a new pull request #14485:
URL: https://github.com/apache/flink/pull/14485


   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   When creating JobMaster service, we may recover from savepoints and update the HA storage(e.g. ConfigMap, ZooKeeper). So leader election service needs to be started first. Also we should guarantee that ConfigMap has been created after `leaderElectionService#start`. Then We need to block the constructor of KubernetesLeaderElectionDriver until the ConfigMap exists.
   
   **Note:** This PR only applies to branch 1.12. In the master branch, we have a more graceful fix. Refer FLINK-11719 for more information.
   
   ## Brief change log
   
   This PR will have the following three major changes.
   * Start leader election service before creating JobMasterService
   * Make constructor of `KubernetesLeaderElectionDriver` blocking until the leader ConfigMap is created
   * `StandaloneLeaderElectionService` should grant leadership in a separate thread to ensure `JobManagerRunnerImpl#grantLeadership` is executed after the constructor
   
   
   ## Verifying this change
   
   * Update some related tests to pass
   * Add a new ITCase `KubernetesHighAvailabilityRecoverFromSavepointITCase` for recovering from savepoints when HA is enabled
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (yes / **no**)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / **no**)
     - The serializers: (yes / **no** / don't know)
     - The runtime per-record code paths (performance sensitive): (yes / **no** / don't know)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn/Mesos, ZooKeeper: (**yes** / no / don't know)
     - The S3 file system connector: (yes / **no** / don't know)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes / **no**)
     - If yes, how is the feature documented? (**not applicable** / docs / JavaDocs / not documented)
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750730115


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277",
       "triggerID" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 43b08437ca99e16ade2e93be1f391feddfe0bab7 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750730115


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277",
       "triggerID" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11286",
       "triggerID" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11286) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] xintongsong closed pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
xintongsong closed pull request #14485:
URL: https://github.com/apache/flink/pull/14485


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750726986


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6 (Fri May 28 06:57:20 UTC 2021)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] wangyang0918 commented on a change in pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
wangyang0918 commented on a change in pull request #14485:
URL: https://github.com/apache/flink/pull/14485#discussion_r548418975



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/StandaloneLeaderElectionService.java
##########
@@ -44,8 +50,12 @@ public void start(LeaderContender newContender) throws Exception {
 
 		contender = Preconditions.checkNotNull(newContender);
 
-		// directly grant leadership to the given contender
-		contender.grantLeadership(HighAvailabilityServices.DEFAULT_LEADER_ID);
+		// Grant leadership in a separate thread
+		leaderElectorExecutor = Executors.newSingleThreadExecutor(
+			new ExecutorThreadFactory("LeaderElectorExecutor"));
+		CompletableFuture.runAsync(
+			() -> contender.grantLeadership(HighAvailabilityServices.DEFAULT_LEADER_ID),
+			leaderElectorExecutor);

Review comment:
       Good suggestion. Using the `whenComplete` to ensure the executing order could help to manage the changes in a smaller scope.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] wangyang0918 commented on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
wangyang0918 commented on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750742032


   cc @tillrohrmann @xintongsong Could you please have a look?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] xintongsong commented on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
xintongsong commented on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-751142558


   Merged in 2c92ea9a06fc13d8adedd4cd5737ab89cd0a7334 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750730115


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "status" : "CANCELED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277",
       "triggerID" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11286",
       "triggerID" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 43b08437ca99e16ade2e93be1f391feddfe0bab7 Azure: [CANCELED](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277) 
   * ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11286) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750726986


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit 43b08437ca99e16ade2e93be1f391feddfe0bab7 (Thu Dec 24 03:47:51 UTC 2020)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750730115


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277",
       "triggerID" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 43b08437ca99e16ade2e93be1f391feddfe0bab7 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277) 
   * ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] xintongsong commented on a change in pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
xintongsong commented on a change in pull request #14485:
URL: https://github.com/apache/flink/pull/14485#discussion_r548399979



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/StandaloneLeaderElectionService.java
##########
@@ -44,8 +50,12 @@ public void start(LeaderContender newContender) throws Exception {
 
 		contender = Preconditions.checkNotNull(newContender);
 
-		// directly grant leadership to the given contender
-		contender.grantLeadership(HighAvailabilityServices.DEFAULT_LEADER_ID);
+		// Grant leadership in a separate thread
+		leaderElectorExecutor = Executors.newSingleThreadExecutor(
+			new ExecutorThreadFactory("LeaderElectorExecutor"));
+		CompletableFuture.runAsync(
+			() -> contender.grantLeadership(HighAvailabilityServices.DEFAULT_LEADER_ID),
+			leaderElectorExecutor);

Review comment:
       I think having to invoke `grantLeadership` in a separate thread is probably an indicator that, the internal correctness of `JobManagerRunnerImpl` is depending on how its interfaces are called.
   
   IIUC, `JobManagerRunnerImpl` is trying to keep the following steps in order.
   1. `leaderElectionService.start()`
   2. create `jobMasterService`
   3. `grantLeadership` 
   
   Instead of introducing additional requirements on how and when leader election service should invoke `grantLeadership`, an alternative is to allow calling `grantLeadership` from any thread at any time, and delay the actual processing inside `grantLeadership` if needed.
   
   E.g., something like the following.
   ```
   public class JobManagerRunnerImpl implements XXX {
       private final CompletableFuture<JobMasterService> jmServiceFuture = new CompletableFuture<>();
       public JobManagerRunnerImpl(XXX) {
           synchronized (lock) {
               leaderElectionService.start();
               jmServiceFuture.complete(jobMasterFactory.createJobMasterService(XXX));
           }
       }
       public void grantLeadership(XXX) {
           jmServiceFuture.whenComplete((xxx) -> {
               synchronized (lock) {
                   XXX
               }
           })
       }
   }
   ```
   This should help encapsulate all the logics needed for keeping the above mentioned order inside `JobManagerRunnerImpl`.

##########
File path: flink-kubernetes/src/test/java/org/apache/flink/kubernetes/highavailability/KubernetesHighAvailabilityRecoverFromSavepointITCase.java
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.kubernetes.highavailability;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.JobStatus;
+import org.apache.flink.api.common.functions.RichFlatMapFunction;
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.api.common.time.Deadline;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.client.program.ClusterClient;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.HighAvailabilityOptions;
+import org.apache.flink.kubernetes.KubernetesResource;
+import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
+import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient;
+import org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap;
+import org.apache.flink.kubernetes.utils.Constants;
+import org.apache.flink.kubernetes.utils.KubernetesUtils;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings;
+import org.apache.flink.runtime.state.StateBackend;
+import org.apache.flink.runtime.state.filesystem.FsStateBackend;
+import org.apache.flink.runtime.testutils.CommonTestUtils;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.DiscardingSink;
+import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.TestLogger;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.Optional;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for recovering from savepoint when Kubernetes HA is enabled. The savepoint will be persisted as a checkpoint
+ * and stored in the ConfigMap when recovered successfully.
+ */
+public class KubernetesHighAvailabilityRecoverFromSavepointITCase extends TestLogger {
+
+	private static final long TIMEOUT = 60 * 1000;
+
+	private static final String CLUSTER_ID = "flink-on-k8s-cluster-" + System.currentTimeMillis();
+
+	private static final String FLAT_MAP_UID = "my-flat-map";
+
+	@ClassRule
+	public static KubernetesResource kubernetesResource = new KubernetesResource();
+
+	@ClassRule
+	public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+	@Rule
+	public MiniClusterWithClientResource miniClusterResource = new MiniClusterWithClientResource(
+		new MiniClusterResourceConfiguration.Builder()
+			.setConfiguration(getConfiguration())
+			.setNumberTaskManagers(1)
+			.setNumberSlotsPerTaskManager(1)
+			.build());
+
+	private FlinkKubeClient flinkKubeClient;
+
+	private ClusterClient clusterClient;

Review comment:
       IDE is complaining about "raw use of parameterized class"
   ```suggestion
   	private ClusterClient<?> clusterClient;
   ```

##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/TestingLeaderElectionService.java
##########
@@ -53,7 +59,7 @@ public synchronized void start(LeaderContender contender) {
 		this.contender = contender;
 
 		if (hasLeadership) {
-			contender.grantLeadership(issuedLeaderSessionId);
+			CompletableFuture.runAsync(() -> contender.grantLeadership(issuedLeaderSessionId), leaderElectorExecutor);

Review comment:
       This reveals the same problem as `StandaloneLeaderElectionService` does.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750730115


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277",
       "triggerID" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11286",
       "triggerID" : "ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 43b08437ca99e16ade2e93be1f391feddfe0bab7 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11277) 
   * ef7f8e5f15c5db6a760976709d4a7bb4ef61aae6 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=11286) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] xintongsong commented on a change in pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
xintongsong commented on a change in pull request #14485:
URL: https://github.com/apache/flink/pull/14485#discussion_r549318707



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/JobManagerRunnerImpl.java
##########
@@ -268,21 +276,27 @@ private void unregisterJobFromHighAvailability() {
 
 	@Override
 	public void grantLeadership(final UUID leaderSessionID) {
-		synchronized (lock) {
-			if (shutdown) {
-				log.debug("JobManagerRunner cannot be granted leadership because it is already shut down.");
-				return;
-			}
-
-			leadershipOperation = leadershipOperation.thenCompose(
-				(ignored) -> {
-					synchronized (lock) {
-						return verifyJobSchedulingStatusAndStartJobManager(leaderSessionID);
+		jobMasterCreationFuture.whenComplete(

Review comment:
       It is possible that the `LeaderElectionService` call `grantLeadership` on the same thread that `LeaderElectionService#start` is called (e.g., `StandaloneLeaderElectionService` & `TestingLeaderElectionService`). In that case, the lock does not block the thread from entering `grantLeadership` when `JobManagerRunnerImpl` has not been properly initialized.
   
   Please see this comment:
   https://github.com/apache/flink/pull/14485#discussion_r548399979




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #14485:
URL: https://github.com/apache/flink/pull/14485#issuecomment-750730115


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "43b08437ca99e16ade2e93be1f391feddfe0bab7",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 43b08437ca99e16ade2e93be1f391feddfe0bab7 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] tillrohrmann commented on a change in pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
tillrohrmann commented on a change in pull request #14485:
URL: https://github.com/apache/flink/pull/14485#discussion_r549294630



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/JobManagerRunnerImpl.java
##########
@@ -268,21 +276,27 @@ private void unregisterJobFromHighAvailability() {
 
 	@Override
 	public void grantLeadership(final UUID leaderSessionID) {
-		synchronized (lock) {
-			if (shutdown) {
-				log.debug("JobManagerRunner cannot be granted leadership because it is already shut down.");
-				return;
-			}
-
-			leadershipOperation = leadershipOperation.thenCompose(
-				(ignored) -> {
-					synchronized (lock) {
-						return verifyJobSchedulingStatusAndStartJobManager(leaderSessionID);
+		jobMasterCreationFuture.whenComplete(

Review comment:
       Why do we need the `jobMasterCreationFuture`? Wouldn't it be enough to wait on the lock and check whether the `JobManagerRunnerImpl` has been properly initialized?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] wangyang0918 commented on a change in pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
wangyang0918 commented on a change in pull request #14485:
URL: https://github.com/apache/flink/pull/14485#discussion_r548418975



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/StandaloneLeaderElectionService.java
##########
@@ -44,8 +50,12 @@ public void start(LeaderContender newContender) throws Exception {
 
 		contender = Preconditions.checkNotNull(newContender);
 
-		// directly grant leadership to the given contender
-		contender.grantLeadership(HighAvailabilityServices.DEFAULT_LEADER_ID);
+		// Grant leadership in a separate thread
+		leaderElectorExecutor = Executors.newSingleThreadExecutor(
+			new ExecutorThreadFactory("LeaderElectorExecutor"));
+		CompletableFuture.runAsync(
+			() -> contender.grantLeadership(HighAvailabilityServices.DEFAULT_LEADER_ID),
+			leaderElectorExecutor);

Review comment:
       Good suggestion. Using the `whenComplete` to ensure the executing order could help to manage the changes in a smaller scope.
   
   Only one thing we need to care is that now the `grantLeadership` is executing on the `io-executor`, not the `leader-elector-executor`. I think this change makes sense.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] tillrohrmann commented on a change in pull request #14485: [FLINK-20648][coordination][k8s] Start leader election service before creating JobMaster

Posted by GitBox <gi...@apache.org>.
tillrohrmann commented on a change in pull request #14485:
URL: https://github.com/apache/flink/pull/14485#discussion_r549334538



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/JobManagerRunnerImpl.java
##########
@@ -268,21 +276,27 @@ private void unregisterJobFromHighAvailability() {
 
 	@Override
 	public void grantLeadership(final UUID leaderSessionID) {
-		synchronized (lock) {
-			if (shutdown) {
-				log.debug("JobManagerRunner cannot be granted leadership because it is already shut down.");
-				return;
-			}
-
-			leadershipOperation = leadershipOperation.thenCompose(
-				(ignored) -> {
-					synchronized (lock) {
-						return verifyJobSchedulingStatusAndStartJobManager(leaderSessionID);
+		jobMasterCreationFuture.whenComplete(

Review comment:
       Now I understand it. Thanks for the clarification @xintongsong.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org