dmvk commented on a change in pull request #11874:
URL: https://github.com/apache/beam/pull/11874#discussion_r433877187



##########
File path: 
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/functions/FlinkNonMergingReduceFunction.java
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.flink.translation.functions;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.transforms.windowing.PaneInfo;
+import org.apache.beam.sdk.util.WindowedValue;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.PeekingIterator;
+import org.apache.flink.api.common.functions.GroupReduceFunction;
+import org.apache.flink.util.Collector;
+import org.joda.time.Instant;
+
+/**
+ * Reduce function for non-merging GBK implementation. Implementation tries to 
return non-iterable
+ * results when possible, so we do not have to materialize all values for a 
single key in memory.
+ *
+ * @param <K> Key type.
+ * @param <InputT> Input type.
+ */
+public class FlinkNonMergingReduceFunction<K, InputT>
+    implements GroupReduceFunction<
+        WindowedValue<KV<K, InputT>>, WindowedValue<KV<K, Iterable<InputT>>>> {
+
+  private static class OnceIterable<T> implements Iterable<T> {
+
+    private final Iterator<T> iterator;
+
+    private final AtomicBoolean used = new AtomicBoolean(false);
+
+    OnceIterable(Iterator<T> iterator) {
+      this.iterator = iterator;
+    }
+
+    @Override
+    public Iterator<T> iterator() {
+      if (used.compareAndSet(false, true)) {
+        return iterator;
+      }
+      throw new IllegalStateException(
+          "GBK result is not re-iterable. You can enable re-iterations by 
setting '--reIterableGroupByKeyResult=true'.");

Review comment:
       👍 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to