Search in sources :

Example 1 with KillPolicy

use of io.prestosql.spi.resourcegroups.KillPolicy in project hetu-core by openlookeng.

the class DistributedResourceGroup method internalCancelQuery.

public void internalCancelQuery() {
    checkState(Thread.holdsLock(root), "Must hold lock to check cancel query");
    synchronized (root) {
        if (!subGroups.isEmpty()) {
            for (BaseResourceGroup group : subGroups()) {
                ((DistributedResourceGroup) group).internalCancelQuery();
            }
            return;
        }
        if (cachedMemoryUsageBytes <= softMemoryLimitBytes) {
            return;
        }
        Optional<SharedResourceGroupState> resourceGroupState = getSharedResourceGroupState();
        if (!resourceGroupState.isPresent()) {
            return;
        }
        Set<SharedQueryState> globalRunningQueries = resourceGroupState.get().getRunningQueries();
        List<SharedQueryState> sortedQueryList;
        Lock lock = stateStore.getLock(id.toString());
        boolean locked = false;
        try {
            // If lock is not free, then we return immediately, so no need to refresh after taking lock.
            // Before next call of this function, refresh will already happen.
            locked = lock.tryLock();
            if (locked) {
                switch(killPolicy) {
                    case HIGH_MEMORY_QUERIES:
                        double absMemoryMargin = 1 - (double) memoryMarginPercent / 100;
                        double absQueryProgressMargin = 1 - (double) queryProgressMarginPercent / 100;
                        sortedQueryList = globalRunningQueries.stream().sorted((o1, o2) -> {
                            if (o1.getTotalMemoryReservation().toBytes() < o2.getTotalMemoryReservation().toBytes() * absMemoryMargin || o2.getTotalMemoryReservation().toBytes() < o1.getTotalMemoryReservation().toBytes() * absMemoryMargin) {
                                return ((Long) o2.getTotalMemoryReservation().toBytes()).compareTo(o1.getTotalMemoryReservation().toBytes());
                            }
                            // if query progress difference is within 5%, then order will be decided based on memory itself.
                            if (o1.getQueryProgress().orElse(0) < o2.getQueryProgress().orElse(0) * absQueryProgressMargin || o2.getQueryProgress().orElse(0) < o1.getQueryProgress().orElse(0) * absQueryProgressMargin) {
                                return ((Double) o1.getQueryProgress().orElse(0)).compareTo((o2.getQueryProgress().orElse(0)));
                            }
                            return ((Long) o2.getTotalMemoryReservation().toBytes()).compareTo(o1.getTotalMemoryReservation().toBytes());
                        }).collect(Collectors.toList());
                        break;
                    case OLDEST_QUERIES:
                        sortedQueryList = globalRunningQueries.stream().sorted(Comparator.comparing(o -> (o.getExecutionStartTime().get()))).collect(Collectors.toList());
                        break;
                    case RECENT_QUERIES:
                        sortedQueryList = globalRunningQueries.stream().sorted(Comparator.comparing(o -> (o.getExecutionStartTime().get()), Comparator.reverseOrder())).collect(Collectors.toList());
                        break;
                    case FINISH_PERCENTAGE_QUERIES:
                        sortedQueryList = globalRunningQueries.stream().sorted(Comparator.comparing(o -> (o.getQueryProgress().orElse(0)))).collect(Collectors.toList());
                        break;
                    case NO_KILL:
                    // fall through
                    default:
                        sortedQueryList = new ArrayList<>();
                }
                long tempGlobalCachedMemoryUsage = cachedMemoryUsageBytes;
                long tempLocalCachedMemoryUsage = cachedMemoryUsageBytes;
                // So in this case only Q1 will be killed from local coordinator.
                for (SharedQueryState query : sortedQueryList) {
                    for (ManagedQueryExecution localQuery : runningQueries) {
                        if (query.getBasicQueryInfo().getQueryId().equals(localQuery.getBasicQueryInfo().getQueryId())) {
                            LOG.info("Query " + localQuery.getBasicQueryInfo().getQueryId() + " is getting killed for resource group " + this + " query will be killed with policy " + killPolicy);
                            localQuery.fail(new PrestoException(GENERIC_INSUFFICIENT_RESOURCES, "Memory consumption " + tempLocalCachedMemoryUsage + " exceed the limit " + softMemoryLimitBytes + "for resource group " + this));
                            queryFinished(localQuery);
                            tempLocalCachedMemoryUsage -= query.getTotalMemoryReservation().toBytes();
                            break;
                        }
                    }
                    tempGlobalCachedMemoryUsage -= query.getTotalMemoryReservation().toBytes();
                    if (tempGlobalCachedMemoryUsage <= softMemoryLimitBytes) {
                        break;
                    }
                }
            }
        } catch (RuntimeException e) {
            return;
        } finally {
            if (locked) {
                lock.unlock();
            }
        }
    }
}
Also used : SharedResourceGroupState(io.prestosql.statestore.SharedResourceGroupState) Logger(io.airlift.log.Logger) PriorityQueue(java.util.PriorityQueue) StateStore(io.prestosql.spi.statestore.StateStore) GENERIC_INSUFFICIENT_RESOURCES(io.prestosql.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES) CounterStat(io.airlift.stats.CounterStat) Duration(io.airlift.units.Duration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) BiConsumer(java.util.function.BiConsumer) DistributedResourceGroupUtils(io.prestosql.utils.DistributedResourceGroupUtils) StateCacheStore(io.prestosql.statestore.StateCacheStore) LinkedList(java.util.LinkedList) QueryStateInfo.createQueryStateInfo(io.prestosql.server.QueryStateInfo.createQueryStateInfo) PrestoException(io.prestosql.spi.PrestoException) SharedQueryState(io.prestosql.statestore.SharedQueryState) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DateTime(org.joda.time.DateTime) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) KillPolicy(io.prestosql.spi.resourcegroups.KillPolicy) TimeUnit(java.util.concurrent.TimeUnit) ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) DataSize(io.airlift.units.DataSize) List(java.util.List) Lock(java.util.concurrent.locks.Lock) SchedulingPolicy(io.prestosql.spi.resourcegroups.SchedulingPolicy) QueryState(io.prestosql.execution.QueryState) Optional(java.util.Optional) Queue(java.util.Queue) Comparator(java.util.Comparator) QueryStateInfo(io.prestosql.server.QueryStateInfo) ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) StateStoreConstants(io.prestosql.statestore.StateStoreConstants) ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) PrestoException(io.prestosql.spi.PrestoException) Lock(java.util.concurrent.locks.Lock) SharedQueryState(io.prestosql.statestore.SharedQueryState) SharedResourceGroupState(io.prestosql.statestore.SharedResourceGroupState)

Example 2 with KillPolicy

use of io.prestosql.spi.resourcegroups.KillPolicy in project hetu-core by openlookeng.

the class DistributedResourceGroupTemp method internalCancelQuery.

public void internalCancelQuery() {
    checkState(Thread.holdsLock(root), "Must hold lock to check cancel query");
    synchronized (root) {
        if (!subGroups.isEmpty()) {
            for (BaseResourceGroup group : subGroups()) {
                ((DistributedResourceGroupTemp) group).internalCancelQuery();
            }
            return;
        }
        long cachedMemoryUsageBytes = getGlobalCachedMemoryUsageBytes();
        if (cachedMemoryUsageBytes <= softMemoryLimitBytes) {
            return;
        }
        Optional<SharedResourceGroupState> resourceGroupState = getSharedResourceGroupState();
        if (!resourceGroupState.isPresent()) {
            return;
        }
        Set<SharedQueryState> globalRunningQueries = resourceGroupState.get().getRunningQueries();
        List<SharedQueryState> sortedQueryList;
        Lock lock = stateStore.getLock(id.toString());
        boolean locked = false;
        try {
            // If lock is not free, then we return immediately, so no need to refresh after taking lock.
            // Before next call of this function, refresh will already happen.
            locked = lock.tryLock();
            if (locked) {
                switch(killPolicy) {
                    case HIGH_MEMORY_QUERIES:
                        double absMemoryMargin = 1 - (double) memoryMarginPercent / 100;
                        double absQueryProgressMargin = 1 - (double) queryProgressMarginPercent / 100;
                        sortedQueryList = globalRunningQueries.stream().sorted((o1, o2) -> {
                            if (o1.getTotalMemoryReservation().toBytes() < o2.getTotalMemoryReservation().toBytes() * absMemoryMargin || o2.getTotalMemoryReservation().toBytes() < o1.getTotalMemoryReservation().toBytes() * absMemoryMargin) {
                                return ((Long) o2.getTotalMemoryReservation().toBytes()).compareTo(o1.getTotalMemoryReservation().toBytes());
                            }
                            // if query progress difference is within 5%, then order will be decided based on memory itself.
                            if (o1.getQueryProgress().orElse(0) < o2.getQueryProgress().orElse(0) * absQueryProgressMargin || o2.getQueryProgress().orElse(0) < o1.getQueryProgress().orElse(0) * absQueryProgressMargin) {
                                return ((Double) o1.getQueryProgress().orElse(0)).compareTo((o2.getQueryProgress().orElse(0)));
                            }
                            return ((Long) o2.getTotalMemoryReservation().toBytes()).compareTo(o1.getTotalMemoryReservation().toBytes());
                        }).collect(Collectors.toList());
                        break;
                    case OLDEST_QUERIES:
                        sortedQueryList = globalRunningQueries.stream().sorted(Comparator.comparing(o -> (o.getExecutionStartTime().get()))).collect(Collectors.toList());
                        break;
                    case RECENT_QUERIES:
                        sortedQueryList = globalRunningQueries.stream().sorted(Comparator.comparing(o -> (o.getExecutionStartTime().get()), Comparator.reverseOrder())).collect(Collectors.toList());
                        break;
                    case FINISH_PERCENTAGE_QUERIES:
                        sortedQueryList = globalRunningQueries.stream().sorted(Comparator.comparing(o -> (o.getQueryProgress().orElse(0)))).collect(Collectors.toList());
                        break;
                    case NO_KILL:
                    // fall through
                    default:
                        sortedQueryList = new ArrayList<>();
                }
                long tempGlobalCachedMemoryUsage = cachedMemoryUsageBytes;
                long tempLocalCachedMemoryUsage = localCachedMemoryUsageBytes;
                // So in this case only Q1 will be killed from local coordinator.
                for (SharedQueryState query : sortedQueryList) {
                    for (ManagedQueryExecution localQuery : localRunningQueries) {
                        if (query.getBasicQueryInfo().getQueryId().equals(localQuery.getBasicQueryInfo().getQueryId())) {
                            LOG.info("Query " + localQuery.getBasicQueryInfo().getQueryId() + " is getting killed for resource group " + this + " query will be killed with policy " + killPolicy);
                            localQuery.fail(new PrestoException(GENERIC_INSUFFICIENT_RESOURCES, "Memory consumption " + tempLocalCachedMemoryUsage + " exceed the limit " + softMemoryLimitBytes + "for resource group " + this));
                            queryFinished(localQuery);
                            tempLocalCachedMemoryUsage -= query.getTotalMemoryReservation().toBytes();
                            break;
                        }
                    }
                    tempGlobalCachedMemoryUsage -= query.getTotalMemoryReservation().toBytes();
                    if (tempGlobalCachedMemoryUsage <= softMemoryLimitBytes) {
                        break;
                    }
                }
            }
        } catch (RuntimeException e) {
            return;
        } finally {
            if (locked) {
                lock.unlock();
            }
        }
    }
}
Also used : SharedResourceGroupState(io.prestosql.statestore.SharedResourceGroupState) PriorityQueue(java.util.PriorityQueue) StateStore(io.prestosql.spi.statestore.StateStore) GENERIC_INSUFFICIENT_RESOURCES(io.prestosql.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES) Duration(io.airlift.units.Duration) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) DistributedResourceGroupUtils(io.prestosql.utils.DistributedResourceGroupUtils) StateCacheStore(io.prestosql.statestore.StateCacheStore) LongMath.saturatedMultiply(com.google.common.math.LongMath.saturatedMultiply) QueryStateInfo.createQueryStateInfo(io.prestosql.server.QueryStateInfo.createQueryStateInfo) PrestoException(io.prestosql.spi.PrestoException) SharedQueryState(io.prestosql.statestore.SharedQueryState) StateMap(io.prestosql.spi.statestore.StateMap) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) DataSize(io.airlift.units.DataSize) List(java.util.List) LongMath.saturatedAdd(com.google.common.math.LongMath.saturatedAdd) SchedulingPolicy(io.prestosql.spi.resourcegroups.SchedulingPolicy) Optional(java.util.Optional) Queue(java.util.Queue) ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) Logger(io.airlift.log.Logger) CounterStat(io.airlift.stats.CounterStat) LongMath.saturatedSubtract(com.google.common.math.LongMath.saturatedSubtract) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) USER_ERROR(io.prestosql.spi.ErrorType.USER_ERROR) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) Objects.requireNonNull(java.util.Objects.requireNonNull) BiConsumer(java.util.function.BiConsumer) LinkedList(java.util.LinkedList) InternalNodeManager(io.prestosql.metadata.InternalNodeManager) Executor(java.util.concurrent.Executor) StateCollection(io.prestosql.spi.statestore.StateCollection) InternalNode(io.prestosql.metadata.InternalNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KillPolicy(io.prestosql.spi.resourcegroups.KillPolicy) TimeUnit(java.util.concurrent.TimeUnit) Lock(java.util.concurrent.locks.Lock) QueryState(io.prestosql.execution.QueryState) Comparator(java.util.Comparator) QueryStateInfo(io.prestosql.server.QueryStateInfo) StateStoreConstants(io.prestosql.statestore.StateStoreConstants) ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) PrestoException(io.prestosql.spi.PrestoException) Lock(java.util.concurrent.locks.Lock) SharedQueryState(io.prestosql.statestore.SharedQueryState) SharedResourceGroupState(io.prestosql.statestore.SharedResourceGroupState)

Example 3 with KillPolicy

use of io.prestosql.spi.resourcegroups.KillPolicy in project hetu-core by openlookeng.

the class InternalResourceGroup method internalCancelQuery.

public void internalCancelQuery() {
    checkState(Thread.holdsLock(root), "Must hold lock to check cancel query");
    synchronized (root) {
        if (!subGroups.isEmpty()) {
            for (Iterator<InternalResourceGroup> iterator = dirtySubGroups.iterator(); iterator.hasNext(); ) {
                InternalResourceGroup subGroup = iterator.next();
                subGroup.internalCancelQuery();
            }
            return;
        }
        if (cachedMemoryUsageBytes <= softMemoryLimitBytes) {
            return;
        }
        List<ManagedQueryExecution> sortedQueryList;
        switch(killPolicy) {
            case HIGH_MEMORY_QUERIES:
                double absMemoryMargin = 1 - (double) memoryMarginPercent / 100;
                double absQueryProgressMargin = 1 - (double) queryProgressMarginPercent / 100;
                sortedQueryList = runningQueries.stream().sorted((o1, o2) -> {
                    if (o1.getTotalMemoryReservation().toBytes() < o2.getTotalMemoryReservation().toBytes() * absMemoryMargin || o2.getTotalMemoryReservation().toBytes() < o1.getTotalMemoryReservation().toBytes() * absMemoryMargin) {
                        return ((Long) o2.getTotalMemoryReservation().toBytes()).compareTo(o1.getTotalMemoryReservation().toBytes());
                    }
                    // if query progress difference is within 5%, then order will be decided based on memory itself.
                    if (o1.getQueryProgress().orElse(0) < o2.getQueryProgress().orElse(0) * absQueryProgressMargin || o2.getQueryProgress().orElse(0) < o1.getQueryProgress().orElse(0) * absQueryProgressMargin) {
                        return ((Double) o1.getQueryProgress().orElse(0)).compareTo((o2.getQueryProgress().orElse(0)));
                    }
                    return ((Long) o2.getTotalMemoryReservation().toBytes()).compareTo(o1.getTotalMemoryReservation().toBytes());
                }).collect(Collectors.toList());
                break;
            case OLDEST_QUERIES:
                sortedQueryList = runningQueries.stream().sorted(Comparator.comparing(o -> (o.getQueryExecutionStartTime().get()))).collect(Collectors.toList());
                break;
            case RECENT_QUERIES:
                sortedQueryList = runningQueries.stream().sorted(Comparator.comparing(o -> (o.getQueryExecutionStartTime().get()), Comparator.reverseOrder())).collect(Collectors.toList());
                break;
            case FINISH_PERCENTAGE_QUERIES:
                sortedQueryList = runningQueries.stream().sorted(Comparator.comparing(o -> (o.getQueryProgress().orElse(0)))).collect(Collectors.toList());
                break;
            case NO_KILL:
            // fall through
            default:
                sortedQueryList = new ArrayList<>();
        }
        long tempCacheMemoryUsageBytes = cachedMemoryUsageBytes;
        for (ManagedQueryExecution query : sortedQueryList) {
            LOG.info("Query " + query.getBasicQueryInfo().getQueryId() + " is getting killed for resource group " + this + " query will be killed with policy " + killPolicy);
            query.fail(new PrestoException(GENERIC_INSUFFICIENT_RESOURCES, "Memory consumption " + tempCacheMemoryUsageBytes + " exceed the limit " + softMemoryLimitBytes + "for resource group " + this));
            queryFinished(query);
            tempCacheMemoryUsageBytes -= query.getTotalMemoryReservation().toBytes();
            if (tempCacheMemoryUsageBytes <= softMemoryLimitBytes) {
                break;
            }
        }
    }
}
Also used : WEIGHTED(io.prestosql.spi.resourcegroups.SchedulingPolicy.WEIGHTED) Logger(io.airlift.log.Logger) GENERIC_INSUFFICIENT_RESOURCES(io.prestosql.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES) CounterStat(io.airlift.stats.CounterStat) LongMath.saturatedSubtract(com.google.common.math.LongMath.saturatedSubtract) WEIGHTED_FAIR(io.prestosql.spi.resourcegroups.SchedulingPolicy.WEIGHTED_FAIR) Duration(io.airlift.units.Duration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) USER_ERROR(io.prestosql.spi.ErrorType.USER_ERROR) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Managed(org.weakref.jmx.Managed) Objects.requireNonNull(java.util.Objects.requireNonNull) BiConsumer(java.util.function.BiConsumer) LongMath.saturatedMultiply(com.google.common.math.LongMath.saturatedMultiply) QueryStateInfo.createQueryStateInfo(io.prestosql.server.QueryStateInfo.createQueryStateInfo) Usage(io.prestosql.execution.resourcegroups.WeightedFairQueue.Usage) SystemSessionProperties.getQueryPriority(io.prestosql.SystemSessionProperties.getQueryPriority) PrestoException(io.prestosql.spi.PrestoException) QUERY_PRIORITY(io.prestosql.spi.resourcegroups.SchedulingPolicy.QUERY_PRIORITY) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Math.min(java.lang.Math.min) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) KillPolicy(io.prestosql.spi.resourcegroups.KillPolicy) DataSize(io.airlift.units.DataSize) List(java.util.List) LongMath.saturatedAdd(com.google.common.math.LongMath.saturatedAdd) SchedulingPolicy(io.prestosql.spi.resourcegroups.SchedulingPolicy) Optional(java.util.Optional) Comparator(java.util.Comparator) QueryStateInfo(io.prestosql.server.QueryStateInfo) ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) PrestoException(io.prestosql.spi.PrestoException)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Logger (io.airlift.log.Logger)3 CounterStat (io.airlift.stats.CounterStat)3 DataSize (io.airlift.units.DataSize)3 Duration (io.airlift.units.Duration)3 ManagedQueryExecution (io.prestosql.execution.ManagedQueryExecution)3 QueryStateInfo (io.prestosql.server.QueryStateInfo)3 QueryStateInfo.createQueryStateInfo (io.prestosql.server.QueryStateInfo.createQueryStateInfo)3 PrestoException (io.prestosql.spi.PrestoException)3 GENERIC_INSUFFICIENT_RESOURCES (io.prestosql.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES)3 KillPolicy (io.prestosql.spi.resourcegroups.KillPolicy)3 SchedulingPolicy (io.prestosql.spi.resourcegroups.SchedulingPolicy)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Comparator (java.util.Comparator)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3