|
40 | 40 | import com.google.cloud.dataflow.sdk.util.AppliedCombineFn; |
41 | 41 | import com.google.cloud.dataflow.sdk.util.IOChannelUtils; |
42 | 42 | import com.google.cloud.dataflow.sdk.util.MapAggregatorValues; |
| 43 | +import com.google.cloud.dataflow.sdk.util.PerKeyCombineFnRunner; |
| 44 | +import com.google.cloud.dataflow.sdk.util.PerKeyCombineFnRunners; |
43 | 45 | import com.google.cloud.dataflow.sdk.util.SerializableUtils; |
44 | 46 | import com.google.cloud.dataflow.sdk.util.TestCredential; |
45 | 47 | import com.google.cloud.dataflow.sdk.util.WindowedValue; |
@@ -240,7 +242,8 @@ private <K, InputT, AccumT, OutputT> PCollection<KV<K, OutputT>> applyTestCombin |
240 | 242 | PCollection<KV<K, Iterable<InputT>>> input) { |
241 | 243 |
|
242 | 244 | PCollection<KV<K, OutputT>> output = input |
243 | | - .apply(ParDo.of(TestCombineDoFn.create(transform, input, testSerializability, rand))); |
| 245 | + .apply(ParDo.of(TestCombineDoFn.create(transform, input, testSerializability, rand)) |
| 246 | + .withSideInputs(transform.getSideInputs())); |
244 | 247 |
|
245 | 248 | try { |
246 | 249 | output.setCoder(transform.getDefaultOutputCoder(input)); |
@@ -261,7 +264,7 @@ private <K, InputT, AccumT, OutputT> PCollection<KV<K, OutputT>> applyTestCombin |
261 | 264 | */ |
262 | 265 | public static class TestCombineDoFn<K, InputT, AccumT, OutputT> |
263 | 266 | extends DoFn<KV<K, Iterable<InputT>>, KV<K, OutputT>> { |
264 | | - private final KeyedCombineFn<? super K, ? super InputT, AccumT, OutputT> fn; |
| 267 | + private final PerKeyCombineFnRunner<? super K, ? super InputT, AccumT, OutputT> fnRunner; |
265 | 268 | private final Coder<AccumT> accumCoder; |
266 | 269 | private final boolean testSerializability; |
267 | 270 | private final Random rand; |
@@ -273,21 +276,21 @@ public static <K, InputT, AccumT, OutputT> TestCombineDoFn<K, InputT, AccumT, Ou |
273 | 276 | Random rand) { |
274 | 277 |
|
275 | 278 | AppliedCombineFn<? super K, ? super InputT, ?, OutputT> fn = transform.getAppliedFn( |
276 | | - input.getPipeline().getCoderRegistry(), input.getCoder()); |
| 279 | + input.getPipeline().getCoderRegistry(), input.getCoder(), input.getWindowingStrategy()); |
277 | 280 |
|
278 | 281 | return new TestCombineDoFn( |
279 | | - fn.getFn(), |
| 282 | + PerKeyCombineFnRunners.create(fn.getFn()), |
280 | 283 | fn.getAccumulatorCoder(), |
281 | 284 | testSerializability, |
282 | 285 | rand); |
283 | 286 | } |
284 | 287 |
|
285 | 288 | public TestCombineDoFn( |
286 | | - KeyedCombineFn<? super K, ? super InputT, AccumT, OutputT> fn, |
| 289 | + PerKeyCombineFnRunner<? super K, ? super InputT, AccumT, OutputT> fnRunner, |
287 | 290 | Coder<AccumT> accumCoder, |
288 | 291 | boolean testSerializability, |
289 | 292 | Random rand) { |
290 | | - this.fn = fn; |
| 293 | + this.fnRunner = fnRunner; |
291 | 294 | this.accumCoder = accumCoder; |
292 | 295 | this.testSerializability = testSerializability; |
293 | 296 | this.rand = rand; |
@@ -302,15 +305,15 @@ public void processElement(ProcessContext c) throws Exception { |
302 | 305 | Iterable<InputT> values = c.element().getValue(); |
303 | 306 | List<AccumT> groupedPostShuffle = |
304 | 307 | ensureSerializableByCoder(ListCoder.of(accumCoder), |
305 | | - addInputsRandomly(fn, key, values, rand), |
306 | | - "After addInputs of KeyedCombineFn " + fn.toString()); |
| 308 | + addInputsRandomly(fnRunner, key, values, rand, c), |
| 309 | + "After addInputs of KeyedCombineFn " + fnRunner.fn().toString()); |
307 | 310 | AccumT merged = |
308 | 311 | ensureSerializableByCoder(accumCoder, |
309 | | - fn.mergeAccumulators(key, groupedPostShuffle), |
310 | | - "After mergeAccumulators of KeyedCombineFn " + fn.toString()); |
| 312 | + fnRunner.mergeAccumulators(key, groupedPostShuffle, c), |
| 313 | + "After mergeAccumulators of KeyedCombineFn " + fnRunner.fn().toString()); |
311 | 314 | // Note: The serializability of KV<K, OutputT> is ensured by the |
312 | 315 | // runner itself, since it's a transform output. |
313 | | - c.output(KV.of(key, fn.extractOutput(key, merged))); |
| 316 | + c.output(KV.of(key, fnRunner.extractOutput(key, merged, c))); |
314 | 317 | } |
315 | 318 |
|
316 | 319 | /** |
@@ -319,17 +322,18 @@ public void processElement(ProcessContext c) throws Exception { |
319 | 322 | * <p>Visible for testing purposes only. |
320 | 323 | */ |
321 | 324 | public static <K, AccumT, InputT> List<AccumT> addInputsRandomly( |
322 | | - KeyedCombineFn<? super K, ? super InputT, AccumT, ?> fn, |
| 325 | + PerKeyCombineFnRunner<? super K, ? super InputT, AccumT, ?> fnRunner, |
323 | 326 | K key, |
324 | 327 | Iterable<InputT> values, |
325 | | - Random random) { |
| 328 | + Random random, |
| 329 | + DoFn<?, ?>.ProcessContext c) { |
326 | 330 | List<AccumT> out = new ArrayList<AccumT>(); |
327 | 331 | int i = 0; |
328 | | - AccumT accumulator = fn.createAccumulator(key); |
| 332 | + AccumT accumulator = fnRunner.createAccumulator(key, c); |
329 | 333 | boolean hasInput = false; |
330 | 334 |
|
331 | 335 | for (InputT value : values) { |
332 | | - accumulator = fn.addInput(key, accumulator, value); |
| 336 | + accumulator = fnRunner.addInput(key, accumulator, value, c); |
333 | 337 | hasInput = true; |
334 | 338 |
|
335 | 339 | // For each index i, flip a 1/2^i weighted coin for whether to |
@@ -340,10 +344,10 @@ public static <K, AccumT, InputT> List<AccumT> addInputsRandomly( |
340 | 344 | // of the accumulators. |
341 | 345 | if (i == 0 || random.nextInt(1 << Math.min(i, 30)) == 0) { |
342 | 346 | if (i % 2 == 0) { |
343 | | - accumulator = fn.compact(key, accumulator); |
| 347 | + accumulator = fnRunner.compact(key, accumulator, c); |
344 | 348 | } |
345 | 349 | out.add(accumulator); |
346 | | - accumulator = fn.createAccumulator(key); |
| 350 | + accumulator = fnRunner.createAccumulator(key, c); |
347 | 351 | hasInput = false; |
348 | 352 | } |
349 | 353 | i++; |
|