@@ -17,6 +17,10 @@ import (
1717
1818const (
1919 cmdlineFileName = "cmdline"
20+
21+ // chMemoryRestoreOnDemand uses userfaultfd (UFFD) to lazily page in
22+ // guest memory from the snapshot file, avoiding a full upfront copy.
23+ chMemoryRestoreOnDemand chMemoryRestoreMode = "OnDemand"
2024)
2125
2226var runtimeFiles = []string {hypervisor .APISocketName , "ch.pid" , hypervisor .ConsoleSockName , cmdlineFileName }
@@ -68,15 +72,17 @@ func resumeVM(ctx context.Context, hc *http.Client) error {
6872 return vmAPI (ctx , hc , "vm.resume" , nil )
6973}
7074
71- // snapshotVM and restoreVM bypass vmAPI's retry layer — see hypervisor.VMMemTransferTimeout.
72- func snapshotVM (ctx context.Context , sockPath , destDir string ) error {
75+ // snapshotVM and restoreVM temporarily extend the client timeout for
76+ // long-running memory transfers, then restore it for subsequent calls.
77+ func snapshotVM (ctx context.Context , hc * http.Client , destDir string ) error {
78+ hc .Timeout = hypervisor .VMMemTransferTimeout
79+ defer func () { hc .Timeout = utils .HTTPTimeout }()
7380 body , err := json .Marshal (map [string ]string {
7481 "destination_url" : "file://" + destDir ,
7582 })
7683 if err != nil {
7784 return fmt .Errorf ("marshal snapshot request: %w" , err )
7885 }
79- hc := utils .NewSocketHTTPClientWithTimeout (sockPath , hypervisor .VMMemTransferTimeout )
8086 _ , err = utils .DoAPI (ctx , hc , http .MethodPut ,
8187 "http://localhost/api/v1/vm.snapshot" , body , http .StatusNoContent )
8288 return err
@@ -85,18 +91,14 @@ func snapshotVM(ctx context.Context, sockPath, destDir string) error {
8591// chMemoryRestoreMode controls how CH restores guest memory from a snapshot.
8692type chMemoryRestoreMode string
8793
88- const (
89- // chMemoryRestoreOnDemand uses userfaultfd (UFFD) to lazily page in
90- // guest memory from the snapshot file, avoiding a full upfront copy.
91- chMemoryRestoreOnDemand chMemoryRestoreMode = "OnDemand"
92- )
93-
9494type chRestoreConfig struct {
9595 SourceURL string `json:"source_url"`
9696 MemoryRestoreMode chMemoryRestoreMode `json:"memory_restore_mode,omitempty"`
9797}
9898
9999func restoreVM (ctx context.Context , hc * http.Client , sourceDir string , onDemand bool ) error {
100+ hc .Timeout = hypervisor .VMMemTransferTimeout
101+ defer func () { hc .Timeout = utils .HTTPTimeout }()
100102 cfg := chRestoreConfig {
101103 SourceURL : "file://" + sourceDir ,
102104 }
0 commit comments