Skip to content

Commit b7d8ecb

Browse files
committed
fix(discovery): wait for collection capacity
Signed-off-by: Taylor Mutch <taylormutch@gmail.com>
1 parent 1aae34e commit b7d8ecb

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎crates/openshell-server/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ invalidate the view. This is not a cross-resource transaction. Reads are bounded
320320
to 1,000 records, 4,096 routes, 4 MiB, and ten seconds. A per-workspace cache shares
321321
one collection for a one-second refresh interval, retains at most 16 workspaces,
322322
and never returns expired routes. At most four collections and 16 unary snapshot
323-
requests run concurrently per gateway. Unary admission permits four requests per
323+
requests run concurrently per gateway. Collections wait for a free slot within
324+
the ten-second timeout. Unary admission permits four requests per
324325
principal and eight per workspace. Overflow returns `RESOURCE_EXHAUSTED`; storage
325326
failure or timeout returns `UNAVAILABLE`; malformed records return `INTERNAL`.
326327
Watches recheck bearer authentication and workspace authorization each second,

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

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ async fn cached_snapshot(
176176
.discovery
177177
.collections
178178
.clone()
179-
.try_acquire_owned()
180-
.map_err(|_| Status::resource_exhausted("too many discovery collections"))?;
179+
.acquire_owned()
180+
.await
181+
.map_err(|_| Status::unavailable("discovery collections closed"))?;
181182
let result = snapshot(state, workspace).await;
182183
*cached = Some((tokio::time::Instant::now(), result.clone()));
183184
result
@@ -961,10 +962,6 @@ mod tests {
961962
);
962963
assert_eq!(a.unwrap(), first);
963964
assert_eq!(b.unwrap(), first);
964-
assert_eq!(
965-
cached_snapshot(&state, "other").await.unwrap_err().code(),
966-
tonic::Code::ResourceExhausted
967-
);
968965
drop(busy);
969966
state
970967
.store
@@ -981,6 +978,65 @@ mod tests {
981978
);
982979
}
983980

981+
#[tokio::test]
982+
async fn collection_waits_for_a_slot_and_resumes_when_one_is_released() {
983+
let (state, owner, report) = installed_fixture().await;
984+
accept(&state, &owner, &report, current_time_ms() + 10_000)
985+
.await
986+
.unwrap();
987+
let mut busy = state
988+
.discovery
989+
.collections
990+
.clone()
991+
.acquire_many_owned(u32::try_from(MAX_COLLECTIONS).unwrap())
992+
.await
993+
.unwrap();
994+
let pending = cached_snapshot(&state, "default");
995+
tokio::pin!(pending);
996+
assert!(
997+
tokio::time::timeout(Duration::from_millis(20), &mut pending)
998+
.await
999+
.is_err(),
1000+
"collection must wait rather than fail when all slots are occupied"
1001+
);
1002+
drop(busy.split(1).unwrap());
1003+
let snapshot = tokio::time::timeout(Duration::from_secs(3), pending)
1004+
.await
1005+
.unwrap()
1006+
.unwrap();
1007+
assert_eq!(snapshot.routes.len(), 1);
1008+
assert_eq!(state.discovery.collections.available_permits(), 1);
1009+
drop(busy);
1010+
assert_eq!(
1011+
state.discovery.collections.available_permits(),
1012+
MAX_COLLECTIONS
1013+
);
1014+
}
1015+
1016+
#[tokio::test]
1017+
async fn collection_slot_wait_is_bounded_by_snapshot_timeout() {
1018+
let (state, _, _) = installed_fixture().await;
1019+
let busy = state
1020+
.discovery
1021+
.collections
1022+
.clone()
1023+
.acquire_many_owned(u32::try_from(MAX_COLLECTIONS).unwrap())
1024+
.await
1025+
.unwrap();
1026+
tokio::time::pause();
1027+
let start = tokio::time::Instant::now();
1028+
let error = cached_snapshot(&state, "default").await.unwrap_err();
1029+
assert_eq!(error.code(), tonic::Code::Unavailable);
1030+
assert_eq!(error.message(), "service discovery collection timed out");
1031+
assert!(start.elapsed() >= SNAPSHOT_TIMEOUT);
1032+
assert!(start.elapsed() < SNAPSHOT_TIMEOUT + Duration::from_secs(1));
1033+
drop(busy);
1034+
assert_eq!(
1035+
state.discovery.collections.available_permits(),
1036+
MAX_COLLECTIONS
1037+
);
1038+
}
1039+
9841040
#[tokio::test]
9851041
async fn expired_cached_routes_are_never_returned() {
9861042
let (state, _, _) = installed_fixture().await;

‎docs/how-it-works/gateways/configuration.mdx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,8 @@ relist when they end. Discovery-only machine credentials are separate work.
20182018

20192019
Discovery limits apply per gateway process: 1,000 installation records, 4,096
20202020
routes, and 4 MiB per snapshot, with a ten-second collection timeout. Up to four
2021-
collections and 16 unary requests run concurrently; unary requests are limited
2021+
collections and 16 unary requests run concurrently. Collections wait for a free
2022+
slot within the ten-second timeout; unary requests are limited
20222023
to four per principal and eight per workspace. Watches are limited to 32 total,
20232024
four per principal, and 16 per workspace. Capacity and size limits return
20242025
`RESOURCE_EXHAUSTED`; storage failures and timeouts return `UNAVAILABLE`; corrupt

0 commit comments

Comments
 (0)