Repository navigation
feat(orchestrator): shape batch pause cohorts with a concurrency cap and TTL jitter #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
e9b51f3
6b6d201
16f5fde
fe72e54
2ac576d
6eca741
42d339c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -642,6 +642,11 @@ pub struct NodeIdentityConfig { | |
| pub service_instance_id: Option<String>, | ||
| } | ||
|
|
||
| /// Upper bound for `[orchestrator].ttl_jitter_percent`. The jitter band is | ||
| /// +/- the configured percent, so values at or above 100 could collapse a | ||
| /// sandbox timeout to zero. | ||
| pub(crate) const MAX_TTL_JITTER_PERCENT: u32 = 99; | ||
|
|
||
| #[derive(Debug, Config, Clone)] | ||
| pub struct OrchestratorConfig { | ||
| #[config(default = 15u64)] | ||
|
|
@@ -661,6 +666,23 @@ pub struct OrchestratorConfig { | |
| pub default_sandbox_timeout_secs: u64, | ||
| #[config(default = 300u64)] | ||
| pub auto_resume_min_sandbox_timeout_secs: u64, | ||
| /// Maximum number of pause operations that may run concurrently on this | ||
| /// node. TTL auto-eviction and explicit pause requests share the same | ||
| /// bound. Pause snapshots are dense (~1x guest RAM), so a same-second | ||
| /// expiry cohort can saturate node write bandwidth; a small cap keeps the | ||
| /// cohort tail latency near the intrinsic per-pause cost. 0 (default) | ||
| /// preserves the unbounded behavior. Values above the platform semaphore | ||
| /// capacity are clamped to it. | ||
| #[config(default = 0u64)] | ||
| pub pause_concurrency_limit: u64, | ||
|
Comment on lines
+676
to
+677
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6b6d201 by documenting the behavior at each entry point instead of adding an arbitrary ceiling: the |
||
| /// Deterministic per-sandbox jitter applied when a timeout is assigned, | ||
|
Comment on lines
+676
to
+678
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one does not reproduce: both keys are documented in |
||
| /// uniform within +/- this percent of the requested TTL and derived from | ||
| /// the sandbox ID. Staggers same-phase expiry cohorts before they form so | ||
| /// auto-eviction sees a spread instead of one pause batch. 0 (default) | ||
| /// disables jitter; 10-20 is the measured useful range when combined with | ||
| /// `pause_concurrency_limit`. | ||
| #[config(default = 0u64)] | ||
| pub ttl_jitter_percent: u64, | ||
| #[config( | ||
| default = "$AENV_HOME/persisted-sandboxes", | ||
| env = "AENV_PERSISTED_SANDBOX_STORE_PATH", | ||
|
|
@@ -1038,6 +1060,7 @@ impl AppConfig { | |
|
|
||
| fn validate(&self) -> Result<()> { | ||
| self.validate_pool_config()?; | ||
| self.validate_orchestrator_pause_shaping()?; | ||
| self.image.cache.gc.validate()?; | ||
| NetworkConfig::validate(&self.network)?; | ||
| if self.ublk.overlaybd.resize_timeout_secs == 0 { | ||
|
|
@@ -1055,6 +1078,15 @@ impl AppConfig { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Validates the batch pause-shaping knobs so operator mistakes fail at | ||
| /// load time instead of silently degrading expiry behavior. | ||
| fn validate_orchestrator_pause_shaping(&self) -> Result<()> { | ||
| if self.orchestrator.ttl_jitter_percent > u64::from(MAX_TTL_JITTER_PERCENT) { | ||
| bail!("orchestrator.ttl_jitter_percent must be between 0 and {MAX_TTL_JITTER_PERCENT}"); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
Comment on lines
+1083
to
+1088
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2ac576d: the mapping is extracted into |
||
|
|
||
| fn validate_template_builder(&self) -> Result<()> { | ||
| let builder = &self.template_build; | ||
| if builder.max_concurrent_builds == 0 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,7 @@ use std::sync::{ | |||||||||||||||||||||||||||||||||||||||||||||||
| use std::time::{Duration, SystemTime}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| use anyhow::Context; | ||||||||||||||||||||||||||||||||||||||||||||||||
| use tokio::sync::{broadcast, oneshot, watch, Mutex, OnceCell, RwLock}; | ||||||||||||||||||||||||||||||||||||||||||||||||
| use tokio::sync::{broadcast, oneshot, watch, Mutex, OnceCell, RwLock, Semaphore}; | ||||||||||||||||||||||||||||||||||||||||||||||||
| use tokio::time::MissedTickBehavior; | ||||||||||||||||||||||||||||||||||||||||||||||||
| use tracing::{debug, info, trace, warn}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -66,6 +66,47 @@ const SANDBOX_EVENT_CHANNEL_CAPACITY: usize = 1024; | |||||||||||||||||||||||||||||||||||||||||||||||
| const MAX_SANDBOX_LOG_LIMIT: usize = 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||
| const SANDBOX_LOG_QUERY_TIMEOUT: Duration = Duration::from_secs(5); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// Applies deterministic per-sandbox jitter to a newly assigned TTL. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Same-phase expiry cohorts (episodes of one rollout ending together, node | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// drains, recovery bursts) would otherwise reach the evictor as a single | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// batch, and their pause snapshots (~1x guest RAM each) contend for node | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// write bandwidth. Spreading the deadlines before the cohort forms disperses | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// the batch at zero runtime cost. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// The offset is uniform within +/- `percent` of `timeout` and is a pure | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// function of the sandbox ID, so every assignment for a given sandbox lands | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// on the same point of the band: the offset never accumulates across | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// create/resume/keep-alive cycles, and equal inputs always produce equal | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// outputs. | ||||||||||||||||||||||||||||||||||||||||||||||||
| fn jittered_ttl(sandbox_id: &SandboxId, timeout: Duration, percent: u32) -> Duration { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let percent = percent.min(crate::cfg::MAX_TTL_JITTER_PERCENT); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if percent == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return timeout; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Fold both UUID halves into the offset. UUIDv7 keeps its random bits in | ||||||||||||||||||||||||||||||||||||||||||||||||
| // the second half, so this mixes well without depending on a particular | ||||||||||||||||||||||||||||||||||||||||||||||||
| // hash implementation staying stable across releases. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let bytes = sandbox_id.into_inner().into_bytes(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| let mixed = u64::from_le_bytes(bytes[0..8].try_into().expect("UUID half is 8 bytes")) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ^ u64::from_le_bytes(bytes[8..16].try_into().expect("UUID half is 8 bytes")); | ||||||||||||||||||||||||||||||||||||||||||||||||
| let span = u64::from(percent) * 2 + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
| let offset = (mixed % span) as i64 - i64::from(percent); | ||||||||||||||||||||||||||||||||||||||||||||||||
| let factor = (100 + offset) as u32; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Integer division truncates to whole nanoseconds, so a sub-100ns timeout | ||||||||||||||||||||||||||||||||||||||||||||||||
| // can round down to zero. A non-zero TTL must never collapse into | ||||||||||||||||||||||||||||||||||||||||||||||||
| // "expire immediately"; keep at least one nanosecond of lifetime. Zero | ||||||||||||||||||||||||||||||||||||||||||||||||
| // stays zero. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let scaled = timeout.saturating_mul(factor) / 100; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6eca741: the scaling now computes the exact quotient in u128 nanoseconds ( |
||||||||||||||||||||||||||||||||||||||||||||||||
| if scaled.is_zero() && !timeout.is_zero() { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6eca741 together with the sibling finding: the scaling computes the exact quotient in u128 nanoseconds with no intermediate saturation (see the reply above), so a large timeout scaled by an increase factor clamps at |
||||||||||||||||||||||||||||||||||||||||||||||||
| Duration::from_nanos(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| scaled | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Clone, Debug)] | ||||||||||||||||||||||||||||||||||||||||||||||||
| enum ShutdownOutcome { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Success, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -127,6 +168,12 @@ pub struct Orchestrator< | |||||||||||||||||||||||||||||||||||||||||||||||
| sandbox_metrics: Mutex<SandboxMetrics>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| sandbox_event_tx: broadcast::Sender<SandboxLifecycleEvent>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| default_sandbox_timeout: Duration, | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Node-level bound on concurrent pause operations, shared by TTL | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// auto-eviction and explicit pause requests. `None` means unbounded. | ||||||||||||||||||||||||||||||||||||||||||||||||
| pause_semaphore: Option<Arc<Semaphore>>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Deterministic TTL jitter percent applied when a timeout is assigned | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// (0 disables jitter). Used by [`Self::shaped_timeout`]. | ||||||||||||||||||||||||||||||||||||||||||||||||
| ttl_jitter_percent: u32, | ||||||||||||||||||||||||||||||||||||||||||||||||
| is_shutting_down: std::sync::atomic::AtomicBool, | ||||||||||||||||||||||||||||||||||||||||||||||||
| shutdown_tx: watch::Sender<bool>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| shutdown_outcome: OnceCell<ShutdownOutcome>, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -233,6 +280,19 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| store.add(metadata).await?; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Batch pause shaping: the semaphore bounds how many pause snapshots | ||||||||||||||||||||||||||||||||||||||||||||||||
| // run at once across every entry point, and the jitter percent | ||||||||||||||||||||||||||||||||||||||||||||||||
| // staggers same-phase expiry cohorts at TTL assignment time. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let pause_semaphore = match config.pause_concurrency_limit { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 0 => None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| limit => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let permits = usize::try_from(limit) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap_or(usize::MAX) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .min(Semaphore::MAX_PERMITS); | ||||||||||||||||||||||||||||||||||||||||||||||||
| Some(Arc::new(Semaphore::new(permits))) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let orchestrator = Arc::new(Self { | ||||||||||||||||||||||||||||||||||||||||||||||||
| store, | ||||||||||||||||||||||||||||||||||||||||||||||||
| factory, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -246,6 +306,9 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| sandbox_metrics: Mutex::new(SandboxMetrics::default()), | ||||||||||||||||||||||||||||||||||||||||||||||||
| sandbox_event_tx, | ||||||||||||||||||||||||||||||||||||||||||||||||
| default_sandbox_timeout: Duration::from_secs(config.default_sandbox_timeout_secs), | ||||||||||||||||||||||||||||||||||||||||||||||||
| pause_semaphore, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ttl_jitter_percent: u32::try_from(config.ttl_jitter_percent) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap_or(crate::cfg::MAX_TTL_JITTER_PERCENT), | ||||||||||||||||||||||||||||||||||||||||||||||||
| is_shutting_down: std::sync::atomic::AtomicBool::new(false), | ||||||||||||||||||||||||||||||||||||||||||||||||
| shutdown_tx, | ||||||||||||||||||||||||||||||||||||||||||||||||
| shutdown_outcome: OnceCell::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -543,7 +606,10 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| snapshot, | ||||||||||||||||||||||||||||||||||||||||||||||||
| launch_config, | ||||||||||||||||||||||||||||||||||||||||||||||||
| transitional_metadata, | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::Set(timeout.unwrap_or(self.default_sandbox_timeout)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.shaped_timeout( | ||||||||||||||||||||||||||||||||||||||||||||||||
| &sandbox_id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::Set(timeout.unwrap_or(self.default_sandbox_timeout)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -609,7 +675,10 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| build_spec, | ||||||||||||||||||||||||||||||||||||||||||||||||
| launch_config, | ||||||||||||||||||||||||||||||||||||||||||||||||
| transitional_metadata, | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::Set(timeout.unwrap_or(self.default_sandbox_timeout)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.shaped_timeout( | ||||||||||||||||||||||||||||||||||||||||||||||||
| &sandbox_id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::Set(timeout.unwrap_or(self.default_sandbox_timeout)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -812,7 +881,7 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| metadata.created_at = now; | ||||||||||||||||||||||||||||||||||||||||||||||||
| metadata.paused_state = None; | ||||||||||||||||||||||||||||||||||||||||||||||||
| metadata.volume_mounts = child.volume_mounts; | ||||||||||||||||||||||||||||||||||||||||||||||||
| metadata.update_timeout(new_timeout); | ||||||||||||||||||||||||||||||||||||||||||||||||
| metadata.update_timeout(self.shaped_timeout(&sandbox_id, new_timeout)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let proxy_target = match Self::proxy_target_from_sandbox(backend.as_ref()) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(proxy_target) => proxy_target, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -992,7 +1061,14 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| debug!(?timeout, "updating keep-alive timeout"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| let valid_timeout = timeout.unwrap_or(self.default_sandbox_timeout); | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Jitter before the `allow_shorter` comparison below so a refresh | ||||||||||||||||||||||||||||||||||||||||||||||||
| // request is not rejected because the stored deadline was jittered | ||||||||||||||||||||||||||||||||||||||||||||||||
| // upward. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let valid_timeout = jittered_ttl( | ||||||||||||||||||||||||||||||||||||||||||||||||
| &sandbox_id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| timeout.unwrap_or(self.default_sandbox_timeout), | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.ttl_jitter_percent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let mut metadata = match self.store.get(&sandbox_id).await? { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Some(metadata) => metadata, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1391,6 +1467,48 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| outcome.as_result() | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// Maps a newly assigned timeout through the configured TTL jitter. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// `NewTimeout::Set` assigns a concrete duration, and `EnsureMinimum` | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// assigns one too: it sets the deadline when the sandbox has no timeout | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// yet and raises it when the stored one is shorter than the minimum (the | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// auto-resume path uses this variant). Both candidates are jittered so a | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// same-phase cohort still disperses; for `EnsureMinimum` the jittered | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// candidate is then clamped back to the minimum, preserving its | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// documented no-less-than contract. `UseExisting`/`None` assign nothing | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// new, so they pass through unchanged. | ||||||||||||||||||||||||||||||||||||||||||||||||
| fn shaped_timeout(&self, sandbox_id: &SandboxId, timeout: NewTimeout) -> NewTimeout { | ||||||||||||||||||||||||||||||||||||||||||||||||
| match timeout { | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::Set(duration) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::Set(jittered_ttl(sandbox_id, duration, self.ttl_jitter_percent)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| NewTimeout::EnsureMinimum(minimum) => NewTimeout::EnsureMinimum( | ||||||||||||||||||||||||||||||||||||||||||||||||
| jittered_ttl(sandbox_id, minimum, self.ttl_jitter_percent).max(minimum), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| other => other, | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1516
to
+1524
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 16f5fde: |
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// Waits for a pause slot when batch pause shaping is configured. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Pause snapshots are dense (~1x guest RAM), so a cohort of concurrent | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// pauses saturates node write bandwidth and every pause in the cohort | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// slows down. Callers reserve the slot before claiming the `Pausing` | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// transition, so a sandbox queued behind the limit stays `Running` | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// (reachable while it waits) and the permit is held across the whole | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// capture-and-stop sequence. | ||||||||||||||||||||||||||||||||||||||||||||||||
| async fn acquire_pause_slot(&self) -> Option<tokio::sync::OwnedSemaphorePermit> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| match &self.pause_semaphore { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Some(semaphore) => Some( | ||||||||||||||||||||||||||||||||||||||||||||||||
| Arc::clone(semaphore) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .acquire_owned() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
| .expect("pause semaphore is never closed"), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| None => None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// Pauses a running sandbox by taking a snapshot and stopping its VM. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// If another `pause_sandbox` call is already in progress for the same | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1411,13 +1529,22 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| )] | ||||||||||||||||||||||||||||||||||||||||||||||||
| async fn pause_sandbox_inner(self: &Arc<Self>, sandbox_id: SandboxId) -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| info!("pausing sandbox"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Reserve shaping capacity before claiming the transition: a sandbox | ||||||||||||||||||||||||||||||||||||||||||||||||
| // queued behind the pause limit must stay `Running` (proxyable, | ||||||||||||||||||||||||||||||||||||||||||||||||
| // keep-alive-able, deletable) until its pause actually starts. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let pause_slot = self.acquire_pause_slot().await; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2ac576d: |
||||||||||||||||||||||||||||||||||||||||||||||||
| match self | ||||||||||||||||||||||||||||||||||||||||||||||||
| .store | ||||||||||||||||||||||||||||||||||||||||||||||||
| .update_state_if_state(&sandbox_id, SandboxState::Pausing, &[SandboxState::Running]) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(_) => {} | ||||||||||||||||||||||||||||||||||||||||||||||||
| Err(StoreError::StateConflict { actual_state, .. }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // No pause work will run for this call: another operation | ||||||||||||||||||||||||||||||||||||||||||||||||
| // already owns the transition. Release the slot before | ||||||||||||||||||||||||||||||||||||||||||||||||
| // joining/waiting on it so queued capacity is not consumed | ||||||||||||||||||||||||||||||||||||||||||||||||
| // by a caller that is not pausing anything. | ||||||||||||||||||||||||||||||||||||||||||||||||
| drop(pause_slot); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return match actual_state { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Another task is already performing the pause. Wait for | ||||||||||||||||||||||||||||||||||||||||||||||||
| // it to finish and then report the final outcome. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1439,10 +1566,20 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| Err(err) => return Err(OrchestratorError::from(err)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| self.pause_sandbox_impl(sandbox_id).await | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.pause_sandbox_impl(sandbox_id, pause_slot).await | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| async fn pause_sandbox_impl(self: &Arc<Self>, sandbox_id: SandboxId) -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Runs the pause capture-and-stop sequence for a sandbox whose transition | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// the caller has already claimed. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// `_pause_slot` is the caller's pause-shaping permit (see | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// [`Self::acquire_pause_slot`]). Every caller acquires it before claiming | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// `Pausing`, and it is held for the whole sequence below. | ||||||||||||||||||||||||||||||||||||||||||||||||
| async fn pause_sandbox_impl( | ||||||||||||||||||||||||||||||||||||||||||||||||
| self: &Arc<Self>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| sandbox_id: SandboxId, | ||||||||||||||||||||||||||||||||||||||||||||||||
| _pause_slot: Option<tokio::sync::OwnedSemaphorePermit>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Pin paused runtime artifacts before detaching from the running set. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let runtime_artifacts = { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let handle = self.sandboxes.read().await.get(&sandbox_id).cloned(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1660,6 +1797,11 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<SandboxMetadata> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.ensure_accepting_lifecycle_operations()?; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Apply TTL staggering once at the entry point so every downstream | ||||||||||||||||||||||||||||||||||||||||||||||||
| // path (launch, concurrent-resume join, keep-alive refresh) uses the | ||||||||||||||||||||||||||||||||||||||||||||||||
| // same jittered deadline. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let timeout = self.shaped_timeout(&sandbox_id, timeout); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not reproduce: |
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| info!("resuming sandbox"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| let mut metadata = self | ||||||||||||||||||||||||||||||||||||||||||||||||
| .store | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -2288,12 +2430,21 @@ where | |||||||||||||||||||||||||||||||||||||||||||||||
| // Use the same operation lock as explicit deletion before claiming Killing. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let deletion = self.deletion_progress(metadata.id).await; | ||||||||||||||||||||||||||||||||||||||||||||||||
| let mut progress = deletion.lock().await; | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Reserve pause capacity before claiming `Pausing`, so an expired | ||||||||||||||||||||||||||||||||||||||||||||||||
| // sandbox queued behind the pause limit keeps running (and stays | ||||||||||||||||||||||||||||||||||||||||||||||||
| // proxyable) until its pause actually starts. | ||||||||||||||||||||||||||||||||||||||||||||||||
| let pause_slot = match metadata.timeout_action { | ||||||||||||||||||||||||||||||||||||||||||||||||
| SandboxTimeoutAction::Pause => self.acquire_pause_slot().await, | ||||||||||||||||||||||||||||||||||||||||||||||||
| SandboxTimeoutAction::Delete => None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in fe72e54: the evictor now acquires the pause slot before
Comment on lines
+2506
to
+2509
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2ac576d for the actionable part: the eviction pass now claims deletion entries before pause entries (stable sort on the timeout action), so deletes are no longer reachable only after a parked pause entry — deletes need no slot and are claimed/processed regardless of pool depth. Per-entry spawning was left out deliberately: pause entries are processed serially in list order by design (the semaphore additionally bounds cross-entry-point pauses; the sequential scan predates this PR), and making the scan concurrent would introduce overlapping eviction passes for no shaping benefit. Regression
Comment on lines
+2506
to
+2509
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recording this as a design-level item rather than changing it here, with the current behavior spelled out:
If you spot a concrete stall path that is not bounded by pause-work duration (the stalled-backend case saturates any pause entry point, not just the evictor), please point at it and I will fix it. |
||||||||||||||||||||||||||||||||||||||||||||||||
| let result = match self | ||||||||||||||||||||||||||||||||||||||||||||||||
| .claim_expired_sandbox(metadata.id, eviction_cutoff, metadata.state, claimed_state) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(true) => match metadata.timeout_action { | ||||||||||||||||||||||||||||||||||||||||||||||||
| SandboxTimeoutAction::Pause => self.pause_sandbox_impl(metadata.id).await, | ||||||||||||||||||||||||||||||||||||||||||||||||
| SandboxTimeoutAction::Pause => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.pause_sandbox_impl(metadata.id, pause_slot).await | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| SandboxTimeoutAction::Delete => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| self.delete_sandbox_impl(metadata.id, metadata.state, &mut progress) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This upper bound does not actually guarantee a positive jittered timeout:
jittered_ttlperforms integer duration scaling, so a non-zero 1-second timeout can become zero at an offset below -0% (for example, 1s × 1% is 0ms after truncation) even though the configured value is within the allowed 99%. Since the setting is documented as preserving a usable TTL and the implementation's test only checks a 15-second timeout, either reject/limit jitter based on the minimum supported TTL or ensure the jitter calculation preserves at least one tick for every non-zero input.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 6b6d201:
jittered_ttlnow floors the scaled result at 1 ns whenever the input timeout is non-zero (zero stays zero), so integer truncation cannot turn a valid short TTL into immediate expiry.ttl_jitter_percent_is_clamped_to_a_positive_timeoutnow asserts the sub-100 ns case (50 ns input).