|
14 | 14 |
|
15 | 15 | package kernel
|
16 | 16 |
|
| 17 | +import ( |
| 18 | + "gvisor.dev/gvisor/pkg/log" |
| 19 | + "gvisor.dev/gvisor/pkg/sync" |
| 20 | +) |
| 21 | + |
17 | 22 | // Saver is an interface for saving the kernel.
|
18 | 23 | type Saver interface {
|
19 | 24 | SaveAsync() error
|
20 | 25 | SpecEnviron(containerName string) []string
|
21 | 26 | }
|
22 | 27 |
|
| 28 | +// CheckpointGeneration stores information about the last checkpoint taken. |
| 29 | +// |
| 30 | +// +stateify savable |
| 31 | +type CheckpointGeneration struct { |
| 32 | + // Count is incremented every time a checkpoint is triggered, even if the |
| 33 | + // chekpoint failed. |
| 34 | + Count uint32 |
| 35 | + // Restore indicates if the current instance resumed after the checkpoint or |
| 36 | + // it was restored from a checkpoint. |
| 37 | + Restore bool |
| 38 | +} |
| 39 | + |
23 | 40 | // AddStateToCheckpoint adds a key-value pair to be additionally checkpointed.
|
24 | 41 | func (k *Kernel) AddStateToCheckpoint(key, v any) {
|
25 | 42 | k.checkpointMu.Lock()
|
@@ -58,62 +75,120 @@ func (k *Kernel) Saver() Saver {
|
58 | 75 | return k.saver
|
59 | 76 | }
|
60 | 77 |
|
61 |
| -// IncCheckpointCount increments the checkpoint counter. |
62 |
| -func (k *Kernel) IncCheckpointCount() { |
| 78 | +// CheckpointGen returns the current checkpoint generation. |
| 79 | +func (k *Kernel) CheckpointGen() CheckpointGeneration { |
63 | 80 | k.checkpointMu.Lock()
|
64 | 81 | defer k.checkpointMu.Unlock()
|
65 |
| - k.checkpointCounter++ |
| 82 | + |
| 83 | + return k.checkpointGen |
66 | 84 | }
|
67 | 85 |
|
68 |
| -// CheckpointCount returns the current checkpoint count. Note that the result |
69 |
| -// may be stale by the time the caller uses it. |
70 |
| -func (k *Kernel) CheckpointCount() uint32 { |
| 86 | +// OnRestoreDone is called to notify the kernel that a checkpoint restore has been |
| 87 | +// completed successfully. |
| 88 | +func (k *Kernel) OnRestoreDone() { |
71 | 89 | k.checkpointMu.Lock()
|
72 | 90 | defer k.checkpointMu.Unlock()
|
73 |
| - return k.checkpointCounter |
| 91 | + |
| 92 | + k.checkpointGen.Count++ |
| 93 | + k.checkpointGen.Restore = true |
| 94 | + |
| 95 | + k.CheckpointWait.signal(k.checkpointGen, nil) |
74 | 96 | }
|
75 | 97 |
|
76 | 98 | // OnCheckpointAttempt is called when a checkpoint attempt is completed. err is
|
77 | 99 | // any checkpoint errors that may have occurred.
|
78 | 100 | func (k *Kernel) OnCheckpointAttempt(err error) {
|
79 |
| - k.checkpointMu.Lock() |
80 |
| - defer k.checkpointMu.Unlock() |
81 | 101 | if err == nil {
|
82 |
| - k.checkpointCounter++ |
| 102 | + log.Infof("Checkpoint completed successfully.") |
| 103 | + } else { |
| 104 | + log.Warningf("Checkpoint attempt failed with error: %v", err) |
83 | 105 | }
|
84 |
| - k.lastCheckpointStatus = err |
85 |
| - k.checkpointCond.Broadcast() |
86 |
| -} |
87 | 106 |
|
88 |
| -// ResetCheckpointStatus resets the last checkpoint status, indicating a new |
89 |
| -// checkpoint is in progress. Caller must call OnCheckpointAttempt when the |
90 |
| -// checkpoint attempt is completed. |
91 |
| -func (k *Kernel) ResetCheckpointStatus() { |
92 | 107 | k.checkpointMu.Lock()
|
93 | 108 | defer k.checkpointMu.Unlock()
|
94 |
| - k.lastCheckpointStatus = nil |
| 109 | + |
| 110 | + k.checkpointGen.Count++ |
| 111 | + k.checkpointGen.Restore = false |
| 112 | + |
| 113 | + k.CheckpointWait.signal(k.checkpointGen, err) |
95 | 114 | }
|
96 | 115 |
|
97 |
| -// WaitCheckpoint waits for the Kernel to have been successfully checkpointed |
98 |
| -// n-1 times, then waits for either the n-th successful checkpoint (in which |
99 |
| -// case it returns nil) or any number of failed checkpoints (in which case it |
100 |
| -// returns an error returned by any such failure). |
101 |
| -func (k *Kernel) WaitCheckpoint(n uint32) error { |
102 |
| - if n == 0 { |
103 |
| - return nil |
| 116 | +// WaitForCheckpoint waits for the Kernel to have been successfully checkpointed. |
| 117 | +func (k *Kernel) WaitForCheckpoint() error { |
| 118 | + // Send checkpoint result to a channel and wait on it. |
| 119 | + ch := make(chan error, 1) |
| 120 | + callback := func(_ CheckpointGeneration, err error) { ch <- err } |
| 121 | + key := k.CheckpointWait.Register(callback, k.CheckpointGen().Count+1) |
| 122 | + defer k.CheckpointWait.Unregister(key) |
| 123 | + |
| 124 | + return <-ch |
| 125 | +} |
| 126 | + |
| 127 | +type checkpointWaiter struct { |
| 128 | + // count indicates the checkpoint generation that this waiter is interested in. |
| 129 | + count uint32 |
| 130 | + // callback is the function that will be called when the checkpoint generation |
| 131 | + // reaches the desired count. It is set to nil after the callback is called. |
| 132 | + callback func(CheckpointGeneration, error) |
| 133 | +} |
| 134 | + |
| 135 | +// CheckpointWaitable is a waitable object that waits for a |
| 136 | +// checkpoint to complete. |
| 137 | +// |
| 138 | +// +stateify savable |
| 139 | +type CheckpointWaitable struct { |
| 140 | + k *Kernel |
| 141 | + |
| 142 | + mu sync.Mutex `state:"nosave"` |
| 143 | + |
| 144 | + // Don't save the waiters, because they are repopulated after restore. It also |
| 145 | + // allows for external entities to wait for the checkpoint. |
| 146 | + waiters map[*checkpointWaiter]struct{} `state:"nosave"` |
| 147 | +} |
| 148 | + |
| 149 | +// Register registers a callback that is notified when the checkpoint generation count is higher |
| 150 | +// than the desired count. |
| 151 | +func (w *CheckpointWaitable) Register(cb func(CheckpointGeneration, error), count uint32) any { |
| 152 | + w.mu.Lock() |
| 153 | + defer w.mu.Unlock() |
| 154 | + |
| 155 | + waiter := &checkpointWaiter{ |
| 156 | + count: count, |
| 157 | + callback: cb, |
104 | 158 | }
|
105 |
| - k.checkpointMu.Lock() |
106 |
| - defer k.checkpointMu.Unlock() |
107 |
| - if k.checkpointCounter >= n { |
108 |
| - // n-th checkpoint already completed successfully. |
109 |
| - return nil |
| 159 | + if w.waiters == nil { |
| 160 | + w.waiters = make(map[*checkpointWaiter]struct{}) |
| 161 | + } |
| 162 | + w.waiters[waiter] = struct{}{} |
| 163 | + |
| 164 | + if gen := w.k.CheckpointGen(); count <= gen.Count { |
| 165 | + // The checkpoint has already occurred. Signal immediately. |
| 166 | + waiter.callback(gen, nil) |
| 167 | + waiter.callback = nil |
110 | 168 | }
|
111 |
| - for k.checkpointCounter < n { |
112 |
| - if k.checkpointCounter == n-1 && k.lastCheckpointStatus != nil { |
113 |
| - // n-th checkpoint was attempted but it had failed. |
114 |
| - return k.lastCheckpointStatus |
| 169 | + return waiter |
| 170 | +} |
| 171 | + |
| 172 | +// Unregister unregisters a waiter. It must be called even if the channel |
| 173 | +// was signalled. |
| 174 | +func (w *CheckpointWaitable) Unregister(key any) { |
| 175 | + w.mu.Lock() |
| 176 | + defer w.mu.Unlock() |
| 177 | + |
| 178 | + delete(w.waiters, key.(*checkpointWaiter)) |
| 179 | + if len(w.waiters) == 0 { |
| 180 | + w.waiters = nil |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func (w *CheckpointWaitable) signal(gen CheckpointGeneration, err error) { |
| 185 | + w.mu.Lock() |
| 186 | + defer w.mu.Unlock() |
| 187 | + |
| 188 | + for waiter := range w.waiters { |
| 189 | + if waiter.callback != nil && waiter.count <= gen.Count { |
| 190 | + waiter.callback(gen, err) |
| 191 | + waiter.callback = nil |
115 | 192 | }
|
116 |
| - k.checkpointCond.Wait() |
117 | 193 | }
|
118 |
| - return nil |
119 | 194 | }
|
0 commit comments