Search in sources :

Example 6 with SharedQueryState

use of io.prestosql.statestore.SharedQueryState in project hetu-core by openlookeng.

the class TestDistributedResourceGroup method updateQueryStateCache.

// update cached queryStates for a single query
private static void updateQueryStateCache(MockManagedQueryExecution query) {
    Map<String, SharedQueryState> queryStates = StateCacheStore.get().getCachedStates(StateStoreConstants.QUERY_STATE_COLLECTION_NAME);
    if (queryStates == null) {
        queryStates = new HashMap<>();
    }
    String queryKey = query.toString();
    queryStates.put(queryKey, getSharedQueryState(query));
    StateCacheStore.get().setCachedStates(StateStoreConstants.QUERY_STATE_COLLECTION_NAME, queryStates);
    DistributedResourceGroupUtils.mapCachedStates();
}
Also used : SharedQueryState(io.prestosql.statestore.SharedQueryState) Matchers.anyString(org.mockito.Matchers.anyString)

Example 7 with SharedQueryState

use of io.prestosql.statestore.SharedQueryState in project hetu-core by openlookeng.

the class SqlQueryManager method killBlockingQuery.

/**
 * Kill query when cluster is in OOM state
 */
private void killBlockingQuery() {
    if (isMultiCoordinatorEnabled()) {
        return;
    }
    List<QueryExecution> localRunningQueries = queryTracker.getAllQueries().stream().filter(query -> query.getState() == RUNNING).collect(toImmutableList());
    Map<String, SharedQueryState> queries = StateCacheStore.get().getCachedStates(StateStoreConstants.OOM_QUERY_STATE_COLLECTION_NAME);
    if (queries != null) {
        for (SharedQueryState query : queries.values()) {
            for (QueryExecution localQuery : localRunningQueries) {
                if (query.getBasicQueryInfo().getQueryId().equals(localQuery.getQueryId())) {
                    memoryManager.killLocalQuery(localQuery);
                }
            }
        }
    }
}
Also used : Plan(io.prestosql.sql.planner.Plan) Duration(io.airlift.units.Duration) PreDestroy(javax.annotation.PreDestroy) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) Locale(java.util.Locale) Map(java.util.Map) QueryMonitor(io.prestosql.event.QueryMonitor) StateCacheStore(io.prestosql.statestore.StateCacheStore) ClusterMemoryManager(io.prestosql.memory.ClusterMemoryManager) PrestoException(io.prestosql.spi.PrestoException) SharedQueryState(io.prestosql.statestore.SharedQueryState) StateMap(io.prestosql.spi.statestore.StateMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ErrorType(io.prestosql.spi.ErrorType) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) ExceededCpuLimitException(io.prestosql.ExceededCpuLimitException) Threads.threadsNamed(io.airlift.concurrent.Threads.threadsNamed) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) SessionPropertyManager(io.prestosql.metadata.SessionPropertyManager) PostConstruct(javax.annotation.PostConstruct) GENERIC_INTERNAL_ERROR(io.prestosql.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) SystemSessionProperties.getQueryMaxCpuTime(io.prestosql.SystemSessionProperties.getQueryMaxCpuTime) QueryEditorUIModule(io.prestosql.queryeditorui.QueryEditorUIModule) Nested(org.weakref.jmx.Nested) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) StateChangeListener(io.prestosql.execution.StateMachine.StateChangeListener) Logger(io.airlift.log.Logger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ThreadPoolExecutorMBean(io.airlift.concurrent.ThreadPoolExecutorMBean) EmbedVersion(io.prestosql.version.EmbedVersion) Flatten(org.weakref.jmx.Flatten) QUERY_EXPIRE(io.prestosql.spi.StandardErrorCode.QUERY_EXPIRE) Supplier(java.util.function.Supplier) Inject(javax.inject.Inject) Managed(org.weakref.jmx.Managed) BasicQueryInfo(io.prestosql.server.BasicQueryInfo) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RUNNING(io.prestosql.execution.QueryState.RUNNING) QueryId(io.prestosql.spi.QueryId) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) QuerySnapshotManager(io.prestosql.snapshot.QuerySnapshotManager) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StateUtils.isMultiCoordinatorEnabled(io.prestosql.utils.StateUtils.isMultiCoordinatorEnabled) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Ordering(com.google.common.collect.Ordering) QueryOutputInfo(io.prestosql.execution.QueryExecution.QueryOutputInfo) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) StateStoreProvider(io.prestosql.statestore.StateStoreProvider) Futures.immediateFailedFuture(com.google.common.util.concurrent.Futures.immediateFailedFuture) StateStoreConstants(io.prestosql.statestore.StateStoreConstants) SharedQueryState(io.prestosql.statestore.SharedQueryState)

Example 8 with SharedQueryState

use of io.prestosql.statestore.SharedQueryState in project hetu-core by openlookeng.

the class DistributedResourceGroup method internalStartNext.

/**
 * Check if there is any queued query in any eligible sub-group that can be started
 *
 * @return if any query has been started in sub-groups
 */
protected boolean internalStartNext() {
    checkState(Thread.holdsLock(root), "Must hold lock to find next query");
    synchronized (root) {
        if (!canRunMore()) {
            return false;
        }
        // Only start the query if it exists locally
        Optional<SharedResourceGroupState> resourceGroupState = getSharedResourceGroupState();
        PriorityQueue<SharedQueryState> globalQueuedQueries = resourceGroupState.isPresent() ? resourceGroupState.get().getQueuedQueries() : new PriorityQueue<>();
        if (!globalQueuedQueries.isEmpty() && !queuedQueries.isEmpty()) {
            // Get queued query with longest queued time from state cache store.
            // Remove it if local queued queries contains it.
            SharedQueryState nextQuery = globalQueuedQueries.peek();
            for (ManagedQueryExecution localQuery : queuedQueries) {
                if (nextQuery.getBasicQueryInfo().getQueryId().equals(localQuery.getBasicQueryInfo().getQueryId())) {
                    Lock lock = stateStore.getLock(id.toString());
                    boolean locked = false;
                    try {
                        locked = lock.tryLock(MILLISECONDS_PER_SECOND, TimeUnit.MILLISECONDS);
                        if (locked) {
                            // Get the most recent cached state store status and check canRunMore again
                            // Avoid the race condition that state store is updated by other process
                            // Make sure queued query start is synchronized
                            DistributedResourceGroupUtils.mapCachedStates();
                            if (canRunMore()) {
                                queuedQueries.remove(localQuery);
                                startInBackground(localQuery);
                                return true;
                            }
                        }
                        return false;
                    } catch (InterruptedException | RuntimeException e) {
                        return false;
                    } finally {
                        if (locked) {
                            lock.unlock();
                        }
                    }
                }
            }
        }
        // Try to find least recently used eligible group
        DistributedResourceGroup chosenGroup = findLeastRecentlyExecutedSubgroup();
        if (chosenGroup == null) {
            return false;
        }
        // Try to start queued query in the group
        boolean started = chosenGroup.internalStartNext();
        long currentTime = System.currentTimeMillis();
        if (lastStartMillis != 0) {
            timeBetweenStartsSec.update(Math.max(0, (currentTime - lastStartMillis) / MILLISECONDS_PER_SECOND));
        }
        lastStartMillis = currentTime;
        return started;
    }
}
Also used : ManagedQueryExecution(io.prestosql.execution.ManagedQueryExecution) Lock(java.util.concurrent.locks.Lock) SharedQueryState(io.prestosql.statestore.SharedQueryState) SharedResourceGroupState(io.prestosql.statestore.SharedResourceGroupState)

Example 9 with SharedQueryState

use of io.prestosql.statestore.SharedQueryState in project hetu-core by openlookeng.

the class TestDistributedResourceGroup method updateStateCacheStore.

// update cached queryStates for a set of queries
private static void updateStateCacheStore(Set<MockManagedQueryExecution> queries) {
    Map<String, SharedQueryState> queryStates = new HashMap<>();
    for (Iterator<MockManagedQueryExecution> iterator = queries.iterator(); iterator.hasNext(); ) {
        MockManagedQueryExecution query = iterator.next();
        queryStates.put(query.toString(), getSharedQueryState(query));
    }
    StateCacheStore.get().setCachedStates(StateStoreConstants.QUERY_STATE_COLLECTION_NAME, queryStates);
    DistributedResourceGroupUtils.mapCachedStates();
}
Also used : SharedQueryState(io.prestosql.statestore.SharedQueryState) HashMap(java.util.HashMap) MockManagedQueryExecution(io.prestosql.execution.MockManagedQueryExecution) Matchers.anyString(org.mockito.Matchers.anyString)

Example 10 with SharedQueryState

use of io.prestosql.statestore.SharedQueryState in project hetu-core by openlookeng.

the class TestDistributedResourceGroupUtils method testMapCachedStates.

@Test
public void testMapCachedStates() {
    synchronized (lock) {
        ResourceGroupId root = new ResourceGroupId("root");
        // set up queryStates
        MockManagedQueryExecution query1 = new MockManagedQueryExecution(0);
        MockManagedQueryExecution query2 = new MockManagedQueryExecution(0);
        MockManagedQueryExecution query3 = new MockManagedQueryExecution(0);
        query1.setResourceGroupId(root);
        query2.setResourceGroupId(root);
        query3.setResourceGroupId(root);
        // query1 running, query2 queued, query3 completed
        query1.startWaitingForResources();
        query3.complete();
        SharedQueryState queryState1 = getSharedQueryState(query1);
        SharedQueryState queryState2 = getSharedQueryState(query2);
        SharedQueryState queryState3 = getSharedQueryState(query3);
        Map<String, SharedQueryState> queryStates = ImmutableMap.of(query1.toString(), queryState1, query2.toString(), queryState2, query3.toString(), queryState3);
        StateCacheStore.get().setCachedStates(StateStoreConstants.QUERY_STATE_COLLECTION_NAME, queryStates);
        // set up cpu usage states
        Map<String, Long> cpuUsageStates = ImmutableMap.of(root.toString(), 1000L);
        StateCacheStore.get().setCachedStates(StateStoreConstants.CPU_USAGE_STATE_COLLECTION_NAME, cpuUsageStates);
        // map QueryStates and cpuUsageStates to ResourceGroups
        DistributedResourceGroupUtils.mapCachedStates();
        Map resourceGroupStates = StateCacheStore.get().getCachedStates(StateStoreConstants.RESOURCE_GROUP_STATE_COLLECTION_NAME);
        // check ResourceGroupState
        assertTrue(resourceGroupStates.containsKey(root));
        SharedResourceGroupState resourceGroupState = (SharedResourceGroupState) resourceGroupStates.get(root);
        assertEquals(resourceGroupState.getRunningQueries().size(), 1);
        assertTrue(resourceGroupState.getRunningQueries().contains(queryState1));
        assertEquals(resourceGroupState.getQueuedQueries().size(), 1);
        assertEquals(resourceGroupState.getQueuedQueries().peek(), queryState2);
        assertEquals(resourceGroupState.getFinishedQueries().size(), 1);
        assertEquals(resourceGroupState.getFinishedQueries().peek(), queryState3);
        StateCacheStore.get().resetCachedStates();
    }
}
Also used : SharedQueryState(io.prestosql.statestore.SharedQueryState) ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) MockManagedQueryExecution(io.prestosql.execution.MockManagedQueryExecution) SharedResourceGroupState(io.prestosql.statestore.SharedResourceGroupState) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

SharedQueryState (io.prestosql.statestore.SharedQueryState)12 SharedResourceGroupState (io.prestosql.statestore.SharedResourceGroupState)8 Map (java.util.Map)7 ResourceGroupId (io.prestosql.spi.resourcegroups.ResourceGroupId)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Logger (io.airlift.log.Logger)5 Duration (io.airlift.units.Duration)5 ManagedQueryExecution (io.prestosql.execution.ManagedQueryExecution)5 PrestoException (io.prestosql.spi.PrestoException)5 StateCacheStore (io.prestosql.statestore.StateCacheStore)5 StateStoreConstants (io.prestosql.statestore.StateStoreConstants)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 Set (java.util.Set)4 TimeUnit (java.util.concurrent.TimeUnit)4 Lock (java.util.concurrent.locks.Lock)4 Collectors (java.util.stream.Collectors)4 ThreadSafe (javax.annotation.concurrent.ThreadSafe)4 Managed (org.weakref.jmx.Managed)4