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
//! Forwarding a legacy route's client frames without parking the connection
//! reader on request credit; the queue itself is described in
//! `forwarding_waiting_requests`.
use super::*;
use crate::control::upgrade_in_place::{Admission, AdmissionGate};
use crate::forwarding::{ChannelFlowClosed, LegacyArrival};
// One receiver and one pinned wait survive all the drain's credit waits.
// Watch the change notification rather than borrowing the phase in Open.
async fn wait_for_drain_fence(
gate: &AdmissionGate,
mut changes: tokio::sync::watch::Receiver<Admission>,
) -> tokio::sync::watch::Receiver<Admission> {
while !gate.phase().fenced() && changes.changed().await.is_ok() {}
changes
}
impl ForwardBackend {
/// Forward, queue, or (only when the route's queue is full) wait to queue
/// a client frame on a route opened without `busy_refusals`.
pub(super) async fn offer_to_legacy_route(
&self,
frame: Frame,
route: Arc<RouteBinding>,
) -> Result<(), RouterError> {
let mut frame = frame;
loop {
match route.flow.offer_legacy(frame) {
LegacyArrival::Forward { frame, credited } => {
return self.forward_to_module(frame, &route, credited).await;
}
LegacyArrival::Queued { start_forwarding } => {
if start_forwarding {
let task = crate::forwarding::CutoverTask::new(self.clone(), |backend| {
&backend.forwarding.upgrade_refusals.tasks
});
tokio::spawn(async move {
task.owner().forward_waiting(route).await;
});
}
return Ok(());
}
LegacyArrival::Full(refused) => {
// This offer already has to wait for room. Subscribe only
// here so immediately admitted requests pay nothing for it.
let mut admission = self.forwarding.upgrade_admission.subscribe();
frame = refused;
let wait = self.forwarding.counters_ref().observe_reader_wait(
|| (route.module_id.clone(), principal_label(&route.principal)),
"legacy_credit",
route.flow.waiting_room(),
);
tokio::select! {
_ = wait => {},
_ = crate::control::upgrade_in_place::wait_for_fence(&mut admission) => {
if frame.header.ty != FrameType::Request { return Ok(()); }
return Err(RouterError::DaemonUpgrade { channel: frame.header.channel, epoch: frame.header.epoch, corr: frame.header.corr });
},
}
}
LegacyArrival::Closed(frame) => {
let header = &frame.header;
return Err(self.legacy_credit_error(
&route,
header.channel,
header.epoch,
header.corr,
ChannelFlowClosed,
)?);
}
}
}
}
/// Move a route's waiting frames to the module in order, each request once
/// it has credit. A frame that cannot go gets the ERROR the connection
/// reader would have sent for it. Every wait here also ends when the
/// route's credit window closes, as it does when the route is released or
/// its client connection is cleaned up: nothing then reaches the module,
/// and a client that stopped reading cannot hold this task.
async fn forward_waiting(&self, route: Arc<RouteBinding>) {
let gate = &self.forwarding.upgrade_admission;
let fence = wait_for_drain_fence(gate, gate.subscribe());
tokio::pin!(fence);
let mut observed_fence = None;
while let Some(frame) = route.flow.next_waiting() {
let (ty, channel, epoch, corr) = (
frame.header.ty,
frame.header.channel,
frame.header.epoch,
frame.header.corr,
);
let forward = async {
let credited = ty == FrameType::Request;
if credited {
let subscription = frame.header.flags.is_subscription();
let acquired = loop {
if let Some(receiver) = observed_fence.take() {
if gate.phase().fenced() {
observed_fence = Some(receiver);
return Err(RouterError::DaemonUpgrade {
channel,
epoch,
corr,
});
}
fence.set(wait_for_drain_fence(gate, receiver));
}
tokio::select! {
biased;
result = route.flow.acquire_tagged(corr, subscription) => break result,
receiver = &mut fence => observed_fence = Some(receiver),
}
};
if let Err(closed) = acquired {
return Err(self.legacy_credit_error(&route, channel, epoch, corr, closed)?);
}
}
self.forward_to_module(frame, &route, credited).await
};
let error = tokio::select! {
biased;
() = route.flow.closed() => {
if ty != FrameType::Request {
continue;
}
if gate.phase().fenced() {
RouterError::DaemonUpgrade { channel, epoch, corr }
} else { match self.legacy_credit_error(&route, channel, epoch, corr, ChannelFlowClosed) {
Ok(error) | Err(error) => error,
} }
}
result = forward => match result {
Ok(()) => continue,
Err(error) => error,
},
};
let Some(mut error_frame) = error.to_error_frame() else {
continue;
};
if matches!(error, RouterError::DaemonUpgrade { .. }) {
match self
.forwarding
.upgrade_refusals
.track_fence_refusal(error_frame)
{
Ok(refusal) => {
refusal.send(&route.client_sink);
continue;
}
Err(plain) => error_frame = plain,
}
}
tokio::select! {
biased;
() = route.flow.closed() => {
// Best effort only once the route is gone: never wait.
let _ = route.client_sink.try_send(error_frame);
}
_ = route.client_sink.send(error_frame.clone()) => {}
}
}
}
/// The error for a request whose route closed its credit window before
/// the request could be forwarded.
fn legacy_credit_error(
&self,
route: &RouteBinding,
channel: u16,
epoch: u32,
corr: u64,
err: ChannelFlowClosed,
) -> Result<RouterError, RouterError> {
self.forwarding.warn_frame_drop(
route.client_connection_id,
channel,
epoch,
"client_to_module",
"endpoint_gone",
);
// `module_reloading` here is answered BEFORE the frame is forwarded,
// so the module never sees the request and callers may re-dispatch it
// after reopening the route. That is a wire guarantee documented on
// `error_codes::MODULE_RELOADING`; do not emit this code for a request
// that already reached the module's queue.
if self
.forwarding
.endpoint_is_draining(route.module_endpoint)
.map_err(RouterError::Forwarding)?
{
return Ok(RouterError::route_error_with_epoch(
channel,
epoch,
corr,
"module_reloading",
format!("module endpoint for route channel {channel} is reloading"),
));
}
Ok(RouterError::backend_with_epoch(
channel,
epoch,
corr,
format!("{err} for route channel {channel}"),
))
}
}