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

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

                Author: ASF GitHub Bot
            Created on: 06/Nov/18 22:02
            Start Date: 06/Nov/18 22:02
    Worklog Time Spent: 10m 
      Work Description: iemejia closed pull request #6960: [BEAM-5987] Fix 
Spark SideInputReader performance.
URL: https://github.com/apache/beam/pull/6960
 
 
   

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/runners/spark/src/main/java/org/apache/beam/runners/spark/translation/MultiDoFnFunction.java
 
b/runners/spark/src/main/java/org/apache/beam/runners/spark/translation/MultiDoFnFunction.java
index c70634c0733..4efe1e1438e 100644
--- 
a/runners/spark/src/main/java/org/apache/beam/runners/spark/translation/MultiDoFnFunction.java
+++ 
b/runners/spark/src/main/java/org/apache/beam/runners/spark/translation/MultiDoFnFunction.java
@@ -35,6 +35,7 @@
 import org.apache.beam.runners.core.TimerInternals;
 import org.apache.beam.runners.core.construction.SerializablePipelineOptions;
 import org.apache.beam.runners.core.metrics.MetricsContainerStepMap;
+import org.apache.beam.runners.spark.util.CachedSideInputReader;
 import org.apache.beam.runners.spark.util.SideInputBroadcast;
 import org.apache.beam.runners.spark.util.SparkSideInputReader;
 import org.apache.beam.sdk.coders.Coder;
@@ -153,7 +154,7 @@ public TimerInternals timerInternals() {
         DoFnRunners.simpleRunner(
             options.get(),
             doFn,
-            new SparkSideInputReader(sideInputs),
+            CachedSideInputReader.of(new SparkSideInputReader(sideInputs)),
             outputManager,
             mainOutputTag,
             additionalOutputTags,
diff --git 
a/runners/spark/src/main/java/org/apache/beam/runners/spark/util/CachedSideInputReader.java
 
b/runners/spark/src/main/java/org/apache/beam/runners/spark/util/CachedSideInputReader.java
new file mode 100644
index 00000000000..49200c6c4b4
--- /dev/null
+++ 
b/runners/spark/src/main/java/org/apache/beam/runners/spark/util/CachedSideInputReader.java
@@ -0,0 +1,103 @@
+/*
+ * 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.util;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import javax.annotation.Nullable;
+import org.apache.beam.runners.core.SideInputReader;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.values.PCollectionView;
+
+/** {@link SideInputReader} that caches materialized views. */
+public class CachedSideInputReader implements SideInputReader {
+
+  /**
+   * Create a new cached {@link SideInputReader}.
+   *
+   * @param delegate wrapped reader
+   * @return cached reader
+   */
+  public static CachedSideInputReader of(SideInputReader delegate) {
+    return new CachedSideInputReader(delegate);
+  }
+
+  /**
+   * Composite key of {@link PCollectionView} and {@link BoundedWindow} used 
to identify
+   * materialized results.
+   *
+   * @param <T> type of result
+   */
+  private static class Key<T> {
+
+    private final PCollectionView<T> view;
+    private final BoundedWindow window;
+
+    Key(PCollectionView<T> view, BoundedWindow window) {
+      this.view = view;
+      this.window = window;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) {
+        return true;
+      }
+      if (o == null || getClass() != o.getClass()) {
+        return false;
+      }
+      final Key<?> key = (Key<?>) o;
+      return Objects.equals(view, key.view) && Objects.equals(window, 
key.window);
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(view, window);
+    }
+  }
+
+  /** Wrapped {@link SideInputReader} which results will be cached. */
+  private final SideInputReader delegate;
+
+  /** Materialized results. */
+  private final Map<Key<?>, ?> materialized = new HashMap<>();
+
+  private CachedSideInputReader(SideInputReader delegate) {
+    this.delegate = delegate;
+  }
+
+  @Nullable
+  @Override
+  public <T> T get(PCollectionView<T> view, BoundedWindow window) {
+    @SuppressWarnings("unchecked")
+    final Map<Key<T>, T> materializedCasted = (Map) materialized;
+    return materializedCasted.computeIfAbsent(
+        new Key<>(view, window), key -> delegate.get(view, window));
+  }
+
+  @Override
+  public <T> boolean contains(PCollectionView<T> view) {
+    return delegate.contains(view);
+  }
+
+  @Override
+  public boolean isEmpty() {
+    return delegate.isEmpty();
+  }
+}


 

----------------------------------------------------------------
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:
us...@infra.apache.org


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

    Worklog Id:     (was: 163217)
    Time Spent: 50m  (was: 40m)

> Spark SideInputReader performance
> ---------------------------------
>
>                 Key: BEAM-5987
>                 URL: https://issues.apache.org/jira/browse/BEAM-5987
>             Project: Beam
>          Issue Type: Bug
>          Components: runner-spark
>    Affects Versions: 2.8.0
>            Reporter: David Moravek
>            Assignee: David Moravek
>            Priority: Major
>             Fix For: 2.9.0
>
>         Attachments: Screen Shot 2018-11-06 at 13.05.36.png
>
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> We did some profiling of a spark job and 90% of the application time was 
> spent on side input deserialization.
> For spark, an easy fix is to cache materialized side inputs per bundle. This 
> improved running time of the profiled job from 3 hours to 30 minutes.



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

Reply via email to