Search in sources :

Example 1 with VoidTraversingQueryContextVisitor

use of io.prestosql.memory.VoidTraversingQueryContextVisitor in project hetu-core by openlookeng.

the class MemoryRevokingScheduler method requestRevoking.

private void requestRevoking(MemoryPool memoryPool, Collection<SqlTask> sqlTasks, long remainingBytesToRevoke) {
    AtomicLong remainingBytesToRevokeAtomic = new AtomicLong(remainingBytesToRevoke);
    Map<Long, List<SqlTask>> revocableMemoryMapping = new HashMap<>();
    // This approach disregards create time
    if (prioritizeLargerSpiltsMemoryRevoke) {
        sqlTasks.stream().filter(task -> task.getTaskStatus().getState() == TaskState.RUNNING).filter(task -> task.getQueryContext().getMemoryPool() == memoryPool).forEach(task -> task.getQueryContext().accept(new VoidTraversingQueryContextVisitor<AtomicLong>() {

            @Override
            public Void visitQueryContext(QueryContext queryContext, AtomicLong remainingBytesToRevoke) {
                if (remainingBytesToRevoke.get() < 0) {
                    // exit immediately if no work needs to be done
                    return null;
                }
                return super.visitQueryContext(queryContext, remainingBytesToRevoke);
            }

            @Override
            public Void visitOperatorContext(OperatorContext operatorContext, AtomicLong remainingBytesToRevoke) {
                if (remainingBytesToRevoke.get() <= 0) {
                    return null;
                }
                long revocableBytes = operatorContext.getRevocableMemory();
                if (revocableBytes > 0) {
                    if (revocableBytes >= revocableMemorySelectionThreshold) {
                        remainingBytesToRevoke.addAndGet(-revocableBytes);
                    }
                    revocableMemoryMapping.computeIfAbsent(revocableBytes, list -> new ArrayList()).add(task);
                }
                return null;
            }
        }, remainingBytesToRevokeAtomic));
        if (revocableMemoryMapping.size() > 0) {
            revocableMemoryMapping.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).flatMap(list -> list.getValue().stream()).filter(task -> task.getTaskStatus().getState() == TaskState.RUNNING).forEach(task -> task.getQueryContext().accept(new VoidTraversingQueryContextVisitor<AtomicLong>() {

                @Override
                public Void visitQueryContext(QueryContext queryContext, AtomicLong remainingBytesToRevoke) {
                    if (remainingBytesToRevoke.get() < 0) {
                        // exit immediately if no work needs to be done
                        return null;
                    }
                    return super.visitQueryContext(queryContext, remainingBytesToRevoke);
                }

                @Override
                public Void visitOperatorContext(OperatorContext operatorContext, AtomicLong remainingBytesToRevoke) {
                    if (remainingBytesToRevoke.get() > 0) {
                        long revokedBytes = operatorContext.requestMemoryRevoking();
                        if (revokedBytes > 0) {
                            remainingBytesToRevoke.addAndGet(-revokedBytes);
                            log.debug("memoryPool=%s: requested revoking %s; remaining %s", memoryPool.getId(), revokedBytes, remainingBytesToRevoke.get());
                        }
                    }
                    return null;
                }
            }, remainingBytesToRevokeAtomic));
        }
    } else {
        sqlTasks.stream().filter(task -> task.getTaskStatus().getState() == TaskState.RUNNING).filter(task -> task.getQueryContext().getMemoryPool() == memoryPool).sorted(ORDER_BY_CREATE_TIME).forEach(task -> task.getQueryContext().accept(new VoidTraversingQueryContextVisitor<AtomicLong>() {

            @Override
            public Void visitQueryContext(QueryContext queryContext, AtomicLong remainingBytesToRevoke) {
                if (remainingBytesToRevoke.get() < 0) {
                    // exit immediately if no work needs to be done
                    return null;
                }
                return super.visitQueryContext(queryContext, remainingBytesToRevoke);
            }

            @Override
            public Void visitOperatorContext(OperatorContext operatorContext, AtomicLong remainingBytesToRevoke) {
                if (remainingBytesToRevoke.get() > 0) {
                    long revokedBytes = operatorContext.requestMemoryRevoking();
                    if (revokedBytes > 0) {
                        remainingBytesToRevoke.addAndGet(-revokedBytes);
                        log.debug("memoryPool=%s: requested revoking %s; remaining %s", memoryPool.getId(), revokedBytes, remainingBytesToRevoke.get());
                    }
                }
                return null;
            }
        }, remainingBytesToRevokeAtomic));
    }
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) Logger(io.airlift.log.Logger) VoidTraversingQueryContextVisitor(io.prestosql.memory.VoidTraversingQueryContextVisitor) LocalMemoryManager(io.prestosql.memory.LocalMemoryManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) MemoryPool(io.prestosql.memory.MemoryPool) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) MemoryPoolListener(io.prestosql.memory.MemoryPoolListener) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TraversingQueryContextVisitor(io.prestosql.memory.TraversingQueryContextVisitor) Nullable(javax.annotation.Nullable) OperatorContext(io.prestosql.operator.OperatorContext) Collection(java.util.Collection) QueryContext(io.prestosql.memory.QueryContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Ordering(com.google.common.collect.Ordering) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) PostConstruct(javax.annotation.PostConstruct) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) SECONDS(java.util.concurrent.TimeUnit.SECONDS) AtomicLong(java.util.concurrent.atomic.AtomicLong) VoidTraversingQueryContextVisitor(io.prestosql.memory.VoidTraversingQueryContextVisitor) HashMap(java.util.HashMap) OperatorContext(io.prestosql.operator.OperatorContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryContext(io.prestosql.memory.QueryContext)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 Ordering (com.google.common.collect.Ordering)1 Logger (io.airlift.log.Logger)1 LocalMemoryManager (io.prestosql.memory.LocalMemoryManager)1 MemoryPool (io.prestosql.memory.MemoryPool)1 MemoryPoolListener (io.prestosql.memory.MemoryPoolListener)1 QueryContext (io.prestosql.memory.QueryContext)1 TraversingQueryContextVisitor (io.prestosql.memory.TraversingQueryContextVisitor)1 VoidTraversingQueryContextVisitor (io.prestosql.memory.VoidTraversingQueryContextVisitor)1 OperatorContext (io.prestosql.operator.OperatorContext)1 FeaturesConfig (io.prestosql.sql.analyzer.FeaturesConfig)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1