wirybeaver commented on code in PR #15743: URL: https://github.com/apache/pinot/pull/15743#discussion_r2085627131
########## pinot-query-planner/src/main/java/org/apache/pinot/query/planner/physical/v2/opt/rules/LiteModeWorkerAssignmentRule.java: ########## @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.query.planner.physical.v2.opt.rules; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.apache.calcite.rel.RelDistribution; +import org.apache.pinot.calcite.rel.traits.PinotExecStrategyTrait; +import org.apache.pinot.query.context.PhysicalPlannerContext; +import org.apache.pinot.query.planner.physical.v2.ExchangeStrategy; +import org.apache.pinot.query.planner.physical.v2.PRelNode; +import org.apache.pinot.query.planner.physical.v2.PinotDataDistribution; +import org.apache.pinot.query.planner.physical.v2.nodes.PhysicalExchange; +import org.apache.pinot.query.planner.physical.v2.opt.PRelNodeTransformer; + + +/** + * Lite mode uses a single worker for all stages except the leaf stage. Since leaf stage assignment is done before this + * rule is called, we simply need to sample a random worker from the leaf stage, and assign it to all the non-leaf + * plan nodes. + */ +public class LiteModeWorkerAssignmentRule implements PRelNodeTransformer { + private static final Random RANDOM = new Random(); + private final PhysicalPlannerContext _context; + + public LiteModeWorkerAssignmentRule(PhysicalPlannerContext context) { + _context = context; + } + + @Override + public PRelNode execute(PRelNode currentNode) { + Set<String> workerSet = new HashSet<>(); + accumulateWorkers(currentNode, workerSet); + List<String> workers = List.of(sampleWorker(workerSet)); + PinotDataDistribution pdd = new PinotDataDistribution(RelDistribution.Type.SINGLETON, workers, workers.hashCode(), + null, null); + return addExchangeAndWorkers(currentNode, null, pdd); + } + + public PRelNode addExchangeAndWorkers(PRelNode currentNode, @Nullable PRelNode parent, PinotDataDistribution pdd) { + if (currentNode.isLeafStage()) { + if (parent == null) { + return currentNode; + } + return new PhysicalExchange(nodeId(), currentNode, pdd, Collections.emptyList(), + ExchangeStrategy.SINGLETON_EXCHANGE, null, PinotExecStrategyTrait.getDefaultExecStrategy()); + } + List<PRelNode> newInputs = new ArrayList<>(); + for (PRelNode input : currentNode.getPRelInputs()) { + newInputs.add(addExchangeAndWorkers(input, currentNode, pdd)); + } + return currentNode.with(newInputs, pdd); + } + + /** + * Stores workers assigned to the leaf stage nodes into the provided Set. Note that each worker has an integer prefix + * which denotes the "workerId". We remove that prefix before storing them in the set. + */ + @VisibleForTesting + static void accumulateWorkers(PRelNode currentNode, Set<String> workerSink) { + if (currentNode.isLeafStage()) { + workerSink.addAll(currentNode.getPinotDataDistributionOrThrow().getWorkers().stream() + .map(LiteModeWorkerAssignmentRule::stripIdPrefixFromWorker).collect(Collectors.toSet())); Review Comment: the workSink itself is a Set. The addAll itself would do dedup. So I think collect(Collectors.toList()) can be more efficient? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
