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
use {
super::{retry::retry_etcd_legacy, Revision},
crate::retry::is_transient,
etcd_client::{EventType, WatchClient, WatchFilterType, WatchOptions},
retry::delay::Exponential,
serde::de::DeserializeOwned,
tokio::sync::{broadcast, mpsc},
tokio_stream::StreamExt,
tracing::{error, info, warn},
};
///
/// Custom types for watch events.
///
/// Unwrap the etcd watch event to a more user-friendly event.
///
pub enum WatchEvent<V> {
Put {
key: Vec<u8>,
value: V,
revision: Revision,
},
Delete {
key: Vec<u8>,
prev_value: Option<V>,
revision: Revision,
},
}
///
/// Extension trait for [`WatchClient`].
///
/// This trait provides utility methods for working with [`WatchClient`].
///
/// This extension trait provides rust channel of watch stream and more reliability in case of transient errors.
///
/// On transient errors, the watch stream will be retried and resume where you left off.
///
#[async_trait::async_trait]
pub trait WatchClientExt {
fn get_watch_client(&self) -> WatchClient;
///
/// Creates a channel that watches for changes to a key in etcd.
///
/// The channel will send a [`WatchEvent`] for each change to the key.
/// The channel will be retried on transient errors.
///
/// The channel will be closed if the watch is cancelled or if the stream is closed.
///
/// The watch expect value to be JSON encoded.
fn json_watch_channel<V>(
&self,
key: impl Into<Vec<u8>>,
watch_options: Option<WatchOptions>,
) -> mpsc::Receiver<WatchEvent<V>>
where
V: DeserializeOwned + Send + 'static,
{
let wc = self.get_watch_client();
let (tx, rx) = tokio::sync::mpsc::channel(10);
let key: Vec<u8> = key.into();
tokio::spawn(async move {
let wopts_prototype = watch_options.unwrap_or_default().with_prev_key();
let mut last_revision = None; // 0 = latest revision
'outer: loop {
let mut wopts = wopts_prototype.clone();
if let Some(rev) = last_revision {
wopts = wopts.with_start_revision(rev);
}
let wc2 = wc.clone();
let key2 = key.clone();
let retry_strategy = Exponential::from_millis_with_factor(10, 10.0).take(3);
let (mut watcher, mut stream) = retry_etcd_legacy(retry_strategy, move || {
let mut wc = wc2.clone();
let key = key2.clone();
let wopts = wopts.clone();
async move { wc.watch(key.clone(), Some(wopts)).await }
})
.await
.expect("watch retry failed");
'inner: while let Some(watch_resp) = stream.next().await {
match watch_resp {
Ok(watch_resp) => {
if watch_resp.canceled() {
// This is probably because the compaction_revision < initial revision
error!("watch cancelled: {watch_resp:?}");
break 'outer;
}
for event in watch_resp.events() {
let watch_event = match event.event_type() {
EventType::Put => {
let kv = event.kv().expect("put event with no kv");
let key = Vec::from(kv.key());
let value = serde_json::from_slice::<V>(kv.value())
.expect("failed to deserialize controller state");
last_revision.replace(kv.mod_revision());
WatchEvent::Put {
key,
value,
revision: kv.mod_revision(),
}
}
EventType::Delete => {
let kv = event.kv().expect("delete event with no kv");
let prev_value = event
.prev_kv()
.map(|prev_kv| prev_kv.value())
.map(|prev_v| serde_json::from_slice::<V>(prev_v))
.transpose()
.expect("failed to deserialize prev controller state");
let key = Vec::from(kv.key());
last_revision.replace(kv.mod_revision());
WatchEvent::Delete {
key,
prev_value,
revision: kv.mod_revision(),
}
}
};
if tx.send(watch_event).await.is_err() {
warn!("closed watch event receiver");
break 'outer;
}
}
}
Err(e) => {
error!("watch stream error: {:?}", e);
break 'inner;
}
}
}
let _ = watcher.cancel().await;
}
});
rx
}
fn json_put_watch_channel<T>(
&self,
key: impl Into<Vec<u8>>,
watch_options: Option<WatchOptions>,
) -> mpsc::Receiver<(Revision, T)>
where
T: DeserializeOwned + Send + 'static,
{
let wc = self.get_watch_client();
let (tx, rx) = tokio::sync::mpsc::channel(10);
let key: Vec<u8> = key.into();
tokio::spawn(async move {
let mut last_revision = None; // 0 = latest revision
let wopts_prototype = watch_options
.unwrap_or_default()
.with_filters(vec![WatchFilterType::NoDelete]);
'outer: loop {
let mut wopts = wopts_prototype.clone();
if let Some(rev) = last_revision {
wopts = wopts.with_start_revision(rev);
}
let retry_strategy = Exponential::from_millis_with_factor(10, 10.0).take(3);
let wc2 = wc.clone();
let key2 = key.clone();
let (mut watcher, mut stream) = retry_etcd_legacy(retry_strategy, move || {
let mut wc = wc2.clone();
let key = key2.clone();
let wopts = wopts.clone();
async move { wc.watch(key.clone(), Some(wopts)).await }
})
.await
.expect("watch retry failed");
'inner: while let Some(watch_resp) = stream.next().await {
match watch_resp {
Ok(watch_resp) => {
let max_kv = watch_resp
.events()
.iter()
.filter_map(|ev| ev.kv())
.max_by_key(|kv| kv.mod_revision());
if let Some(kv) = max_kv {
let revision = kv.mod_revision();
last_revision.replace(revision);
let state = serde_json::from_slice::<T>(kv.value())
.expect("failed to deserialize kv value");
if tx.send((revision, state)).await.is_err() {
let key_str = String::from_utf8(key).expect("key is not utf8");
warn!("json watch channel closed its receiving half for {key_str}");
break 'outer;
}
} else if watch_resp.canceled() {
// This is probably because the compaction_revision < initial revision
error!("watch cancelled: {watch_resp:?}");
break 'outer;
}
}
Err(e) => {
error!("watch stream error: {:?}", e);
break 'inner;
}
}
}
let _ = watcher.cancel().await;
}
});
rx
}
///
/// Creates a broadcast channel that watches for a lock key that gets deleted.
///
fn watch_lock_key_change(
&self,
key: impl Into<Vec<u8>>,
key_mod_revision: Revision,
) -> broadcast::Sender<Revision> {
let wc = self.get_watch_client();
let key: Vec<u8> = key.into();
let (tx, _) = broadcast::channel(1);
let tx2 = tx.clone();
tokio::spawn(async move {
let tx = tx2;
'outer: loop {
let key2 = key.clone();
let key = key.clone();
let wopts = WatchOptions::new().with_start_revision(key_mod_revision);
let retry_strategy = Exponential::from_millis_with_factor(10, 10.0).take(3);
let wc2 = wc.clone();
let (mut watcher, mut stream) = retry_etcd_legacy(retry_strategy, move || {
let mut wc = wc2.clone();
let key = key2.clone();
let wopts = wopts.clone();
async move { wc.watch(key.clone(), Some(wopts)).await }
})
.await
.expect("watch retry failed");
'inner: while let Some(result) = stream.next().await {
match result {
Ok(watch_resp) => {
if watch_resp.canceled() {
// This is probably because the compaction_revision < initial revision
error!("watch cancelled: {watch_resp:?}");
break 'inner;
}
for event in watch_resp.events() {
match event.event_type() {
EventType::Put => {
let kv = event.kv().expect("put event with no kv");
if kv.key() == key {
continue;
}
let revision = kv.mod_revision();
if revision <= key_mod_revision {
continue 'inner;
}
info!("watcher detected put event on key {key:?} with revision {revision} > {key_mod_revision}");
let _ = tx.send(revision);
let _ = watcher.cancel().await;
break 'outer;
}
EventType::Delete => {
let kv = event.kv().expect("delete event with no kv");
let revision = kv.mod_revision();
if revision < key_mod_revision {
continue;
}
if kv.key() == key {
let key_label = String::from_utf8_lossy(&key);
info!("watcher detected delete event on key {key_label:?} with revision {revision} >= {key_mod_revision}");
let _ = tx.send(revision);
let _ = watcher.cancel().await;
break 'outer;
}
}
}
}
}
Err(e) => {
if !is_transient(&e) {
tracing::error!("watch stream error: {e}");
break 'outer;
}
}
}
}
let _ = watcher.cancel().await;
}
});
tx
}
}
impl WatchClientExt for WatchClient {
fn get_watch_client(&self) -> WatchClient {
self.clone()
}
}