-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task low memory killer which picks ups a task which has highest used-memory/task-runtime ratio.
- Loading branch information
Showing
6 changed files
with
417 additions
and
1 deletion.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
core/trino-main/src/main/java/io/trino/memory/LeastWastedEffortTaskLowMemoryKiller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed 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 io.trino.memory; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import io.trino.execution.TaskId; | ||
import io.trino.execution.TaskInfo; | ||
import io.trino.operator.RetryPolicy; | ||
import io.trino.operator.TaskStats; | ||
import io.trino.spi.QueryId; | ||
import io.trino.spi.memory.MemoryPoolInfo; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
import static java.util.Comparator.comparing; | ||
|
||
public class LeastWastedEffortTaskLowMemoryKiller | ||
implements LowMemoryKiller | ||
{ | ||
private static final long MIN_WALL_TIME = Duration.of(30, ChronoUnit.SECONDS).toMillis(); | ||
|
||
@Override | ||
public Optional<KillTarget> chooseTargetToKill(List<RunningQueryInfo> runningQueries, List<MemoryInfo> nodes) | ||
{ | ||
Set<QueryId> queriesWithTaskRetryPolicy = runningQueries.stream() | ||
.filter(query -> query.getRetryPolicy() == RetryPolicy.TASK) | ||
.map(RunningQueryInfo::getQueryId) | ||
.collect(toImmutableSet()); | ||
|
||
if (queriesWithTaskRetryPolicy.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
|
||
ImmutableSet.Builder<TaskId> tasksToKillBuilder = ImmutableSet.builder(); | ||
|
||
Map<TaskId, TaskInfo> taskInfos = runningQueries.stream() | ||
.filter(queryInfo -> queriesWithTaskRetryPolicy.contains(queryInfo.getQueryId())) | ||
.flatMap(queryInfo -> queryInfo.getTaskInfos().entrySet().stream()) | ||
.collect(toImmutableMap( | ||
Map.Entry::getKey, | ||
Map.Entry::getValue)); | ||
|
||
for (MemoryInfo node : nodes) { | ||
MemoryPoolInfo memoryPool = node.getPool(); | ||
if (memoryPool == null) { | ||
continue; | ||
} | ||
if (memoryPool.getFreeBytes() + memoryPool.getReservedRevocableBytes() > 0) { | ||
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) | ||
.ifPresent(tasksToKillBuilder::add); | ||
} | ||
Set<TaskId> tasksToKill = tasksToKillBuilder.build(); | ||
if (tasksToKill.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(KillTarget.selectedTasks(tasksToKill)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.