You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ma...@apache.org on 2023/02/01 13:40:53 UTC

[flink] branch release-1.15 updated: [FLINK-30462][runtime] The leader session ID is now saved properly

This is an automated email from the ASF dual-hosted git repository.

mapohl pushed a commit to branch release-1.15
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.15 by this push:
     new cfef56c2b9b [FLINK-30462][runtime] The leader session ID is now saved properly
cfef56c2b9b is described below

commit cfef56c2b9b9bdb73c9e020473e4d00849ca9e27
Author: Matthias Pohl <ma...@aiven.io>
AuthorDate: Tue Dec 20 14:41:53 2022 +0100

    [FLINK-30462][runtime] The leader session ID is now saved properly
    
    Previous to this fix, different components could have used different
    session IDs even though they belonged to the same session.
    
    Signed-off-by: Matthias Pohl <ma...@aiven.io>
---
 ...aultMultipleComponentLeaderElectionService.java |  5 +-
 ...MultipleComponentLeaderElectionServiceTest.java | 65 ++++++++++++++++++++--
 2 files changed, 64 insertions(+), 6 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionService.java b/flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionService.java
index 6afc54f68d8..1348c5b0593 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionService.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionService.java
@@ -192,7 +192,10 @@ public class DefaultMultipleComponentLeaderElectionService
                 return;
             }
 
-            currentLeaderSessionId = UUID.randomUUID();
+            Preconditions.checkState(
+                    currentLeaderSessionId == null,
+                    "notLeader() wasn't called by the LeaderElection backend before assigning leadership to this LeaderElectionService.");
+            currentLeaderSessionId = newLeaderSessionId;
 
             forEachLeaderElectionEventHandler(
                     leaderElectionEventHandler ->
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionServiceTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionServiceTest.java
index 54281a5f975..cb2da45cf6c 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionServiceTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionServiceTest.java
@@ -166,6 +166,51 @@ class DefaultMultipleComponentLeaderElectionServiceTest {
         }
     }
 
+    @Test
+    public void testLeaderSessionIdMatchesBetweenComponents() throws Exception {
+        final TestingMultipleComponentLeaderElectionDriver leaderElectionDriver =
+                TestingMultipleComponentLeaderElectionDriver.newBuilder().build();
+        final DefaultMultipleComponentLeaderElectionService leaderElectionService =
+                createDefaultMultiplexingLeaderElectionService(leaderElectionDriver);
+
+        try {
+            final Component preLeadershipGrantedComponent =
+                    new Component(
+                            "test-component-0",
+                            new SimpleTestingLeaderElectionEventListener(),
+                            LeaderInformation.empty());
+            final Component postLeadershipGrantedComponent =
+                    new Component(
+                            "test-component-1",
+                            new SimpleTestingLeaderElectionEventListener(),
+                            LeaderInformation.empty());
+
+            leaderElectionService.registerLeaderElectionEventHandler(
+                    preLeadershipGrantedComponent.getComponentId(),
+                    preLeadershipGrantedComponent.getLeaderElectionEventListener());
+
+            leaderElectionDriver.grantLeadership();
+
+            leaderElectionService.registerLeaderElectionEventHandler(
+                    postLeadershipGrantedComponent.getComponentId(),
+                    postLeadershipGrantedComponent.getLeaderElectionEventListener());
+
+            final UUID preGrantLeaderInformation =
+                    preLeadershipGrantedComponent
+                            .getLeaderElectionEventListener()
+                            .getLeaderSessionID();
+
+            final UUID postGrantLeaderInformation =
+                    postLeadershipGrantedComponent
+                            .getLeaderElectionEventListener()
+                            .getLeaderSessionID();
+
+            assertThat(preGrantLeaderInformation).isEqualTo(postGrantLeaderInformation);
+        } finally {
+            leaderElectionService.close();
+        }
+    }
+
     @Test
     public void allKnownLeaderInformationCallsEventHandlers() throws Exception {
         final TestingMultipleComponentLeaderElectionDriver leaderElectionDriver =
@@ -321,27 +366,32 @@ class DefaultMultipleComponentLeaderElectionServiceTest {
     private static final class SimpleTestingLeaderElectionEventListener
             implements LeaderElectionEventHandler {
 
-        private boolean hasLeadership;
+        /**
+         * {@code currentLeaderSessionId} is set if the current LeaderElection client is select as
+         * the leader by the HA backend.
+         */
+        @Nullable private UUID currentLeaderSessionId;
 
+        /** {@code leaderInformation} is set if the leader information in the HA backend changes. */
         @Nullable private LeaderInformation leaderInformation;
 
         SimpleTestingLeaderElectionEventListener() {
-            hasLeadership = false;
+            currentLeaderSessionId = null;
             leaderInformation = null;
         }
 
         public boolean hasLeadership() {
-            return hasLeadership;
+            return currentLeaderSessionId != null;
         }
 
         @Override
         public void onGrantLeadership(UUID newLeaderSessionId) {
-            hasLeadership = true;
+            currentLeaderSessionId = newLeaderSessionId;
         }
 
         @Override
         public void onRevokeLeadership() {
-            hasLeadership = false;
+            currentLeaderSessionId = null;
             leaderInformation = null;
         }
 
@@ -354,5 +404,10 @@ class DefaultMultipleComponentLeaderElectionServiceTest {
         LeaderInformation getLeaderInformation() {
             return leaderInformation;
         }
+
+        @Nullable
+        UUID getLeaderSessionID() {
+            return currentLeaderSessionId;
+        }
     }
 }