je-ik commented on code in PR #33772: URL: https://github.com/apache/beam/pull/33772#discussion_r1956622563
########## runners/spark/src/main/java/org/apache/beam/runners/spark/DependentTransformsVisitor.java: ########## @@ -0,0 +1,52 @@ +/* + * 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.beam.runners.spark; + +import java.util.Map; +import org.apache.beam.runners.spark.translation.EvaluationContext; +import org.apache.beam.runners.spark.translation.SparkPipelineTranslator; +import org.apache.beam.sdk.runners.TransformHierarchy; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.TupleTag; + +/** + * Traverses the pipeline to populate information on how many {@link + * org.apache.beam.sdk.transforms.PTransform}s do consume / depends on each {@link PCollection} in + * the pipeline. + */ +class DependentTransformsVisitor extends SparkRunner.Evaluator { + + DependentTransformsVisitor( + SparkPipelineTranslator translator, EvaluationContext evaluationContext) { + super(translator, evaluationContext); + } + + @Override + public void doVisitTransform(TransformHierarchy.Node node) { + + Map<PCollection<?>, Integer> dependentTransforms = ctxt.getDependentTransforms(); + for (Map.Entry<TupleTag<?>, PCollection<?>> entry : node.getInputs().entrySet()) { + int dependants = dependentTransforms.getOrDefault(entry.getValue(), 0); + dependentTransforms.put(entry.getValue(), dependants + 1); + } + + for (PCollection<?> pOut : node.getOutputs().values()) { + dependentTransforms.computeIfAbsent(pOut, k -> 0); + } + } Review Comment: This is hard to follow. The `dependentTransforms` seems to be local variable. There probably should be get/update calls here. ########## runners/spark/src/main/java/org/apache/beam/runners/spark/translation/TransformTranslator.java: ########## @@ -498,6 +508,37 @@ private boolean hasMultipleOutputs(Map<TupleTag<?>, PCollection<?>> outputs) { return outputs.size() > 1; } + /** + * Filter out obsolete, unused output tags except for {@code mainTag}. + * + * <p>This can help to avoid unnecessary caching in case of multiple outputs if only {@code + * mainTag} is consumed. + */ + private Map<TupleTag<?>, PCollection<?>> skipObsoleteOutputs( Review Comment: What is "obsolete" output in this case? ########## runners/spark/src/main/java/org/apache/beam/runners/spark/translation/EvaluationContext.java: ########## @@ -307,6 +308,26 @@ public <K, V> boolean isCandidateForGroupByKeyAndWindow(GroupByKey<K, V> transfo return groupByKeyCandidatesForMemoryOptimizedTranslation.containsKey(transform); } + /** + * Get the map of dependent transforms hold by the evaluation context. + * + * @return The current {@link Map} of dependent transforms. + */ + public Map<PCollection<?>, Integer> getDependentTransforms() { + return this.dependentTransforms; + } + + /** + * Get if given {@link PCollection} is a leaf or not. {@link PCollection} is a leaf when there is Review Comment: ```suggestion * Check if given {@link PCollection} is a leaf or not. {@link PCollection} is a leaf when there is ``` ########## runners/spark/src/main/java/org/apache/beam/runners/spark/translation/EvaluationContext.java: ########## @@ -307,6 +308,26 @@ public <K, V> boolean isCandidateForGroupByKeyAndWindow(GroupByKey<K, V> transfo return groupByKeyCandidatesForMemoryOptimizedTranslation.containsKey(transform); } + /** + * Get the map of dependent transforms hold by the evaluation context. + * + * @return The current {@link Map} of dependent transforms. + */ + public Map<PCollection<?>, Integer> getDependentTransforms() { + return this.dependentTransforms; Review Comment: A getter should ideally return unmodifiable map here. -- 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]
