-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BEAM-313] Enable the use of an existing spark context with the SparkPipelineRunner #401
Merged
+164
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
runners/spark/src/test/java/org/apache/beam/runners/spark/ProvidedSparkContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* 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 static org.junit.Assert.fail; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.beam.runners.spark.examples.WordCount; | ||
import org.apache.beam.sdk.Pipeline; | ||
import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
import org.apache.beam.sdk.testing.PAssert; | ||
import org.apache.beam.sdk.transforms.Create; | ||
import org.apache.beam.sdk.transforms.MapElements; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Provided Spark Context tests. | ||
*/ | ||
public class ProvidedSparkContextTest { | ||
private static final String[] WORDS_ARRAY = { | ||
"hi there", "hi", "hi sue bob", | ||
"hi sue", "", "bob hi"}; | ||
private static final List<String> WORDS = Arrays.asList(WORDS_ARRAY); | ||
private static final Set<String> EXPECTED_COUNT_SET = | ||
ImmutableSet.of("hi: 5", "there: 1", "sue: 2", "bob: 2"); | ||
private static final String PROVIDED_CONTEXT_EXCEPTION = | ||
"The provided Spark context was not created or was stopped"; | ||
|
||
/** | ||
* Provide a context and call pipeline run. | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testWithProvidedContext() throws Exception { | ||
JavaSparkContext jsc = new JavaSparkContext("local[*]", "Existing_Context"); | ||
|
||
SparkPipelineOptions options = PipelineOptionsFactory.as(SparkPipelineOptions.class); | ||
options.setRunner(SparkRunner.class); | ||
options.setUsesProvidedSparkContext(true); | ||
options.setProvidedSparkContext(jsc); | ||
|
||
Pipeline p = Pipeline.create(options); | ||
PCollection<String> inputWords = p.apply(Create.of(WORDS).withCoder(StringUtf8Coder | ||
.of())); | ||
PCollection<String> output = inputWords.apply(new WordCount.CountWords()) | ||
.apply(MapElements.via(new WordCount.FormatAsTextFn())); | ||
|
||
PAssert.that(output).containsInAnyOrder(EXPECTED_COUNT_SET); | ||
|
||
// Run test from pipeline | ||
p.run(); | ||
|
||
jsc.stop(); | ||
} | ||
|
||
/** | ||
* Provide a context and call pipeline run. | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testWithNullContext() throws Exception { | ||
JavaSparkContext jsc = null; | ||
|
||
SparkPipelineOptions options = PipelineOptionsFactory.as(SparkPipelineOptions.class); | ||
options.setRunner(SparkRunner.class); | ||
options.setUsesProvidedSparkContext(true); | ||
options.setProvidedSparkContext(jsc); | ||
|
||
Pipeline p = Pipeline.create(options); | ||
PCollection<String> inputWords = p.apply(Create.of(WORDS).withCoder(StringUtf8Coder | ||
.of())); | ||
PCollection<String> output = inputWords.apply(new WordCount.CountWords()) | ||
.apply(MapElements.via(new WordCount.FormatAsTextFn())); | ||
|
||
PAssert.that(output).containsInAnyOrder(EXPECTED_COUNT_SET); | ||
|
||
try { | ||
p.run(); | ||
fail("Should throw an exception when The provided Spark context is null"); | ||
} catch (RuntimeException e){ | ||
assert(e.getMessage().contains(PROVIDED_CONTEXT_EXCEPTION)); | ||
} | ||
} | ||
|
||
/** | ||
* A SparkRunner with a stopped provided Spark context cannot run pipelines. | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testWithStoppedProvidedContext() throws Exception { | ||
JavaSparkContext jsc = new JavaSparkContext("local[*]", "Existing_Context"); | ||
// Stop the provided Spark context directly | ||
jsc.stop(); | ||
|
||
SparkPipelineOptions options = PipelineOptionsFactory.as(SparkPipelineOptions.class); | ||
options.setRunner(SparkRunner.class); | ||
options.setUsesProvidedSparkContext(true); | ||
options.setProvidedSparkContext(jsc); | ||
|
||
Pipeline p = Pipeline.create(options); | ||
PCollection<String> inputWords = p.apply(Create.of(WORDS).withCoder(StringUtf8Coder | ||
.of())); | ||
PCollection<String> output = inputWords.apply(new WordCount.CountWords()) | ||
.apply(MapElements.via(new WordCount.FormatAsTextFn())); | ||
|
||
PAssert.that(output).containsInAnyOrder(EXPECTED_COUNT_SET); | ||
|
||
try { | ||
p.run(); | ||
fail("Should throw an exception when The provided Spark context is stopped"); | ||
} catch (RuntimeException e){ | ||
assert(e.getMessage().contains(PROVIDED_CONTEXT_EXCEPTION)); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again,
SparkRunner
is the correct name.