Search in sources :

Example 1 with QueryMemoryInfo

use of io.trino.memory.LowMemoryKiller.QueryMemoryInfo in project trino by trinodb.

the class ClusterMemoryManager method callOomKiller.

private synchronized void callOomKiller(Iterable<QueryExecution> runningQueries) {
    List<QueryMemoryInfo> queryMemoryInfoList = Streams.stream(runningQueries).map(this::createQueryMemoryInfo).collect(toImmutableList());
    List<MemoryInfo> nodeMemoryInfos = nodes.values().stream().map(RemoteNodeMemory::getInfo).filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
    Optional<KillTarget> killTarget = lowMemoryKiller.chooseQueryToKill(queryMemoryInfoList, nodeMemoryInfos);
    if (killTarget.isPresent()) {
        if (killTarget.get().isWholeQuery()) {
            QueryId queryId = killTarget.get().getQuery();
            log.debug("Low memory killer chose %s", queryId);
            Optional<QueryExecution> chosenQuery = findRunningQuery(runningQueries, killTarget.get().getQuery());
            if (chosenQuery.isPresent()) {
                // See comments in  isQueryGone for why chosenQuery might be absent.
                chosenQuery.get().fail(new TrinoException(CLUSTER_OUT_OF_MEMORY, "Query killed because the cluster is out of memory. Please try again in a few minutes."));
                queriesKilledDueToOutOfMemory.incrementAndGet();
                lastKillTarget = killTarget.get();
                logQueryKill(queryId, nodeMemoryInfos);
            }
        } else {
            Set<TaskId> tasks = killTarget.get().getTasks();
            log.debug("Low memory killer chose %s", tasks);
            ImmutableSet.Builder<TaskId> killedTasksBuilder = ImmutableSet.builder();
            for (TaskId task : tasks) {
                Optional<QueryExecution> runningQuery = findRunningQuery(runningQueries, task.getQueryId());
                if (runningQuery.isPresent()) {
                    runningQuery.get().failTask(task, new TrinoException(CLUSTER_OUT_OF_MEMORY, "Task killed because the cluster is out of memory."));
                    tasksKilledDueToOutOfMemory.incrementAndGet();
                    killedTasksBuilder.add(task);
                }
            }
            // only record tasks actually killed
            ImmutableSet<TaskId> killedTasks = killedTasksBuilder.build();
            if (!killedTasks.isEmpty()) {
                lastKillTarget = KillTarget.selectedTasks(killedTasks);
                logTasksKill(killedTasks, nodeMemoryInfos);
            }
        }
    }
}
Also used : TaskId(io.trino.execution.TaskId) Optional(java.util.Optional) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) QueryId(io.trino.spi.QueryId) QueryExecution(io.trino.execution.QueryExecution) QueryMemoryInfo(io.trino.memory.LowMemoryKiller.QueryMemoryInfo) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) QueryMemoryInfo(io.trino.memory.LowMemoryKiller.QueryMemoryInfo) TrinoException(io.trino.spi.TrinoException)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 MoreCollectors.toOptional (com.google.common.collect.MoreCollectors.toOptional)1 QueryExecution (io.trino.execution.QueryExecution)1 TaskId (io.trino.execution.TaskId)1 QueryMemoryInfo (io.trino.memory.LowMemoryKiller.QueryMemoryInfo)1 QueryId (io.trino.spi.QueryId)1 TrinoException (io.trino.spi.TrinoException)1 Optional (java.util.Optional)1