/
LateDataDroppingDoFnRunner.java
166 lines (152 loc) · 6.49 KB
/
LateDataDroppingDoFnRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* 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.core;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.beam.sdk.metrics.Counter;
import org.apache.beam.sdk.metrics.Metrics;
import org.apache.beam.sdk.state.TimeDomain;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
import org.apache.beam.sdk.util.WindowTracing;
import org.apache.beam.sdk.util.WindowedValue;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.WindowingStrategy;
import org.joda.time.Instant;
/**
* A customized {@link DoFnRunner} that handles late data dropping for
* a {@link KeyedWorkItem} input {@link DoFn}.
*
* <p>It expands windows before checking data lateness.
*
* <p>{@link KeyedWorkItem KeyedWorkItems} are always in empty windows.
*
* @param <K> key type
* @param <InputT> input value element type
* @param <OutputT> output value element type
* @param <W> window type
*/
public class LateDataDroppingDoFnRunner<K, InputT, OutputT, W extends BoundedWindow>
implements DoFnRunner<KeyedWorkItem<K, InputT>, KV<K, OutputT>> {
private final DoFnRunner<KeyedWorkItem<K, InputT>, KV<K, OutputT>> doFnRunner;
private final LateDataFilter lateDataFilter;
public static final String DROPPED_DUE_TO_LATENESS = "droppedDueToLateness";
public LateDataDroppingDoFnRunner(
DoFnRunner<KeyedWorkItem<K, InputT>, KV<K, OutputT>> doFnRunner,
WindowingStrategy<?, ?> windowingStrategy,
TimerInternals timerInternals) {
this.doFnRunner = doFnRunner;
lateDataFilter = new LateDataFilter(windowingStrategy, timerInternals);
}
@Override
public void startBundle() {
doFnRunner.startBundle();
}
@Override
public void processElement(WindowedValue<KeyedWorkItem<K, InputT>> elem) {
Iterable<WindowedValue<InputT>> nonLateElements = lateDataFilter.filter(
elem.getValue().key(), elem.getValue().elementsIterable());
KeyedWorkItem<K, InputT> keyedWorkItem = KeyedWorkItems.workItem(
elem.getValue().key(), elem.getValue().timersIterable(), nonLateElements);
doFnRunner.processElement(elem.withValue(keyedWorkItem));
}
@Override
public void onTimer(String timerId, BoundedWindow window, Instant timestamp,
TimeDomain timeDomain) {
doFnRunner.onTimer(timerId, window, timestamp, timeDomain);
}
@Override
public void finishBundle() {
doFnRunner.finishBundle();
}
/**
* It filters late data in a {@link KeyedWorkItem}.
*/
@VisibleForTesting
static class LateDataFilter {
private final WindowingStrategy<?, ?> windowingStrategy;
private final TimerInternals timerInternals;
private final Counter droppedDueToLateness;
public LateDataFilter(
WindowingStrategy<?, ?> windowingStrategy,
TimerInternals timerInternals) {
this.windowingStrategy = windowingStrategy;
this.timerInternals = timerInternals;
this.droppedDueToLateness = Metrics.counter(LateDataDroppingDoFnRunner.class,
DROPPED_DUE_TO_LATENESS);
}
/**
* Returns an {@code Iterable<WindowedValue<InputT>>} that only contains
* non-late input elements.
*/
public <K, InputT> Iterable<WindowedValue<InputT>> filter(
final K key, Iterable<WindowedValue<InputT>> elements) {
Iterable<Iterable<WindowedValue<InputT>>> windowsExpandedElements =
StreamSupport.stream(elements.spliterator(), false)
.map(
input ->
input
.getWindows()
.stream()
.map(
window ->
WindowedValue.of(
input.getValue(),
input.getTimestamp(),
window,
input.getPane()))
.collect(Collectors.toList()))
.collect(Collectors.toList());
Iterable<WindowedValue<InputT>> concatElements = Iterables.concat(windowsExpandedElements);
// Bump the counter separately since we don't want multiple iterations to
// increase it multiple times.
for (WindowedValue<InputT> input : concatElements) {
BoundedWindow window = Iterables.getOnlyElement(input.getWindows());
if (canDropDueToExpiredWindow(window)) {
// The element is too late for this window.
droppedDueToLateness.inc();
WindowTracing.debug(
"{}: Dropping element at {} for key:{}; window:{} "
+ "since too far behind inputWatermark:{}; outputWatermark:{}",
LateDataFilter.class.getSimpleName(),
input.getTimestamp(),
key,
window,
timerInternals.currentInputWatermarkTime(),
timerInternals.currentOutputWatermarkTime());
}
}
Iterable<WindowedValue<InputT>> nonLateElements =
StreamSupport.stream(concatElements.spliterator(), false)
.filter(
input -> {
BoundedWindow window = Iterables.getOnlyElement(input.getWindows());
return !canDropDueToExpiredWindow(window);
})
.collect(Collectors.toList());
return nonLateElements;
}
/** Is {@code window} expired w.r.t. the garbage collection watermark? */
private boolean canDropDueToExpiredWindow(BoundedWindow window) {
Instant inputWM = timerInternals.currentInputWatermarkTime();
return LateDataUtils.garbageCollectionTime(window, windowingStrategy).isBefore(inputWM);
}
}
}