Skip to content

An open-source, faster Devin alternative

TypeScript

fix(microvm): discard a requested cpuCount instead of recording it — this runtime has no CPU knob (RunMicrovmInput carries no vCPU field), so carrying the customer's number into the usage record was the CPU form of the silent-wrong-size billing that delivered() already prevents for memory

Brian Reardonf04fa840b9a9
diff --git a/internal/awsvmlite/cpucount_test.go b/internal/awsvmlite/cpucount_test.go
new file mode 100644
index 00000000..1e7c487e
--- /dev/null
+++ b/internal/awsvmlite/cpucount_test.go
@@ -0,0 +1,43 @@
+package awsvmlite
+
+import "testing"
+
+// A customer-supplied cpuCount must never reach metering.
+//
+// This runtime has no CPU knob — RunMicrovmInput carries no vCPU field — so a
+// requested count cannot be honoured. Recording it anyway would let a customer
+// set a number in their own usage records for a machine they did not receive,
+// which is the CPU form of the silent-wrong-size failure delivered() exists to
+// prevent for memory.
+func TestRequestedCPUCountIsDiscardedNotRecorded(t *testing.T) {
+ var m *Manager // nil-safe by design: delivered() is pure bookkeeping
+
+ for _, tc := range []struct {
+ name string
+ requested int
+ }{
+ {"unset", 0},
+ {"the deliverable value", 1},
+ {"more than we can deliver", 4},
+ {"absurd", 512},
+ {"negative", -3},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got := m.delivered(Meta{CPUCount: tc.requested}).CPUCount
+ if got != deliveredCPUCount {
+ t.Errorf("requested cpuCount=%d was recorded as %d; metering must always see %d, "+
+ "or a customer picks the CPU count on their own usage records",
+ tc.requested, got, deliveredCPUCount)
+ }
+ })
+ }
+}
+
+// Memory keeps its existing behaviour — the CPU change must not disturb the
+// sizing logic sharing this function.
+func TestDeliveredStillDefaultsMemoryWithoutAClient(t *testing.T) {
+ var m *Manager
+ if got := m.delivered(Meta{MemoryMB: 4096}).MemoryMB; got != 4096 {
+ t.Errorf("memory rewritten to %d with no client configured; expected the request to stand", got)
+ }
+}
diff --git a/internal/awsvmlite/lite.go b/internal/awsvmlite/lite.go
index 7f224620..72620f01 100644
--- a/internal/awsvmlite/lite.go
+++ b/internal/awsvmlite/lite.go
@@ -511,6 +511,12 @@ func (m *Manager) popLocked(sandboxID string, meta Meta) *Box {
return b
}
+// deliveredCPUCount is what a box actually provides, and the only value that
+// may reach metering. CPU is not selectable on this runtime; it scales with the
+// memory tier, so there is one honest number to record rather than a per-tier
+// table we cannot verify from inside the guest.
+const deliveredCPUCount = 1
+
// delivered resolves what the customer will actually get, which is not always
// what they asked for: every box in the warm set is built from one image, so a
// request for another tier is served at the image's size or not at all.
@@ -530,8 +536,26 @@ func (m *Manager) delivered(meta Meta) Meta {
meta.MemoryMB = cfg.DefaultMemoryMB
}
}
- if meta.CPUCount <= 0 {
- meta.CPUCount = 1
+ // CPU gets the same treatment as memory, and for the same reason. This
+ // platform has no CPU knob at all — RunMicrovmInput carries no vCPU field,
+ // and cpu-configurations at image-publish time takes an architecture and
+ // nothing else. CPU is allocated as a function of memory.
+ //
+ // So a customer-supplied cpuCount cannot be honoured, and carrying it
+ // through would put a number the customer chose into the usage record this
+ // function exists to keep truthful — the CPU equivalent of billing a 4 GB
+ // box as the 16 GB that was asked for. It is discarded rather than
+ // recorded.
+ //
+ // Logged when a request is dropped, because "cpuCount did nothing" is
+ // otherwise invisible: the create succeeds, the sandbox is correct, and
+ // only the meter would have been wrong.
+ if meta.CPUCount != deliveredCPUCount {
+ if meta.CPUCount > 0 {
+ log.Printf("awsvmlite: cpuCount=%d requested but this runtime does not size CPU — metering as %d",
+ meta.CPUCount, deliveredCPUCount)
+ }
+ meta.CPUCount = deliveredCPUCount
}
return meta
}
diff --git a/internal/awsvmlite/lite_test.go b/internal/awsvmlite/lite_test.go
index dc488bd6..2461efd7 100644
--- a/internal/awsvmlite/lite_test.go
+++ b/internal/awsvmlite/lite_test.go
@@ -84,10 +84,19 @@ func TestDeliveredSizeFallsBackToTheImageRatherThanZero(t *testing.T) {
got.CPUCount)
}
- // An explicitly requested size must survive when there is nothing to resolve
- // it against, rather than being zeroed.
- if got := m.delivered(Meta{MemoryMB: 8192, CPUCount: 4}); got.MemoryMB != 8192 || got.CPUCount != 4 {
- t.Fatalf("delivered = %dMB/%dcpu, want the requested 8192MB/4cpu", got.MemoryMB, got.CPUCount)
+ // An explicitly requested MEMORY size must survive when there is nothing to
+ // resolve it against, rather than being zeroed.
+ //
+ // CPU is deliberately not symmetric with it. Memory is a real property of
+ // the image, so a request for it can be honoured or refused; CPU has no
+ // knob on this platform at all, so a requested count is discarded rather
+ // than carried into the meter. See deliveredCPUCount.
+ if got := m.delivered(Meta{MemoryMB: 8192, CPUCount: 4}); got.MemoryMB != 8192 {
+ t.Fatalf("delivered %dMB, want the requested 8192MB to survive", got.MemoryMB)
+ }
+ if got := m.delivered(Meta{MemoryMB: 8192, CPUCount: 4}); got.CPUCount != deliveredCPUCount {
+ t.Fatalf("delivered %dcpu, want %d — a requested CPU count must not reach metering",
+ got.CPUCount, deliveredCPUCount)
}
}