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
use std::any::Any;
use std::collections::HashMap;
use std::rc::Rc;
use differential_dataflow::operators::arrange::TraceAgent;
use differential_dataflow::trace::implementations::ord::{OrdKeySpine, OrdValSpine};
use differential_dataflow::trace::TraceReader;
use ore::metric;
use ore::metrics::{
CounterVec, CounterVecExt, DeleteOnDropCounter, DeleteOnDropGauge, GaugeVecExt,
MetricsRegistry, UIntGaugeVec,
};
use timely::progress::frontier::{Antichain, AntichainRef};
use dataflow_types::DataflowError;
use expr::GlobalId;
use repr::{Diff, Row, Timestamp};
pub type RowSpine<K, V, T, R, O = usize> = OrdValSpine<K, V, T, R, O>;
pub type ErrSpine<K, T, R, O = usize> = OrdKeySpine<K, T, R, O>;
pub type TraceRowHandle<K, V, T, R> = TraceAgent<RowSpine<K, V, T, R>>;
pub type TraceErrHandle<K, T, R> = TraceAgent<ErrSpine<K, T, R>>;
pub type KeysValsHandle = TraceRowHandle<Row, Row, Timestamp, Diff>;
pub type ErrsHandle = TraceErrHandle<DataflowError, Timestamp, Diff>;
use prometheus::core::{AtomicF64, AtomicU64};
use std::time::Instant;
#[derive(Clone, Debug)]
pub struct TraceMetrics {
total_maintenance_time: CounterVec,
doing_maintenance: UIntGaugeVec,
}
impl TraceMetrics {
pub(crate) fn register_with(registry: &MetricsRegistry) -> Self {
Self {
total_maintenance_time: registry.register(metric!(
name: "mz_arrangement_maintenance_seconds_total",
help: "The total time spent maintaining an arrangement",
var_labels: ["worker_id", "arrangement_id"],
)),
doing_maintenance: registry.register(metric!(
name: "mz_arrangement_maintenance_active_info",
help: "Whether or not maintenance is currently occurring",
var_labels: ["worker_id"],
)),
}
}
fn maintenance_time_metric(
&self,
worker_id: usize,
id: GlobalId,
) -> DeleteOnDropCounter<'static, AtomicF64, Vec<String>> {
self.total_maintenance_time
.get_delete_on_drop_counter(vec![worker_id.to_string(), id.to_string()])
}
fn maintenance_flag_metric(
&self,
worker_id: usize,
) -> DeleteOnDropGauge<'static, AtomicU64, Vec<String>> {
self.doing_maintenance
.get_delete_on_drop_gauge(vec![worker_id.to_string()])
}
}
struct MaintenanceMetrics {
total_maintenance_time: DeleteOnDropCounter<'static, AtomicF64, Vec<String>>,
}
impl MaintenanceMetrics {
fn new(metrics: &TraceMetrics, worker_id: usize, arrangement_id: GlobalId) -> Self {
MaintenanceMetrics {
total_maintenance_time: metrics.maintenance_time_metric(worker_id, arrangement_id),
}
}
}
pub struct TraceManager {
pub traces: HashMap<GlobalId, TraceBundle>,
worker_id: usize,
maintenance_metrics: HashMap<GlobalId, MaintenanceMetrics>,
doing_maintenance: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
metrics: TraceMetrics,
}
impl TraceManager {
pub fn new(metrics: TraceMetrics, worker_id: usize) -> Self {
let doing_maintenance = metrics.maintenance_flag_metric(worker_id);
TraceManager {
traces: HashMap::new(),
worker_id,
metrics,
maintenance_metrics: HashMap::new(),
doing_maintenance,
}
}
pub fn maintenance(&mut self) {
let mut antichain = Antichain::new();
for (arrangement_id, bundle) in self.traces.iter_mut() {
let maintenance_metrics = self.maintenance_metrics.get_mut(arrangement_id).unwrap();
self.doing_maintenance.set(1);
let now = Instant::now();
bundle.oks.read_upper(&mut antichain);
bundle.oks.set_physical_compaction(antichain.borrow());
bundle.errs.read_upper(&mut antichain);
bundle.errs.set_physical_compaction(antichain.borrow());
maintenance_metrics
.total_maintenance_time
.inc_by(now.elapsed().as_secs_f64());
self.doing_maintenance.set(0);
}
}
pub fn allow_compaction(&mut self, id: GlobalId, frontier: AntichainRef<Timestamp>) {
if let Some(bundle) = self.traces.get_mut(&id) {
bundle.oks.set_logical_compaction(frontier);
bundle.errs.set_logical_compaction(frontier);
}
}
pub fn get(&self, id: &GlobalId) -> Option<&TraceBundle> {
self.traces.get(&id)
}
pub fn get_mut(&mut self, id: &GlobalId) -> Option<&mut TraceBundle> {
self.traces.get_mut(&id)
}
pub fn set(&mut self, id: GlobalId, trace: TraceBundle) {
self.maintenance_metrics.insert(
id,
MaintenanceMetrics::new(&self.metrics, self.worker_id, id),
);
self.traces.insert(id, trace);
}
pub fn del_trace(&mut self, id: &GlobalId) -> bool {
self.maintenance_metrics.remove(id);
self.traces.remove(&id).is_some()
}
pub fn del_all_traces(&mut self) {
self.maintenance_metrics.clear();
self.traces.clear();
}
}
#[derive(Clone)]
pub struct TraceBundle {
oks: KeysValsHandle,
errs: ErrsHandle,
to_drop: Option<Rc<dyn Any>>,
}
impl TraceBundle {
pub fn new(oks: KeysValsHandle, errs: ErrsHandle) -> TraceBundle {
TraceBundle {
oks,
errs,
to_drop: None,
}
}
pub fn with_drop<T>(self, to_drop: T) -> TraceBundle
where
T: 'static,
{
TraceBundle {
to_drop: Some(Rc::new(Box::new(to_drop))),
..self
}
}
pub fn oks_mut(&mut self) -> &mut KeysValsHandle {
&mut self.oks
}
pub fn errs_mut(&mut self) -> &mut ErrsHandle {
&mut self.errs
}
pub fn to_drop(&self) -> &Option<Rc<dyn Any>> {
&self.to_drop
}
}