Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.structuredstreaming.translation;

import org.apache.beam.runners.spark.structuredstreaming.translation.batch.PipelineTranslatorBatch;
import org.apache.beam.sdk.annotations.Internal;

/**
* This class shadows the shared base file of the same name. The Spark 4 module compiles the
* override tree with later wins, so this copy replaces the base one that throws for streaming.
*/
@Internal
public final class PipelineTranslatorFactory {
private PipelineTranslatorFactory() {}

/** Creates a {@link PipelineTranslator} for the given execution mode. */
public static PipelineTranslator create(boolean streaming) {
return streaming ? new PipelineTranslatorStreaming() : new PipelineTranslatorBatch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.structuredstreaming.translation;

import java.util.Collection;
import org.apache.beam.runners.spark.SparkCommonPipelineOptions;
import org.apache.beam.runners.spark.structuredstreaming.translation.batch.PipelineTranslatorBatch;
import org.apache.beam.runners.spark.structuredstreaming.translation.streaming.ReadUnboundedTranslator;
import org.apache.beam.sdk.annotations.Internal;
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.GroupByKey;
import org.apache.beam.sdk.transforms.Impulse;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.reflect.DoFnSignature;
import org.apache.beam.sdk.transforms.reflect.DoFnSignatures;
import org.apache.beam.sdk.util.construction.SplittableParDo;
import org.apache.beam.sdk.values.PInput;
import org.apache.beam.sdk.values.POutput;
import org.apache.spark.sql.SparkSession;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Pipeline translator for streaming pipelines on Spark 4. It extends the batch translator to reuse
* the stateless single output ParDo, Window.Assign, Flatten and Reshuffle translators, which are
* safe on a streaming Dataset. Every other primitive fails at translation, the batch translators
* for them persist or collect the Dataset, which Spark rejects for streaming plans.
*/
@Internal
public class PipelineTranslatorStreaming extends PipelineTranslatorBatch {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it works and resuses some code, future change in PipelineTranslatorBatch may result in surprises on streaming path. For example, new translations meant only for batch now silently port to streaming. On the other hand, refactoring it sounds an risky choice either. At minimum can we rename "PipelineTranslatorBatch" to "PipelineTranslatorCommon" and create a thin subclass "PipelineTranslatorBatch" to it?


/** Returns a {@link TransformTranslator} for the given {@link PTransform} if known. */
@Override
@SuppressWarnings({"rawtypes", "unchecked"})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up SuppressWarnings

@Nullable
protected <InT extends PInput, OutT extends POutput, TransformT extends PTransform<InT, OutT>>
TransformTranslator<InT, OutT, TransformT> getTransformTranslator(TransformT transform) {

if (transform instanceof SplittableParDo.PrimitiveUnboundedRead) {
return (TransformTranslator) new ReadUnboundedTranslator<>();
}

if (transform instanceof SplittableParDo.PrimitiveBoundedRead) {
throw unsupported(
"Bounded Read (Read.from(BoundedSource), Create with two or more elements)");
}

if (transform instanceof Impulse) {
throw unsupported("Impulse (Create with fewer than two elements, PAssert)");
}

if (transform instanceof GroupByKey) {
throw unsupported("GroupByKey");
}

if (transform instanceof Combine.PerKey) {
throw unsupported("Combine.perKey");
}

if (transform instanceof ParDo.MultiOutput) {
ParDo.MultiOutput<?, ?> parDo = (ParDo.MultiOutput<?, ?>) transform;
DoFnSignature signature = DoFnSignatures.signatureForDoFn(parDo.getFn());
if (signature.usesState() || signature.usesTimers()) {
throw unsupported("Stateful ParDo (" + signature.fnClass().getName() + ")");
}
if (!parDo.getSideInputs().isEmpty()) {
throw unsupported("ParDo with side inputs (" + signature.fnClass().getName() + ")");
}
if (!parDo.getAdditionalOutputTags().getAll().isEmpty()) {
throw unsupported("ParDo with additional outputs (" + signature.fnClass().getName() + ")");
}
}

return super.getTransformTranslator(transform);
}

private static UnsupportedOperationException unsupported(String what) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a thin wrapper. In convention exceptions should be created at the exact moment an error occurs.

return new UnsupportedOperationException(
what
+ " is not supported by the Spark 4 streaming runner yet, see"
+ " https://github.com/apache/beam/issues/36841");
}

@Override
protected EvaluationContext createEvaluationContext(
Collection<? extends EvaluationContext.NamedDataset<?>> leaves,
SparkSession session,
SparkCommonPipelineOptions options) {
return new StreamingEvaluationContext(leaves, session, options);
}
}
Loading
Loading