An open-source, faster Devin alternative
TypeScript
restore full on fork
Brian Reardona3e9716b8302
diff --git a/internal/qemu/manager.go b/internal/qemu/manager.goindex f7eba95d..bbb8b999 100644--- a/internal/qemu/manager.go+++ b/internal/qemu/manager.go@@ -19,7 +19,6 @@ import ("github.com/google/uuid""github.com/opensandbox/opensandbox/internal/sandbox"- "github.com/opensandbox/opensandbox/internal/sparse""github.com/opensandbox/opensandbox/internal/storage""github.com/opensandbox/opensandbox/pkg/types"pb "github.com/opensandbox/opensandbox/proto/agent"@@ -1958,45 +1957,43 @@ func (m *Manager) CreateCheckpoint(ctx context.Context, sandboxID, checkpointIDlog.Printf("qemu: checkpoint %s: cache saved (%dms)", checkpointID, time.Since(t0).Milliseconds())- // Upload checkpoint to S3 in the background so cross-worker forks can download it.- // Returns a channel that closes when the upload completes — callers that need- // the upload to finish (e.g. image builder) can wait on it.- // Rootfs: tar.zst archive (download path uses extractArchiveCmd)- // Workspace: sparse.zst archive (download path uses extractSparseCmd / OSBSPAR1 format)+ // Upload full checkpoint to S3 in the background so cross-worker forks can download it.+ // The archive includes drives + memory dump + metadata — everything ForkFromCheckpoint needs.+ // Image builder waits for upload to finish (via WaitUploads) before forking.if checkpointStore != nil {+ // Build list of files to archive+ var archiveFiles []string+ archiveFiles = append(archiveFiles, "rootfs.qcow2", "workspace.qcow2")+ if fileExists(filepath.Join(cacheDir, "mem.zst")) {+ archiveFiles = append(archiveFiles, "mem.zst")+ } else if fileExists(filepath.Join(cacheDir, "mem")) {+ archiveFiles = append(archiveFiles, "mem")+ }+ if fileExists(filepath.Join(cacheDir, "snapshot", "snapshot-meta.json")) {+ archiveFiles = append(archiveFiles, filepath.Join("snapshot", "snapshot-meta.json"))+ }+m.uploadWg.Add(1)go func() {defer m.uploadWg.Done()t1 := time.Now()- // Rootfs → tar.zst (archive contains rootfs.qcow2)- rootfsArchive := filepath.Join(cacheDir, "rootfs.tar.zst")- if err := createArchive(rootfsArchive, cacheDir, []string{"rootfs.qcow2"}); err != nil {- log.Printf("qemu: checkpoint %s: rootfs archive failed: %v", checkpointID, err)- } else {- uploadCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)- if _, err := checkpointStore.Upload(uploadCtx, rootfsKey, rootfsArchive); err != nil {- log.Printf("qemu: checkpoint %s: rootfs S3 upload failed: %v", checkpointID, err)- }- cancel()- os.Remove(rootfsArchive)+ archivePath := filepath.Join(cacheDir, "checkpoint.tar.zst")+ if err := createArchive(archivePath, cacheDir, archiveFiles); err != nil {+ log.Printf("qemu: checkpoint %s: archive failed: %v", checkpointID, err)+ return}+ defer os.Remove(archivePath)- // Workspace → sparse.zst (OSBSPAR1 format, download path restores to workspace.ext4)- workspaceSrc := filepath.Join(cacheDir, "workspace.qcow2")- workspaceArchive := filepath.Join(cacheDir, "workspace.sparse.zst")- if _, err := sparse.Create(workspaceSrc, workspaceArchive); err != nil {- log.Printf("qemu: checkpoint %s: workspace sparse archive failed: %v", checkpointID, err)- } else {- uploadCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)- if _, err := checkpointStore.Upload(uploadCtx, workspaceKey, workspaceArchive); err != nil {- log.Printf("qemu: checkpoint %s: workspace S3 upload failed: %v", checkpointID, err)- }- cancel()- os.Remove(workspaceArchive)+ uploadCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)+ defer cancel()+ if _, err := checkpointStore.Upload(uploadCtx, rootfsKey, archivePath); err != nil {+ log.Printf("qemu: checkpoint %s: S3 upload failed: %v", checkpointID, err)+ return}- log.Printf("qemu: checkpoint %s: S3 upload complete (%dms)", checkpointID, time.Since(t1).Milliseconds())+ log.Printf("qemu: checkpoint %s: S3 upload complete (%dms, files=%v)",+ checkpointID, time.Since(t1).Milliseconds(), archiveFiles)}()}diff --git a/internal/worker/grpc_server.go b/internal/worker/grpc_server.goindex a04c8ed1..7f10fa23 100644--- a/internal/worker/grpc_server.go+++ b/internal/worker/grpc_server.go@@ -3,6 +3,7 @@ package workerimport ("context""fmt"+ "io""log""net""os"@@ -139,12 +140,9 @@ func (s *GRPCServer) CreateSandbox(ctx context.Context, req *pb.CreateSandboxReq// Warm fork: if checkpoint_id is set, fork from the local checkpoint cache.// ForkFromCheckpoint uses the local cache directly — no S3 needed.- // Skip resolveTemplateDrives here: it tries S3 first which can hang on DNS- // failures, and ForkFromCheckpoint already handles cache lookup internally.if req.CheckpointId != "" {sb, err := s.manager.ForkFromCheckpoint(ctx, req.CheckpointId, cfg)if err == nil {- // Register with sandbox router for rolling timeout trackingif s.router != nil {timeout := cfg.Timeoutif timeout <= 0 {@@ -157,14 +155,33 @@ func (s *GRPCServer) CreateSandbox(ctx context.Context, req *pb.CreateSandboxReqStatus: string(sb.Status),}, nil}- // Only fall back to S3 download if the checkpoint isn't cached locally- // (cross-worker restore). If the fork failed for other reasons (agent- // timeout, QMP error), retrying via S3 won't help and the blob may- // not even exist.- if !strings.Contains(err.Error(), "not found in cache") {+ // If not in local cache, download the full checkpoint from S3 and retry.+ // The archive includes drives + memory dump — everything needed for restore.+ if strings.Contains(err.Error(), "not found in cache") && req.TemplateRootfsKey != "" && s.checkpointStore != nil {+ log.Printf("grpc: warm fork %s: not in local cache, downloading from S3", req.CheckpointId)+ if dlErr := s.downloadFullCheckpoint(ctx, req.CheckpointId, req.TemplateRootfsKey); dlErr != nil {+ log.Printf("grpc: warm fork %s: S3 download failed: %v, falling back to template create", req.CheckpointId, dlErr)+ } else {+ sb, err = s.manager.ForkFromCheckpoint(ctx, req.CheckpointId, cfg)+ if err == nil {+ if s.router != nil {+ timeout := cfg.Timeout+ if timeout <= 0 {+ timeout = 300+ }+ s.router.Register(sb.ID, time.Duration(timeout)*time.Second)+ }+ return &pb.CreateSandboxResponse{+ SandboxId: sb.ID,+ Status: string(sb.Status),+ }, nil+ }+ log.Printf("grpc: warm fork %s: retry after S3 download failed: %v, falling back to template create", req.CheckpointId, err)+ }+ } else if !strings.Contains(err.Error(), "not found in cache") {return nil, fmt.Errorf("fork from checkpoint %s: %w", req.CheckpointId, err)}- log.Printf("grpc: ForkFromCheckpoint %s: not in local cache, falling back to S3 download", req.CheckpointId)+ log.Printf("grpc: warm fork %s: resolve drives failed: %v, continuing with ForkFromCheckpoint", req.CheckpointId, err)}// Handle sandbox snapshot template: resolve S3 keys to local paths.@@ -811,6 +828,49 @@ func (s *GRPCServer) downloadAndCacheCheckpointDrives(ctx context.Context, checktype extractFunc func(archivePath, destPath string) error// extractArchiveCmd extracts a tar.zst archive to a directory.+// downloadFullCheckpoint downloads a full checkpoint archive (drives + memory + metadata)+// from S3 and extracts it into the checkpoint cache directory for ForkFromCheckpoint.+func (s *GRPCServer) downloadFullCheckpoint(ctx context.Context, checkpointID, s3Key string) error {+ if s.checkpointStore == nil {+ return fmt.Errorf("checkpoint store not configured")+ }++ cacheDir := filepath.Join(s.manager.DataDir(), "checkpoint-snapshots", checkpointID)+ if err := os.MkdirAll(cacheDir, 0755); err != nil {+ return fmt.Errorf("create cache dir: %w", err)+ }++ // Download archive+ archivePath := filepath.Join(cacheDir, "checkpoint-download.tar.zst")+ rc, err := s.checkpointStore.Download(ctx, s3Key)+ if err != nil {+ return fmt.Errorf("download %s: %w", s3Key, err)+ }+ f, err := os.Create(archivePath)+ if err != nil {+ rc.Close()+ return fmt.Errorf("create archive file: %w", err)+ }+ if _, err := io.Copy(f, rc); err != nil {+ f.Close()+ rc.Close()+ os.Remove(archivePath)+ return fmt.Errorf("write archive: %w", err)+ }+ f.Close()+ rc.Close()++ // Extract (tar.zst)+ if err := extractArchiveCmd(archivePath, cacheDir); err != nil {+ os.Remove(archivePath)+ return fmt.Errorf("extract archive: %w", err)+ }+ os.Remove(archivePath)++ log.Printf("grpc: checkpoint %s: downloaded and cached from S3", checkpointID)+ return nil+}+func extractArchiveCmd(archivePath, destDir string) error {cmd := exec.Command("tar", "--zstd", "-xf", archivePath, "-C", destDir)if out, err := cmd.CombinedOutput(); err != nil {