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



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/DefaultJobLeaderService.java
##########
@@ -0,0 +1,451 @@
+/*
+ * 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.annotation.VisibleForTesting;
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.time.Time;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.runtime.highavailability.HighAvailabilityServices;
+import org.apache.flink.runtime.jobmaster.JMTMRegistrationSuccess;
+import org.apache.flink.runtime.jobmaster.JobMasterGateway;
+import org.apache.flink.runtime.jobmaster.JobMasterId;
+import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalListener;
+import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService;
+import org.apache.flink.runtime.registration.RegisteredRpcConnection;
+import org.apache.flink.runtime.registration.RegistrationResponse;
+import org.apache.flink.runtime.registration.RetryingRegistration;
+import org.apache.flink.runtime.registration.RetryingRegistrationConfiguration;
+import org.apache.flink.runtime.rpc.RpcService;
+import org.apache.flink.runtime.taskmanager.UnresolvedTaskManagerLocation;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executor;
+
+/**
+ * Default implementation of {@link JobLeaderService}.
+ */
+public class DefaultJobLeaderService implements JobLeaderService {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(DefaultJobLeaderService.class);
+
+       /** Self's location, used for the job manager connection. */
+       private final UnresolvedTaskManagerLocation ownLocation;
+
+       /** The leader retrieval service and listener for each registered job. 
*/
+       private final Map<JobID, Tuple2<LeaderRetrievalService, 
DefaultJobLeaderService.JobManagerLeaderListener>> jobLeaderServices;
+
+       private final RetryingRegistrationConfiguration 
retryingRegistrationConfiguration;
+
+       /** Internal state of the service. */
+       private volatile DefaultJobLeaderService.State state;
+
+       /** Address of the owner of this service. This address is used for the 
job manager connection. */
+       private String ownerAddress;
+
+       /** Rpc service to use for establishing connections. */
+       private RpcService rpcService;
+
+       /** High availability services to create the leader retrieval services 
from. */
+       private HighAvailabilityServices highAvailabilityServices;
+
+       /** Job leader listener listening for job leader changes. */
+       private JobLeaderListener jobLeaderListener;
+
+       public DefaultJobLeaderService(
+                       UnresolvedTaskManagerLocation location,
+                       RetryingRegistrationConfiguration 
retryingRegistrationConfiguration) {
+               this.ownLocation = Preconditions.checkNotNull(location);
+               this.retryingRegistrationConfiguration = 
Preconditions.checkNotNull(retryingRegistrationConfiguration);
+
+               // Has to be a concurrent hash map because tests might access 
this service
+               // concurrently via containsJob
+               jobLeaderServices = new ConcurrentHashMap<>(4);
+
+               state = DefaultJobLeaderService.State.CREATED;
+
+               ownerAddress = null;
+               rpcService = null;
+               highAvailabilityServices = null;
+               jobLeaderListener = null;
+       }
+
+       // 
-------------------------------------------------------------------------------
+       // Methods
+       // 
-------------------------------------------------------------------------------
+
+       @Override
+       public void start(
+               final String initialOwnerAddress,
+               final RpcService initialRpcService,
+               final HighAvailabilityServices initialHighAvailabilityServices,
+               final JobLeaderListener initialJobLeaderListener) {
+
+               if (DefaultJobLeaderService.State.CREATED != state) {
+                       throw new IllegalStateException("The service has 
already been started.");
+               } else {
+                       LOG.info("Start job leader service.");
+
+                       this.ownerAddress = 
Preconditions.checkNotNull(initialOwnerAddress);
+                       this.rpcService = 
Preconditions.checkNotNull(initialRpcService);
+                       this.highAvailabilityServices = 
Preconditions.checkNotNull(initialHighAvailabilityServices);
+                       this.jobLeaderListener = 
Preconditions.checkNotNull(initialJobLeaderListener);
+                       state = DefaultJobLeaderService.State.STARTED;
+               }
+       }
+
+       @Override
+       public void stop() throws Exception {
+               LOG.info("Stop job leader service.");
+
+               if (DefaultJobLeaderService.State.STARTED == state) {
+
+                       for (Tuple2<LeaderRetrievalService, 
DefaultJobLeaderService.JobManagerLeaderListener> leaderRetrievalServiceEntry: 
jobLeaderServices.values()) {
+                               LeaderRetrievalService leaderRetrievalService = 
leaderRetrievalServiceEntry.f0;
+                               
DefaultJobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = 
leaderRetrievalServiceEntry.f1;
+
+                               jobManagerLeaderListener.stop();
+                               leaderRetrievalService.stop();
+                       }
+
+                       jobLeaderServices.clear();
+               }
+
+               state = DefaultJobLeaderService.State.STOPPED;
+       }
+
+       @Override
+       public void removeJob(JobID jobId) throws Exception {
+               Preconditions.checkState(DefaultJobLeaderService.State.STARTED 
== state, "The service is currently not running.");
+
+               Tuple2<LeaderRetrievalService, 
DefaultJobLeaderService.JobManagerLeaderListener> entry = 
jobLeaderServices.remove(jobId);
+
+               if (entry != null) {
+                       LOG.info("Remove job {} from job leader monitoring.", 
jobId);
+
+                       LeaderRetrievalService leaderRetrievalService = 
entry.f0;
+                       DefaultJobLeaderService.JobManagerLeaderListener 
jobManagerLeaderListener = entry.f1;
+
+                       leaderRetrievalService.stop();

Review comment:
       I think it is not properly closed. I will add a fix for 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.

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


Reply via email to