Search in sources :

Example 1 with StateCollection

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

the class StateFetcher method fetchRunningQueryStates.

/**
 * Fetch states only related to running queries from state store to cache store
 * This is used when not all the states are needed for better states fetching performance
 *
 * @throws IOException exception when failed to deserialize states
 */
public void fetchRunningQueryStates(StateStore stateStore) throws IOException {
    synchronized (this) {
        long start = System.currentTimeMillis();
        LOG.debug("fetchStates starts at current time milliseconds: %s, at format HH:mm:ss:SSS:%s", start, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(start)));
        StateCollection cpuUsageCollection = stateStore.getStateCollection(CPU_USAGE_STATE_COLLECTION_NAME);
        StateCollection queryStateCollection = stateStore.getStateCollection(QUERY_STATE_COLLECTION_NAME);
        StateCacheStore.get().setCachedStates(CPU_USAGE_STATE_COLLECTION_NAME, ((StateMap) cpuUsageCollection).getAll());
        Map<String, String> states = ((StateMap<String, String>) queryStateCollection).getAll();
        StateCacheStore.get().setCachedStates(QUERY_STATE_COLLECTION_NAME, deserializeFetchedStates(states));
        long end = System.currentTimeMillis();
        LOG.debug("fetchStates ends at current time milliseconds: %s, at format HH:mm:ss:SSS:%s, total time use: %s", end, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(end)), end - start);
    }
}
Also used : StateCollection(io.prestosql.spi.statestore.StateCollection) StateMap(io.prestosql.spi.statestore.StateMap) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with StateCollection

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

the class StateFetcher method fetchAllStates.

/**
 * Fetch all states from state store to cache store
 *
 * @throws IOException exception when failed to deserialize states
 */
public void fetchAllStates() throws IOException {
    synchronized (this) {
        // State store hasn't been loaded yet
        if (stateStoreProvider.getStateStore() == null) {
            return;
        }
        long start = System.currentTimeMillis();
        LOG.debug("fetchStates starts at current time milliseconds: %s, at format HH:mm:ss:SSS:%s", start, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(start)));
        for (String stateCollectionName : stateCollections) {
            StateCollection stateCollection = stateStoreProvider.getStateStore().getStateCollection(stateCollectionName);
            if (stateCollection == null) {
                continue;
            }
            if (stateCollectionName.equals(CPU_USAGE_STATE_COLLECTION_NAME)) {
                StateCacheStore.get().setCachedStates(stateCollectionName, ((StateMap) stateCollection).getAll());
                continue;
            }
            if (stateCollection.getType() == StateCollection.Type.MAP) {
                Map<String, String> states = ((StateMap<String, String>) stateCollection).getAll();
                StateCacheStore.get().setCachedStates(stateCollectionName, deserializeFetchedStates(states));
            } else {
                LOG.warn("Unsupported state collection type: %s", stateCollection.getType());
            }
        }
        long end = System.currentTimeMillis();
        LOG.debug("fetchStates ends at current time milliseconds: %s, at format HH:mm:ss:SSS:%s, total time use: %s", end, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(end)), end - start);
    }
}
Also used : StateCollection(io.prestosql.spi.statestore.StateCollection) StateMap(io.prestosql.spi.statestore.StateMap) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 3 with StateCollection

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

the class StateFetcher method handleExpiredQueryState.

private void handleExpiredQueryState(SharedQueryState state) {
    // State store hasn't been loaded yet
    final StateStore stateStore = stateStoreProvider.getStateStore();
    if (stateStore == null) {
        return;
    }
    Lock lock = null;
    boolean locked = false;
    try {
        lock = stateStore.getLock(HANDLE_EXPIRED_QUERY_LOCK_NAME);
        locked = lock.tryLock(DEFAULT_ACQUIRED_LOCK_TIME_MS, TimeUnit.MILLISECONDS);
        if (locked) {
            LOG.debug(String.format("EXPIRED!!! REMOVING... Id: %s, state: %s, uri: %s, query: %s", state.getBasicQueryInfo().getQueryId().getId(), state.getBasicQueryInfo().getState().toString(), state.getBasicQueryInfo().getSelf().toString(), state.getBasicQueryInfo().getQuery()));
            // remove expired query from oom
            StateCollection stateCollection = stateStore.getStateCollection(OOM_QUERY_STATE_COLLECTION_NAME);
            removeState(stateCollection, Optional.of(state.getBasicQueryInfo().getQueryId()), LOG);
            // update query to failed in QUERY_STATE_COLLECTION_NAME if exists
            stateCollection = stateStore.getStateCollection(QUERY_STATE_COLLECTION_NAME);
            StateCollection finishStateCollection = stateStore.getStateCollection(FINISHED_QUERY_STATE_COLLECTION_NAME);
            if (stateCollection != null && stateCollection.getType().equals(StateCollection.Type.MAP)) {
                String queryState = ((StateMap<String, String>) stateCollection).get(state.getBasicQueryInfo().getQueryId().getId());
                if (queryState != null) {
                    BasicQueryInfo oldQueryInfo = state.getBasicQueryInfo();
                    SharedQueryState newState = createExpiredState(oldQueryInfo, state);
                    String stateJson = MAPPER.writeValueAsString(newState);
                    ((StateMap) finishStateCollection).put(newState.getBasicQueryInfo().getQueryId().getId(), stateJson);
                    removeState(stateCollection, Optional.of(state.getBasicQueryInfo().getQueryId()), LOG);
                }
            }
        }
    } catch (Exception e) {
        LOG.error("Error handleExpiredQueryState: " + e.getMessage());
    } finally {
        if (locked) {
            lock.unlock();
        }
    }
}
Also used : StateCollection(io.prestosql.spi.statestore.StateCollection) StateMap(io.prestosql.spi.statestore.StateMap) BasicQueryInfo(io.prestosql.server.BasicQueryInfo) StateStore(io.prestosql.spi.statestore.StateStore) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock)

Example 4 with StateCollection

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

the class StateUpdater method removeFromStateCollection.

private void removeFromStateCollection(StateStore stateStore, String stateCollectionName, ManagedQueryExecution query) {
    unregisterQuery(stateCollectionName, query);
    StateCollection stateCollection = stateStore.getStateCollection(stateCollectionName);
    removeState(stateCollection, Optional.of(query.getBasicQueryInfo().getQueryId()), LOG);
}
Also used : StateCollection(io.prestosql.spi.statestore.StateCollection)

Example 5 with StateCollection

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

the class StateUpdater method updateStates.

/**
 * Update local queued query states to state store
 *
 * @throws JsonProcessingException exception when fail to serialize states to json
 */
public void updateStates() throws JsonProcessingException {
    // State store hasn't been loaded yet
    final StateStore stateStore = stateStoreProvider.getStateStore();
    if (stateStore == null) {
        return;
    }
    long start = System.currentTimeMillis();
    LOG.debug("UpdateStates starts at current time milliseconds: %s, at format HH:mm:ss:SSS:%s", start, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(start)));
    StateCollection finishedQueries = stateStore.getStateCollection(FINISHED_QUERY_STATE_COLLECTION_NAME);
    StateCollection queries = stateStore.getStateCollection(QUERY_STATE_COLLECTION_NAME);
    List<DispatchQuery> queriesToUnregister = new LinkedList<>();
    synchronized (registeredQueries) {
        for (DispatchQuery query : registeredQueries.get(QUERY_STATE_COLLECTION_NAME)) {
            SharedQueryState state = SharedQueryState.create(query);
            String stateJson = MAPPER.writeValueAsString(state);
            if (state.getBasicQueryInfo().getState() == QueryState.FINISHED || state.getBasicQueryInfo().getState() == QueryState.FAILED) {
                // No need to update states for finished queries
                // also move finished queries to finished-query state collection
                queriesToUnregister.add(query);
                ((StateMap) finishedQueries).put(state.getBasicQueryInfo().getQueryId().getId(), stateJson);
                continue;
            }
            ((StateMap) queries).put(state.getBasicQueryInfo().getQueryId().getId(), stateJson);
        }
    }
    for (DispatchQuery query : queriesToUnregister) {
        removeFromStateCollection(stateStore, QUERY_STATE_COLLECTION_NAME, query);
    }
    long end = System.currentTimeMillis();
    LOG.debug("updateStates ends at current time milliseconds: %s, at format HH:mm:ss:SSS:%s, total time use: %s", end, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(end)), end - start);
}
Also used : DispatchQuery(io.prestosql.dispatcher.DispatchQuery) StateCollection(io.prestosql.spi.statestore.StateCollection) StateMap(io.prestosql.spi.statestore.StateMap) StateStore(io.prestosql.spi.statestore.StateStore) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) LinkedList(java.util.LinkedList)

Aggregations

StateCollection (io.prestosql.spi.statestore.StateCollection)16 StateMap (io.prestosql.spi.statestore.StateMap)7 StateStore (io.prestosql.spi.statestore.StateStore)6 Test (org.testng.annotations.Test)6 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 FileBasedSeedStoreFactory (io.hetu.core.seedstore.filebased.FileBasedSeedStoreFactory)2 SeedStoreFactory (io.prestosql.spi.seedstore.SeedStoreFactory)2 HashMap (java.util.HashMap)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 FileBasedSeed (io.hetu.core.seedstore.filebased.FileBasedSeed)1 DispatchQuery (io.prestosql.dispatcher.DispatchQuery)1 BasicQueryInfo (io.prestosql.server.BasicQueryInfo)1 PrestoException (io.prestosql.spi.PrestoException)1 Seed (io.prestosql.spi.seedstore.Seed)1 SeedStore (io.prestosql.spi.seedstore.SeedStore)1 StateStoreBootstrapper (io.prestosql.spi.statestore.StateStoreBootstrapper)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Lock (java.util.concurrent.locks.Lock)1