azagrebin commented on a change in pull request #11427: [FLINK-15790][k8s] Make 
FlinkKubeClient and its implementations asynchronous
URL: https://github.com/apache/flink/pull/11427#discussion_r401699894
 
 

 ##########
 File path: 
flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/Fabric8FlinkKubeClient.java
 ##########
 @@ -61,158 +61,180 @@
        private final String clusterId;
        private final String nameSpace;
 
-       public Fabric8FlinkKubeClient(Configuration flinkConfig, 
KubernetesClient client) {
+       private final ExecutorWrapper executorWrapper;
+
+       public Fabric8FlinkKubeClient(Configuration flinkConfig, 
KubernetesClient client, ExecutorWrapper executorWrapper) {
                this.flinkConfig = checkNotNull(flinkConfig);
                this.internalClient = checkNotNull(client);
                this.clusterId = 
checkNotNull(flinkConfig.getString(KubernetesConfigOptions.CLUSTER_ID));
 
                this.nameSpace = 
flinkConfig.getString(KubernetesConfigOptions.NAMESPACE);
+
+               this.executorWrapper = executorWrapper;
        }
 
        @Override
-       public void createJobManagerComponent(KubernetesJobManagerSpecification 
kubernetesJMSpec) {
+       public CompletableFuture<Void> 
createJobManagerComponent(KubernetesJobManagerSpecification kubernetesJMSpec) {
                final Deployment deployment = kubernetesJMSpec.getDeployment();
                final List<HasMetadata> accompanyingResources = 
kubernetesJMSpec.getAccompanyingResources();
 
                // create Deployment
                LOG.debug("Start to create deployment with spec {}", 
deployment.getSpec().toString());
-               final Deployment createdDeployment = this.internalClient
-                       .apps()
-                       .deployments()
-                       .inNamespace(this.nameSpace)
-                       .create(deployment);
-
-               // Note that we should use the uid of the created Deployment 
for the OwnerReference.
-               setOwnerReference(createdDeployment, accompanyingResources);
 
-               this.internalClient
-                       .resourceList(accompanyingResources)
-                       .inNamespace(this.nameSpace)
-                       .createOrReplace();
+               return CompletableFuture.runAsync(() -> {
+                       final Deployment createdDeployment = this.internalClient
+                               .apps()
+                               .deployments()
+                               .inNamespace(this.nameSpace)
+                               .create(deployment);
+
+                       // Note that we should use the uid of the created 
Deployment for the OwnerReference.
+                       setOwnerReference(createdDeployment, 
accompanyingResources);
+
+                       this.internalClient
+                               .resourceList(accompanyingResources)
+                               .inNamespace(this.nameSpace)
+                               .createOrReplace();
+               }, executorWrapper.getExecutor());
        }
 
        @Override
        public void createTaskManagerPod(KubernetesPod kubernetesPod) {
-               final Deployment masterDeployment = this.internalClient
-                       .apps()
-                       .deployments()
-                       .inNamespace(this.nameSpace)
-                       .withName(KubernetesUtils.getDeploymentName(clusterId))
-                       .get();
-
-               if (masterDeployment == null) {
-                       throw new RuntimeException(
-                               "Failed to find Deployment named " + clusterId 
+ " in namespace " + this.nameSpace);
-               }
+               CompletableFuture.runAsync(() -> {
+                       final Deployment masterDeployment = this.internalClient
+                               .apps()
+                               .deployments()
+                               .inNamespace(this.nameSpace)
+                               
.withName(KubernetesUtils.getDeploymentName(clusterId))
+                               .get();
+
+                       if (masterDeployment == null) {
+                               throw new RuntimeException(
+                                       "Failed to find Deployment named " + 
clusterId + " in namespace " + this.nameSpace);
+                       }
 
-               // Note that we should use the uid of the master Deployment for 
the OwnerReference.
-               setOwnerReference(masterDeployment, 
Collections.singletonList(kubernetesPod.getInternalResource()));
+                       // Note that we should use the uid of the master 
Deployment for the OwnerReference.
+                       setOwnerReference(masterDeployment, 
Collections.singletonList(kubernetesPod.getInternalResource()));
 
-               LOG.debug("Start to create pod with metadata {}, spec {}",
-                       kubernetesPod.getInternalResource().getMetadata(),
-                       kubernetesPod.getInternalResource().getSpec());
+                       LOG.debug("Start to create pod with metadata {}, spec 
{}",
+                               
kubernetesPod.getInternalResource().getMetadata(),
+                               kubernetesPod.getInternalResource().getSpec());
 
-               this.internalClient
-                       .pods()
-                       .inNamespace(this.nameSpace)
-                       .create(kubernetesPod.getInternalResource());
+                       this.internalClient
+                               .pods()
+                               .inNamespace(this.nameSpace)
+                               .create(kubernetesPod.getInternalResource());
+               }, executorWrapper.getExecutor());
        }
 
        @Override
        public void stopPod(String podName) {
-               this.internalClient.pods().withName(podName).delete();
+               CompletableFuture.runAsync(
+                       () -> 
this.internalClient.pods().withName(podName).delete(),
+                       executorWrapper.getExecutor());
        }
 
        @Override
-       @Nullable
-       public Endpoint getRestEndpoint(String clusterId) {
-               int restPort = this.flinkConfig.getInteger(RestOptions.PORT);
+       public CompletableFuture<Optional<Endpoint>> getRestEndpoint(String 
clusterId) {
+               final int restPort = 
this.flinkConfig.getInteger(RestOptions.PORT);
                final KubernetesConfigOptions.ServiceExposedType 
serviceExposedType =
                        
flinkConfig.get(KubernetesConfigOptions.REST_SERVICE_EXPOSED_TYPE);
 
                // Return the service.namespace directly when use ClusterIP.
                if (serviceExposedType == 
KubernetesConfigOptions.ServiceExposedType.ClusterIP) {
-                       return new 
Endpoint(KubernetesUtils.getInternalServiceName(clusterId) + "." + nameSpace, 
restPort);
+                       return CompletableFuture.completedFuture(
+                               Optional.of(new 
Endpoint(KubernetesUtils.getInternalServiceName(clusterId) + "." + nameSpace, 
restPort)));
                }
 
-               KubernetesService restService = getRestService(clusterId);
-               if (restService == null) {
-                       return null;
-               }
-               Service service = restService.getInternalResource();
-
-               String address = null;
-
-               if (service.getStatus() != null && 
(service.getStatus().getLoadBalancer() != null ||
-                       service.getStatus().getLoadBalancer().getIngress() != 
null)) {
-                       if 
(service.getStatus().getLoadBalancer().getIngress().size() > 0) {
-                               address = 
service.getStatus().getLoadBalancer().getIngress().get(0).getIp();
-                               if (address == null || address.isEmpty()) {
-                                       address = 
service.getStatus().getLoadBalancer().getIngress().get(0).getHostname();
+               return getRestService(clusterId).thenApply(restService -> {
+                       if (!restService.isPresent()) {
+                               return Optional.empty();
+                       }
+                       Service service = 
restService.get().getInternalResource();
+
+                       String address = null;
+                       int endpointPort = restPort;
+
+                       if (service.getStatus() != null && 
(service.getStatus().getLoadBalancer() != null ||
+                               
service.getStatus().getLoadBalancer().getIngress() != null)) {
+                               if 
(service.getStatus().getLoadBalancer().getIngress().size() > 0) {
+                                       address = 
service.getStatus().getLoadBalancer().getIngress().get(0).getIp();
+                                       if (address == null || 
address.isEmpty()) {
+                                               address = 
service.getStatus().getLoadBalancer().getIngress().get(0).getHostname();
+                                       }
+                               } else {
+                                       address = 
this.internalClient.getMasterUrl().getHost();
+                                       endpointPort = 
getServiceNodePort(service, RestOptions.PORT);
                                }
-                       } else {
-                               address = 
this.internalClient.getMasterUrl().getHost();
-                               restPort = getServiceNodePort(service, 
RestOptions.PORT);
+                       } else if (service.getSpec().getExternalIPs() != null 
&& service.getSpec().getExternalIPs().size() > 0) {
 
 Review comment:
   ```suggestion
                        } else if (service.getSpec().getExternalIPs() != null 
&& !service.getSpec().getExternalIPs().isEmpty()) {
   ```

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to