ifndef-SleePy closed pull request #3296: [FLINK-5784] Fix bug about registering JobManagerLeaderListener in LeaderRetrievalService multiple times URL: https://github.com/apache/flink/pull/3296
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/JobLeaderService.java b/flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/JobLeaderService.java index 93c7bb71cf3..84d563c8e7e 100644 --- a/flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/JobLeaderService.java +++ b/flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/JobLeaderService.java @@ -188,16 +188,18 @@ public void removeJob(JobID jobId) throws Exception { public void addJob(final JobID jobId, final String defaultTargetAddress) throws Exception { Preconditions.checkState(JobLeaderService.State.STARTED == state, "The service is currently not running."); - LOG.info("Add job {} for job leader monitoring.", jobId); + if (!jobLeaderServices.containsKey(jobId)) { + LOG.info("Add job {} for job leader monitoring.", jobId); - final LeaderRetrievalService leaderRetrievalService = highAvailabilityServices.getJobManagerLeaderRetriever( - jobId); + final LeaderRetrievalService leaderRetrievalService = highAvailabilityServices.getJobManagerLeaderRetriever( + jobId); - JobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = new JobManagerLeaderListener(jobId); + JobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = new JobManagerLeaderListener(jobId); - leaderRetrievalService.start(jobManagerLeaderListener); + leaderRetrievalService.start(jobManagerLeaderListener); - jobLeaderServices.put(jobId, Tuple2.of(leaderRetrievalService, jobManagerLeaderListener)); + jobLeaderServices.put(jobId, Tuple2.of(leaderRetrievalService, jobManagerLeaderListener)); + } } /** diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/taskexecutor/JobLeaderServiceTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/taskexecutor/JobLeaderServiceTest.java new file mode 100644 index 00000000000..4d29844bb37 --- /dev/null +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/taskexecutor/JobLeaderServiceTest.java @@ -0,0 +1,60 @@ +/* + * 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.taskexecutor; + +import org.apache.flink.api.common.JobID; +import org.apache.flink.runtime.highavailability.HighAvailabilityServices; +import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalListener; +import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService; +import org.apache.flink.runtime.rpc.RpcService; +import org.apache.flink.runtime.taskmanager.TaskManagerLocation; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class JobLeaderServiceTest { + @Test + public void testAddJob() throws Exception { + JobLeaderService jobLeaderService = new JobLeaderService(mock(TaskManagerLocation.class)); + + HighAvailabilityServices highAvailabilityServices = mock(HighAvailabilityServices.class); + LeaderRetrievalService leaderRetrievalService = mock(LeaderRetrievalService.class); + + when(highAvailabilityServices.getJobManagerLeaderRetriever(any(JobID.class))) + .thenReturn(leaderRetrievalService); + + jobLeaderService.start("localhost", mock(RpcService.class), highAvailabilityServices, + mock(JobLeaderListener.class)); + + JobID jobID = JobID.generate(); + + jobLeaderService.addJob(jobID, "targetAddress"); + + assertTrue(jobLeaderService.containsJob(jobID)); + + // Invoke addJob multiple times + jobLeaderService.addJob(jobID, "targetAddress"); + + assertTrue(jobLeaderService.containsJob(jobID)); + // Just start one leaderRetrievalService + verify(leaderRetrievalService, times(1)).start(any(LeaderRetrievalListener.class)); + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services