You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "nlippis (via GitHub)" <gi...@apache.org> on 2023/04/26 17:19:02 UTC

[GitHub] [druid] nlippis commented on a diff in pull request #14156: queue tasks in kubernetes task runner if capacity is fully utilized

nlippis commented on code in PR #14156:
URL: https://github.com/apache/druid/pull/14156#discussion_r1178162786


##########
extensions-contrib/kubernetes-overlord-extensions/pom.xml:
##########
@@ -157,6 +157,18 @@
       <version>5.8.2</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-core</artifactId>

Review Comment:
   The dependency checker complained that this was an undeclared dependency so I added it.



##########
extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/common/KubernetesPeonClient.java:
##########
@@ -19,37 +19,244 @@
 
 package org.apache.druid.k8s.overlord.common;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
 import io.fabric8.kubernetes.api.model.Pod;
 import io.fabric8.kubernetes.api.model.batch.v1.Job;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.dsl.LogWatch;
+import org.apache.druid.java.util.common.RetryUtils;
+import org.apache.druid.java.util.emitter.EmittingLogger;
 
 import java.io.InputStream;
+import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
-/**
- * A Kubernetes client wrapper to assist with peon task managment.
- * It provides a high level api to retreive jobs, launch jobs, delete jobs and various other
- * tasks like getting task logs, listing all active tasks.
- */
-public interface KubernetesPeonClient
+public class KubernetesPeonClient
 {
+  private static final EmittingLogger log = new EmittingLogger(KubernetesPeonClient.class);
+
+  private final KubernetesClientApi clientApi;
+  private final String namespace;
+  private final boolean debugJobs;
+
+  public KubernetesPeonClient(KubernetesClientApi clientApi, String namespace, boolean debugJobs)
+  {
+    this.clientApi = clientApi;
+    this.namespace = namespace;
+    this.debugJobs = debugJobs;
+  }
+
+  public Optional<Job> getPeonJob(K8sTaskId taskId)
+  {
+    return clientApi.executeRequest(
+        client -> {
+          return Optional.fromNullable(
+              client.batch()
+                    .v1()
+                    .jobs()
+                    .inNamespace(namespace)
+                    .withName(taskId.getK8sTaskId())
+                    .get());
+        }
+    );
+  }
+
+  public Pod launchPeonJobAndWaitForStart(Job job, long howLong, TimeUnit timeUnit)
+  {
+    long start = System.currentTimeMillis();
+    // launch job
+    return clientApi.executeRequest(client -> {
+      client.batch().v1().jobs().inNamespace(namespace).resource(job).create();
+      K8sTaskId taskId = new K8sTaskId(job.getMetadata().getName());
+      log.info("Successfully submitted job: %s ... waiting for job to launch", taskId);
+      // wait until the pod is running or complete or failed, any of those is fine
+      // TODO: I believe we can do the following instead and remove getPeonPodWithRetries
+      // Pod result = client.pods()
+      //     .inNamespace(namespace)
+      //     .withLabel("job-name", job.getMetadata().getName())
+      //     .waitUntilCondition(pod -> {
+      //       if (pod == null || pod.getStatus() == null) {
+      //         return false;
+      //       }
+      //       return pod.getStatus().getPodIP() != null;
+      //     }, howLong, timeUnit);
+      Pod mainPod = getPeonPodWithRetries(taskId);
+      Pod result = client.pods().inNamespace(namespace).withName(mainPod.getMetadata().getName())

Review Comment:
   Is this the case you are thinking of?
   
   Thread 1: client.launchPeonJobAndWaitForStart(...)
   Thread 1: client.launchPeonJobAndWaitForStart::getPeonPodWithRetries(...) -> Pod
   Thread 2: client.deletePeonJob(...) -> true
   Thread 1: client.launchPeonJobAndWaitForStart::waitUntilCondition -> false
   
   



##########
extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/PeonLifecycleFactory.java:
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.druid.k8s.overlord;
+
+import org.apache.druid.indexing.common.task.Task;
+
+public interface PeonLifecycleFactory
+{
+  KubernetesPeonLifecycle build(Task task);

Review Comment:
   We are using it for testing, otherwise there is no way to mock/stub behavior for the PeonLifecycle in the unit tests.  Having to set up mocks for the classes/methods that the KubernetesPeonLifecycle object uses would tightly couple the KubernetesTaskRunner unit tests to the implementation of KubernetesPeonLifecycle.



-- 
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: commits-unsubscribe@druid.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org