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 @@ -155,6 +155,7 @@ public ListenableFuture<TaskStatus> run(Task task)
synchronized (tasks) {
tasks.computeIfAbsent(
task.getId(), k -> new K8sWorkItem(
client,
task,
exec.submit(() -> {
K8sTaskId k8sTaskId = new K8sTaskId(task);
Expand Down Expand Up @@ -375,7 +376,12 @@ public Collection<? extends TaskRunnerWorkItem> getKnownTasks()
try {
Task task = adapter.toTask(existingTask);
ListenableFuture<TaskStatus> future = run(task);
result.add(new K8sWorkItem(task, future, DateTimes.of(existingTask.getMetadata().getCreationTimestamp())));
result.add(new K8sWorkItem(
client,
task,
future,
DateTimes.of(existingTask.getMetadata().getCreationTimestamp())
));
}
catch (IOException e) {
log.error("Error deserializing task from pod: " + existingTask.getMetadata().getName());
Expand Down Expand Up @@ -457,7 +463,12 @@ public Collection<TaskRunnerWorkItem> getRunningTasks()
try {
Task task = adapter.toTask(existingTask);
ListenableFuture<TaskStatus> future = run(task);
result.add(new K8sWorkItem(task, future, DateTime.parse(existingTask.getMetadata().getCreationTimestamp())));
result.add(new K8sWorkItem(
client,
task,
future,
DateTime.parse(existingTask.getMetadata().getCreationTimestamp())
));
}
catch (IOException e) {
log.error("Error deserializing task from pod: " + existingTask.getMetadata().getName());
Expand Down Expand Up @@ -494,20 +505,28 @@ public RunnerTaskState getRunnerTaskState(String taskId)
}
}

public class K8sWorkItem extends TaskRunnerWorkItem
public static class K8sWorkItem extends TaskRunnerWorkItem
{
private final Task task;
private KubernetesPeonClient client;

private K8sWorkItem(Task task, ListenableFuture<TaskStatus> statusFuture)
public K8sWorkItem(KubernetesPeonClient client, Task task, ListenableFuture<TaskStatus> statusFuture)
{
super(task.getId(), statusFuture);
this.task = task;
this.client = client;
}

private K8sWorkItem(Task task, ListenableFuture<TaskStatus> statusFuture, DateTime createdTime)
public K8sWorkItem(
KubernetesPeonClient client,
Task task,
ListenableFuture<TaskStatus> statusFuture,
DateTime createdTime
)
{
super(task.getId(), statusFuture, createdTime, createdTime);
this.task = task;
this.client = client;

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.

where does it get accessed?

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.

Used for testing. I needed to inject the client to mock out

}

@Override
Expand All @@ -516,6 +535,9 @@ public TaskLocation getLocation()
K8sTaskId taskId = new K8sTaskId(task.getId());
try {
Pod mainPod = client.getMainJobPod(new K8sTaskId(task.getId()));
if (mainPod.getStatus() == null || mainPod.getStatus().getPodIP() == null) {
return TaskLocation.unknown();
}
boolean tlsEnabled = Boolean.parseBoolean(
mainPod.getMetadata()
.getAnnotations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.druid.k8s.overlord.common.JobResponse;
import org.apache.druid.k8s.overlord.common.K8sTaskAdapter;
import org.apache.druid.k8s.overlord.common.K8sTaskId;
import org.apache.druid.k8s.overlord.common.KubernetesPeonClient;
import org.apache.druid.k8s.overlord.common.PeonCommandContext;
import org.apache.druid.k8s.overlord.common.PeonPhase;
import org.apache.druid.server.DruidNode;
Expand Down Expand Up @@ -161,7 +162,10 @@ void testAlreadyRunningJobInK8s() throws Exception

when(peonClient.jobExists(eq(k8sTaskId))).thenReturn(Optional.of(job));
when(peonClient.getMainJobPod(eq(k8sTaskId))).thenReturn(peonPod);
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(job, PeonPhase.SUCCEEDED));
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(
job,
PeonPhase.SUCCEEDED
));
when(peonClient.cleanUpJob(eq(k8sTaskId))).thenReturn(true);

KubernetesTaskRunner taskRunner = new KubernetesTaskRunner(
Expand Down Expand Up @@ -214,7 +218,10 @@ void testJobNeedsToLaunchInK8s() throws Exception
when(peonClient.jobExists(eq(k8sTaskId))).thenReturn(Optional.fromNullable(null));
when(peonClient.launchJobAndWaitForStart(isA(Job.class), anyLong(), isA(TimeUnit.class))).thenReturn(peonPod);
when(peonClient.getMainJobPod(eq(k8sTaskId))).thenReturn(peonPod);
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(job, PeonPhase.SUCCEEDED));
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(
job,
PeonPhase.SUCCEEDED
));
when(peonClient.cleanUpJob(eq(k8sTaskId))).thenReturn(true);

KubernetesTaskRunner taskRunner = new KubernetesTaskRunner(
Expand Down Expand Up @@ -278,7 +285,10 @@ void testTheK8sRestartState() throws Exception
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);
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(job, PeonPhase.SUCCEEDED));
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(
job,
PeonPhase.SUCCEEDED
));
when(peonClient.cleanUpJob(eq(k8sTaskId))).thenReturn(true);

KubernetesTaskRunner taskRunner = new KubernetesTaskRunner(
Expand Down Expand Up @@ -339,7 +349,10 @@ void testTheK8sRestartStateAndHandleJobsThatAlreadyCompletedWhileDown() throws E
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);
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(job, PeonPhase.SUCCEEDED));
when(peonClient.waitForJobCompletion(eq(k8sTaskId), anyLong(), isA(TimeUnit.class))).thenReturn(new JobResponse(
job,
PeonPhase.SUCCEEDED
));
when(peonClient.cleanUpJob(eq(k8sTaskId))).thenReturn(true);

KubernetesTaskRunner taskRunner = new KubernetesTaskRunner(
Expand Down Expand Up @@ -427,6 +440,31 @@ void testMaxQueueSizeIsEnforced()
));
}

@Test
void testWorkItemGetLocation()
{
KubernetesPeonClient client = mock(KubernetesPeonClient.class);
Pod pod = mock(Pod.class);
PodStatus status = mock(PodStatus.class);
when(status.getPodIP()).thenReturn(null).thenReturn("tweak");
when(pod.getStatus()).thenReturn(status);

ObjectMeta metadata = mock(ObjectMeta.class);
when(metadata.getAnnotations()).thenReturn(ImmutableMap.of(DruidK8sConstants.TLS_ENABLED, "false"));

when(pod.getMetadata()).thenReturn(metadata);
when(client.getMainJobPod(any())).thenReturn(pod);

Task task = mock(Task.class);
when(task.getId()).thenReturn("butters");
KubernetesTaskRunner.K8sWorkItem k8sWorkItem = new KubernetesTaskRunner.K8sWorkItem(client, task, null);
TaskLocation location = k8sWorkItem.getLocation();
assertEquals(TaskLocation.unknown(), location);

TaskLocation realLocation = k8sWorkItem.getLocation();
assertEquals(TaskLocation.create("tweak", DruidK8sConstants.PORT, DruidK8sConstants.TLS_PORT, false), realLocation);
}

private Task makeTask()
{
return new TestableNoopTask(
Expand Down