-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
windowed_value.py
444 lines (363 loc) · 13.5 KB
/
windowed_value.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#
# 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.
#
"""Core windowing data structures."""
# This module is carefully crafted to have optimal performance when
# compiled while still being valid Python. Care needs to be taken when
# editing this file as WindowedValues are created for every element for
# every step in a Beam pipeline.
# pytype: skip-file
import collections
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Iterable
from typing import List
from typing import Optional
from typing import Sequence
from typing import Tuple
from apache_beam.utils.timestamp import MAX_TIMESTAMP
from apache_beam.utils.timestamp import MIN_TIMESTAMP
from apache_beam.utils.timestamp import Timestamp
from apache_beam.utils.timestamp import TimestampTypes # pylint: disable=unused-import
if TYPE_CHECKING:
from apache_beam.transforms.window import BoundedWindow
class PaneInfoTiming(object):
"""The timing of a PaneInfo."""
EARLY = 0
ON_TIME = 1
LATE = 2
UNKNOWN = 3
@classmethod
def to_string(cls, value):
return {
cls.EARLY: 'EARLY',
cls.ON_TIME: 'ON_TIME',
cls.LATE: 'LATE',
cls.UNKNOWN: 'UNKNOWN',
}[value]
@classmethod
def from_string(cls, value):
return {
'EARLY': cls.EARLY,
'ON_TIME': cls.ON_TIME,
'LATE': cls.LATE,
'UNKNOWN': cls.UNKNOWN
}[value]
class PaneInfo(object):
"""Describes the trigger firing information for a given WindowedValue.
"Panes" represent individual firings on a single window. ``PaneInfo``s are
passed downstream after trigger firings. They contain information about
whether it's an early/on time/late firing, if it's the last or first firing
from a window, and the index of the firing.
"""
def __init__(self, is_first, is_last, timing, index, nonspeculative_index):
self._is_first = is_first
self._is_last = is_last
self._timing = timing
self._index = index
self._nonspeculative_index = nonspeculative_index
self._encoded_byte = self._get_encoded_byte()
def _get_encoded_byte(self):
byte = 0
if self._is_first:
byte |= 1
if self._is_last:
byte |= 2
byte |= self._timing << 2
return byte
@staticmethod
def from_encoded_byte(encoded_byte):
assert encoded_byte in _BYTE_TO_PANE_INFO
return _BYTE_TO_PANE_INFO[encoded_byte]
# Because common PaneInfo objects are cached, it is important that the value
# is immutable. We therefore explicitly enforce this here with read-only
# properties.
@property
def is_first(self):
return self._is_first
@property
def is_last(self):
return self._is_last
@property
def timing(self):
return self._timing
@property
def index(self):
# type: () -> int
return self._index
@property
def nonspeculative_index(self):
# type: () -> int
return self._nonspeculative_index
@property
def encoded_byte(self):
# type: () -> int
return self._encoded_byte
def __repr__(self):
return (
'PaneInfo(first: %r, last: %r, timing: %s, index: %d, '
'nonspeculative_index: %d)') % (
self.is_first,
self.is_last,
PaneInfoTiming.to_string(self.timing),
self.index,
self.nonspeculative_index)
def __eq__(self, other):
if self is other:
return True
if isinstance(other, PaneInfo):
return (
self.is_first == other.is_first and self.is_last == other.is_last and
self.timing == other.timing and self.index == other.index and
self.nonspeculative_index == other.nonspeculative_index)
return NotImplemented
def __hash__(self):
return hash((
self.is_first,
self.is_last,
self.timing,
self.index,
self.nonspeculative_index))
def __reduce__(self):
return PaneInfo, (self._is_first, self._is_last, self._timing, self._index,
self._nonspeculative_index)
def _construct_well_known_pane_infos():
# type: () -> List[PaneInfo]
pane_infos = []
for timing in (PaneInfoTiming.EARLY,
PaneInfoTiming.ON_TIME,
PaneInfoTiming.LATE,
PaneInfoTiming.UNKNOWN):
nonspeculative_index = -1 if timing == PaneInfoTiming.EARLY else 0
pane_infos.append(PaneInfo(True, True, timing, 0, nonspeculative_index))
pane_infos.append(PaneInfo(True, False, timing, 0, nonspeculative_index))
pane_infos.append(PaneInfo(False, True, timing, -1, nonspeculative_index))
pane_infos.append(PaneInfo(False, False, timing, -1, nonspeculative_index))
result = [None] * (
max(p.encoded_byte for p in pane_infos) + 1
) # type: List[PaneInfo] # type: ignore[list-item]
for pane_info in pane_infos:
result[pane_info.encoded_byte] = pane_info
return result
# Cache of well-known PaneInfo objects.
_BYTE_TO_PANE_INFO = _construct_well_known_pane_infos()
# Default PaneInfo descriptor for when a value is not the output of triggering.
PANE_INFO_UNKNOWN = _BYTE_TO_PANE_INFO[0xF]
class WindowedValue(object):
"""A windowed value having a value, a timestamp and set of windows.
Attributes:
value: The underlying value of a windowed value.
timestamp: Timestamp associated with the value as seconds since Unix epoch.
windows: A set (iterable) of window objects for the value. The window
object are descendants of the BoundedWindow class.
pane_info: A PaneInfo descriptor describing the triggering information for
the pane that contained this value. If None, will be set to
PANE_INFO_UNKNOWN.
"""
def __init__(
self,
value,
timestamp, # type: TimestampTypes
windows, # type: Tuple[BoundedWindow, ...]
pane_info=PANE_INFO_UNKNOWN # type: PaneInfo
):
# type: (...) -> None
# For performance reasons, only timestamp_micros is stored by default
# (as a C int). The Timestamp object is created on demand below.
self.value = value
if isinstance(timestamp, int):
self.timestamp_micros = timestamp * 1000000
if TYPE_CHECKING:
self.timestamp_object = None # type: Optional[Timestamp]
else:
self.timestamp_object = (
timestamp
if isinstance(timestamp, Timestamp) else Timestamp.of(timestamp))
self.timestamp_micros = self.timestamp_object.micros
self.windows = windows
self.pane_info = pane_info
@property
def timestamp(self):
# type: () -> Timestamp
if self.timestamp_object is None:
self.timestamp_object = Timestamp(0, self.timestamp_micros)
return self.timestamp_object
def __repr__(self):
return '(%s, %s, %s, %s)' % (
repr(self.value),
'MIN_TIMESTAMP' if self.timestamp == MIN_TIMESTAMP else 'MAX_TIMESTAMP'
if self.timestamp == MAX_TIMESTAMP else float(self.timestamp),
self.windows,
self.pane_info)
def __eq__(self, other):
if isinstance(other, WindowedValue):
return (
type(self) == type(other) and
self.timestamp_micros == other.timestamp_micros and
self.value == other.value and self.windows == other.windows and
self.pane_info == other.pane_info)
return NotImplemented
def __hash__(self):
return ((hash(self.value) & 0xFFFFFFFFFFFFFFF) + 3 *
(self.timestamp_micros & 0xFFFFFFFFFFFFFF) + 7 *
(hash(tuple(self.windows)) & 0xFFFFFFFFFFFFF) + 11 *
(hash(self.pane_info) & 0xFFFFFFFFFFFFF))
def with_value(self, new_value):
# type: (Any) -> WindowedValue
"""Creates a new WindowedValue with the same timestamps and windows as this.
This is the fasted way to create a new WindowedValue.
"""
return create(
new_value, self.timestamp_micros, self.windows, self.pane_info)
def __reduce__(self):
return WindowedValue, (
self.value, self.timestamp, self.windows, self.pane_info)
# TODO(robertwb): Move this to a static method.
def create(value, timestamp_micros, windows, pane_info=PANE_INFO_UNKNOWN):
wv = WindowedValue.__new__(WindowedValue)
wv.value = value
wv.timestamp_micros = timestamp_micros
wv.windows = windows
wv.pane_info = pane_info
return wv
class WindowedBatch(object):
"""A batch of N windowed values, each having a value, a timestamp and set of
windows."""
def with_values(self, new_values):
# type: (Any) -> WindowedBatch
"""Creates a new WindowedBatch with the same timestamps and windows as this.
This is the fasted way to create a new WindowedValue.
"""
raise NotImplementedError
def as_windowed_values(self, explode_fn: Callable) -> Iterable[WindowedValue]:
raise NotImplementedError
@staticmethod
def from_windowed_values(
windowed_values: Sequence[WindowedValue], *,
produce_fn: Callable) -> Iterable['WindowedBatch']:
return HomogeneousWindowedBatch.from_windowed_values(
windowed_values, produce_fn=produce_fn)
class HomogeneousWindowedBatch(WindowedBatch):
"""A WindowedBatch with Homogeneous event-time information, represented
internally as a WindowedValue.
"""
def __init__(self, wv):
self._wv = wv
@staticmethod
def of(values, timestamp, windows, pane_info):
return HomogeneousWindowedBatch(
WindowedValue(values, timestamp, windows, pane_info))
@property
def values(self):
return self._wv.value
@property
def timestamp(self):
return self._wv.timestamp
@property
def pane_info(self):
return self._wv.pane_info
@property
def windows(self):
return self._wv.windows
@windows.setter
def windows(self, value):
self._wv.windows = value
def with_values(self, new_values):
# type: (Any) -> WindowedBatch
return HomogeneousWindowedBatch(self._wv.with_value(new_values))
def as_windowed_values(self, explode_fn: Callable) -> Iterable[WindowedValue]:
for value in explode_fn(self._wv.value):
yield self._wv.with_value(value)
def as_empty_windowed_value(self):
"""Get a single WindowedValue with identical windowing information to this
HomogeneousWindowedBatch, but with value=None. Useful for re-using APIs that
pull windowing information from a WindowedValue."""
return self._wv.with_value(None)
def __eq__(self, other):
if isinstance(other, HomogeneousWindowedBatch):
return self._wv == other._wv
return NotImplemented
def __hash__(self):
return hash(self._wv)
@staticmethod
def from_batch_and_windowed_value(
*, batch, windowed_value: WindowedValue) -> 'WindowedBatch':
return HomogeneousWindowedBatch(windowed_value.with_value(batch))
@staticmethod
def from_windowed_values(
windowed_values: Sequence[WindowedValue], *,
produce_fn: Callable) -> Iterable['WindowedBatch']:
grouped = collections.defaultdict(lambda: [])
for wv in windowed_values:
grouped[wv.with_value(None)].append(wv.value)
for key, values in grouped.items():
yield HomogeneousWindowedBatch(key.with_value(produce_fn(values)))
try:
WindowedValue.timestamp_object = None
except TypeError:
# When we're compiled, we can't dynamically add attributes to
# the cdef class, but in this case it's OK as it's already present
# on each instance.
pass
class _IntervalWindowBase(object):
"""Optimized form of IntervalWindow storing only microseconds for endpoints.
"""
def __init__(self, start, end):
# type: (TimestampTypes, TimestampTypes) -> None
if start is not None:
self._start_object = Timestamp.of(start) # type: Optional[Timestamp]
try:
self._start_micros = self._start_object.micros
except OverflowError:
self._start_micros = (
MIN_TIMESTAMP.micros
if self._start_object.micros < 0 else MAX_TIMESTAMP.micros)
else:
# Micros must be populated elsewhere.
self._start_object = None
if end is not None:
self._end_object = Timestamp.of(end) # type: Optional[Timestamp]
try:
self._end_micros = self._end_object.micros
except OverflowError:
self._end_micros = (
MIN_TIMESTAMP.micros
if self._end_object.micros < 0 else MAX_TIMESTAMP.micros)
else:
# Micros must be populated elsewhere.
self._end_object = None
@property
def start(self):
# type: () -> Timestamp
if self._start_object is None:
self._start_object = Timestamp(0, self._start_micros)
return self._start_object
@property
def end(self):
# type: () -> Timestamp
if self._end_object is None:
self._end_object = Timestamp(0, self._end_micros)
return self._end_object
def __hash__(self):
return hash((self._start_micros, self._end_micros))
def __eq__(self, other):
return (
type(self) == type(other) and
self._start_micros == other._start_micros and
self._end_micros == other._end_micros)
def __repr__(self):
return '[%s, %s)' % (float(self.start), float(self.end))