-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathdirect_metrics.py
More file actions
253 lines (204 loc) · 7.76 KB
/
direct_metrics.py
File metadata and controls
253 lines (204 loc) · 7.76 KB
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
#
# 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.
#
"""
DirectRunner implementation of MetricResults. It is in charge not only of
responding to queries of current metrics, but also of keeping the common
state consistent.
"""
# pytype: skip-file
import threading
from collections import defaultdict
from typing import Any
from typing import SupportsInt
from apache_beam.metrics.cells import BoundedTrieData
from apache_beam.metrics.cells import DistributionData
from apache_beam.metrics.cells import GaugeData
from apache_beam.metrics.cells import StringSetData
from apache_beam.metrics.execution import MetricKey
from apache_beam.metrics.execution import MetricResult
from apache_beam.metrics.metric import MetricResults
class MetricAggregator(object):
"""For internal use only; no backwards-compatibility guarantees.
Base interface for aggregating metric data during pipeline execution."""
def identity_element(self):
# type: () -> Any
"""Returns the identical element of an Aggregation.
For the identity element, it must hold that
Aggregator.combine(any_element, identity_element) == any_element.
"""
raise NotImplementedError
def combine(self, x, y):
# type: (Any, Any) -> Any
raise NotImplementedError
def result(self, x):
# type: (Any) -> Any
raise NotImplementedError
class CounterAggregator(MetricAggregator):
"""For internal use only; no backwards-compatibility guarantees.
Aggregator for Counter metric data during pipeline execution.
Values aggregated should be ``int`` objects.
"""
@staticmethod
def identity_element():
# type: () -> int
return 0
def combine(self, x, y):
# type: (SupportsInt, SupportsInt) -> int
return int(x) + int(y)
def result(self, x):
# type: (SupportsInt) -> int
return int(x)
_IDENTITY_HISTOGRAM = object()
class HistogramAggregator(MetricAggregator):
@staticmethod
def identity_element():
return _IDENTITY_HISTOGRAM
def combine(self, x, y):
if x is _IDENTITY_HISTOGRAM:
return y
if y is _IDENTITY_HISTOGRAM:
return x
return x.combine(y)
def result(self, x):
if x is _IDENTITY_HISTOGRAM:
raise TypeError
return x.get_result()
class GenericAggregator(MetricAggregator):
def __init__(self, data_class):
self._data_class = data_class
def identity_element(self):
return self._data_class.identity_element()
def combine(self, x, y):
return x.combine(y)
def result(self, x):
return x.get_result()
class DirectMetrics(MetricResults):
def __init__(self):
self._counters = defaultdict(lambda: DirectMetric(CounterAggregator()))
self._distributions = defaultdict(
lambda: DirectMetric(GenericAggregator(DistributionData)))
self._gauges = defaultdict(
lambda: DirectMetric(GenericAggregator(GaugeData)))
self._string_sets = defaultdict(
lambda: DirectMetric(GenericAggregator(StringSetData)))
self._bounded_tries = defaultdict(
lambda: DirectMetric(GenericAggregator(BoundedTrieData)))
self._histograms = defaultdict(lambda: DirectMetric(HistogramAggregator()))
def _apply_operation(self, bundle, updates, op):
for k, v in updates.counters.items():
op(self._counters[k], bundle, v)
for k, v in updates.distributions.items():
op(self._distributions[k], bundle, v)
for k, v in updates.gauges.items():
op(self._gauges[k], bundle, v)
for k, v in updates.string_sets.items():
op(self._string_sets[k], bundle, v)
for k, v in updates.bounded_tries.items():
op(self._bounded_tries[k], bundle, v)
for k, v in updates.histograms.items():
op(self._histograms[k], bundle, v)
def commit_logical(self, bundle, updates):
op = lambda obj, bundle, update: obj.commit_logical(bundle, update)
self._apply_operation(bundle, updates, op)
def commit_physical(self, bundle, updates):
op = lambda obj, bundle, update: obj.commit_physical(bundle, update)
self._apply_operation(bundle, updates, op)
def update_physical(self, bundle, updates):
op = lambda obj, bundle, update: obj.update_physical(bundle, update)
self._apply_operation(bundle, updates, op)
def query(self, filter=None):
counters = [
MetricResult(
MetricKey(k.step, k.metric),
v.extract_committed(),
v.extract_latest_attempted()) for k, v in self._counters.items()
if self.matches(filter, k)
]
distributions = [
MetricResult(
MetricKey(k.step, k.metric),
v.extract_committed(),
v.extract_latest_attempted())
for k, v in self._distributions.items() if self.matches(filter, k)
]
gauges = [
MetricResult(
MetricKey(k.step, k.metric),
v.extract_committed(),
v.extract_latest_attempted()) for k, v in self._gauges.items()
if self.matches(filter, k)
]
string_sets = [
MetricResult(
MetricKey(k.step, k.metric),
v.extract_committed(),
v.extract_latest_attempted()) for k, v in self._string_sets.items()
if self.matches(filter, k)
]
bounded_tries = [
MetricResult(
MetricKey(k.step, k.metric),
v.extract_committed(),
v.extract_latest_attempted())
for k, v in self._bounded_tries.items() if self.matches(filter, k)
]
histograms = [
MetricResult(
MetricKey(k.step, k.metric),
v.extract_committed(),
v.extract_latest_attempted()) for k, v in self._histograms.items()
if self.matches(filter, k)
]
return {
self.COUNTERS: counters,
self.DISTRIBUTIONS: distributions,
self.GAUGES: gauges,
self.STRINGSETS: string_sets,
self.BOUNDED_TRIES: bounded_tries,
self.HISTOGRAMS: histograms,
}
class DirectMetric(object):
""" Keeps a consistent state for a single metric.
It keeps track of the metric's physical and logical updates.
It's thread safe.
"""
def __init__(self, aggregator):
self.aggregator = aggregator
self._attempted_lock = threading.Lock()
self.finished_attempted = aggregator.identity_element()
self.inflight_attempted = {}
self._committed_lock = threading.Lock()
self.finished_committed = aggregator.identity_element()
def commit_logical(self, bundle, update):
with self._committed_lock:
self.finished_committed = self.aggregator.combine(
update, self.finished_committed)
def commit_physical(self, bundle, update):
with self._attempted_lock:
self.inflight_attempted[bundle] = update
self.finished_attempted = self.aggregator.combine(
update, self.finished_attempted)
del self.inflight_attempted[bundle]
def update_physical(self, bundle, update):
self.inflight_attempted[bundle] = update
def extract_committed(self):
return self.aggregator.result(self.finished_committed)
def extract_latest_attempted(self):
res = self.finished_attempted
for _, u in self.inflight_attempted.items():
res = self.aggregator.combine(res, u)
return self.aggregator.result(res)