Abacn commented on code in PR #41158:
URL: https://github.com/apache/beam/pull/41158#discussion_r4789373806
##########
runners/flink/src/test/java/org/apache/beam/runners/flink/FlinkStreamingAggregationsTranslatorsTest.java:
##########
@@ -86,4 +110,88 @@ public void usesSortedBatchCombineOnlyWhenEligible() {
eligible(options(false, true), global,
Collections.singletonList(view)),
is(false));
}
+
+ @Test
+ public void concatenateAsIterableAppendsInPlaceToArrayListAccumulator() {
+ FlinkStreamingAggregationsTranslators.ConcatenateAsIterable<Integer>
combineFn =
+ new FlinkStreamingAggregationsTranslators.ConcatenateAsIterable<>();
+
+ Iterable<Integer> acc = combineFn.createAccumulator();
+ Iterable<Integer> afterFirst = combineFn.addInput(acc, 1);
+ Iterable<Integer> afterSecond = combineFn.addInput(afterFirst, 2);
+ Iterable<Integer> afterThird = combineFn.addInput(afterSecond, 3);
+
+ assertThat(afterFirst, sameInstance(acc));
+ assertThat(afterThird, sameInstance(acc));
+ assertThat(combineFn.extractOutput(afterThird), contains(1, 2, 3));
+ }
+
+ @Test
+ public void concatenateAsIterableCopiesNonArrayListAccumulator() {
+ FlinkStreamingAggregationsTranslators.ConcatenateAsIterable<Integer>
combineFn =
+ new FlinkStreamingAggregationsTranslators.ConcatenateAsIterable<>();
+
+ Iterable<Integer> merged =
+ combineFn.mergeAccumulators(Arrays.asList(ImmutableList.of(1, 2),
ImmutableList.of(3)));
+ Iterable<Integer> updated = combineFn.addInput(merged, 4);
+
+ assertThat(combineFn.extractOutput(updated), contains(1, 2, 3, 4));
+ }
+
+ @Test
+ public void
concatenateAsIterablePreGroupsStructuralKeysInPartialReduceOperator()
Review Comment:
The other two tests look fine. This one duplicates
`PartialReduceBundleOperator` harness boilerplate without testing GroupByKey
translator wiring or in-place mutation, while `byte[]` grouping already covered
by the new test class. Consider simply drop the test.
##########
runners/flink/src/main/java/org/apache/beam/runners/flink/translation/wrappers/streaming/PartialReduceBundleOperator.java:
##########
@@ -86,10 +96,29 @@ public PartialReduceBundleOperator(
Collections.emptyMap());
this.combineFn = combineFn;
+ this.bundleKeyCoder = keyCoder(windowedInputCoder);
this.state = ArrayListMultimap.create();
this.checkpointedState = null;
}
+ @SuppressWarnings("unchecked")
+ private static <K, InputT> Coder<K> keyCoder(
+ Coder<WindowedValue<KV<K, InputT>>> windowedInputCoder) {
+ Preconditions.checkArgument(
+ windowedInputCoder instanceof WindowedValues.WindowedValueCoder,
+ "Expected a WindowedValueCoder, got %s",
+ windowedInputCoder);
+ Coder<KV<K, InputT>> valueCoder =
+ ((WindowedValues.WindowedValueCoder<KV<K, InputT>>)
windowedInputCoder).getValueCoder();
Review Comment:
We check KvCoder below, can do samething for WindowedValueCoder here and
eliminate SuppressWarnings
##########
runners/flink/src/test/java/org/apache/beam/runners/flink/translation/wrappers/streaming/PartialReduceBundleOperatorTest.java:
##########
@@ -0,0 +1,325 @@
+/*
+ * 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.wrappers.streaming;
+
+import static java.util.Collections.emptyList;
+import static java.util.Collections.emptyMap;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.emptyIterable;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.beam.runners.core.construction.SerializablePipelineOptions;
+import org.apache.beam.runners.flink.FlinkPipelineOptions;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderRegistry;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.coders.NullableCoder;
+import org.apache.beam.sdk.coders.VarLongCoder;
+import org.apache.beam.sdk.transforms.Combine;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.transforms.windowing.FixedWindows;
+import org.apache.beam.sdk.transforms.windowing.GlobalWindow;
+import org.apache.beam.sdk.transforms.windowing.IntervalWindow;
+import org.apache.beam.sdk.transforms.windowing.PaneInfo;
+import org.apache.beam.sdk.transforms.windowing.Sessions;
+import org.apache.beam.sdk.transforms.windowing.WindowFn;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.sdk.values.WindowedValue;
+import org.apache.beam.sdk.values.WindowedValues;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterables;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests {@link PartialReduceBundleOperator}. */
+@RunWith(JUnit4.class)
+public class PartialReduceBundleOperatorTest {
+
+ /** Sums longs; the accumulator is the running sum so partial results are
easy to compare. */
+ private static class SumLongs extends Combine.CombineFn<Long, Long, Long> {
+ @Override
+ public Long createAccumulator() {
+ return 0L;
+ }
+
+ @Override
+ public Long addInput(Long accumulator, Long input) {
+ return accumulator + input;
+ }
+
+ @Override
+ public Long mergeAccumulators(Iterable<Long> accumulators) {
+ long sum = 0;
+ for (Long accumulator : accumulators) {
+ sum += accumulator;
+ }
+ return sum;
+ }
+
+ @Override
+ public Long extractOutput(Long accumulator) {
+ return accumulator;
+ }
+
+ @Override
+ public Coder<Long> getAccumulatorCoder(CoderRegistry registry, Coder<Long>
inputCoder) {
+ return VarLongCoder.of();
+ }
+ }
+
+ private static <K, W extends BoundedWindow>
+ PartialReduceBundleOperator<K, Long, Long, Long> createOperator(
+ Coder<K> keyCoder, WindowingStrategy<?, W> windowingStrategy, long
maxBundleSize) {
+ FlinkPipelineOptions options = FlinkPipelineOptions.defaults();
+ options.setStreaming(false);
+ options.setMaxBundleSize(maxBundleSize);
+
+ Coder<W> windowCoder = windowingStrategy.getWindowFn().windowCoder();
+ Coder<WindowedValue<KV<K, Long>>> windowedCoder =
+ WindowedValues.getFullCoder(KvCoder.of(keyCoder, VarLongCoder.of()),
windowCoder);
+ TupleTag<KV<K, Long>> mainTag = new TupleTag<>("main-output");
+
+ return new PartialReduceBundleOperator<>(
+ new SumLongs(),
+ "stepName",
+ windowedCoder,
+ mainTag,
+ emptyList(),
+ new DoFnOperator.MultiOutputOutputManagerFactory<>(
+ mainTag, windowedCoder, new SerializablePipelineOptions(options)),
+ windowingStrategy,
+ emptyMap(),
+ emptyList(),
+ options);
+ }
+
+ /** Runs one bundle containing {@code input} through the operator and
returns its output. */
+ private static <K> List<KV<K, Long>> preCombine(Coder<K> keyCoder,
List<KV<K, Long>> input)
+ throws Exception {
+ // Finish the bundle, and so emit the partial results, right after the
last input.
+ PartialReduceBundleOperator<K, Long, Long, Long> operator =
+ createOperator(keyCoder, WindowingStrategy.globalDefault(),
input.size());
+
+ List<KV<K, Long>> output = new ArrayList<>();
+ try (OneInputStreamOperatorTestHarness<WindowedValue<KV<K, Long>>,
WindowedValue<KV<K, Long>>>
+ testHarness = new OneInputStreamOperatorTestHarness<>(operator)) {
+ testHarness.open();
+ for (KV<K, Long> element : input) {
+ testHarness.processElement(new
StreamRecord<>(WindowedValues.valueInGlobalWindow(element)));
+ }
+ for (WindowedValue<KV<K, Long>> value :
+ StreamRecordStripper.<KV<K, Long>>stripStreamRecordFromWindowedValue(
+ testHarness.getOutput())) {
+ output.add(value.getValue());
+ }
+ }
+ return output;
+ }
+
+ private static <K, W extends BoundedWindow> WindowedValue<KV<K, Long>>
windowedElement(
+ WindowFn<Object, W> windowFn, K key, long value, long timestampMs)
throws Exception {
+ Instant timestamp = new Instant(timestampMs);
+ W window =
+ Iterables.getOnlyElement(
+ windowFn.assignWindows(
+ windowFn.new AssignContext() {
+ @Override
+ public Object element() {
+ return key;
+ }
+
+ @Override
+ public Instant timestamp() {
+ return timestamp;
+ }
+
+ @Override
+ public BoundedWindow window() {
+ return GlobalWindow.INSTANCE;
+ }
+ }));
+ return WindowedValues.of(KV.of(key, value), timestamp, window,
PaneInfo.NO_FIRING);
+ }
+
+ @Test
+ public void combinesKeysThatAreEqualOnlyByEncoding() throws Exception {
+ // Distinct byte[] instances with equal contents: byte[] uses identity
equals/hashCode.
+ List<KV<byte[], Long>> input =
+ Arrays.asList(
+ KV.of(new byte[] {1, 2}, 1L),
+ KV.of(new byte[] {3}, 10L),
+ KV.of(new byte[] {1, 2}, 2L),
+ KV.of(new byte[] {1, 2}, 3L),
+ KV.of(new byte[] {3}, 20L));
+
+ List<KV<String, Long>> output = new ArrayList<>();
+ for (KV<byte[], Long> kv : preCombine(ByteArrayCoder.of(), input)) {
+ output.add(KV.of(Arrays.toString(kv.getKey()), kv.getValue()));
+ }
+
+ assertThat(output, containsInAnyOrder(KV.of("[1, 2]", 6L), KV.of("[3]",
30L)));
+ }
+
+ @Test
+ public void combinesKeysWithConsistentEquals() throws Exception {
+ List<KV<Long, Long>> input =
+ Arrays.asList(KV.of(1L, 1L), KV.of(2L, 10L), KV.of(1L, 2L), KV.of(2L,
20L), KV.of(1L, 3L));
+
+ assertThat(
+ preCombine(VarLongCoder.of(), input), containsInAnyOrder(KV.of(1L,
6L), KV.of(2L, 30L)));
+ }
+
+ @Test
+ public void flushesIncompleteBundleOnClose() throws Exception {
+ PartialReduceBundleOperator<byte[], Long, Long, Long> operator =
+ createOperator(ByteArrayCoder.of(), WindowingStrategy.globalDefault(),
100L);
+ OneInputStreamOperatorTestHarness<
+ WindowedValue<KV<byte[], Long>>, WindowedValue<KV<byte[], Long>>>
+ testHarness = new OneInputStreamOperatorTestHarness<>(operator);
+ testHarness.open();
+ testHarness.processElement(
+ new StreamRecord<>(WindowedValues.valueInGlobalWindow(KV.of(new byte[]
{1, 2}, 1L))));
+ testHarness.processElement(
+ new StreamRecord<>(WindowedValues.valueInGlobalWindow(KV.of(new byte[]
{3}, 10L))));
+ testHarness.processElement(
+ new StreamRecord<>(WindowedValues.valueInGlobalWindow(KV.of(new byte[]
{1, 2}, 5L))));
+
+ assertThat(
+
StreamRecordStripper.stripStreamRecordFromWindowedValue(testHarness.getOutput()),
Review Comment:
Consider using `testHarness.extractOutputValues()` if applicable
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]