You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by tg...@apache.org on 2017/03/27 19:54:13 UTC

[2/2] beam git commit: Do not reify timestamps in Reshuffle in Dataflow

Do not reify timestamps in Reshuffle in Dataflow

Dataflow has special handling of the ReshuffleTrigger that outputs
elements with their original timestamps.


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/e991e619
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/e991e619
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/e991e619

Branch: refs/heads/master
Commit: e991e619657cbb02a0edacbb72a95ddf67eaa4c3
Parents: 815c9fa
Author: Thomas Groh <tg...@google.com>
Authored: Fri Mar 17 16:21:27 2017 -0700
Committer: Thomas Groh <tg...@google.com>
Committed: Mon Mar 27 12:54:03 2017 -0700

----------------------------------------------------------------------
 .../beam/runners/dataflow/DataflowRunner.java   |  1 +
 .../dataflow/ReshuffleOverrideFactory.java      | 84 ++++++++++++++++++++
 2 files changed, 85 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/e991e619/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
----------------------------------------------------------------------
diff --git a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
index 24e23da..c612a20 100644
--- a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
+++ b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
@@ -366,6 +366,7 @@ public class DataflowRunner extends PipelineRunner<DataflowPipelineJob> {
                   BatchViewOverrides.BatchViewAsIterable.class, this));
     }
     ptoverrides
+        .put(PTransformMatchers.classEqualTo(Reshuffle.class), new ReshuffleOverrideFactory())
         // Order is important. Streaming views almost all use Combine internally.
         .put(
             PTransformMatchers.classEqualTo(Combine.GroupedValues.class),

http://git-wip-us.apache.org/repos/asf/beam/blob/e991e619/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/ReshuffleOverrideFactory.java
----------------------------------------------------------------------
diff --git a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/ReshuffleOverrideFactory.java b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/ReshuffleOverrideFactory.java
new file mode 100644
index 0000000..230f5dc
--- /dev/null
+++ b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/ReshuffleOverrideFactory.java
@@ -0,0 +1,84 @@
+/*
+ * 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.dataflow;
+
+import org.apache.beam.runners.core.construction.SingleInputOutputOverrideFactory;
+import org.apache.beam.sdk.runners.PTransformOverrideFactory;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.GroupByKey;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.transforms.windowing.Window;
+import org.apache.beam.sdk.util.IdentityWindowFn;
+import org.apache.beam.sdk.util.Reshuffle;
+import org.apache.beam.sdk.util.ReshuffleTrigger;
+import org.apache.beam.sdk.util.WindowingStrategy;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.joda.time.Duration;
+
+/**
+ * A {@link PTransformOverrideFactory} that overrides {@link Reshuffle} with a version that does not
+ * reify timestamps. Dataflow has special handling of the {@link ReshuffleTrigger} which never
+ * buffers elements and outputs elements with their original timestamp.
+ */
+class ReshuffleOverrideFactory<K, V>
+    extends SingleInputOutputOverrideFactory<
+        PCollection<KV<K, V>>, PCollection<KV<K, V>>, Reshuffle<K, V>> {
+  @Override
+  public PTransform<PCollection<KV<K, V>>, PCollection<KV<K, V>>> getReplacementTransform(
+      Reshuffle<K, V> transform) {
+    return new ReshuffleWithOnlyTrigger<>();
+  }
+
+  private static class ReshuffleWithOnlyTrigger<K, V>
+      extends PTransform<PCollection<KV<K, V>>, PCollection<KV<K, V>>> {
+    @Override
+    public PCollection<KV<K, V>> expand(PCollection<KV<K, V>> input) {
+      WindowingStrategy<?, ?> originalStrategy = input.getWindowingStrategy();
+      // If the input has already had its windows merged, then the GBK that performed the merge
+      // will have set originalStrategy.getWindowFn() to InvalidWindows, causing the GBK contained
+      // here to fail. Instead, we install a valid WindowFn that leaves all windows unchanged.
+      Window.Bound<KV<K, V>> rewindow =
+          Window.<KV<K, V>>into(
+              new IdentityWindowFn<>(
+                  originalStrategy.getWindowFn().windowCoder()))
+              .triggering(new ReshuffleTrigger<>())
+              .discardingFiredPanes()
+              .withAllowedLateness(Duration.millis(BoundedWindow.TIMESTAMP_MAX_VALUE.getMillis()));
+
+      return input.apply(rewindow)
+          .apply(GroupByKey.<K, V>create())
+          // Set the windowing strategy directly, so that it doesn't get counted as the user having
+          // set allowed lateness.
+          .setWindowingStrategyInternal(originalStrategy)
+          .apply("ExpandIterable", ParDo.of(
+              new DoFn<KV<K, Iterable<V>>, KV<K, V>>() {
+                @ProcessElement
+                public void processElement(ProcessContext c) {
+                  K key = c.element().getKey();
+                  for (V value : c.element().getValue()) {
+                    c.output(KV.of(key, value));
+                  }
+                }
+              }));
+    }
+  }
+}