forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LateDataUtils.java
119 lines (113 loc) · 4.85 KB
/
LateDataUtils.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
/*
* 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.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import javax.annotation.Nullable;
import org.apache.beam.runners.core.metrics.CounterCell;
import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
import org.apache.beam.sdk.transforms.windowing.GlobalWindow;
import org.apache.beam.sdk.util.WindowTracing;
import org.apache.beam.sdk.util.WindowedValue;
import org.apache.beam.sdk.values.WindowingStrategy;
import org.joda.time.Duration;
import org.joda.time.Instant;
/**
* Utils to handle late data.
*/
public class LateDataUtils {
private LateDataUtils() {}
/**
* Return when {@code window} should be garbage collected. If the window's expiration time is on
* or after the end of the global window, it will be truncated to the end of the global window.
*/
public static Instant garbageCollectionTime(
BoundedWindow window, WindowingStrategy windowingStrategy) {
return garbageCollectionTime(window, windowingStrategy.getAllowedLateness());
}
/**
* Return when {@code window} should be garbage collected. If the window's expiration time is on
* or after the end of the global window, it will be truncated to the end of the global window.
*/
public static Instant garbageCollectionTime(BoundedWindow window, Duration allowedLateness) {
// If the end of the window + allowed lateness is beyond the "end of time" aka the end of the
// global window, then we truncate it. The conditional is phrased like it is because the
// addition of EOW + allowed lateness might even overflow the maximum allowed Instant
if (GlobalWindow.INSTANCE
.maxTimestamp()
.minus(allowedLateness)
.isBefore(window.maxTimestamp())) {
return GlobalWindow.INSTANCE.maxTimestamp();
} else {
return window.maxTimestamp().plus(allowedLateness);
}
}
/**
* Returns an {@code Iterable<WindowedValue<InputT>>} that only contains non-late input elements.
*/
public static <K, V> Iterable<WindowedValue<V>> dropExpiredWindows(
final K key,
Iterable<WindowedValue<V>> elements,
final TimerInternals timerInternals,
final WindowingStrategy<?, ?> windowingStrategy,
final CounterCell droppedDueToLateness) {
return FluentIterable.from(elements)
.transformAndConcat(
// Explode windows to filter out expired ones
new Function<WindowedValue<V>, Iterable<WindowedValue<V>>>() {
@Override
public Iterable<WindowedValue<V>> apply(@Nullable WindowedValue<V> input) {
if (input == null) {
return null;
}
return input.explodeWindows();
}
})
.filter(
new Predicate<WindowedValue<V>>() {
@Override
public boolean apply(@Nullable WindowedValue<V> input) {
if (input == null) {
// drop null elements.
return false;
}
BoundedWindow window = Iterables.getOnlyElement(input.getWindows());
boolean expired =
window
.maxTimestamp()
.plus(windowingStrategy.getAllowedLateness())
.isBefore(timerInternals.currentInputWatermarkTime());
if (expired) {
// The element is too late for this window.
droppedDueToLateness.inc();
WindowTracing.debug(
"GroupAlsoByWindow: Dropping element at {} for key: {}; "
+ "window: {} since it is too far behind inputWatermark: {}",
input.getTimestamp(),
key,
window,
timerInternals.currentInputWatermarkTime());
}
// Keep the element if the window is not expired.
return !expired;
}
});
}
}