use of com.facebook.presto.execution.ManagedQueryExecution in project presto by prestodb.
the class InternalResourceGroup method setSchedulingPolicy.
@Override
public void setSchedulingPolicy(SchedulingPolicy policy) {
synchronized (root) {
if (policy == schedulingPolicy) {
return;
}
if (parent.isPresent() && parent.get().schedulingPolicy == QUERY_PRIORITY) {
checkArgument(policy == QUERY_PRIORITY, "Parent of %s uses query priority scheduling, so %s must also", id, id);
}
// Switch to the appropriate queue implementation to implement the desired policy
Queue<InternalResourceGroup> queue;
TieredQueue<ManagedQueryExecution> queryQueue;
switch(policy) {
case FAIR:
queue = new FifoQueue<>();
queryQueue = new TieredQueue<>(FifoQueue::new);
break;
case WEIGHTED:
queue = new StochasticPriorityQueue<>();
queryQueue = new TieredQueue<>(StochasticPriorityQueue::new);
break;
case WEIGHTED_FAIR:
queue = new WeightedFairQueue<>();
queryQueue = new TieredQueue<>(IndexedPriorityQueue::new);
break;
case QUERY_PRIORITY:
// Sub groups must use query priority to ensure ordering
for (InternalResourceGroup group : subGroups.values()) {
group.setSchedulingPolicy(QUERY_PRIORITY);
}
queue = new IndexedPriorityQueue<>();
queryQueue = new TieredQueue<>(IndexedPriorityQueue::new);
break;
default:
throw new UnsupportedOperationException("Unsupported scheduling policy: " + policy);
}
schedulingPolicy = policy;
while (!eligibleSubGroups.isEmpty()) {
InternalResourceGroup group = eligibleSubGroups.poll();
addOrUpdateSubGroup(queue, group);
}
eligibleSubGroups = queue;
while (!queuedQueries.isEmpty()) {
ManagedQueryExecution query = queuedQueries.poll();
queryQueue.addOrUpdate(query, getQueryPriority(query.getSession()));
}
queuedQueries = queryQueue;
}
}
use of com.facebook.presto.execution.ManagedQueryExecution in project presto by prestodb.
the class InternalResourceGroup method internalStartNext.
protected boolean internalStartNext() {
checkState(Thread.holdsLock(root), "Must hold lock to find next query");
synchronized (root) {
if (!canRunMore()) {
return false;
}
ManagedQueryExecution query = queuedQueries.poll();
if (query != null) {
startInBackground(query);
return true;
}
// Remove even if the sub group still has queued queries, so that it goes to the back of the queue
InternalResourceGroup subGroup = eligibleSubGroups.poll();
if (subGroup == null) {
return false;
}
boolean started = subGroup.internalStartNext();
if (started) {
long currentTime = System.currentTimeMillis();
if (lastStartMillis != 0) {
timeBetweenStartsSec.update(Math.max(0, (currentTime - lastStartMillis) / 1000));
}
lastStartMillis = currentTime;
descendantQueuedQueries--;
// Don't call updateEligibility here, as we're in a recursive call, and don't want to repeatedly update our ancestors.
if (subGroup.isEligibleToStartNext()) {
addOrUpdateSubGroup(subGroup);
}
} else {
// If subGroup not able to start the query, we should add it back.
addOrUpdateSubGroup(subGroup);
}
return started;
}
}
use of com.facebook.presto.execution.ManagedQueryExecution in project presto by prestodb.
the class InternalResourceGroup method internalRefreshStats.
// Memory usage stats are expensive to maintain, so this method must be called periodically to update them
protected void internalRefreshStats() {
checkState(Thread.holdsLock(root), "Must hold lock to refresh stats");
synchronized (root) {
if (subGroups.isEmpty()) {
cachedMemoryUsageBytes = 0;
for (ManagedQueryExecution query : runningQueries) {
cachedMemoryUsageBytes += query.getUserMemoryReservation().toBytes();
}
Optional<ResourceGroupRuntimeInfo> resourceGroupRuntimeInfo = getAdditionalRuntimeInfo();
resourceGroupRuntimeInfo.ifPresent(groupRuntimeInfo -> cachedMemoryUsageBytes += groupRuntimeInfo.getMemoryUsageBytes());
} else {
for (Iterator<InternalResourceGroup> iterator = dirtySubGroups.iterator(); iterator.hasNext(); ) {
InternalResourceGroup subGroup = iterator.next();
long oldMemoryUsageBytes = subGroup.cachedMemoryUsageBytes;
cachedMemoryUsageBytes -= oldMemoryUsageBytes;
subGroup.internalRefreshStats();
cachedMemoryUsageBytes += subGroup.cachedMemoryUsageBytes;
if (!subGroup.isDirty()) {
iterator.remove();
}
if (oldMemoryUsageBytes != subGroup.cachedMemoryUsageBytes || subGroup.isDirty.get()) {
subGroup.updateEligibility();
subGroup.isDirty.set(false);
}
}
}
}
}
Aggregations