An open-source, faster Devin alternative
TypeScript
query actual size
Brian Reardon22485739be00
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.goindex b99dd5bc..14f677ab 100644--- a/internal/api/sandbox.go+++ b/internal/api/sandbox.go@@ -793,6 +793,10 @@ func (s *Server) migrateSandbox(c echo.Context) error {IncomingAddr: prepResp.IncomingAddr,})if err != nil {+ cleanCtx, cleanCancel := context.WithTimeout(context.Background(), 10*time.Second)+ targetClient.DestroySandbox(cleanCtx, &pb.DestroySandboxRequest{SandboxId: id})+ cleanCancel()+ log.Printf("migrate %s: live migrate failed, cleaned up target on %s: %v", id, req.TargetWorker, err)return c.JSON(http.StatusInternalServerError, map[string]string{"error": "live migrate: " + err.Error()})}@@ -805,6 +809,10 @@ func (s *Server) migrateSandbox(c echo.Context) error {SandboxId: id,})if err != nil {+ cleanCtx, cleanCancel := context.WithTimeout(context.Background(), 10*time.Second)+ targetClient.DestroySandbox(cleanCtx, &pb.DestroySandboxRequest{SandboxId: id})+ cleanCancel()+ log.Printf("migrate %s: complete failed, cleaned up target on %s: %v", id, req.TargetWorker, err)return c.JSON(http.StatusInternalServerError, map[string]string{"error": "complete migration: " + err.Error()})}@@ -1061,6 +1069,7 @@ func (s *Server) setLimitsRemote(c echo.Context, sandboxID string, maxPids int32retryCancel()if err != nil {+ log.Printf("scale-migrate %s: post-migration SetLimits failed on %s: %v", sandboxID, workerID, err)return c.JSON(http.StatusInternalServerError, map[string]string{"error": "set limits failed after migration: " + err.Error(),})@@ -1138,23 +1147,11 @@ func (s *Server) migrateForScale(ctx context.Context, sandboxID string, sessiontemplate = "default"}- // The target QEMU must use the same base memory as the source for migration- // to work (QEMU requires matching RAM sizes). Use the session config to get- // the original base memory, not the new requested size. The virtio-mem pool- // handles scaling after migration completes.- var sbCfg struct {- MemoryMB int `json:"memoryMB"`- CpuCount int `json:"cpuCount"`- }- _ = json.Unmarshal(session.Config, &sbCfg)- baseMem := sbCfg.MemoryMB- if baseMem <= 0 {- baseMem = 256 // default from golden snapshot- }- baseCPU := sbCfg.CpuCount- if baseCPU <= 0 {- baseCPU = 1- }+ // IMPORTANT: Use the QEMU base memory (from golden snapshot), not the API-requested total.+ // Virtio-mem hotplug state transfers during migration — the target QEMU must start+ // with the same base memory as the source for the memory layout to match.+ baseMem := 256+ baseCPU := 1log.Printf("scale-migrate %s: migrating to %s (template=%s, baseMem=%dMB, targetMem=%dMB, cpu=%d)", sandboxID, target.ID, template, baseMem, memoryMB, cpuCount)@@ -1181,6 +1178,11 @@ func (s *Server) migrateForScale(ctx context.Context, sandboxID string, sessionIncomingAddr: prepResp.IncomingAddr,})if err != nil {+ // Clean up orphaned target QEMU — prep succeeded but migration failed+ cleanCtx, cleanCancel := context.WithTimeout(context.Background(), 10*time.Second)+ targetClient.DestroySandbox(cleanCtx, &pb.DestroySandboxRequest{SandboxId: sandboxID})+ cleanCancel()+ log.Printf("scale-migrate %s: live migrate failed, cleaned up target on %s: %v", sandboxID, target.ID, err)return fmt.Errorf("live migrate: %w", err)}@@ -1191,6 +1193,11 @@ func (s *Server) migrateForScale(ctx context.Context, sandboxID string, sessionSandboxId: sandboxID,})if err != nil {+ // Clean up orphaned target QEMU — migration transferred but completion failed+ cleanCtx, cleanCancel := context.WithTimeout(context.Background(), 10*time.Second)+ targetClient.DestroySandbox(cleanCtx, &pb.DestroySandboxRequest{SandboxId: sandboxID})+ cleanCancel()+ log.Printf("scale-migrate %s: complete failed, cleaned up target on %s: %v", sandboxID, target.ID, err)return fmt.Errorf("complete migration: %w", err)}diff --git a/internal/qemu/migration.go b/internal/qemu/migration.goindex 8e8233a5..3800f27d 100644--- a/internal/qemu/migration.go+++ b/internal/qemu/migration.go@@ -524,6 +524,19 @@ func (m *Manager) CompleteIncomingMigration(ctx context.Context, sandboxID strinlog.Printf("qemu: migration %s: clock sync failed: %v", sandboxID, err)}+ // Sync VM memory tracking with actual QEMU state.+ // After migration, the QEMU process has the source's virtio-mem hotplugged memory,+ // but the Go struct still has the prep values (virtioMemRequestedMB=0, MemoryMB=baseMem).+ // This causes totalCommittedMemoryMB() to underreport, leading to overcommit.+ if vm.qmp != nil {+ if actualVirtioMB := vm.qmp.GetVirtioMemSize(); actualVirtioMB > 0 {+ vm.virtioMemRequestedMB = actualVirtioMB+ vm.MemoryMB = vm.baseMemoryMB + actualVirtioMB+ log.Printf("qemu: migration %s: synced memory tracking: virtio-mem=%dMB, total=%dMB",+ sandboxID, actualVirtioMB, vm.MemoryMB)+ }+ }+// Notify metadata serverif m.onSandboxReady != nil {m.onSandboxReady(sandboxID, vm.network.GuestIP, vm.Template, vm.StartedAt)diff --git a/internal/qemu/qmp.go b/internal/qemu/qmp.goindex d800f10a..f0a74bf7 100644--- a/internal/qemu/qmp.go+++ b/internal/qemu/qmp.go@@ -268,6 +268,27 @@ func (q *QMPClient) SetVirtioMemSize(sizeMB int) error {})}+// GetVirtioMemSize returns the current requested-size of the virtio-mem device in MB.+// Returns 0 if the device doesn't exist or the query fails.+func (q *QMPClient) GetVirtioMemSize() int {+ cmd := qmpCommand{+ Execute: "qom-get",+ Arguments: map[string]interface{}{+ "path": "/machine/peripheral/vm0",+ "property": "requested-size",+ },+ }+ resp, err := q.execute(cmd, 5*time.Second)+ if err != nil || resp == nil || resp.Return == nil {+ return 0+ }+ var sizeBytes int64+ if err := json.Unmarshal(resp.Return, &sizeBytes); err != nil {+ return 0+ }+ return int(sizeBytes / (1024 * 1024))+}+// SendFd passes an open file descriptor to QEMU via the QMP getfd command.// QEMU receives the fd via SCM_RIGHTS and registers it under the given name.func (q *QMPClient) SendFd(name string, f *os.File) error {