Search in sources :

Example 6 with StateStore

use of io.prestosql.spi.statestore.StateStore 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)

Example 7 with StateStore

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

the class SplitCacheStateUpdater method updateStateStore.

public void updateStateStore() {
    StateStore stateStore = provider.getStateStore();
    if (stateStore == null) {
        log.debug("State store not yet initialized.");
        return;
    }
    if (status.get() == InitializationStatus.INITIALIZING) {
        log.debug("Split cache map not yet initialized.");
        return;
    }
    // create state store collection
    if (stateStore.getStateCollection(StateStoreConstants.SPLIT_CACHE_METADATA_NAME) == null) {
        stateStore.createStateMap(StateStoreConstants.SPLIT_CACHE_METADATA_NAME, new SplitCacheStateStoreChangesListener(SplitCacheMap.getInstance(), mapper));
    }
    StateMap<String, String> stateMap = (StateMap<String, String>) stateStore.getStateCollection(StateStoreConstants.SPLIT_CACHE_METADATA_NAME);
    splitCacheMap.getAndClearDroppedCaches().forEach(stateMap::remove);
    splitCacheMap.tableCacheInfoMap().forEach((fqTableName, localCacheInfo) -> {
        try {
            // Async update works only when new cache predicates added and splits are scheduled or updated
            // It does not perform merge also in case if both local info and state store are updated.
            // The logic is very simple. Only update state store if local copy is more recent than the one in state store.
            TableCacheInfo stateStoreCacheInfo = stateMap.containsKey(fqTableName) ? mapper.readerFor(TableCacheInfo.class).readValue(stateMap.get(fqTableName)) : null;
            if (stateStoreCacheInfo == null || localCacheInfo.getLastUpdated().isAfter(stateStoreCacheInfo.getLastUpdated())) {
                log.info("Updating state store split cache map for table %s.", fqTableName);
                String json = mapper.writeValueAsString(localCacheInfo);
                stateMap.put(fqTableName, json);
            } else if (localCacheInfo.getLastUpdated().isBefore(stateStoreCacheInfo.getLastUpdated())) {
                log.debug("Not updating state store split cache map for table %s. Local copy is outdated. State store split cache map is more recent. Local split cache map should be updated.", fqTableName);
            }
        } catch (IOException e) {
            log.error(e, "Unable to update state store split cache map.");
        }
    });
}
Also used : StateMap(io.prestosql.spi.statestore.StateMap) StateStore(io.prestosql.spi.statestore.StateStore) IOException(java.io.IOException)

Example 8 with StateStore

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

the class SqlQueryExecution method handleCrossRegionDynamicFilter.

private void handleCrossRegionDynamicFilter(PlanRoot plan) {
    if (!isCrossRegionDynamicFilterEnabled(getSession()) || plan == null) {
        return;
    }
    StateStore stateStore = stateStoreProvider.getStateStore();
    if (stateStore == null) {
        return;
    }
    String queryId = getSession().getQueryId().getId();
    log.debug("queryId=%s begin to find columnToColumnMapping.", queryId);
    PlanNode outputNode = plan.getRoot().getFragment().getRoot();
    Map<String, Set<String>> columnToSymbolMapping = new HashMap<>();
    if (outputNode != null && outputNode instanceof OutputNode) {
        List<String> queryColumnNames = ((OutputNode) outputNode).getColumnNames();
        List<Symbol> outputSymbols = outputNode.getOutputSymbols();
        Map<String, Set<String>> tmpMapping = new HashMap<>(outputSymbols.size());
        for (Symbol symbol : outputNode.getOutputSymbols()) {
            Set<String> sets = new HashSet();
            sets.add(symbol.getName());
            tmpMapping.put(symbol.getName(), sets);
        }
        for (PlanFragment fragment : plan.getRoot().getAllFragments()) {
            if ("0".equals(fragment.getId().toString())) {
                continue;
            }
            PlanNode sourceNode = fragment.getRoot();
            findMappingFromPlan(tmpMapping, sourceNode);
        }
        for (int i = 0; i < outputSymbols.size(); i++) {
            columnToSymbolMapping.put(queryColumnNames.get(i), tmpMapping.get(outputSymbols.get(i).getName()));
        }
    }
    // save mapping into stateStore
    StateMap<String, Object> mappingStateMap = (StateMap<String, Object>) stateStore.getOrCreateStateCollection(CROSS_REGION_DYNAMIC_FILTERS, StateCollection.Type.MAP);
    mappingStateMap.put(queryId + QUERY_COLUMN_NAME_TO_SYMBOL_MAPPING, columnToSymbolMapping);
    log.debug("queryId=%s, add columnToSymbolMapping into hazelcast success.", queryId + QUERY_COLUMN_NAME_TO_SYMBOL_MAPPING);
}
Also used : OutputNode(io.prestosql.sql.planner.plan.OutputNode) Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Symbol(io.prestosql.spi.plan.Symbol) StateMap(io.prestosql.spi.statestore.StateMap) StateStore(io.prestosql.spi.statestore.StateStore) PlanFragment(io.prestosql.sql.planner.PlanFragment) PlanNode(io.prestosql.spi.plan.PlanNode) HashSet(java.util.HashSet)

Example 9 with StateStore

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

the class TestHazelcastClusterLifecycleListener method createStateStoreClient.

private StateStore createStateStoreClient(Set<Seed> seeds) {
    Map<String, String> config = new HashMap<>(0);
    config.put("hazelcast.discovery.mode", "tcp-ip");
    config.put("state-store.cluster", TEST_CLUSTER_NAME);
    config.put(DISCOVERY_PORT_CONFIG_NAME, PORT1);
    MockSeedStore mockSeedStore = new MockSeedStore();
    mockSeedStore.add(seeds);
    StateStoreFactory factory = new HazelcastStateStoreFactory();
    StateStore stateStore = factory.create("testHazelcast", mockSeedStore, config);
    stateStore.registerClusterFailureHandler(event -> isClusterShutdownEventCaptured = true);
    return stateStore;
}
Also used : HashMap(java.util.HashMap) StateStore(io.prestosql.spi.statestore.StateStore) StateStoreFactory(io.prestosql.spi.statestore.StateStoreFactory)

Example 10 with StateStore

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

the class TestHazelcastClusterLifecycleListener method testClusterShutdown.

@Test
void testClusterShutdown() throws InterruptedException {
    final long sleep = 30000L;
    isClusterShutdownEventCaptured = false;
    final StateStore member1 = createStateStoreCluster(PORT1);
    final StateStore member2 = createStateStoreCluster(PORT2);
    HashSet<Seed> seeds = new HashSet<>(0);
    seeds.add(new MockSeed(MEMBER_1_ADDRESS));
    seeds.add(new MockSeed(MEMBER_2_ADDRESS));
    StateStore client = createStateStoreClient(seeds);
    assertFalse(isClusterShutdownEventCaptured);
    ((HazelcastStateStore) member1).shutdown();
    assertFalse(isClusterShutdownEventCaptured);
    ((HazelcastStateStore) member2).shutdown();
    Thread.sleep(sleep);
    assertTrue(isClusterShutdownEventCaptured);
}
Also used : Seed(io.prestosql.spi.seedstore.Seed) StateStore(io.prestosql.spi.statestore.StateStore) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

StateStore (io.prestosql.spi.statestore.StateStore)40 StateMap (io.prestosql.spi.statestore.StateMap)16 HashMap (java.util.HashMap)16 Test (org.testng.annotations.Test)14 StateStoreProvider (io.prestosql.statestore.StateStoreProvider)11 ImmutableMap (com.google.common.collect.ImmutableMap)8 StateCollection (io.prestosql.spi.statestore.StateCollection)8 IOException (java.io.IOException)8 HashSet (java.util.HashSet)8 PrestoException (io.prestosql.spi.PrestoException)7 Map (java.util.Map)7 Set (java.util.Set)6 Symbol (io.prestosql.spi.plan.Symbol)5 Optional (java.util.Optional)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Duration (io.airlift.units.Duration)4 DynamicFilter (io.prestosql.spi.dynamicfilter.DynamicFilter)4 StateSet (io.prestosql.spi.statestore.StateSet)4 StateStoreBootstrapper (io.prestosql.spi.statestore.StateStoreBootstrapper)4 BeforeTest (org.testng.annotations.BeforeTest)4