|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.fn.harness; |
| 19 | + |
| 20 | +import com.google.auto.service.AutoService; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import com.google.common.collect.Sets; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import org.apache.beam.model.pipeline.v1.RunnerApi; |
| 30 | +import org.apache.beam.model.pipeline.v1.RunnerApi.PTransform; |
| 31 | +import org.apache.beam.model.pipeline.v1.RunnerApi.StandardPTransforms; |
| 32 | +import org.apache.beam.runners.core.construction.BeamUrns; |
| 33 | +import org.apache.beam.runners.core.construction.WindowingStrategyTranslation; |
| 34 | +import org.apache.beam.sdk.fn.function.ThrowingFunction; |
| 35 | +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; |
| 36 | +import org.apache.beam.sdk.transforms.windowing.WindowFn; |
| 37 | +import org.apache.beam.sdk.transforms.windowing.WindowFn.MergeContext; |
| 38 | +import org.apache.beam.sdk.values.KV; |
| 39 | + |
| 40 | +/** |
| 41 | + * Merges windows using a {@link org.apache.beam.sdk.transforms.windowing.WindowFn}. |
| 42 | + * |
| 43 | + * <p>Window merging function: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li>Input: {@code KV<nonce, iterable<OriginalWindow>>} |
| 47 | + * <li>Output: {@code KV<nonce, KV<iterable<UnmergedOriginalWindow>, iterable<KV<MergedWindow, |
| 48 | + * iterable<ConsumedOriginalWindow>>>>} |
| 49 | + * </ul> |
| 50 | + * |
| 51 | + * <p>For each set of original windows, a list of all unmerged windows is output alongside a map of |
| 52 | + * merged window to set of consumed windows. All original windows must be contained in either the |
| 53 | + * unmerged original window set or one of the consumed original window sets. Each original window |
| 54 | + * can only be part of one output set. The nonce is used by a runner to associate each input with |
| 55 | + * its output. The nonce is represented as an opaque set of bytes. |
| 56 | + */ |
| 57 | +public abstract class WindowMergingFnRunner<T, W extends BoundedWindow> { |
| 58 | + static final String URN = BeamUrns.getUrn(StandardPTransforms.Primitives.MERGE_WINDOWS); |
| 59 | + |
| 60 | + /** |
| 61 | + * A registrar which provides a factory to handle merging windows based upon the {@link WindowFn}. |
| 62 | + */ |
| 63 | + @AutoService(PTransformRunnerFactory.Registrar.class) |
| 64 | + public static class Registrar implements PTransformRunnerFactory.Registrar { |
| 65 | + |
| 66 | + @Override |
| 67 | + public Map<String, PTransformRunnerFactory> getPTransformRunnerFactories() { |
| 68 | + return ImmutableMap.of( |
| 69 | + URN, |
| 70 | + MapFnRunners.forValueMapFnFactory(WindowMergingFnRunner::createMapFunctionForPTransform)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + static <T, W extends BoundedWindow> |
| 75 | + ThrowingFunction<KV<T, Iterable<W>>, KV<T, KV<Iterable<W>, Iterable<KV<W, Iterable<W>>>>>> |
| 76 | + createMapFunctionForPTransform(String ptransformId, PTransform ptransform) |
| 77 | + throws IOException { |
| 78 | + RunnerApi.SdkFunctionSpec payload = |
| 79 | + RunnerApi.SdkFunctionSpec.parseFrom(ptransform.getSpec().getPayload()); |
| 80 | + |
| 81 | + WindowFn<?, W> windowFn = |
| 82 | + (WindowFn<?, W>) WindowingStrategyTranslation.windowFnFromProto(payload); |
| 83 | + return WindowMergingFnRunner.<T, W>create(windowFn)::mergeWindows; |
| 84 | + } |
| 85 | + |
| 86 | + static <T, W extends BoundedWindow> WindowMergingFnRunner<T, W> create(WindowFn<?, W> windowFn) { |
| 87 | + if (windowFn.isNonMerging()) { |
| 88 | + return new NonMergingWindowFnRunner(); |
| 89 | + } else { |
| 90 | + return new MergingViaWindowFnRunner(windowFn); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns the set of unmerged windows and a mapping from merged windows to sets of original |
| 96 | + * windows. |
| 97 | + */ |
| 98 | + abstract KV<T, KV<Iterable<W>, Iterable<KV<W, Iterable<W>>>>> mergeWindows( |
| 99 | + KV<T, Iterable<W>> windowsToMerge) throws Exception; |
| 100 | + |
| 101 | + ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 102 | + |
| 103 | + /** |
| 104 | + * An optimized version of window merging where the {@link WindowFn} does not do any window |
| 105 | + * merging. |
| 106 | + * |
| 107 | + * <p>Note that this is likely to never be invoked and the identity mapping will be handled |
| 108 | + * directly by runners. We have this here because runners may not perform this optimization. |
| 109 | + */ |
| 110 | + private static class NonMergingWindowFnRunner<T, W extends BoundedWindow> |
| 111 | + extends WindowMergingFnRunner<T, W> { |
| 112 | + @Override |
| 113 | + KV<T, KV<Iterable<W>, Iterable<KV<W, Iterable<W>>>>> mergeWindows( |
| 114 | + KV<T, Iterable<W>> windowsToMerge) { |
| 115 | + return KV.of( |
| 116 | + windowsToMerge.getKey(), KV.of(windowsToMerge.getValue(), Collections.emptyList())); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** An implementation which uses a {@link WindowFn} to merge windows. */ |
| 121 | + private static class MergingViaWindowFnRunner<T, W extends BoundedWindow> |
| 122 | + extends WindowMergingFnRunner<T, W> { |
| 123 | + private final WindowFn<?, W> windowFn; |
| 124 | + private final WindowFn<?, W>.MergeContext mergeContext; |
| 125 | + private Collection<W> currentWindows; |
| 126 | + private List<KV<W, Collection<W>>> mergedWindows; |
| 127 | + |
| 128 | + private MergingViaWindowFnRunner(WindowFn<?, W> windowFn) { |
| 129 | + this.windowFn = windowFn; |
| 130 | + this.mergedWindows = new ArrayList<>(); |
| 131 | + this.currentWindows = new ArrayList<>(); |
| 132 | + this.mergeContext = |
| 133 | + windowFn.new MergeContext() { |
| 134 | + |
| 135 | + @Override |
| 136 | + public Collection<W> windows() { |
| 137 | + return currentWindows; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void merge(Collection<W> toBeMerged, W mergeResult) throws Exception { |
| 142 | + mergedWindows.add(KV.of(mergeResult, toBeMerged)); |
| 143 | + } |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + KV<T, KV<Iterable<W>, Iterable<KV<W, Iterable<W>>>>> mergeWindows( |
| 149 | + KV<T, Iterable<W>> windowsToMerge) throws Exception { |
| 150 | + currentWindows = Sets.newHashSet(windowsToMerge.getValue()); |
| 151 | + windowFn.mergeWindows((MergeContext) mergeContext); |
| 152 | + for (KV<W, Collection<W>> mergedWindow : mergedWindows) { |
| 153 | + currentWindows.removeAll(mergedWindow.getValue()); |
| 154 | + } |
| 155 | + return KV.of(windowsToMerge.getKey(), KV.of(currentWindows, (Iterable) mergedWindows)); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments