Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
Expand Down Expand Up @@ -52,6 +51,7 @@
import org.apache.druid.java.util.http.client.response.InputStreamResponseHandler;
import org.apache.druid.k8s.overlord.common.DruidK8sConstants;
import org.apache.druid.k8s.overlord.common.JobResponse;
import org.apache.druid.k8s.overlord.common.JobStatus;
import org.apache.druid.k8s.overlord.common.K8sTaskId;
import org.apache.druid.k8s.overlord.common.KubernetesPeonClient;
import org.apache.druid.k8s.overlord.common.KubernetesResourceNotFoundException;
Expand Down Expand Up @@ -80,6 +80,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* Runs tasks as k8s jobs using the "internal peon" verb.
Expand Down Expand Up @@ -339,7 +340,7 @@ public Map<String, Long> getTotalTaskSlotCount()
public Collection<? extends TaskRunnerWorkItem> getKnownTasks()
{
List<TaskRunnerWorkItem> result = new ArrayList<>();
for (Pod existingTask : client.listPeonPods()) {
for (Job existingTask : client.listAllPeonJobs()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The listPeonPods() and listPeonPods(Set<PeonPhase> phases) methods will be unused if you make this change right? If so please remove them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is done

try {
Task task = adapter.toTask(existingTask);
ListenableFuture<TaskStatus> future = run(task);
Expand Down Expand Up @@ -426,7 +427,7 @@ public void registerListener(TaskRunnerListener listener, Executor executor)
public Collection<TaskRunnerWorkItem> getRunningTasks()
{
List<TaskRunnerWorkItem> result = new ArrayList<>();
for (Pod existingTask : client.listPeonPods(Sets.newHashSet(PeonPhase.RUNNING))) {
for (Job existingTask : client.listAllPeonJobs().stream().filter(JobStatus::isActive).collect(Collectors.toSet())) {
try {
Task task = adapter.toTask(existingTask);
ListenableFuture<TaskStatus> future = run(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class DruidKubernetesPeonClient implements KubernetesPeonClient
{
Expand Down Expand Up @@ -181,23 +179,6 @@ public List<Job> listAllPeonJobs()
.getItems());
}

@Override
public List<Pod> listPeonPods(Set<PeonPhase> phases)
{
return listPeonPods().stream()
.filter(x -> phases.contains(PeonPhase.getPhaseFor(x)))
.collect(Collectors.toList());
}

@Override
public List<Pod> listPeonPods()
{
return clientApi.executeRequest(client -> client.pods().inNamespace(namespace)
.withLabel(DruidK8sConstants.LABEL_KEY)
.list().getItems());

}

@Override
public int cleanCompletedJobsOlderThan(long howFarBack, TimeUnit timeUnit)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.common;

import io.fabric8.kubernetes.api.model.batch.v1.Job;

public class JobStatus
{

public static boolean isActive(Job job)
{
if (job == null || job.getStatus() == null || job.getStatus().getActive() == null) {
return false;
}
return job.getStatus().getActive() > 0;
}

public static boolean isSucceeded(Job job)
{
if (job == null || job.getStatus() == null || job.getStatus().getSucceeded() == null) {
return false;
}
return job.getStatus().getSucceeded() > 0;
}

public static boolean isFailed(Job job)
{
if (job == null || job.getStatus() == null || job.getStatus().getFailed() == null) {
return false;
}
return job.getStatus().getFailed() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ public Job fromTask(Task task) throws IOException
}

@Override
public Task toTask(Pod from) throws IOException
public Task toTask(Job from) throws IOException
{
// all i have to do here is grab the main container...done
PodSpec podSpec = from.getSpec();
PodSpec podSpec = from.getSpec().getTemplate().getSpec();
massageSpec(podSpec, "main");
List<EnvVar> envVars = podSpec.getContainers().get(0).getEnv();
Optional<EnvVar> taskJson = envVars.stream().filter(x -> "TASK_JSON".equals(x.getName())).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.io.InputStream;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -48,10 +47,6 @@ public interface KubernetesPeonClient

List<Job> listAllPeonJobs();

List<Pod> listPeonPods(Set<PeonPhase> phases);

List<Pod> listPeonPods();

int cleanCompletedJobsOlderThan(long howFarBack, TimeUnit timeUnit);

Pod getMainJobPod(K8sTaskId taskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ public Job fromTask(Task task) throws IOException
* @throws IOException
*/
@Override
public Task toTask(Pod from) throws IOException
public Task toTask(Job from) throws IOException
{
Map<String, String> annotations = from.getMetadata().getAnnotations();
Map<String, String> annotations = from.getSpec().getTemplate().getMetadata().getAnnotations();
if (annotations == null) {
throw new IOE("No annotations found on pod [%s]", from.getMetadata().getName());
throw new IOE("No annotations found on pod spec for job [%s]", from.getMetadata().getName());
}
String task = annotations.get(DruidK8sConstants.TASK);
if (task == null) {
throw new IOE("No task annotation found on pod [%s]", from.getMetadata().getName());
throw new IOE("No task annotation found on pod spec for job [%s]", from.getMetadata().getName());
}
return mapper.readValue(Base64Compression.decompressBase64(task), Task.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.druid.k8s.overlord.common;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
import org.apache.druid.indexing.common.task.Task;

Expand All @@ -30,6 +29,6 @@ public interface TaskAdapter

Job fromTask(Task task) throws IOException;

Task toTask(Pod from) throws IOException;
Task toTask(Job from) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ public void testTheK8sRestartState() throws Exception

K8sTaskAdapter adapter = mock(K8sTaskAdapter.class);
when(adapter.fromTask(eq(task))).thenReturn(job);
when(adapter.toTask(eq(peonPod))).thenReturn(task);
when(adapter.toTask(eq(job))).thenReturn(task);

DruidKubernetesPeonClient peonClient = mock(DruidKubernetesPeonClient.class);

when(peonClient.listPeonPods()).thenReturn(Collections.singletonList(peonPod));
when(peonClient.listAllPeonJobs()).thenReturn(Collections.singletonList(job));
when(peonClient.jobExists(eq(k8sTaskId))).thenReturn(Optional.of(job));
when(peonClient.launchJobAndWaitForStart(isA(Job.class), anyLong(), isA(TimeUnit.class))).thenReturn(peonPod);
when(peonClient.getMainJobPod(eq(k8sTaskId))).thenReturn(peonPod);
Expand Down Expand Up @@ -325,11 +325,11 @@ public void testTheK8sRestartStateAndHandleJobsThatAlreadyCompletedWhileDown() t

K8sTaskAdapter adapter = mock(K8sTaskAdapter.class);
when(adapter.fromTask(eq(task))).thenReturn(job);
when(adapter.toTask(eq(peonPod))).thenReturn(task);
when(adapter.toTask(eq(job))).thenReturn(task);

DruidKubernetesPeonClient peonClient = mock(DruidKubernetesPeonClient.class);

when(peonClient.listPeonPods()).thenReturn(Collections.singletonList(peonPod));
when(peonClient.listAllPeonJobs()).thenReturn(Collections.singletonList(job));
when(peonClient.jobExists(eq(k8sTaskId))).thenReturn(Optional.of(job));
when(peonClient.launchJobAndWaitForStart(isA(Job.class), anyLong(), isA(TimeUnit.class))).thenReturn(peonPod);
when(peonClient.getMainJobPod(eq(k8sTaskId))).thenReturn(peonPod);
Expand Down Expand Up @@ -720,7 +720,7 @@ public void testK8sJobManualShutdown() throws Exception

K8sTaskAdapter adapter = mock(K8sTaskAdapter.class);
when(adapter.fromTask(eq(task))).thenReturn(job);
when(adapter.toTask(eq(peonPod))).thenReturn(task);
when(adapter.toTask(eq(job))).thenReturn(task);

DruidKubernetesPeonClient peonClient = mock(DruidKubernetesPeonClient.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.PodTemplateSpec;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
Expand Down Expand Up @@ -104,24 +102,6 @@ void testTheFlow()
Assertions.assertEquals(1, currentJobs.size());
}

@Test
void testListPeonPods()
{
Pod pod = new PodBuilder()
.withNewMetadata()
.withName("foo")
.addToLabels(DruidK8sConstants.LABEL_KEY, "true")
.endMetadata()
.withSpec(K8sTestUtils.getDummyPodSpec())
.build();
client.pods().inNamespace("test").create(pod);
DruidKubernetesPeonClient peonClient = new DruidKubernetesPeonClient(new TestKubernetesClient(this.client), "test",
false
);
List<Pod> pods = peonClient.listPeonPods();
Assertions.assertEquals(1, pods.size());
}

@Test
void testCleanup() throws KubernetesResourceNotFoundException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void testDeployingSomethingToKind(@TempDir Path tempDir) throws Exception
thread.start();

// assert that the env variable is corret
Task taskFromEnvVar = adapter.toTask(peonClient.getMainJobPod(new K8sTaskId(task.getId())));
Task taskFromEnvVar = adapter.toTask(job);
assertEquals(task, taskFromEnvVar);

// now copy the task.json file from the pod and make sure its the same as our task.json we expected
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.common;

import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
import io.fabric8.kubernetes.api.model.batch.v1.JobStatusBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class JobStatusTest
{
@Test
void testJobsActive()
{
Assertions.assertFalse(JobStatus.isActive(null));
Assertions.assertFalse(JobStatus.isActive(new JobBuilder().build()));
Assertions.assertFalse(JobStatus.isActive(new JobBuilder().withStatus(new JobStatusBuilder().withActive(null).build()).build()));
Assertions.assertFalse(JobStatus.isActive(new JobBuilder().withStatus(new JobStatusBuilder().withActive(0).build()).build()));
Assertions.assertTrue(JobStatus.isActive(new JobBuilder().withStatus(new JobStatusBuilder().withActive(1).build()).build()));
}

@Test
void testJobsSucceeded()
{
Assertions.assertFalse(JobStatus.isSucceeded(null));
Assertions.assertFalse(JobStatus.isSucceeded(new JobBuilder().build()));
Assertions.assertFalse(JobStatus.isSucceeded(new JobBuilder().withStatus(new JobStatusBuilder().withSucceeded(null).build()).build()));
Assertions.assertFalse(JobStatus.isSucceeded(new JobBuilder().withStatus(new JobStatusBuilder().withSucceeded(0).build()).build()));
Assertions.assertTrue(JobStatus.isSucceeded(new JobBuilder().withStatus(new JobStatusBuilder().withSucceeded(1).build()).build()));
}

@Test
void testJobsFailed()
{
Assertions.assertFalse(JobStatus.isFailed(null));
Assertions.assertFalse(JobStatus.isFailed(new JobBuilder().build()));
Assertions.assertFalse(JobStatus.isFailed(new JobBuilder().withStatus(new JobStatusBuilder().withFailed(null).build()).build()));
Assertions.assertFalse(JobStatus.isFailed(new JobBuilder().withStatus(new JobStatusBuilder().withFailed(0).build()).build()));
Assertions.assertTrue(JobStatus.isFailed(new JobBuilder().withStatus(new JobStatusBuilder().withFailed(1).build()).build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
import io.fabric8.kubernetes.api.model.batch.v1.JobList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -163,22 +162,19 @@ public void serializingAndDeserializingATask() throws IOException
task,
new PeonCommandContext(new ArrayList<>(), new ArrayList<>(), new File("/tmp/"))
);

// cant launch jobs with test server, we have to hack around this.
Pod pod = K8sTestUtils.createPodFromJob(jobFromSpec);
client.pods().inNamespace("test").create(pod);
PodList podList = client.pods().inNamespace("test").list();
assertEquals(1, podList.getItems().size());
client.batch().v1().jobs().inNamespace("test").create(jobFromSpec);

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation

Invoking [ItemWritableOperation.create](1) should be avoided because it has been deprecated.
JobList jobList = client.batch().v1().jobs().inNamespace("test").list();
assertEquals(1, jobList.getItems().size());

// assert that the size of the pod is 1g
Pod myPod = Iterables.getOnlyElement(podList.getItems());
Quantity containerMemory = myPod.getSpec().getContainers().get(0).getResources().getLimits().get("memory");
Job myJob = Iterables.getOnlyElement(jobList.getItems());
Quantity containerMemory = myJob.getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits().get("memory");
String amount = containerMemory.getAmount();
assertEquals(2400000000L, Long.valueOf(amount));
assertTrue(StringUtils.isBlank(containerMemory.getFormat())); // no units specified we talk in bytes

Task taskFromPod = adapter.toTask(Iterables.getOnlyElement(podList.getItems()));
assertEquals(task, taskFromPod);
Task taskFromJob = adapter.toTask(Iterables.getOnlyElement(jobList.getItems()));
assertEquals(task, taskFromJob);
}

@Test
Expand Down
Loading