Skip to content

Prefer killing speculative tasks #17478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2023
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 @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
Expand Down Expand Up @@ -69,21 +70,8 @@ public Optional<KillTarget> chooseTargetToKill(List<RunningQueryInfo> runningQue
continue;
}

memoryPool.getTaskMemoryReservations().entrySet().stream()
.map(entry -> new SimpleEntry<>(TaskId.valueOf(entry.getKey()), entry.getValue()))
.filter(entry -> queriesWithTaskRetryPolicy.contains(entry.getKey().getQueryId()))
.max(comparing(entry -> {
TaskId taskId = entry.getKey();
Long memoryUsed = entry.getValue();
long wallTime = 0;
if (taskInfos.containsKey(taskId)) {
TaskStats stats = taskInfos.get(taskId).getStats();
wallTime = stats.getTotalScheduledTime().toMillis() + stats.getTotalBlockedTime().toMillis();
}
wallTime = Math.max(wallTime, MIN_WALL_TIME); // only look at memory consumption for fairly short-lived tasks
return (double) memoryUsed / wallTime;
}))
.map(SimpleEntry::getKey)
findBiggestTask(queriesWithTaskRetryPolicy, taskInfos, memoryPool, true) // try just speculative
.or(() -> findBiggestTask(queriesWithTaskRetryPolicy, taskInfos, memoryPool, false)) // fallback to any task
.ifPresent(tasksToKillBuilder::add);
}
Set<TaskId> tasksToKill = tasksToKillBuilder.build();
Expand All @@ -92,4 +80,35 @@ public Optional<KillTarget> chooseTargetToKill(List<RunningQueryInfo> runningQue
}
return Optional.of(KillTarget.selectedTasks(tasksToKill));
}

private static Optional<TaskId> findBiggestTask(Set<QueryId> queriesWithTaskRetryPolicy, Map<TaskId, TaskInfo> taskInfos, MemoryPoolInfo memoryPool, boolean onlySpeculative)
{
Stream<SimpleEntry<TaskId, Long>> stream = memoryPool.getTaskMemoryReservations().entrySet().stream()
.map(entry -> new SimpleEntry<>(TaskId.valueOf(entry.getKey()), entry.getValue()))
.filter(entry -> queriesWithTaskRetryPolicy.contains(entry.getKey().getQueryId()));

if (onlySpeculative) {
stream = stream.filter(entry -> {
TaskInfo taskInfo = taskInfos.get(entry.getKey());
if (taskInfo == null) {
return false;
}
return taskInfo.getTaskStatus().isSpeculative();
});
}

return stream
.max(comparing(entry -> {
TaskId taskId = entry.getKey();
Long memoryUsed = entry.getValue();
long wallTime = 0;
if (taskInfos.containsKey(taskId)) {
TaskStats stats = taskInfos.get(taskId).getStats();
wallTime = stats.getTotalScheduledTime().toMillis() + stats.getTotalBlockedTime().toMillis();
}
wallTime = Math.max(wallTime, MIN_WALL_TIME); // only look at memory consumption for fairly short-lived tasks
return (double) memoryUsed / wallTime;
}))
.map(SimpleEntry::getKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package io.trino.memory;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import io.trino.execution.TaskId;
import io.trino.operator.RetryPolicy;
import io.trino.execution.TaskInfo;
import io.trino.spi.QueryId;
import io.trino.spi.memory.MemoryPoolInfo;

Expand All @@ -25,8 +26,10 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.operator.RetryPolicy.TASK;

public class TotalReservationOnBlockedNodesTaskLowMemoryKiller
implements LowMemoryKiller
Expand All @@ -35,14 +38,16 @@ public class TotalReservationOnBlockedNodesTaskLowMemoryKiller
public Optional<KillTarget> chooseTargetToKill(List<RunningQueryInfo> runningQueries, List<MemoryInfo> nodes)
{
Set<QueryId> queriesWithTaskRetryPolicy = runningQueries.stream()
.filter(query -> query.getRetryPolicy() == RetryPolicy.TASK)
.filter(query -> query.getRetryPolicy() == TASK)
.map(RunningQueryInfo::getQueryId)
.collect(toImmutableSet());

if (queriesWithTaskRetryPolicy.isEmpty()) {
return Optional.empty();
}

Map<QueryId, RunningQueryInfo> runningQueriesById = Maps.uniqueIndex(runningQueries, RunningQueryInfo::getQueryId);

ImmutableSet.Builder<TaskId> tasksToKillBuilder = ImmutableSet.builder();
for (MemoryInfo node : nodes) {
MemoryPoolInfo memoryPool = node.getPool();
Expand All @@ -53,12 +58,8 @@ public Optional<KillTarget> chooseTargetToKill(List<RunningQueryInfo> runningQue
continue;
}

memoryPool.getTaskMemoryReservations().entrySet().stream()
// consider only tasks from queries with task retries enabled
.map(entry -> new SimpleEntry<>(TaskId.valueOf(entry.getKey()), entry.getValue()))
.filter(entry -> queriesWithTaskRetryPolicy.contains(entry.getKey().getQueryId()))
.max(Map.Entry.comparingByValue())
.map(SimpleEntry::getKey)
findBiggestTask(runningQueriesById, memoryPool, true) // try just speculative
.or(() -> findBiggestTask(runningQueriesById, memoryPool, false)) // fallback to any task
.ifPresent(tasksToKillBuilder::add);
}
Set<TaskId> tasksToKill = tasksToKillBuilder.build();
Expand All @@ -67,4 +68,27 @@ public Optional<KillTarget> chooseTargetToKill(List<RunningQueryInfo> runningQue
}
return Optional.of(KillTarget.selectedTasks(tasksToKill));
}

private static Optional<TaskId> findBiggestTask(Map<QueryId, RunningQueryInfo> runningQueries, MemoryPoolInfo memoryPool, boolean onlySpeculative)
{
Stream<SimpleEntry<TaskId, Long>> stream = memoryPool.getTaskMemoryReservations().entrySet().stream()
// consider only tasks from queries with task retries enabled
.map(entry -> new SimpleEntry<>(TaskId.valueOf(entry.getKey()), entry.getValue()))
.filter(entry -> runningQueries.containsKey(entry.getKey().getQueryId()))
.filter(entry -> runningQueries.get(entry.getKey().getQueryId()).getRetryPolicy() == TASK);

if (onlySpeculative) {
stream = stream.filter(entry -> {
TaskInfo taskInfo = runningQueries.get(entry.getKey().getQueryId()).getTaskInfos().get(entry.getKey());
if (taskInfo == null) {
return false;
}
return taskInfo.getTaskStatus().isSpeculative();
});
}

return stream
.max(Map.Entry.comparingByValue())
.map(SimpleEntry::getKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ private void testKillsBiggestTasksIfAllExecuteSameTime(Duration scheduledTime, D
else {
taskInfos = ImmutableMap.of(
"q_1", ImmutableMap.of(
1, buildTaskInfo(taskId("q_1", 1), TaskState.RUNNING, scheduledTime, blockedTime)),
1, buildTaskInfo(taskId("q_1", 1), TaskState.RUNNING, scheduledTime, blockedTime, false)),
"q_2", ImmutableMap.of(
1, buildTaskInfo(taskId("q_2", 1), TaskState.RUNNING, scheduledTime, blockedTime),
2, buildTaskInfo(taskId("q_2", 2), TaskState.RUNNING, scheduledTime, blockedTime),
3, buildTaskInfo(taskId("q_2", 3), TaskState.RUNNING, scheduledTime, blockedTime),
4, buildTaskInfo(taskId("q_2", 4), TaskState.RUNNING, scheduledTime, blockedTime),
5, buildTaskInfo(taskId("q_2", 5), TaskState.RUNNING, scheduledTime, blockedTime),
6, buildTaskInfo(taskId("q_2", 6), TaskState.RUNNING, scheduledTime, blockedTime),
7, buildTaskInfo(taskId("q_2", 7), TaskState.RUNNING, scheduledTime, blockedTime),
8, buildTaskInfo(taskId("q_2", 8), TaskState.RUNNING, scheduledTime, blockedTime)));
1, buildTaskInfo(taskId("q_2", 1), TaskState.RUNNING, scheduledTime, blockedTime, false),
2, buildTaskInfo(taskId("q_2", 2), TaskState.RUNNING, scheduledTime, blockedTime, false),
3, buildTaskInfo(taskId("q_2", 3), TaskState.RUNNING, scheduledTime, blockedTime, false),
4, buildTaskInfo(taskId("q_2", 4), TaskState.RUNNING, scheduledTime, blockedTime, false),
5, buildTaskInfo(taskId("q_2", 5), TaskState.RUNNING, scheduledTime, blockedTime, false),
6, buildTaskInfo(taskId("q_2", 6), TaskState.RUNNING, scheduledTime, blockedTime, false),
7, buildTaskInfo(taskId("q_2", 7), TaskState.RUNNING, scheduledTime, blockedTime, false),
8, buildTaskInfo(taskId("q_2", 8), TaskState.RUNNING, scheduledTime, blockedTime, false)));
}

assertEquals(
Expand Down Expand Up @@ -194,12 +194,12 @@ public void testKillsSmallerTaskIfWastedEffortRatioIsBetter()

Map<String, Map<Integer, TaskInfo>> taskInfos = ImmutableMap.of(
"q_1", ImmutableMap.of(
1, buildTaskInfo(taskId("q_1", 1), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS)),
2, buildTaskInfo(taskId("q_1", 2), TaskState.RUNNING, new Duration(400, SECONDS), new Duration(200, SECONDS))),
1, buildTaskInfo(taskId("q_1", 1), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS), false),
2, buildTaskInfo(taskId("q_1", 2), TaskState.RUNNING, new Duration(400, SECONDS), new Duration(200, SECONDS), false)),
"q_2", ImmutableMap.of(
1, buildTaskInfo(taskId("q_2", 1), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS)),
2, buildTaskInfo(taskId("q_2", 2), TaskState.RUNNING, new Duration(100, SECONDS), new Duration(100, SECONDS)),
3, buildTaskInfo(taskId("q_2", 3), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS))));
1, buildTaskInfo(taskId("q_2", 1), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS), false),
2, buildTaskInfo(taskId("q_2", 2), TaskState.RUNNING, new Duration(100, SECONDS), new Duration(100, SECONDS), false),
3, buildTaskInfo(taskId("q_2", 3), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS), false)));

// q1_1; n1; walltime 60s; memory 3; ratio 0.05 (pick for n1)
// q1_2; n2; walltime 600s; memory 8; ratio 0.0133
Expand All @@ -217,7 +217,46 @@ public void testKillsSmallerTaskIfWastedEffortRatioIsBetter()
taskId("q_2", 3)))));
}

private static TaskInfo buildTaskInfo(TaskId taskId, TaskState state, Duration scheduledTime, Duration blockedTime)
@Test
public void testPrefersKillingSpeculativeTasks()
{
int memoryPool = 8;
Map<String, Map<String, Long>> queries = ImmutableMap.<String, Map<String, Long>>builder()
.put("q_1", ImmutableMap.of("n1", 3L, "n2", 8L))
.put("q_2", ImmutableMap.of("n1", 7L, "n2", 2L))
.buildOrThrow();

Map<String, Map<String, Map<Integer, Long>>> tasks = ImmutableMap.<String, Map<String, Map<Integer, Long>>>builder()
.put("q_1", ImmutableMap.of(
"n1", ImmutableMap.of(1, 3L),
"n2", ImmutableMap.of(2, 8L)))
.put("q_2", ImmutableMap.of(
"n1", ImmutableMap.of(
1, 1L,
2, 6L),
"n2", ImmutableMap.of(3, 2L)))
.buildOrThrow();

Map<String, Map<Integer, TaskInfo>> taskInfos = ImmutableMap.of(
"q_1", ImmutableMap.of(
1, buildTaskInfo(taskId("q_1", 1), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS), false),
2, buildTaskInfo(taskId("q_1", 2), TaskState.RUNNING, new Duration(400, SECONDS), new Duration(200, SECONDS), false)),
"q_2", ImmutableMap.of(
1, buildTaskInfo(taskId("q_2", 1), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS), true),
2, buildTaskInfo(taskId("q_2", 2), TaskState.RUNNING, new Duration(100, SECONDS), new Duration(100, SECONDS), false),
3, buildTaskInfo(taskId("q_2", 3), TaskState.RUNNING, new Duration(30, SECONDS), new Duration(30, SECONDS), false)));

assertEquals(
lowMemoryKiller.chooseTargetToKill(
toRunningQueryInfoList(queries, ImmutableSet.of("q_1", "q_2"), taskInfos),
toNodeMemoryInfoList(memoryPool, queries, tasks)),
Optional.of(KillTarget.selectedTasks(
ImmutableSet.of(
taskId("q_2", 1), // if q_2_1 was not speculative then "q_1_1 would be picked
taskId("q_2", 3)))));
}

private static TaskInfo buildTaskInfo(TaskId taskId, TaskState state, Duration scheduledTime, Duration blockedTime, boolean speculative)
{
return new TaskInfo(
new TaskStatus(
Expand All @@ -227,7 +266,7 @@ private static TaskInfo buildTaskInfo(TaskId taskId, TaskState state, Duration s
state,
URI.create("fake://task/" + taskId + "/node/some_node"),
"some_node",
false,
speculative,
ImmutableList.of(),
0,
0,
Expand Down
Loading