microsandbox-runtime 0.6.9

Runtime library for the microsandbox sandbox process and microVM entry points.
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! SQLite-backed cooperative CPU allocation and process-held leases.

use std::collections::{BTreeMap, HashSet};
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

use microsandbox_db::DbWriteConnection;
use microsandbox_db::entity::{cpu_allocation, cpu_allocation_cpu, memory_allocation_node};
use microsandbox_utils::process_lock;
use sea_orm::sea_query::Expr;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, Set};

use super::PlacementRequest;
use super::planner::{self, ResolvedPlacement};
use super::topology::{CpuTopology, LogicalCpuId};
use crate::{RuntimeError, RuntimeResult};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

const PLANNER_LOCK_NAME: &str = "allocator.lock";
const PLANNER_LOCK_TIMEOUT: Duration = Duration::from_millis(250);
const PLANNER_LOCK_RETRY_INTERVAL: Duration = Duration::from_millis(2);

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

pub(crate) struct AllocationLease {
    allocation_id: String,
    lease_path: PathBuf,
    file: File,
    released: AtomicBool,
    shared: bool,
}

/// Short-lived cross-process lock covering the allocation snapshot, plan, and catalog commit.
///
/// CPU load and NUMA memory capacity are aggregate promises that cannot be committed from a stale
/// snapshot. Holding this lock gives both resources one atomic admission boundary. The operating
/// system releases it if the planner process exits unexpectedly.
struct AllocationPlannerLock(File);

struct StaleAllocation {
    id: String,
    lease_name: String,
    lease_path: PathBuf,
    file: File,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl AllocationLease {
    pub(crate) async fn reconcile(
        &self,
        db: &DbWriteConnection,
        report: &msb_krun::PlacementReport,
    ) -> RuntimeResult<()> {
        if self.released.load(Ordering::Acquire) {
            return Ok(());
        }

        let allocation_id = self.allocation_id.clone();
        let vcpus = report.vcpus.clone();
        let memory = report.memory.clone();
        let shared = self.shared;
        db.transaction(|transaction| {
            let allocation_id = allocation_id.clone();
            let vcpus = vcpus.clone();
            let memory = memory.clone();
            async move {
                let rows = cpu_allocation_cpu::Entity::find()
                    .filter(cpu_allocation_cpu::Column::AllocationId.eq(allocation_id.clone()))
                    .all(&transaction)
                    .await?;
                if rows.len() != vcpus.len() {
                    return Err(RuntimeError::Custom(format!(
                        "placement report has {} vCPUs but allocation {} has {} planned rows",
                        vcpus.len(),
                        allocation_id,
                        rows.len()
                    )));
                }

                let mut pinned = 0usize;
                for result in &vcpus {
                    let (vcpu_index, reported_cpu) = match result {
                        msb_krun::VcpuPlacementResult::Pinned {
                            vcpu_index,
                            host_cpu,
                        } => (*vcpu_index, Some(*host_cpu)),
                        msb_krun::VcpuPlacementResult::Inherited {
                            vcpu_index,
                            requested_host_cpu,
                            ..
                        } => (*vcpu_index, *requested_host_cpu),
                    };
                    let row = rows
                        .iter()
                        .find(|row| row.vcpu_index == i32::from(vcpu_index))
                        .ok_or_else(|| {
                            RuntimeError::Custom(format!(
                                "placement report references unplanned vCPU {vcpu_index}"
                            ))
                        })?;
                    if let Some(host_cpu) = reported_cpu {
                        let reported_key =
                            ((i64::from(host_cpu.group)) << 16) | i64::from(host_cpu.index);
                        if row.logical_cpu != reported_key {
                            return Err(RuntimeError::Custom(format!(
                                "placement report maps vCPU {vcpu_index} to host CPU {reported_key}, expected {}",
                                row.logical_cpu
                            )));
                        }
                    }

                    match result {
                        msb_krun::VcpuPlacementResult::Pinned { .. } => {
                            pinned += 1;
                            cpu_allocation_cpu::Entity::update_many()
                                .col_expr(cpu_allocation_cpu::Column::Role, Expr::value("assigned"))
                                .filter(
                                    cpu_allocation_cpu::Column::AllocationId
                                        .eq(allocation_id.clone()),
                                )
                                .filter(
                                    cpu_allocation_cpu::Column::VcpuIndex
                                        .eq(i32::from(vcpu_index)),
                                )
                                .exec(&transaction)
                                .await?;
                        }
                        msb_krun::VcpuPlacementResult::Inherited { .. } => {
                            cpu_allocation_cpu::Entity::delete_many()
                                .filter(
                                    cpu_allocation_cpu::Column::AllocationId
                                        .eq(allocation_id.clone()),
                                )
                                .filter(
                                    cpu_allocation_cpu::Column::VcpuIndex
                                        .eq(i32::from(vcpu_index)),
                                )
                                .exec(&transaction)
                                .await?;
                        }
                    }
                }

                if !matches!(memory, msb_krun::MemoryPlacementResult::Applied) {
                    memory_allocation_node::Entity::delete_many()
                        .filter(
                            memory_allocation_node::Column::AllocationId.eq(allocation_id.clone()),
                        )
                        .exec(&transaction)
                        .await?;
                }

                let enforcement = match (pinned, vcpus.len(), shared) {
                    (0, _, _) => "inherited",
                    (count, total, false) if count == total => "thread-affinity-exclusive",
                    (count, total, true) if count == total => "thread-affinity-shared",
                    (_, _, false) => "thread-affinity-partial",
                    (_, _, true) => "thread-affinity-partial-shared",
                };
                cpu_allocation::Entity::update_many()
                    .col_expr(cpu_allocation::Column::Enforcement, Expr::value(enforcement))
                    .col_expr(cpu_allocation::Column::State, Expr::value("active"))
                    .filter(cpu_allocation::Column::Id.eq(allocation_id.clone()))
                    .exec(&transaction)
                    .await?;

                Ok::<_, RuntimeError>((transaction, ()))
            }
        })
        .await
    }

    pub(crate) async fn release(&self, db: &DbWriteConnection) -> RuntimeResult<()> {
        if self.released.load(Ordering::Acquire) {
            return Ok(());
        }

        cpu_allocation::Entity::delete_many()
            .filter(cpu_allocation::Column::Id.eq(self.allocation_id.clone()))
            .exec(db)
            .await?;
        process_lock::unlock(&self.file)?;
        self.released.store(true, Ordering::Release);
        if let Err(error) = std::fs::remove_file(&self.lease_path)
            && error.kind() != io::ErrorKind::NotFound
        {
            tracing::debug!(path = %self.lease_path.display(), %error, "remove CPU lease file");
        }
        Ok(())
    }
}

//--------------------------------------------------------------------------------------------------
// Trait Implementations
//--------------------------------------------------------------------------------------------------

impl Drop for AllocationPlannerLock {
    fn drop(&mut self) {
        if let Err(error) = process_lock::unlock(&self.0) {
            tracing::warn!(%error, "release allocation planner lock");
        }
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

pub(crate) async fn acquire(
    db: &DbWriteConnection,
    run_id: i32,
    lease_dir: &Path,
    topology: &CpuTopology,
    request: PlacementRequest,
) -> RuntimeResult<(AllocationLease, ResolvedPlacement, usize)> {
    prepare_lease_dir(lease_dir)?;
    let planner_lock = process_lock::open_lock_file(&lease_dir.join(PLANNER_LOCK_NAME))?;
    lock_planner(&planner_lock).await?;
    let _planner_lock = AllocationPlannerLock(planner_lock);
    let allocation_id = format!("{:032x}", rand::random::<u128>());
    let lease_name = format!("{allocation_id}.lock");
    let lease_path = lease_dir.join(&lease_name);
    let file = process_lock::create_new_lock_file(&lease_path)?;
    process_lock::lock_exclusive(&file)?;

    let attempt = 0;
    let snapshot_started = Instant::now();
    let allocations = match cpu_allocation::Entity::find().all(db).await {
        Ok(allocations) => allocations,
        Err(error) => {
            clean_unpublished_lease(&file, &lease_path);
            return Err(error.into());
        }
    };
    let cpu_rows = match cpu_allocation_cpu::Entity::find().all(db).await {
        Ok(cpu_rows) => cpu_rows,
        Err(error) => {
            clean_unpublished_lease(&file, &lease_path);
            return Err(error.into());
        }
    };
    let memory_rows = match memory_allocation_node::Entity::find().all(db).await {
        Ok(memory_rows) => memory_rows,
        Err(error) => {
            clean_unpublished_lease(&file, &lease_path);
            return Err(error.into());
        }
    };
    let stale = probe_stale_allocations(lease_dir, &allocations);
    let stale_ids: HashSet<_> = stale.iter().map(|entry| entry.id.as_str()).collect();
    let mut loads = BTreeMap::<LogicalCpuId, usize>::new();
    for row in cpu_rows
        .iter()
        .filter(|row| !stale_ids.contains(row.allocation_id.as_str()))
    {
        let logical_cpu = match LogicalCpuId::from_catalog_key(row.logical_cpu) {
            Ok(logical_cpu) => logical_cpu,
            Err(error) => {
                clean_unpublished_lease(&file, &lease_path);
                return Err(RuntimeError::Custom(format!(
                    "CPU allocation {} contains invalid logical CPU {}: {error}",
                    row.allocation_id, row.logical_cpu
                )));
            }
        };
        *loads.entry(logical_cpu).or_default() += 1;
    }
    let mut reserved_memory_mib = BTreeMap::<u32, u64>::new();
    for row in memory_rows
        .iter()
        .filter(|row| !stale_ids.contains(row.allocation_id.as_str()))
    {
        let host_node = u32::try_from(row.host_numa_node).map_err(|_| {
            RuntimeError::Custom(format!(
                "memory allocation {} contains invalid host NUMA node {}",
                row.allocation_id, row.host_numa_node
            ))
        })?;
        let max_mib = u64::try_from(row.max_mib).map_err(|_| {
            RuntimeError::Custom(format!(
                "memory allocation {} contains invalid maximum memory {}",
                row.allocation_id, row.max_mib
            ))
        })?;
        *reserved_memory_mib.entry(host_node).or_default() += max_mib;
    }
    let resolved = match planner::plan(topology, &loads, request, &reserved_memory_mib) {
        Ok(resolved) => resolved,
        Err(error) => {
            clean_unpublished_lease(&file, &lease_path);
            return Err(error);
        }
    };

    let transaction_started = Instant::now();
    let insert_result = db
        .transaction(|transaction| {
            let stale = stale
                .iter()
                .map(|entry| (entry.id.clone(), entry.lease_name.clone()))
                .collect::<Vec<_>>();
            let allocation_id = allocation_id.clone();
            let lease_name = lease_name.clone();
            let topology_fingerprint = topology.fingerprint.clone();
            let reservations = resolved.reservations.clone();
            let memory_nodes = resolved
                .numa
                .as_ref()
                .map(|numa| numa.nodes.clone())
                .unwrap_or_default();
            async move {
                for (id, expected_lease_name) in stale {
                    cpu_allocation::Entity::delete_many()
                        .filter(cpu_allocation::Column::Id.eq(id))
                        .filter(cpu_allocation::Column::LeaseName.eq(expected_lease_name))
                        .exec(&transaction)
                        .await?;
                }

                cpu_allocation::Entity::insert(cpu_allocation::ActiveModel {
                    id: Set(allocation_id.clone()),
                    run_id: Set(run_id),
                    requested_policy: Set(request.policy.to_string()),
                    resolved_policy: Set(resolved.resolved.to_string()),
                    enforcement: Set("pending".into()),
                    topology_fingerprint: Set(topology_fingerprint),
                    lease_name: Set(lease_name),
                    state: Set("pending".into()),
                    created_at: Set(chrono::Utc::now().naive_utc()),
                })
                .exec(&transaction)
                .await?;

                for reservation in reservations {
                    cpu_allocation_cpu::Entity::insert(cpu_allocation_cpu::ActiveModel {
                        allocation_id: Set(allocation_id.clone()),
                        vcpu_index: Set(i32::from(reservation.vcpu_index.ok_or_else(|| {
                            RuntimeError::Custom(
                                "CPU assignment is missing its guest vCPU index".into(),
                            )
                        })?)),
                        logical_cpu: Set(reservation.logical_cpu.catalog_key()),
                        role: Set(reservation.role.into()),
                    })
                    .exec(&transaction)
                    .await?;
                }
                for node in memory_nodes {
                    memory_allocation_node::Entity::insert(memory_allocation_node::ActiveModel {
                        allocation_id: Set(allocation_id.clone()),
                        guest_numa_node: Set(i32::from(node.guest_node_id)),
                        host_numa_node: Set(i32::try_from(node.host_node_id).map_err(|_| {
                            RuntimeError::Custom(format!(
                                "host NUMA node {} exceeds the catalog range",
                                node.host_node_id
                            ))
                        })?),
                        boot_mib: Set(i64::from(node.boot_memory_mib)),
                        max_mib: Set(i64::from(node.max_memory_mib)),
                    })
                    .exec(&transaction)
                    .await?;
                }
                Ok::<_, RuntimeError>((transaction, ()))
            }
        })
        .await;

    match insert_result {
        Ok(()) => {
            clean_stale_files(stale);
            tracing::debug!(
                attempt,
                snapshot_us = snapshot_started.elapsed().as_micros(),
                transaction_us = transaction_started.elapsed().as_micros(),
                "CPU allocation committed"
            );
            Ok((
                AllocationLease {
                    allocation_id,
                    lease_path,
                    file,
                    released: AtomicBool::new(false),
                    shared: resolved.shared,
                },
                resolved,
                attempt,
            ))
        }
        Err(error) => {
            clean_unpublished_lease(&file, &lease_path);
            Err(error)
        }
    }
}

async fn lock_planner(file: &File) -> RuntimeResult<()> {
    let started = Instant::now();
    loop {
        if process_lock::try_lock_exclusive(file)? {
            return Ok(());
        }
        if started.elapsed() >= PLANNER_LOCK_TIMEOUT {
            return Err(RuntimeError::Custom(format!(
                "CPU placement planner remained busy for {} ms",
                PLANNER_LOCK_TIMEOUT.as_millis()
            )));
        }
        tokio::time::sleep(PLANNER_LOCK_RETRY_INTERVAL).await;
    }
}

fn probe_stale_allocations(
    lease_dir: &Path,
    allocations: &[cpu_allocation::Model],
) -> Vec<StaleAllocation> {
    let mut stale = Vec::new();
    for allocation in allocations {
        if !is_valid_lease_name(&allocation.id, &allocation.lease_name) {
            // Catalog contents are not trusted as filesystem paths. Preserve an invalid row as
            // occupied instead of probing a path outside the private lease directory.
            tracing::warn!(
                allocation_id = %allocation.id,
                lease_name = %allocation.lease_name,
                "CPU allocation has an invalid lease name; preserving reservation"
            );
            continue;
        }
        let lease_path = lease_dir.join(&allocation.lease_name);
        let Ok(file) = process_lock::open_existing_lock_file(&lease_path) else {
            // Missing or unverifiable leases remain conservatively occupied.
            continue;
        };
        match process_lock::try_lock_exclusive(&file) {
            Ok(true) => stale.push(StaleAllocation {
                id: allocation.id.clone(),
                lease_name: allocation.lease_name.clone(),
                lease_path,
                file,
            }),
            Ok(false) => {}
            Err(error) => tracing::warn!(
                allocation_id = %allocation.id,
                %error,
                "could not verify CPU allocation lease; preserving reservation"
            ),
        }
    }
    stale
}

fn clean_stale_files(stale: Vec<StaleAllocation>) {
    for entry in stale {
        let _ = process_lock::unlock(&entry.file);
        if let Err(error) = std::fs::remove_file(&entry.lease_path)
            && error.kind() != io::ErrorKind::NotFound
        {
            tracing::debug!(path = %entry.lease_path.display(), %error, "remove stale CPU lease");
        }
    }
}

fn clean_unpublished_lease(file: &File, lease_path: &Path) {
    let _ = process_lock::unlock(file);
    if let Err(error) = std::fs::remove_file(lease_path)
        && error.kind() != io::ErrorKind::NotFound
    {
        tracing::debug!(path = %lease_path.display(), %error, "remove unpublished CPU lease");
    }
}

fn prepare_lease_dir(path: &Path) -> RuntimeResult<()> {
    std::fs::create_dir_all(path)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))?;
    }
    Ok(())
}

fn is_valid_lease_name(allocation_id: &str, lease_name: &str) -> bool {
    allocation_id.len() == 32
        && allocation_id.bytes().all(|byte| byte.is_ascii_hexdigit())
        && lease_name == format!("{allocation_id}.lock")
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::time::{Duration, Instant};

    use microsandbox_db::DbWriteConnection;
    use microsandbox_db::entity::{
        cpu_allocation, cpu_allocation_cpu, memory_allocation_node, run, sandbox,
    };
    use microsandbox_migration::{Migrator, MigratorTrait};
    use microsandbox_types::{CpuPlacement, MemoryPlacement, NumaPlacement, PlacementProfile};
    use microsandbox_utils::process_lock;
    use sea_orm::{ActiveModelTrait, EntityTrait, Set};

    use super::{PlacementRequest, acquire, is_valid_lease_name, lock_planner};
    use crate::cpu::topology::{CpuTopology, HostNumaNode, LogicalCpu, LogicalCpuId};

    #[test]
    fn lease_names_are_derived_from_canonical_allocation_ids() {
        let id = "0123456789abcdef0123456789abcdef";
        assert!(is_valid_lease_name(id, &format!("{id}.lock")));
        assert!(!is_valid_lease_name(id, "../outside.lock"));
        assert!(!is_valid_lease_name("short", "short.lock"));
        assert!(!is_valid_lease_name(
            "0123456789abcdef0123456789abcdeg",
            "0123456789abcdef0123456789abcdeg.lock"
        ));
    }

    #[tokio::test]
    async fn planner_lock_wait_is_bounded() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("allocator.lock");
        let held = process_lock::open_lock_file(&path).unwrap();
        process_lock::lock_exclusive(&held).unwrap();
        let contender = process_lock::open_lock_file(&path).unwrap();

        let started = Instant::now();
        let error = lock_planner(&contender).await.unwrap_err();

        assert!(error.to_string().contains("remained busy"));
        assert!(started.elapsed() < Duration::from_secs(1));
        process_lock::unlock(&held).unwrap();
    }

    #[tokio::test]
    async fn allocations_share_a_logical_cpu_after_exclusive_capacity_is_used() {
        let dir = tempfile::tempdir().unwrap();
        let db = DbWriteConnection::open(
            &dir.path().join("catalog.db"),
            Duration::from_secs(5),
            Duration::from_secs(5),
        )
        .await
        .unwrap();
        Migrator::up(db.inner(), None).await.unwrap();
        let sandbox_id = sandbox::ActiveModel {
            name: Set("cpu-sharing-test".into()),
            config: Set("{}".into()),
            status: Set(sandbox::SandboxStatus::Running),
            ephemeral: Set(false),
            ..Default::default()
        }
        .insert(&db)
        .await
        .unwrap()
        .id;
        let mut run_ids = Vec::new();
        for _ in 0..2 {
            run_ids.push(
                run::ActiveModel {
                    sandbox_id: Set(sandbox_id),
                    status: Set(run::RunStatus::Running),
                    ..Default::default()
                }
                .insert(&db)
                .await
                .unwrap()
                .id,
            );
        }
        let topology = CpuTopology {
            logical_cpus: vec![LogicalCpu {
                id: LogicalCpuId::new(0),
                package: 0,
                die: 0,
                core: 0,
                numa_node: 0,
                performance_class: 0,
            }],
            numa_nodes: vec![HostNumaNode {
                id: 0,
                package: 0,
                total_memory_mib: 16_384,
                available_memory_mib: 16_384,
                distances: BTreeMap::from([(0, 10)]),
            }],
            fingerprint: "single-logical-cpu".into(),
        };
        let request = PlacementRequest {
            policy: CpuPlacement::Auto,
            max_vcpus: 1,
            boot_memory_mib: 512,
            max_memory_mib: 512,
            profile: None,
        };

        let first = acquire(
            &db,
            run_ids[0],
            &dir.path().join("leases"),
            &topology,
            request,
        )
        .await
        .unwrap();
        let second = acquire(
            &db,
            run_ids[1],
            &dir.path().join("leases"),
            &topology,
            request,
        )
        .await
        .unwrap();

        let rows = cpu_allocation_cpu::Entity::find().all(&db).await.unwrap();
        assert_eq!(rows.len(), 2);
        assert!(rows.iter().all(|row| row.logical_cpu == 0));
        assert!(!first.1.shared);
        assert!(second.1.shared);

        first.0.release(&db).await.unwrap();
        second.0.release(&db).await.unwrap();
    }

    #[tokio::test]
    async fn placement_ack_keeps_successful_pins_and_releases_fallback_capacity() {
        let dir = tempfile::tempdir().unwrap();
        let db = DbWriteConnection::open(
            &dir.path().join("catalog.db"),
            Duration::from_secs(5),
            Duration::from_secs(5),
        )
        .await
        .unwrap();
        Migrator::up(db.inner(), None).await.unwrap();
        let sandbox_id = sandbox::ActiveModel {
            name: Set("placement-ack-test".into()),
            config: Set("{}".into()),
            status: Set(sandbox::SandboxStatus::Running),
            ephemeral: Set(false),
            ..Default::default()
        }
        .insert(&db)
        .await
        .unwrap()
        .id;
        let run_id = run::ActiveModel {
            sandbox_id: Set(sandbox_id),
            status: Set(run::RunStatus::Running),
            ..Default::default()
        }
        .insert(&db)
        .await
        .unwrap()
        .id;
        let topology = CpuTopology {
            logical_cpus: vec![
                LogicalCpu {
                    id: LogicalCpuId::new(0),
                    package: 0,
                    die: 0,
                    core: 0,
                    numa_node: 0,
                    performance_class: 0,
                },
                LogicalCpu {
                    id: LogicalCpuId::new(1),
                    package: 0,
                    die: 0,
                    core: 1,
                    numa_node: 0,
                    performance_class: 0,
                },
            ],
            numa_nodes: vec![HostNumaNode {
                id: 0,
                package: 0,
                total_memory_mib: 16_384,
                available_memory_mib: 16_384,
                distances: BTreeMap::from([(0, 10)]),
            }],
            fingerprint: "placement-ack".into(),
        };
        let (lease, _, _) = acquire(
            &db,
            run_id,
            &dir.path().join("leases"),
            &topology,
            PlacementRequest {
                policy: CpuPlacement::Auto,
                max_vcpus: 2,
                boot_memory_mib: 512,
                max_memory_mib: 1024,
                profile: Some(PlacementProfile {
                    numa: NumaPlacement::PreferSingle,
                    memory: MemoryPlacement::FollowCpu,
                }),
            },
        )
        .await
        .unwrap();

        lease
            .reconcile(
                &db,
                &msb_krun::PlacementReport {
                    vcpus: vec![
                        msb_krun::VcpuPlacementResult::Pinned {
                            vcpu_index: 0,
                            host_cpu: msb_krun::HostCpuId::new(0),
                        },
                        msb_krun::VcpuPlacementResult::Inherited {
                            vcpu_index: 1,
                            requested_host_cpu: Some(msb_krun::HostCpuId::new(1)),
                            reason: Some("injected affinity rejection".into()),
                        },
                    ],
                    memory: msb_krun::MemoryPlacementResult::Fallback {
                        reason: "partial CPU placement".into(),
                    },
                },
            )
            .await
            .unwrap();

        let cpu_rows = cpu_allocation_cpu::Entity::find().all(&db).await.unwrap();
        assert_eq!(cpu_rows.len(), 1);
        assert_eq!(cpu_rows[0].vcpu_index, 0);
        assert_eq!(cpu_rows[0].role, "assigned");
        assert!(
            memory_allocation_node::Entity::find()
                .all(&db)
                .await
                .unwrap()
                .is_empty()
        );
        let allocation = cpu_allocation::Entity::find()
            .one(&db)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(allocation.enforcement, "thread-affinity-partial");
        assert_eq!(allocation.state, "active");

        lease.release(&db).await.unwrap();
    }
}