Skip to content

Commit 8c3231e

Browse files
committed
fix(server): load providers concurrently at startup
Signed-off-by: Eric Curtin <[email protected]>
1 parent 30c1f3b commit 8c3231e

2 files changed

Lines changed: 111 additions & 23 deletions

File tree

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

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,20 +2543,12 @@ async fn validate_provider_composition_for_existing_sandboxes(
25432543
current_base_policy_for_sandbox(state.store.as_ref(), &sandbox).await?;
25442544
// A provider deleted under a sandbox leaves a dangling reference. The
25452545
// sandbox already fails closed, so warn and validate the rest.
2546-
let mut records = Vec::with_capacity(provider_names.len());
2547-
let mut missing = Vec::new();
2548-
for name in provider_names {
2549-
match super::provider::load_provider_environment_record(
2550-
state.store.as_ref(),
2551-
&workspace,
2552-
name,
2553-
)
2554-
.await?
2555-
{
2556-
Some(record) => records.push(record),
2557-
None => missing.push(name.as_str()),
2558-
}
2559-
}
2546+
let (records, missing) = super::provider::load_existing_provider_environment_records(
2547+
state.store.as_ref(),
2548+
&workspace,
2549+
provider_names,
2550+
)
2551+
.await?;
25602552
if !missing.is_empty() {
25612553
warn!(
25622554
workspace = %workspace,
@@ -22626,14 +22618,69 @@ mod tests {
2262622618
async fn startup_preflight_still_rejects_invalid_policy_beside_dangling_reference() {
2262722619
let state = test_server_state().await;
2262822620
install_dangling_provider_sandbox(&state).await;
22629-
install_ambiguous_provider_binding(&state, "beside-dangling").await;
22621+
// Sorts after `sandbox-dangling`, so the dangling sandbox is checked first.
22622+
install_ambiguous_provider_binding(&state, "z-beside-dangling").await;
2263022623

2263122624
let error = validate_provider_composition_startup_preflight(&state)
2263222625
.await
2263322626
.expect_err("an invalid composed policy must stay fatal");
2263422627

2263522628
assert_eq!(error.code(), Code::FailedPrecondition);
22636-
assert!(error.message().contains("sandbox-beside-dangling"));
22629+
assert!(error.message().contains("sandbox-z-beside-dangling"));
22630+
}
22631+
22632+
#[tokio::test]
22633+
async fn startup_preflight_validates_providers_beside_dangling_reference_on_one_sandbox() {
22634+
let state = test_server_state().await;
22635+
install_ambiguous_provider_binding(&state, "mixed").await;
22636+
state
22637+
.store
22638+
.put_message(&test_sandbox(
22639+
"sandbox-mixed",
22640+
"sandbox-mixed",
22641+
test_policy_with_rule("base", "api.example.com"),
22642+
vec!["deleted-provider".to_string(), "provider-mixed".to_string()],
22643+
))
22644+
.await
22645+
.unwrap();
22646+
22647+
let error = validate_provider_composition_startup_preflight(&state)
22648+
.await
22649+
.expect_err("a dangling reference must not skip the sandbox's other providers");
22650+
22651+
assert_eq!(error.code(), Code::FailedPrecondition);
22652+
assert!(error.message().contains("sandbox-mixed"));
22653+
}
22654+
22655+
#[tokio::test]
22656+
async fn deleting_global_policy_tolerates_dangling_provider_reference() {
22657+
let state = test_server_state().await;
22658+
install_dangling_provider_sandbox(&state).await;
22659+
22660+
handle_update_config(
22661+
&state,
22662+
with_user(Request::new(UpdateConfigRequest {
22663+
global: true,
22664+
policy: Some(test_policy_with_rule("global", "global.example.com")),
22665+
..Default::default()
22666+
})),
22667+
)
22668+
.await
22669+
.expect("global policy should suppress provider composition");
22670+
handle_update_config(
22671+
&state,
22672+
with_user(Request::new(UpdateConfigRequest {
22673+
global: true,
22674+
setting_key: POLICY_SETTING_KEY.to_string(),
22675+
delete_setting: true,
22676+
..Default::default()
22677+
})),
22678+
)
22679+
.await
22680+
.expect("a dangling provider reference must not block global policy deletion");
22681+
22682+
let settings = load_global_settings(state.store.as_ref()).await.unwrap();
22683+
assert!(!settings.settings.contains_key(POLICY_SETTING_KEY));
2263722684
}
2263822685

2263922686
#[test]

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ pub(super) async fn resolve_provider_environment_with_credentials(
10281028
}
10291029

10301030
/// Loads one provider; `Ok(None)` means it does not exist.
1031-
pub(super) async fn load_provider_environment_record(
1031+
async fn load_provider_environment_record(
10321032
store: &Store,
10331033
workspace: &str,
10341034
name: &str,
@@ -1053,18 +1053,59 @@ pub(super) async fn load_provider_environment_record(
10531053
}))
10541054
}
10551055

1056+
/// Loads providers concurrently, in `provider_names` order; a missing
1057+
/// provider is `None`. Store errors short-circuit.
1058+
async fn load_optional_provider_environment_records(
1059+
store: &Store,
1060+
workspace: &str,
1061+
provider_names: &[String],
1062+
) -> Result<Vec<Option<ProviderEnvironmentRecord>>, Status> {
1063+
// Providers are independent, so load them concurrently.
1064+
futures::future::try_join_all(
1065+
provider_names
1066+
.iter()
1067+
.map(|name| load_provider_environment_record(store, workspace, name)),
1068+
)
1069+
.await
1070+
}
1071+
10561072
pub(super) async fn load_provider_environment_records(
10571073
store: &Store,
10581074
workspace: &str,
10591075
provider_names: &[String],
10601076
) -> Result<Vec<ProviderEnvironmentRecord>, Status> {
1061-
// Providers are independent, so load them concurrently.
1062-
futures::future::try_join_all(provider_names.iter().map(|name| async move {
1063-
load_provider_environment_record(store, workspace, name)
1077+
load_optional_provider_environment_records(store, workspace, provider_names)
1078+
.await?
1079+
.into_iter()
1080+
.zip(provider_names)
1081+
.map(|(record, name)| {
1082+
record
1083+
.ok_or_else(|| Status::failed_precondition(format!("provider '{name}' not found")))
1084+
})
1085+
.collect()
1086+
}
1087+
1088+
/// Like [`load_provider_environment_records`], but returns the names of
1089+
/// missing providers instead of failing.
1090+
pub(super) async fn load_existing_provider_environment_records<'a>(
1091+
store: &Store,
1092+
workspace: &str,
1093+
provider_names: &'a [String],
1094+
) -> Result<(Vec<ProviderEnvironmentRecord>, Vec<&'a str>), Status> {
1095+
let mut records = Vec::with_capacity(provider_names.len());
1096+
let mut missing = Vec::new();
1097+
for (record, name) in
1098+
load_optional_provider_environment_records(store, workspace, provider_names)
10641099
.await?
1065-
.ok_or_else(|| Status::failed_precondition(format!("provider '{name}' not found")))
1066-
}))
1067-
.await
1100+
.into_iter()
1101+
.zip(provider_names)
1102+
{
1103+
match record {
1104+
Some(record) => records.push(record),
1105+
None => missing.push(name.as_str()),
1106+
}
1107+
}
1108+
Ok((records, missing))
10681109
}
10691110

10701111
#[cfg(test)]

0 commit comments

Comments
 (0)