Skip to content

Commit c61376e

Browse files
committed
feat(server): count supervisor session redirects and off-ring admissions
Consistent-hash placement only logged its redirects, so operators had no way to see how often replicas redirect supervisors or whether supervisors end up where the ring places them. The gateway now counts each SessionRedirect it sends, labeled by the step that sent it: connect when it answers a connecting supervisor, or shutdown once a stopping replica has queued a handoff on an open session. It also counts redirected supervisors it admits although its own ring names another owner. That covers rings that disagree and failed redirect dials that fall back to the gateway address, plus handoffs from a stopping replica that arrive before the receiver's ring drops it. Both counters start at zero and carry no sandbox or replica IDs. The gateway metrics page and the debug skill list them. Signed-off-by: Emilien Macchi <emacchi@redhat.com>
1 parent 55bf786 commit c61376e

5 files changed

Lines changed: 376 additions & 19 deletions

File tree

‎crates/openshell-server/src/gateway_metrics.rs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ pub const RELAY_EXPIRED_TOTAL: &str = "openshell_server_relay_expired_total";
3535
pub const ROUTED_REQUEST_ATTEMPTS_TOTAL: &str = "openshell_server_routed_request_attempts_total";
3636
pub const MUTATION_LOCK_TIMEOUTS_TOTAL: &str = "openshell_server_mutation_lock_timeouts_total";
3737
pub const MUTATION_LOCK_ERRORS_TOTAL: &str = "openshell_server_mutation_lock_errors_total";
38+
pub const SUPERVISOR_SESSION_REDIRECTS_TOTAL: &str =
39+
"openshell_server_supervisor_session_redirects_total";
40+
pub const SUPERVISOR_SESSION_OFF_RING_ADMISSIONS_TOTAL: &str =
41+
"openshell_server_supervisor_session_off_ring_admissions_total";
3842
// Histograms (explicit buckets, see BUCKETED_HISTOGRAMS)
3943
pub const RELAY_CLAIM_DURATION_SECONDS: &str = "openshell_server_relay_claim_duration_seconds";
4044
pub const PEER_REQUEST_DURATION_SECONDS: &str = "openshell_server_peer_request_duration_seconds";
@@ -124,6 +128,27 @@ impl RelayRejection {
124128
}
125129
}
126130

131+
/// Event that sent a supervisor session redirect: a supervisor connecting, or this replica
132+
/// shutting down. A stopping replica's connect-time redirects count as `Connect`.
133+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
134+
pub enum RedirectReason {
135+
/// `ConnectSupervisor` answered the hello with a redirect instead of accepting the session.
136+
Connect,
137+
/// A stopping replica queued a redirect on an established session.
138+
Shutdown,
139+
}
140+
141+
impl RedirectReason {
142+
pub const ALL: [Self; 2] = [Self::Connect, Self::Shutdown];
143+
144+
pub const fn label(self) -> &'static str {
145+
match self {
146+
Self::Connect => "connect",
147+
Self::Shutdown => "shutdown",
148+
}
149+
}
150+
}
151+
127152
/// Routed operation. For a peer request, the owning replica records the matching gRPC method in
128153
/// `openshell_server_grpc_requests_total`, for example `PeerRelay` for `relay`.
129154
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -284,6 +309,16 @@ pub fn describe_and_initialize(relay: RelayCapacity, mutation_lock_connections:
284309
Unit::Count,
285310
"Pending relay channels dropped because the supervisor did not connect back in time."
286311
);
312+
describe_counter!(
313+
SUPERVISOR_SESSION_REDIRECTS_TOTAL,
314+
Unit::Count,
315+
"Supervisor sessions redirected to the ring-preferred replica, counted when the redirect is sent at connect or queued at shutdown."
316+
);
317+
describe_counter!(
318+
SUPERVISOR_SESSION_OFF_RING_ADMISSIONS_TOTAL,
319+
Unit::Count,
320+
"Redirected supervisor sessions admitted although this replica's ring names another owner for the sandbox."
321+
);
287322
describe_counter!(
288323
MUTATION_LOCK_TIMEOUTS_TOTAL,
289324
Unit::Count,
@@ -346,6 +381,10 @@ pub fn describe_and_initialize(relay: RelayCapacity, mutation_lock_connections:
346381
counter!(RELAY_REJECTED_TOTAL, LABEL_REASON => reason.label()).increment(0);
347382
}
348383
counter!(RELAY_EXPIRED_TOTAL).increment(0);
384+
for reason in RedirectReason::ALL {
385+
counter!(SUPERVISOR_SESSION_REDIRECTS_TOTAL, LABEL_REASON => reason.label()).increment(0);
386+
}
387+
counter!(SUPERVISOR_SESSION_OFF_RING_ADMISSIONS_TOTAL).increment(0);
349388
for scope in LockScope::ALL {
350389
counter!(MUTATION_LOCK_TIMEOUTS_TOTAL, LABEL_SCOPE => scope.label()).increment(0);
351390
counter!(MUTATION_LOCK_ERRORS_TOTAL, LABEL_SCOPE => scope.label()).increment(0);
@@ -425,6 +464,18 @@ pub fn record_relay_rejected(reason: RelayRejection) {
425464
counter!(RELAY_REJECTED_TOTAL, LABEL_REASON => reason.label()).increment(1);
426465
}
427466

467+
/// One supervisor session redirect, labeled by the step that sent it: `Connect` once the
468+
/// redirect is on the response stream, `Shutdown` once it is queued on the session.
469+
pub fn record_session_redirect(reason: RedirectReason) {
470+
counter!(SUPERVISOR_SESSION_REDIRECTS_TOTAL, LABEL_REASON => reason.label()).increment(1);
471+
}
472+
473+
/// A redirected supervisor session was admitted although this replica's ring names another
474+
/// owner for its sandbox. Recorded only after the session is accepted.
475+
pub fn record_off_ring_admission() {
476+
counter!(SUPERVISOR_SESSION_OFF_RING_ADMISSIONS_TOTAL).increment(1);
477+
}
478+
428479
/// `count` pending relays were dropped unclaimed (late claim or reaper).
429480
pub fn record_relay_expired(count: usize) {
430481
if count > 0 {
@@ -653,6 +704,18 @@ mod tests {
653704
"openshell_server_mutation_lock_errors_total{scope=\"sandbox\"}",
654705
0,
655706
),
707+
(
708+
"openshell_server_supervisor_session_redirects_total{reason=\"connect\"}",
709+
0,
710+
),
711+
(
712+
"openshell_server_supervisor_session_redirects_total{reason=\"shutdown\"}",
713+
0,
714+
),
715+
(
716+
"openshell_server_supervisor_session_off_ring_admissions_total",
717+
0,
718+
),
656719
] {
657720
assert_eq!(metrics.value(series), Some(expected), "{series}");
658721
}

‎crates/openshell-server/src/grpc/mod.rs‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,11 +1050,34 @@ pub mod test_support {
10501050
sandbox_id: &str,
10511051
features: StreamFeatures,
10521052
connection_epoch: u64,
1053+
) -> Result<SupervisorStreamHarness, tonic::Status> {
1054+
connect_supervisor_stream_with_hello(
1055+
state,
1056+
SupervisorHello {
1057+
sandbox_id: sandbox_id.into(),
1058+
instance_id: "instance".into(),
1059+
supports_config_push: features == StreamFeatures::Apply,
1060+
connection_epoch,
1061+
supports_provider_readiness: false,
1062+
redirected: false,
1063+
supports_session_redirect: false,
1064+
},
1065+
)
1066+
.await
1067+
}
1068+
1069+
/// Serve `state` on loopback and open a supervisor stream that sends
1070+
/// `hello` as-is, authenticated as the sandbox the hello names. Returns
1071+
/// the gRPC status when the gateway rejects the stream before accepting
1072+
/// it.
1073+
pub async fn connect_supervisor_stream_with_hello(
1074+
state: &Arc<ServerState>,
1075+
hello: SupervisorHello,
10531076
) -> Result<SupervisorStreamHarness, tonic::Status> {
10541077
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
10551078
let address = listener.local_addr().unwrap();
10561079
let principal = Principal::Sandbox(SandboxPrincipal {
1057-
sandbox_id: sandbox_id.to_string(),
1080+
sandbox_id: hello.sandbox_id.clone(),
10581081
source: SandboxIdentitySource::BootstrapJwt {
10591082
issuer: "openshell-gateway:test".to_string(),
10601083
},
@@ -1080,15 +1103,7 @@ pub mod test_support {
10801103
let (outbound, rx) = mpsc::channel(4);
10811104
outbound
10821105
.send(SupervisorMessage {
1083-
payload: Some(supervisor_message::Payload::Hello(SupervisorHello {
1084-
sandbox_id: sandbox_id.into(),
1085-
instance_id: "instance".into(),
1086-
supports_config_push: features == StreamFeatures::Apply,
1087-
connection_epoch,
1088-
supports_provider_readiness: false,
1089-
redirected: false,
1090-
supports_session_redirect: false,
1091-
})),
1106+
payload: Some(supervisor_message::Payload::Hello(hello)),
10921107
})
10931108
.await
10941109
.unwrap();
@@ -1238,6 +1253,26 @@ pub mod test_support {
12381253
)))
12391254
}
12401255

1256+
/// Build a test state on a store the caller provides, such as a disposable
1257+
/// `PostgreSQL` schema. Like the states above, it sets up the default
1258+
/// workspace, test compute runtime, provider sources, and credential
1259+
/// drivers, but configures no database URL and seeds no provider profiles.
1260+
pub async fn test_server_state_for_store(store: Store) -> Arc<ServerState> {
1261+
let store = Arc::new(store);
1262+
crate::ensure_default_workspace(&store).await.unwrap();
1263+
let compute = new_test_runtime(store.clone()).await;
1264+
with_test_provider_profile_sources(Arc::new(ServerState::new(
1265+
Config::new(None).with_credential_drivers(["test-static"]),
1266+
store,
1267+
compute,
1268+
SandboxIndex::new(),
1269+
SandboxWatchBus::new(),
1270+
TracingLogBus::new(),
1271+
Arc::new(SupervisorSessionRegistry::new()),
1272+
None,
1273+
)))
1274+
}
1275+
12411276
/// Build a test state whose compute driver fails the requested number of
12421277
/// workspace cleanup calls before succeeding.
12431278
pub async fn test_server_state_with_workspace_cleanup_failures(

0 commit comments

Comments
 (0)