[ 
https://issues.apache.org/jira/browse/BEAM-4279?focusedWorklogId=101860&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-101860
 ]

ASF GitHub Bot logged work on BEAM-4279:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 14/May/18 19:41
            Start Date: 14/May/18 19:41
    Worklog Time Spent: 10m 
      Work Description: lukecwik closed pull request #5342: [BEAM-4279] Handle 
Multiple copies of a single input in the Fn Harness
URL: https://github.com/apache/beam/pull/5342
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/control/ProcessBundleHandler.java
 
b/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/control/ProcessBundleHandler.java
index 6551570433a..13f58519e0c 100644
--- 
a/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/control/ProcessBundleHandler.java
+++ 
b/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/control/ProcessBundleHandler.java
@@ -19,10 +19,13 @@
 package org.apache.beam.fn.harness.control;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
+import com.google.common.collect.SetMultimap;
 import com.google.common.collect.Sets;
 import com.google.protobuf.Message;
 import com.google.protobuf.TextFormat;
@@ -43,6 +46,7 @@
 import org.apache.beam.fn.harness.state.BeamFnStateClient;
 import org.apache.beam.fn.harness.state.BeamFnStateGrpcClientCache;
 import org.apache.beam.model.fnexecution.v1.BeamFnApi;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleDescriptor;
 import org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleRequest;
 import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest;
 import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest.Builder;
@@ -51,6 +55,8 @@
 import org.apache.beam.model.pipeline.v1.RunnerApi;
 import org.apache.beam.model.pipeline.v1.RunnerApi.Coder;
 import org.apache.beam.model.pipeline.v1.RunnerApi.PCollection;
+import org.apache.beam.model.pipeline.v1.RunnerApi.PTransform;
+import org.apache.beam.model.pipeline.v1.RunnerApi.WindowingStrategy;
 import org.apache.beam.runners.core.construction.PTransformTranslation;
 import org.apache.beam.sdk.fn.data.FnDataReceiver;
 import org.apache.beam.sdk.fn.function.ThrowingRunnable;
@@ -132,13 +138,14 @@ public ProcessBundleHandler(
   private void createRunnerAndConsumersForPTransformRecursively(
       BeamFnStateClient beamFnStateClient,
       String pTransformId,
-      RunnerApi.PTransform pTransform,
+      PTransform pTransform,
       Supplier<String> processBundleInstructionId,
-      BeamFnApi.ProcessBundleDescriptor processBundleDescriptor,
-      Multimap<String, String> pCollectionIdsToConsumingPTransforms,
-      Multimap<String, FnDataReceiver<WindowedValue<?>>> 
pCollectionIdsToConsumers,
+      ProcessBundleDescriptor processBundleDescriptor,
+      SetMultimap<String, String> pCollectionIdsToConsumingPTransforms,
+      ListMultimap<String, FnDataReceiver<WindowedValue<?>>> 
pCollectionIdsToConsumers,
       Consumer<ThrowingRunnable> addStartFunction,
-      Consumer<ThrowingRunnable> addFinishFunction) throws IOException {
+      Consumer<ThrowingRunnable> addFinishFunction)
+      throws IOException {
 
     // Recursively ensure that all consumers of the output PCollection have 
been created.
     // Since we are creating the consumers first, we know that the we are 
building the DAG
@@ -202,10 +209,9 @@ private void 
createRunnerAndConsumersForPTransformRecursively(
     BeamFnApi.ProcessBundleDescriptor bundleDescriptor =
         (BeamFnApi.ProcessBundleDescriptor) fnApiRegistry.apply(bundleId);
 
-    Multimap<String, String> pCollectionIdsToConsumingPTransforms = 
HashMultimap.create();
-    Multimap<String,
-        FnDataReceiver<WindowedValue<?>>> pCollectionIdsToConsumers =
-        HashMultimap.create();
+    SetMultimap<String, String> pCollectionIdsToConsumingPTransforms = 
HashMultimap.create();
+    ListMultimap<String, FnDataReceiver<WindowedValue<?>>> 
pCollectionIdsToConsumers =
+        ArrayListMultimap.create();
     List<ThrowingRunnable> startFunctions = new ArrayList<>();
     List<ThrowingRunnable> finishFunctions = new ArrayList<>();
 
@@ -339,11 +345,11 @@ public Object createRunnerForPTransform(
         BeamFnDataClient beamFnDataClient,
         BeamFnStateClient beamFnStateClient,
         String pTransformId,
-        RunnerApi.PTransform pTransform,
+        PTransform pTransform,
         Supplier<String> processBundleInstructionId,
         Map<String, PCollection> pCollections,
         Map<String, Coder> coders,
-        Map<String, RunnerApi.WindowingStrategy> windowingStrategies,
+        Map<String, WindowingStrategy> windowingStrategies,
         Multimap<String, FnDataReceiver<WindowedValue<?>>> 
pCollectionIdsToConsumers,
         Consumer<ThrowingRunnable> addStartFunction,
         Consumer<ThrowingRunnable> addFinishFunction) {
diff --git 
a/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/FlattenRunnerTest.java
 
b/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/FlattenRunnerTest.java
index e6f849c23ca..9b1bd759a73 100644
--- 
a/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/FlattenRunnerTest.java
+++ 
b/sdks/java/harness/src/test/java/org/apache/beam/fn/harness/FlattenRunnerTest.java
@@ -21,14 +21,19 @@
 import static org.apache.beam.sdk.util.WindowedValue.valueInGlobalWindow;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
 import static org.junit.Assert.assertThat;
 
 import com.google.common.base.Suppliers;
+import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Multimap;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import org.apache.beam.model.pipeline.v1.RunnerApi;
 import org.apache.beam.runners.core.construction.PTransformTranslation;
@@ -92,12 +97,81 @@ public void testCreatingAndProcessingDoFlatten() throws 
Exception {
     
Iterables.getOnlyElement(consumers.get("inputATarget")).accept(valueInGlobalWindow("A2"));
     
Iterables.getOnlyElement(consumers.get("inputBTarget")).accept(valueInGlobalWindow("B"));
     
Iterables.getOnlyElement(consumers.get("inputCTarget")).accept(valueInGlobalWindow("C"));
-    assertThat(mainOutputValues, contains(
-        valueInGlobalWindow("A1"),
-        valueInGlobalWindow("A2"),
-        valueInGlobalWindow("B"),
-        valueInGlobalWindow("C")));
+    assertThat(
+        mainOutputValues,
+        contains(
+            valueInGlobalWindow("A1"),
+            valueInGlobalWindow("A2"),
+            valueInGlobalWindow("B"),
+            valueInGlobalWindow("C")));
 
     mainOutputValues.clear();
   }
+
+  /**
+   * Create a Flatten that has 4 inputs (inputATarget1, inputATarget2, 
inputBTarget, inputCTarget)
+   * and one output (mainOutput). Validate that inputs are flattened together 
and directed to the
+   * output.
+   */
+  @Test
+  public void testFlattenWithDuplicateInputCollectionProducesMultipleOutputs() 
throws Exception {
+    String pTransformId = "pTransformId";
+    String mainOutputId = "101";
+
+    RunnerApi.FunctionSpec functionSpec =
+        RunnerApi.FunctionSpec.newBuilder()
+            .setUrn(PTransformTranslation.FLATTEN_TRANSFORM_URN)
+            .build();
+    RunnerApi.PTransform pTransform =
+        RunnerApi.PTransform.newBuilder()
+            .setSpec(functionSpec)
+            .putInputs("inputA", "inputATarget")
+            .putInputs("inputAAgain", "inputATarget")
+            .putOutputs(mainOutputId, "mainOutputTarget")
+            .build();
+
+    List<WindowedValue<String>> mainOutputValues = new ArrayList<>();
+    ListMultimap<String, FnDataReceiver<WindowedValue<?>>> consumers = 
ArrayListMultimap.create();
+    consumers.put(
+        "mainOutputTarget",
+        (FnDataReceiver) (FnDataReceiver<WindowedValue<String>>) 
mainOutputValues::add);
+
+    new FlattenRunner.Factory<>()
+        .createRunnerForPTransform(
+            PipelineOptionsFactory.create(),
+            null /* beamFnDataClient */,
+            null /* beamFnStateClient */,
+            pTransformId,
+            pTransform,
+            Suppliers.ofInstance("57L")::get,
+            Collections.emptyMap(),
+            Collections.emptyMap(),
+            Collections.emptyMap(),
+            consumers,
+            null /* addStartFunction */,
+            null /* addFinishFunction */);
+
+    mainOutputValues.clear();
+    assertThat(consumers.keySet(), containsInAnyOrder("inputATarget", 
"mainOutputTarget"));
+
+    assertThat(consumers.get("inputATarget"), hasSize(2));
+    Iterator<FnDataReceiver<WindowedValue<?>>> targets = 
consumers.get("inputATarget").iterator();
+    FnDataReceiver<WindowedValue<?>> first = targets.next();
+    FnDataReceiver<WindowedValue<?>> second = targets.next();
+    // Both of these are the flatten consumer
+    assertThat(first, equalTo(second));
+
+    first.accept(WindowedValue.valueInGlobalWindow("A1"));
+    second.accept(WindowedValue.valueInGlobalWindow("A1"));
+    first.accept(WindowedValue.valueInGlobalWindow("A2"));
+    second.accept(WindowedValue.valueInGlobalWindow("A2"));
+
+    assertThat(
+        mainOutputValues,
+        containsInAnyOrder(
+            valueInGlobalWindow("A1"),
+            valueInGlobalWindow("A1"),
+            valueInGlobalWindow("A2"),
+            valueInGlobalWindow("A2")));
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 101860)
    Time Spent: 40m  (was: 0.5h)

> The Fn Harness doesn't properly handle Flattens which consume a single input 
> multiple times
> -------------------------------------------------------------------------------------------
>
>                 Key: BEAM-4279
>                 URL: https://issues.apache.org/jira/browse/BEAM-4279
>             Project: Beam
>          Issue Type: Bug
>          Components: sdk-java-harness
>            Reporter: Thomas Groh
>            Assignee: Luke Cwik
>            Priority: Major
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> For a {{Flatten}} which consumes a {{PCollection}} n times, each input 
> element should be present {{n}} times in the output {{PCollection}}. 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to