Search in sources :

Example 16 with StateStore

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

the class TestSplitCacheStateInitializer method testCreateStateCollectionIfNotExists.

@Test
public void testCreateStateCollectionIfNotExists() {
    StateStoreProvider provider = mock(StateStoreProvider.class);
    StateStore stateStore = mock(StateStore.class);
    when(stateStore.getName()).thenReturn("mock");
    when(stateStore.getStateCollection(StateStoreConstants.SPLIT_CACHE_METADATA_NAME)).thenReturn(null).thenReturn(new MockStateMap<>(StateStoreConstants.SPLIT_CACHE_METADATA_NAME, new HashMap<>()));
    // simulate behaviour wait until state store is ready
    when(provider.getStateStore()).thenReturn(stateStore);
    SplitCacheMap splitCacheMap = createNew();
    assertFalse(splitCacheMap.cacheExists(table1QN));
    assertFalse(splitCacheMap.cacheExists(table2QN));
    AtomicReference<SplitCacheStateInitializer.InitializationStatus> status = new AtomicReference<>(SplitCacheStateInitializer.InitializationStatus.INITIALIZING);
    SplitCacheStateInitializer initializer = new SplitCacheStateInitializer(provider, splitCacheMap, new Duration(300, TimeUnit.MILLISECONDS), new Duration(1, TimeUnit.SECONDS), objectMapper, status);
    try {
        initializer.start();
        // sleep for sometime till local split cache map is initialized
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        // ignore - nothing to do here
        }
    } finally {
        initializer.stop();
    }
    verify(provider, atLeastOnce()).getStateStore();
    verify(stateStore, times(1)).createStateMap(eq(StateStoreConstants.SPLIT_CACHE_METADATA_NAME), any(SplitCacheStateStoreChangesListener.class));
    assertEquals(status.get(), SplitCacheStateInitializer.InitializationStatus.COMPLETED);
}
Also used : HashMap(java.util.HashMap) StateStore(io.prestosql.spi.statestore.StateStore) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(io.airlift.units.Duration) StateStoreProvider(io.prestosql.statestore.StateStoreProvider) Test(org.testng.annotations.Test)

Example 17 with StateStore

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

the class DynamicFilterService method registerTasksHelper.

private void registerTasksHelper(PlanNode node, Symbol buildSymbol, Map<String, Symbol> dynamicFiltersMap, Set<TaskId> taskIds, Set<InternalNode> workers, StageStateMachine stateMachine) {
    final StateStore stateStore = stateStoreProvider.getStateStore();
    String queryId = stateMachine.getSession().getQueryId().toString();
    for (Map.Entry<String, Symbol> entry : dynamicFiltersMap.entrySet()) {
        Symbol buildSymbolToCheck = buildSymbol != null ? buildSymbol : node.getOutputSymbols().contains(entry.getValue()) ? entry.getValue() : null;
        if (buildSymbolToCheck != null && entry.getValue().getName().equals(buildSymbol.getName())) {
            String filterId = entry.getKey();
            stateStore.createStateCollection(createKey(DynamicFilterUtils.TASKSPREFIX, filterId, queryId), SET);
            stateStore.createStateCollection(createKey(DynamicFilterUtils.PARTIALPREFIX, filterId, queryId), SET);
            dynamicFilters.putIfAbsent(queryId, new ConcurrentHashMap<>());
            Map<String, DynamicFilterRegistryInfo> filters = dynamicFilters.get(queryId);
            if (node instanceof JoinNode) {
                filters.put(filterId, extractDynamicFilterRegistryInfo((JoinNode) node, stateMachine.getSession(), filterId));
            } else if (node instanceof SemiJoinNode) {
                filters.put(filterId, extractDynamicFilterRegistryInfo((SemiJoinNode) node, stateMachine.getSession()));
            }
            dynamicFiltersToTask.putIfAbsent(filterId + "-" + queryId, new CopyOnWriteArraySet<>());
            CopyOnWriteArraySet<TaskId> taskSet = dynamicFiltersToTask.get(filterId + "-" + queryId);
            taskSet.addAll(taskIds);
            log.debug("registerTasks source " + filterId + " filters:" + filters + ", workers: " + workers.stream().map(x -> x.getNodeIdentifier()).collect(Collectors.joining(",")) + ", taskIds: " + taskIds.stream().map(TaskId::toString).collect(Collectors.joining(",")));
        }
    }
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) DynamicFilter(io.prestosql.spi.dynamicfilter.DynamicFilter) Inject(com.google.inject.Inject) StateStore(io.prestosql.spi.statestore.StateStore) DynamicFilterUtils.findFilterNodeInStage(io.prestosql.utils.DynamicFilterUtils.findFilterNodeInStage) CallExpression(io.prestosql.spi.relation.CallExpression) PreDestroy(javax.annotation.PreDestroy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FilterNode(io.prestosql.spi.plan.FilterNode) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) SystemSessionProperties.getDynamicFilteringDataType(io.prestosql.SystemSessionProperties.getDynamicFilteringDataType) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) StateMap(io.prestosql.spi.statestore.StateMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DynamicFilters(io.prestosql.sql.DynamicFilters) PlanNode(io.prestosql.spi.plan.PlanNode) StateSet(io.prestosql.spi.statestore.StateSet) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Threads.threadsNamed(io.airlift.concurrent.Threads.threadsNamed) Collectors(java.util.stream.Collectors) DynamicFilterUtils.getDynamicFilterDataType(io.prestosql.utils.DynamicFilterUtils.getDynamicFilterDataType) Executors(java.util.concurrent.Executors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Type(io.prestosql.spi.dynamicfilter.DynamicFilter.Type) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) Entry(java.util.Map.Entry) GENERIC_INTERNAL_ERROR(io.prestosql.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) Optional(java.util.Optional) LOCAL(io.prestosql.spi.dynamicfilter.DynamicFilter.Type.LOCAL) TaskId(io.prestosql.execution.TaskId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(io.airlift.log.Logger) HASHSET(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType.HASHSET) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BloomFilter(io.prestosql.spi.util.BloomFilter) DataType(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) BloomFilterDynamicFilter(io.prestosql.spi.dynamicfilter.BloomFilterDynamicFilter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) QueryId(io.prestosql.spi.QueryId) JoinNode(io.prestosql.spi.plan.JoinNode) Symbol(io.prestosql.spi.plan.Symbol) StageStateMachine(io.prestosql.execution.StageStateMachine) StateCollection(io.prestosql.spi.statestore.StateCollection) InternalNode(io.prestosql.metadata.InternalNode) IOException(java.io.IOException) BLOOM_FILTER(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType.BLOOM_FILTER) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) TimeUnit(java.util.concurrent.TimeUnit) GLOBAL(io.prestosql.spi.dynamicfilter.DynamicFilter.Type.GLOBAL) DynamicFilterUtils(io.prestosql.utils.DynamicFilterUtils) DynamicFilterUtils.createKey(io.prestosql.utils.DynamicFilterUtils.createKey) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) StateStoreProvider(io.prestosql.statestore.StateStoreProvider) RowExpression(io.prestosql.spi.relation.RowExpression) MAP(io.prestosql.spi.statestore.StateCollection.Type.MAP) DynamicFilterFactory(io.prestosql.spi.dynamicfilter.DynamicFilterFactory) Collections(java.util.Collections) SET(io.prestosql.spi.statestore.StateCollection.Type.SET) TaskId(io.prestosql.execution.TaskId) Symbol(io.prestosql.spi.plan.Symbol) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) StateStore(io.prestosql.spi.statestore.StateStore) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) StateMap(io.prestosql.spi.statestore.StateMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 18 with StateStore

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

the class DynamicFilterService method mergeDynamicFilters.

/**
 * Global Dynamic Filter merging, periodically looks for dynamic filters that can be merged and merges them
 */
private void mergeDynamicFilters() {
    final StateStore stateStore = stateStoreProvider.getStateStore();
    for (Map.Entry<String, Map<String, DynamicFilterRegistryInfo>> queryToDynamicFiltersEntry : dynamicFilters.entrySet()) {
        final String queryId = queryToDynamicFiltersEntry.getKey();
        if (!cachedDynamicFilters.containsKey(queryId)) {
            cachedDynamicFilters.put(queryId, new ConcurrentHashMap<>());
        }
        Map<String, DynamicFilter> cachedDynamicFiltersForQuery = cachedDynamicFilters.get(queryId);
        StateMap mergedDynamicFilters = (StateMap) stateStore.getOrCreateStateCollection(DynamicFilterUtils.MERGED_DYNAMIC_FILTERS, MAP);
        for (Map.Entry<String, DynamicFilterRegistryInfo> columnToDynamicFilterEntry : queryToDynamicFiltersEntry.getValue().entrySet()) {
            if (columnToDynamicFilterEntry.getValue().isMerged()) {
                continue;
            }
            final String filterId = columnToDynamicFilterEntry.getKey();
            final Type filterType = columnToDynamicFilterEntry.getValue().getType();
            final DataType filterDataType = columnToDynamicFilterEntry.getValue().getDataType();
            final Optional<Predicate<List>> dfFilter = columnToDynamicFilterEntry.getValue().getFilter();
            final Symbol column = columnToDynamicFilterEntry.getValue().getSymbol();
            final String filterKey = createKey(DynamicFilterUtils.FILTERPREFIX, filterId, queryId);
            if (!hasMergeCondition(filterId, queryId)) {
                continue;
            }
            Collection<Object> results = ((StateSet) stateStore.getStateCollection(createKey(DynamicFilterUtils.PARTIALPREFIX, filterId, queryId))).getAll();
            try {
                DynamicFilter mergedFilter;
                if (filterDataType == BLOOM_FILTER) {
                    BloomFilter mergedBloomFilter = mergeBloomFilters(results);
                    if (mergedBloomFilter.expectedFpp() > DynamicFilterUtils.BLOOM_FILTER_EXPECTED_FPP) {
                        throw new PrestoException(GENERIC_INTERNAL_ERROR, "FPP too high: " + mergedBloomFilter.approximateElementCount());
                    }
                    mergedFilter = new BloomFilterDynamicFilter(filterKey, null, mergedBloomFilter, filterType);
                    if (filterType == GLOBAL) {
                        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                            mergedBloomFilter.writeTo(out);
                            byte[] filter = out.toByteArray();
                            mergedDynamicFilters.put(filterKey, filter);
                        }
                    }
                } else if (filterDataType == HASHSET) {
                    Set mergedSet = mergeHashSets(results);
                    mergedFilter = DynamicFilterFactory.create(filterKey, null, mergedSet, filterType, dfFilter, Optional.empty());
                    if (filterType == GLOBAL) {
                        mergedDynamicFilters.put(filterKey, mergedSet);
                    }
                } else {
                    throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unsupported filter data type: " + filterDataType);
                }
                log.debug("Merged successfully dynamic filter id: " + filterId + "-" + queryId + " type: " + filterDataType + ", column: " + column + ", item count: " + mergedFilter.getSize());
                cachedDynamicFiltersForQuery.put(filterId, mergedFilter);
            } catch (IOException | PrestoException e) {
                log.warn("Could not merge dynamic filter: " + e.getLocalizedMessage());
            } finally {
                // for each dynamic filter we only try to merge it once
                columnToDynamicFilterEntry.getValue().setMerged();
            }
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) StateSet(io.prestosql.spi.statestore.StateSet) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) Symbol(io.prestosql.spi.plan.Symbol) StateMap(io.prestosql.spi.statestore.StateMap) PrestoException(io.prestosql.spi.PrestoException) BloomFilter(io.prestosql.spi.util.BloomFilter) Predicate(java.util.function.Predicate) SystemSessionProperties.getDynamicFilteringDataType(io.prestosql.SystemSessionProperties.getDynamicFilteringDataType) DynamicFilterUtils.getDynamicFilterDataType(io.prestosql.utils.DynamicFilterUtils.getDynamicFilterDataType) DataType(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType) StateSet(io.prestosql.spi.statestore.StateSet) DynamicFilter(io.prestosql.spi.dynamicfilter.DynamicFilter) BloomFilterDynamicFilter(io.prestosql.spi.dynamicfilter.BloomFilterDynamicFilter) StateStore(io.prestosql.spi.statestore.StateStore) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SystemSessionProperties.getDynamicFilteringDataType(io.prestosql.SystemSessionProperties.getDynamicFilteringDataType) DynamicFilterUtils.getDynamicFilterDataType(io.prestosql.utils.DynamicFilterUtils.getDynamicFilterDataType) Type(io.prestosql.spi.dynamicfilter.DynamicFilter.Type) DataType(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType) BloomFilterDynamicFilter(io.prestosql.spi.dynamicfilter.BloomFilterDynamicFilter) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) StateMap(io.prestosql.spi.statestore.StateMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 19 with StateStore

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

the class DynamicFilterService method removeFinishedQuery.

private void removeFinishedQuery() {
    List<String> handledQuery = new ArrayList<>();
    StateStore stateStore = stateStoreProvider.getStateStore();
    StateMap mergedStateCollection = (StateMap) stateStore.getOrCreateStateCollection(DynamicFilterUtils.MERGED_DYNAMIC_FILTERS, MAP);
    // Clear registered dynamic filter tasks
    synchronized (finishedQuery) {
        for (String queryId : finishedQuery) {
            Map<String, DynamicFilterRegistryInfo> filters = dynamicFilters.get(queryId);
            if (filters != null) {
                for (Entry<String, DynamicFilterRegistryInfo> entry : filters.entrySet()) {
                    String filterId = entry.getKey();
                    clearPartialResults(filterId, queryId);
                    if (entry.getValue().isMerged()) {
                        String filterKey = createKey(DynamicFilterUtils.FILTERPREFIX, filterId, queryId);
                        mergedStateCollection.remove(filterKey);
                    }
                }
            }
            List<String> collectionKeys = stateStore.getStateCollections().keySet().stream().filter(key -> key.contains(queryId)).collect(Collectors.toList());
            for (String key : collectionKeys) {
                clearStatesInStateStore(stateStore, key);
            }
            dynamicFilters.remove(queryId);
            cachedDynamicFilters.remove(queryId);
            handledQuery.add(queryId);
        }
        finishedQuery.removeAll(handledQuery);
    }
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) DynamicFilter(io.prestosql.spi.dynamicfilter.DynamicFilter) Inject(com.google.inject.Inject) StateStore(io.prestosql.spi.statestore.StateStore) DynamicFilterUtils.findFilterNodeInStage(io.prestosql.utils.DynamicFilterUtils.findFilterNodeInStage) CallExpression(io.prestosql.spi.relation.CallExpression) PreDestroy(javax.annotation.PreDestroy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FilterNode(io.prestosql.spi.plan.FilterNode) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) SystemSessionProperties.getDynamicFilteringDataType(io.prestosql.SystemSessionProperties.getDynamicFilteringDataType) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) StateMap(io.prestosql.spi.statestore.StateMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DynamicFilters(io.prestosql.sql.DynamicFilters) PlanNode(io.prestosql.spi.plan.PlanNode) StateSet(io.prestosql.spi.statestore.StateSet) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Threads.threadsNamed(io.airlift.concurrent.Threads.threadsNamed) Collectors(java.util.stream.Collectors) DynamicFilterUtils.getDynamicFilterDataType(io.prestosql.utils.DynamicFilterUtils.getDynamicFilterDataType) Executors(java.util.concurrent.Executors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Type(io.prestosql.spi.dynamicfilter.DynamicFilter.Type) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) Entry(java.util.Map.Entry) GENERIC_INTERNAL_ERROR(io.prestosql.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) Optional(java.util.Optional) LOCAL(io.prestosql.spi.dynamicfilter.DynamicFilter.Type.LOCAL) TaskId(io.prestosql.execution.TaskId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(io.airlift.log.Logger) HASHSET(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType.HASHSET) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BloomFilter(io.prestosql.spi.util.BloomFilter) DataType(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) BloomFilterDynamicFilter(io.prestosql.spi.dynamicfilter.BloomFilterDynamicFilter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) QueryId(io.prestosql.spi.QueryId) JoinNode(io.prestosql.spi.plan.JoinNode) Symbol(io.prestosql.spi.plan.Symbol) StageStateMachine(io.prestosql.execution.StageStateMachine) StateCollection(io.prestosql.spi.statestore.StateCollection) InternalNode(io.prestosql.metadata.InternalNode) IOException(java.io.IOException) BLOOM_FILTER(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType.BLOOM_FILTER) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) TimeUnit(java.util.concurrent.TimeUnit) GLOBAL(io.prestosql.spi.dynamicfilter.DynamicFilter.Type.GLOBAL) DynamicFilterUtils(io.prestosql.utils.DynamicFilterUtils) DynamicFilterUtils.createKey(io.prestosql.utils.DynamicFilterUtils.createKey) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) StateStoreProvider(io.prestosql.statestore.StateStoreProvider) RowExpression(io.prestosql.spi.relation.RowExpression) MAP(io.prestosql.spi.statestore.StateCollection.Type.MAP) DynamicFilterFactory(io.prestosql.spi.dynamicfilter.DynamicFilterFactory) Collections(java.util.Collections) SET(io.prestosql.spi.statestore.StateCollection.Type.SET) StateMap(io.prestosql.spi.statestore.StateMap) ArrayList(java.util.ArrayList) StateStore(io.prestosql.spi.statestore.StateStore)

Example 20 with StateStore

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

the class DynamicFilterService method clearPartialResults.

/**
 * Cleaning local cache and state store cache
 *
 * @param filterId, part of the id for dynamic filter that will be cleaned
 * @param queryId, query id for part of the id of dynamic filter that will be cleaned
 */
private void clearPartialResults(String filterId, String queryId) {
    StateStore stateStore = stateStoreProvider.getStateStore();
    if (stateStore != null) {
        clearStatesInStateStore(stateStore, createKey(DynamicFilterUtils.PARTIALPREFIX, filterId, queryId));
        clearStatesInStateStore(stateStore, createKey(DynamicFilterUtils.TASKSPREFIX, filterId, queryId));
    }
    dynamicFiltersToTask.remove(filterId + "-" + queryId);
}
Also used : StateStore(io.prestosql.spi.statestore.StateStore)

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