tillrohrmann commented on a change in pull request #17485:
URL: https://github.com/apache/flink/pull/17485#discussion_r791592992



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/ZooKeeperMultipleComponentLeaderElectionDriverTest.java
##########
@@ -0,0 +1,432 @@
+/*
+ * 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.leaderelection;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.HighAvailabilityOptions;
+import org.apache.flink.core.testutils.EachCallbackWrapper;
+import 
org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener;
+import org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService;
+import org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver;
+import 
org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriverFactory;
+import org.apache.flink.runtime.rest.util.NoOpFatalErrorHandler;
+import org.apache.flink.runtime.util.ZooKeeperUtils;
+import org.apache.flink.runtime.zookeeper.ZooKeeperExtension;
+import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.TestLoggerExtension;
+
+import org.apache.flink.shaded.curator4.com.google.common.collect.Iterables;
+import 
org.apache.flink.shaded.curator4.org.apache.curator.framework.CuratorFramework;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import javax.annotation.Nonnull;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for the {@link ZooKeeperMultipleComponentLeaderElectionDriver}. */
+@ExtendWith(TestLoggerExtension.class)
+class ZooKeeperMultipleComponentLeaderElectionDriverTest {
+
+    private final ZooKeeperExtension zooKeeperExtension = new 
ZooKeeperExtension();
+
+    @RegisterExtension
+    private final EachCallbackWrapper<ZooKeeperExtension> eachWrapper =
+            new EachCallbackWrapper<>(zooKeeperExtension);
+
+    @Test
+    public void testElectionDriverGainsLeadership() throws Exception {
+        final TestingLeaderElectionListener leaderElectionListener =
+                new TestingLeaderElectionListener();
+        final CuratorFrameworkWithUnhandledErrorListener curatorFramework = 
startCuratorFramework();
+
+        final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver =
+                startLeaderElectionDriver(
+                        leaderElectionListener, 
curatorFramework.asCuratorFramework());
+
+        try {
+            leaderElectionListener.await(IsLeaderEvent.class);
+        } finally {
+            leaderElectionDriver.close();
+            curatorFramework.close();
+        }
+    }
+
+    @Test
+    public void testElectionDriverLosesLeadership() throws Exception {
+        final TestingLeaderElectionListener leaderElectionListener =
+                new TestingLeaderElectionListener();
+        final CuratorFrameworkWithUnhandledErrorListener curatorFramework = 
startCuratorFramework();
+
+        final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver =
+                startLeaderElectionDriver(
+                        leaderElectionListener, 
curatorFramework.asCuratorFramework());
+
+        try {
+            leaderElectionListener.await(IsLeaderEvent.class);
+
+            zooKeeperExtension.stop();
+
+            leaderElectionListener.await(NotLeaderEvent.class);
+        } finally {
+            leaderElectionDriver.close();
+            curatorFramework.close();
+        }
+    }
+
+    @Test
+    public void testPublishLeaderInformation() throws Exception {
+        final TestingLeaderElectionListener leaderElectionListener =
+                new TestingLeaderElectionListener();
+        final CuratorFrameworkWithUnhandledErrorListener curatorFramework = 
startCuratorFramework();
+
+        final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver =
+                startLeaderElectionDriver(
+                        leaderElectionListener, 
curatorFramework.asCuratorFramework());
+
+        try {
+            leaderElectionListener.await(IsLeaderEvent.class);
+
+            final String componentId = "barfoo";
+            final DefaultLeaderRetrievalService defaultLeaderRetrievalService =
+                    new DefaultLeaderRetrievalService(
+                            new ZooKeeperLeaderRetrievalDriverFactory(
+                                    curatorFramework.asCuratorFramework(),
+                                    componentId,
+                                    
ZooKeeperLeaderRetrievalDriver.LeaderInformationClearancePolicy
+                                            .ON_LOST_CONNECTION));
+
+            final TestingListener leaderRetrievalListener = new 
TestingListener();
+            defaultLeaderRetrievalService.start(leaderRetrievalListener);
+
+            final LeaderInformation leaderInformation =
+                    LeaderInformation.known(UUID.randomUUID(), "foobar");
+            leaderElectionDriver.publishLeaderInformation(componentId, 
leaderInformation);
+
+            leaderRetrievalListener.waitForNewLeader(10_000L);
+
+            
assertThat(leaderRetrievalListener.getLeader()).isEqualTo(leaderInformation);
+        } finally {
+            leaderElectionDriver.close();
+            curatorFramework.close();
+        }
+    }
+
+    @Test
+    public void testLeaderInformationChange() throws Exception {
+        final TestingLeaderElectionListener leaderElectionListener =
+                new TestingLeaderElectionListener();
+        final CuratorFrameworkWithUnhandledErrorListener curatorFramework = 
startCuratorFramework();
+
+        final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver =
+                startLeaderElectionDriver(
+                        leaderElectionListener, 
curatorFramework.asCuratorFramework());
+
+        try {
+            leaderElectionListener.await(IsLeaderEvent.class);
+
+            final LeaderInformation leaderInformation =
+                    LeaderInformation.known(UUID.randomUUID(), "foobar");
+            final String componentId = "barfoo";
+            final String path = 
ZooKeeperUtils.generateConnectionInformationPath(componentId);
+
+            ZooKeeperUtils.writeLeaderInformationToZooKeeper(
+                    leaderInformation, curatorFramework.asCuratorFramework(), 
() -> true, path);
+
+            final LeaderInformationChangeEvent leaderInformationChangeEvent =
+                    
leaderElectionListener.await(LeaderInformationChangeEvent.class);
+
+            
assertThat(leaderInformationChangeEvent.componentId).isEqualTo(componentId);
+            
assertThat(leaderInformationChangeEvent.leaderInformation).isEqualTo(leaderInformation);
+        } finally {
+            leaderElectionDriver.close();
+            curatorFramework.close();
+        }
+    }
+
+    @Test
+    public void testLeaderElectionWithMultipleDrivers() throws Exception {
+        final CuratorFrameworkWithUnhandledErrorListener curatorFramework = 
startCuratorFramework();
+
+        try {
+            Set<ElectionDriver> electionDrivers =
+                    Stream.generate(
+                                    () ->
+                                            createLeaderElectionDriver(
+                                                    
curatorFramework.asCuratorFramework()))
+                            .limit(3)
+                            .collect(Collectors.toSet());
+
+            while (!electionDrivers.isEmpty()) {
+                final CompletableFuture<Object> anyLeader =
+                        CompletableFuture.anyOf(
+                                electionDrivers.stream()
+                                        
.map(ElectionDriver::getLeadershipFuture)
+                                        .collect(Collectors.toList())
+                                        .toArray(new CompletableFuture[0]));
+
+                // wait for any leader
+                anyLeader.join();
+
+                final Map<Boolean, Set<ElectionDriver>> leaderAndRest =
+                        electionDrivers.stream()
+                                .collect(
+                                        Collectors.partitioningBy(
+                                                ElectionDriver::hasLeadership, 
Collectors.toSet()));
+
+                assertThat(leaderAndRest.get(true)).hasSize(1);
+                Iterables.getOnlyElement(leaderAndRest.get(true)).close();
+
+                electionDrivers = leaderAndRest.get(false);
+            }
+        } finally {
+            curatorFramework.close();
+        }
+    }
+
+    private static ElectionDriver createLeaderElectionDriver(CuratorFramework 
curatorFramework) {
+        final SimpleLeaderElectionListener leaderElectionListener =
+                new SimpleLeaderElectionListener();
+
+        try {
+            final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver =
+                    startLeaderElectionDriver(leaderElectionListener, 
curatorFramework);
+            return new ElectionDriver(leaderElectionDriver, 
leaderElectionListener);
+        } catch (Exception e) {
+            ExceptionUtils.rethrow(e);
+            return null;
+        }
+    }
+
+    private static final class ElectionDriver {
+        private final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver;
+        private final SimpleLeaderElectionListener leaderElectionListener;
+
+        private ElectionDriver(
+                ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver,
+                SimpleLeaderElectionListener leaderElectionListener) {
+            this.leaderElectionDriver = leaderElectionDriver;
+            this.leaderElectionListener = leaderElectionListener;
+        }
+
+        void close() throws Exception {
+            leaderElectionDriver.close();
+        }
+
+        boolean hasLeadership() {
+            return leaderElectionDriver.hasLeadership();
+        }
+
+        CompletableFuture<Void> getLeadershipFuture() {
+            return leaderElectionListener.getLeadershipFuture();
+        }
+    }
+
+    private static final class SimpleLeaderElectionListener
+            implements MultipleComponentLeaderElectionDriver.Listener {
+
+        private final CompletableFuture<Void> leadershipFuture = new 
CompletableFuture<>();
+
+        CompletableFuture<Void> getLeadershipFuture() {
+            return leadershipFuture;
+        }
+
+        @Override
+        public void isLeader() {
+            leadershipFuture.complete(null);
+        }
+
+        @Override
+        public void notLeader() {}
+
+        @Override
+        public void notifyLeaderInformationChange(
+                String componentId, LeaderInformation leaderInformation) {}
+
+        @Override
+        public void notifyAllKnownLeaderInformation(
+                Collection<LeaderInformationWithComponentId> 
leaderInformationWithComponentIds) {}
+    }
+
+    @Nonnull
+    private static ZooKeeperMultipleComponentLeaderElectionDriver 
startLeaderElectionDriver(
+            MultipleComponentLeaderElectionDriver.Listener 
leaderElectionListener,
+            CuratorFramework curatorFramework)
+            throws Exception {
+        final ZooKeeperMultipleComponentLeaderElectionDriver 
leaderElectionDriver =
+                new ZooKeeperMultipleComponentLeaderElectionDriver(
+                        curatorFramework, "foobar", leaderElectionListener);
+        return leaderElectionDriver;
+    }
+
+    @Nonnull

Review comment:
       Will remove it.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to