Skip to content
Merged
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
Expand Up @@ -17,19 +17,16 @@
*/
package org.apache.beam.runners.flink;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Executors;
import org.apache.beam.model.jobmanagement.v1.JobApi.JobState.Enum;
import org.apache.beam.model.pipeline.v1.RunnerApi;
import org.apache.beam.runners.core.construction.Environments;
import org.apache.beam.runners.core.construction.JavaReadViaImpulse;
import org.apache.beam.runners.core.construction.PipelineTranslation;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.BigEndianLongCoder;
Expand All @@ -39,12 +36,14 @@
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.options.PortablePipelineOptions;
import org.apache.beam.sdk.testing.CrashingRunner;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.GroupByKey;
import org.apache.beam.sdk.transforms.Impulse;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.WithKeys;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -61,7 +60,7 @@
@RunWith(Parameterized.class)
public class PortableExecutionTest implements Serializable {

@Parameters
@Parameters(name = "streaming: {0}")
public static Object[] data() {
return new Object[] {true, false};
}
Expand All @@ -80,9 +79,7 @@ public void tearDown() {
flinkJobExecutor.shutdown();
}

private static ArrayList<KV<String, Iterable<Long>>> outputValues = new ArrayList<>();

@Test
@Test(timeout = 120_000)
public void testExecution() throws Exception {
PipelineOptions options = PipelineOptionsFactory.create();
options.setRunner(CrashingRunner.class);
Expand All @@ -92,45 +89,42 @@ public void testExecution() throws Exception {
.as(PortablePipelineOptions.class)
.setDefaultEnvironmentType(Environments.ENVIRONMENT_EMBEDDED);
Pipeline p = Pipeline.create(options);
p.apply("impulse", Impulse.create())
.apply(
"create",
ParDo.of(
new DoFn<byte[], String>() {
@ProcessElement
public void process(ProcessContext ctxt) {
ctxt.output("zero");
ctxt.output("one");
ctxt.output("two");
}
}))
.apply(
"len",
ParDo.of(
new DoFn<String, Long>() {
@ProcessElement
public void process(ProcessContext ctxt) {
ctxt.output((long) ctxt.element().length());
}
}))
.apply("addKeys", WithKeys.of("foo"))
// Use some unknown coders
.setCoder(KvCoder.of(StringUtf8Coder.of(), BigEndianLongCoder.of()))
// Force the output to be materialized
.apply("gbk", GroupByKey.create())
.apply(
"collect",
ParDo.of(
new DoFn<KV<String, Iterable<Long>>, Void>() {
@ProcessElement
public void process(ProcessContext ctx) {
outputValues.add(ctx.element());
}
}));
PCollection<KV<String, Iterable<Long>>> result =
p.apply("impulse", Impulse.create())
.apply(
"create",
ParDo.of(
new DoFn<byte[], String>() {
@ProcessElement
public void process(ProcessContext ctxt) {
ctxt.output("zero");
ctxt.output("one");
ctxt.output("two");
}
}))
.apply(
"len",
ParDo.of(
new DoFn<String, Long>() {
@ProcessElement
public void process(ProcessContext ctxt) {
ctxt.output((long) ctxt.element().length());
}
}))
.apply("addKeys", WithKeys.of("foo"))
// Use some unknown coders
.setCoder(KvCoder.of(StringUtf8Coder.of(), BigEndianLongCoder.of()))
// Force the output to be materialized
.apply("gbk", GroupByKey.create());

PAssert.that(result).containsInAnyOrder(KV.of("foo", ImmutableList.of(4L, 3L, 3L)));

// This is line below required to convert the PAssert's read to an impulse, which is expected
// by the GreedyPipelineFuser.
p.replaceAll(Collections.singletonList(JavaReadViaImpulse.boundedOverride()));

RunnerApi.Pipeline pipelineProto = PipelineTranslation.toProto(p);

outputValues.clear();
// execute the pipeline
FlinkJobInvocation jobInvocation =
FlinkJobInvocation.create(
Expand All @@ -140,16 +134,10 @@ public void process(ProcessContext ctx) {
pipelineProto,
options.as(FlinkPipelineOptions.class),
null,
Collections.EMPTY_LIST);
Collections.emptyList());
jobInvocation.start();
long timeout = System.currentTimeMillis() + 60 * 1000;
while (jobInvocation.getState() != Enum.DONE && System.currentTimeMillis() < timeout) {
while (jobInvocation.getState() != Enum.DONE) {
Thread.sleep(1000);
}
assertEquals("job state", Enum.DONE, jobInvocation.getState());

assertEquals(1, outputValues.size());
assertEquals("foo", outputValues.get(0).getKey());
assertThat(outputValues.get(0).getValue(), containsInAnyOrder(4L, 3L, 3L));
}
}
Loading