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
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use dynamo_kv_router::indexer::LocalKvIndexer;
use dynamo_kv_router::protocols::*;
use crate::kv_router::metrics::kv_publisher_metrics;
use super::DEFAULT_MAX_BATCH_BLOCKS;
use super::batching::BatchingState;
use super::dedup::{EventDedupFilter, EventDedupPolicy};
use super::sinks::{RouterEventBatchSink, emit};
pub(super) async fn run_event_processor_loop<P: RouterEventBatchSink + 'static>(
publisher: P,
worker_id: u64,
cancellation_token: CancellationToken,
mut rx: mpsc::UnboundedReceiver<super::PublisherInput>,
local_indexer: Option<Arc<LocalKvIndexer>>,
timeout_ms: Option<u64>,
max_batch_blocks: usize,
) {
let mut batching_state = BatchingState::new(max_batch_blocks);
let mut dedup = EventDedupFilter::new();
let mut last_raw_input_id: Option<u64> = None;
loop {
tokio::select! {
_ = cancellation_token.cancelled() => {
tracing::info!("KV Event source received cancellation signal");
let mut output = Vec::new();
batching_state.flush(&local_indexer, worker_id, &mut dedup, &mut output).await;
publish_output(&publisher, worker_id, &output).await;
break;
}
event_batch = rx.recv() => {
let Some(event_batch) = event_batch else {
tracing::debug!("Event processor channel closed.");
let mut output = Vec::new();
batching_state.flush(&local_indexer, worker_id, &mut dedup, &mut output).await;
publish_output(&publisher, worker_id, &output).await;
break;
};
let event_batch = match event_batch {
super::PublisherInput::Events(events) => events,
super::PublisherInput::Barrier(done) => {
let mut output = Vec::new();
batching_state.flush(&local_indexer, worker_id, &mut dedup, &mut output).await;
// Recovery applies events like live ingestion, then waits for the index to finish them.
let result = tokio::select! {
biased;
_ = cancellation_token.cancelled() => Err(anyhow::anyhow!("KV event publisher cancelled during recovery")),
result = flush_local_indexer(&local_indexer) => result,
};
if let Err(error) = &result {
if cancellation_token.is_cancelled() {
tracing::debug!(%error, "KV recovery barrier cancelled");
} else {
tracing::error!(%error, "KV recovery barrier failed; stopping KV event publisher");
}
}
let has_failed = result.is_err();
let _ = done.send(result);
if has_failed {
// Keep the shared token alive so the listener can report the failure.
break;
}
publish_output(&publisher, worker_id, &output).await;
continue;
}
};
let mut output = Vec::new();
// Process the complete source list before returning to `select!` so
// another channel item, the timeout, or cancellation cannot split it.
'event_batch: for placement_event in event_batch {
let raw_event_id = placement_event.event.event_id;
if let Some(last_id) = last_raw_input_id
&& raw_event_id > last_id + 1
{
let gap = raw_event_id - last_id - 1;
tracing::warn!(
worker_id,
last_raw_input_id = last_id,
raw_event_id,
gap,
"Input event gap detected: raw events dropped before batching"
);
if let Some(metrics) = kv_publisher_metrics() {
metrics.increment_engines_dropped_events(gap);
} else {
tracing::warn!(
worker_id,
gap,
"Failed to record dropped events metric: metrics not initialized"
);
}
}
last_raw_input_id = Some(raw_event_id);
let storage_tier = placement_event.placement.tier;
let residency_domain = placement_event.placement.residency_domain;
tracing::trace!(
"Event processor for worker_id {} processing event: {:?}",
worker_id,
placement_event.event.data
);
match &placement_event.event.data {
KvCacheEventData::Removed(_) | KvCacheEventData::Stored(_) => {
batching_state
.push(
placement_event,
&local_indexer,
worker_id,
&mut dedup,
&mut output,
)
.await;
}
KvCacheEventData::Cleared => {
batching_state.flush(&local_indexer, worker_id, &mut dedup, &mut output).await;
let event = placement_event.event;
dedup.clear_rank_domain(
event.dp_rank,
residency_domain,
EventDedupPolicy::RefCounted,
);
let applied = emit(
&local_indexer,
worker_id,
storage_tier,
residency_domain,
KvCacheEvent {
event_id: batching_state.next_publish_id,
data: KvCacheEventData::Cleared,
dp_rank: event.dp_rank,
},
None,
&mut output,
)
.await;
if !applied {
output.pop();
tracing::error!(
worker_id,
dp_rank = event.dp_rank,
?residency_domain,
"Fencing KV event publisher after local reset failure"
);
// NOTE: This token owns the publisher's discovery and recovery
// advertisement too. Once the local reset barrier fails, withdrawing
// the complete source is the only safe way to avoid advertising a
// cursor whose local snapshot has diverged.
cancellation_token.cancel();
break 'event_batch;
}
batching_state.next_publish_id = batching_state
.next_publish_id
.checked_add(1)
.expect("KV event publisher outbound cursor exhausted");
}
}
}
// Without a timeout, flush the compatible tail at the native-list
// boundary. With a timeout, retain it for possible cross-list batching.
if batching_state.has_pending()
&& match timeout_ms {
None => true,
Some(ms) => batching_state.is_timeout_elapsed(ms),
}
{
batching_state.flush(&local_indexer, worker_id, &mut dedup, &mut output).await;
}
publish_output(&publisher, worker_id, &output).await;
}
_ = tokio::time::sleep(
timeout_ms
.map(|ms| batching_state.remaining_timeout(ms))
.unwrap_or(Duration::from_secs(3600))
), if timeout_ms.is_some() && batching_state.has_pending() => {
let mut output = Vec::new();
batching_state.flush(&local_indexer, worker_id, &mut dedup, &mut output).await;
publish_output(&publisher, worker_id, &output).await;
}
}
}
}
async fn flush_local_indexer(local_indexer: &Option<Arc<LocalKvIndexer>>) -> anyhow::Result<()> {
let indexer = local_indexer
.as_ref()
.ok_or_else(|| anyhow::anyhow!("KV recovery requires a local indexer"))?;
Ok(indexer.flush_and_wait().await?)
}
async fn publish_output<P: RouterEventBatchSink>(
publisher: &P,
worker_id: u64,
output: &[RouterEvent],
) {
if output.is_empty() {
return;
}
if let Err(e) = publisher.publish_events(output).await {
tracing::error!(
worker_id,
attempted_event_count = output.len(),
error = %e,
"One or more KV event publishes failed"
);
}
}
pub(super) async fn start_event_processor<P: RouterEventBatchSink + 'static>(
publisher: P,
worker_id: u64,
cancellation_token: CancellationToken,
rx: mpsc::UnboundedReceiver<super::PublisherInput>,
local_indexer: Option<Arc<LocalKvIndexer>>,
batching_timeout_ms: Option<u64>,
) {
run_event_processor_loop(
publisher,
worker_id,
cancellation_token,
rx,
local_indexer,
batching_timeout_ms,
DEFAULT_MAX_BATCH_BLOCKS,
)
.await
}