Skip to content

Commit 0877136

Browse files
Random-LiuthaJeztah
authored andcommitted
Use cached state instead of runc state.
Signed-off-by: Lantao Liu <[email protected]> (cherry picked from commit 18be6e3) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent f71f6d3 commit 0877136

6 files changed

Lines changed: 75 additions & 27 deletions

File tree

runtime/v1/linux/proc/deleted_state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ func (s *deletedState) SetExited(status int) {
7070
func (s *deletedState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
7171
return nil, errors.Errorf("cannot exec in a deleted state")
7272
}
73+
74+
func (s *deletedState) Status(ctx context.Context) (string, error) {
75+
return "stopped", nil
76+
}

runtime/v1/linux/proc/exec.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,5 @@ func (e *execProcess) Status(ctx context.Context) (string, error) {
255255
}
256256
e.mu.Lock()
257257
defer e.mu.Unlock()
258-
// if we don't have a pid(pid=0) then the exec process has just been created
259-
if e.pid.get() == 0 {
260-
return "created", nil
261-
}
262-
if e.pid.get() == StoppedPID {
263-
return "stopped", nil
264-
}
265-
// if we have a pid and it can be signaled, the process is running
266-
if err := unix.Kill(e.pid.get(), 0); err == nil {
267-
return "running", nil
268-
}
269-
// else if we have a pid but it can nolonger be signaled, it has stopped
270-
return "stopped", nil
258+
return e.execState.Status(ctx)
271259
}

runtime/v1/linux/proc/exec_state.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type execState interface {
3131
Delete(context.Context) error
3232
Kill(context.Context, uint32, bool) error
3333
SetExited(int)
34+
Status(context.Context) (string, error)
3435
}
3536

3637
type execCreatedState struct {
@@ -82,6 +83,10 @@ func (s *execCreatedState) SetExited(status int) {
8283
}
8384
}
8485

86+
func (s *execCreatedState) Status(ctx context.Context) (string, error) {
87+
return "created", nil
88+
}
89+
8590
type execRunningState struct {
8691
p *execProcess
8792
}
@@ -120,6 +125,10 @@ func (s *execRunningState) SetExited(status int) {
120125
}
121126
}
122127

128+
func (s *execRunningState) Status(ctx context.Context) (string, error) {
129+
return "running", nil
130+
}
131+
123132
type execStoppedState struct {
124133
p *execProcess
125134
}
@@ -157,3 +166,7 @@ func (s *execStoppedState) Kill(ctx context.Context, sig uint32, all bool) error
157166
func (s *execStoppedState) SetExited(status int) {
158167
// no op
159168
}
169+
170+
func (s *execStoppedState) Status(ctx context.Context) (string, error) {
171+
return "stopped", nil
172+
}

runtime/v1/linux/proc/init.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ type Init struct {
5959

6060
WorkDir string
6161

62-
id string
63-
Bundle string
64-
console console.Console
65-
Platform proc.Platform
66-
io runc.IO
67-
runtime *runc.Runc
62+
id string
63+
Bundle string
64+
console console.Console
65+
Platform proc.Platform
66+
io runc.IO
67+
runtime *runc.Runc
68+
// pausing preserves the pausing state.
69+
pausing *atomicBool
6870
status int
6971
exited time.Time
7072
pid safePid
@@ -99,6 +101,7 @@ func New(id string, runtime *runc.Runc, stdio proc.Stdio) *Init {
99101
p := &Init{
100102
id: id,
101103
runtime: runtime,
104+
pausing: new(atomicBool),
102105
stdio: stdio,
103106
status: 0,
104107
waitBlock: make(chan struct{}),
@@ -231,17 +234,14 @@ func (p *Init) ExitedAt() time.Time {
231234

232235
// Status of the process
233236
func (p *Init) Status(ctx context.Context) (string, error) {
237+
if p.pausing.get() {
238+
return "pausing", nil
239+
}
240+
234241
p.mu.Lock()
235242
defer p.mu.Unlock()
236243

237-
c, err := p.runtime.State(ctx, p.id)
238-
if err != nil {
239-
if strings.Contains(err.Error(), "does not exist") {
240-
return "stopped", nil
241-
}
242-
return "", p.runtimeError(err, "OCI runtime state failed")
243-
}
244-
return c.Status, nil
244+
return p.initState.Status(ctx)
245245
}
246246

247247
// Start the init process

runtime/v1/linux/proc/init_state.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type initState interface {
4343
Exec(context.Context, string, *ExecConfig) (proc.Process, error)
4444
Kill(context.Context, uint32, bool) error
4545
SetExited(int)
46+
Status(context.Context) (string, error)
4647
}
4748

4849
type createdState struct {
@@ -113,6 +114,10 @@ func (s *createdState) Exec(ctx context.Context, path string, r *ExecConfig) (pr
113114
return s.p.exec(ctx, path, r)
114115
}
115116

117+
func (s *createdState) Status(ctx context.Context) (string, error) {
118+
return "created", nil
119+
}
120+
116121
type createdCheckpointState struct {
117122
p *Init
118123
opts *runc.RestoreOpts
@@ -231,6 +236,10 @@ func (s *createdCheckpointState) Exec(ctx context.Context, path string, r *ExecC
231236
return nil, errors.Errorf("cannot exec in a created state")
232237
}
233238

239+
func (s *createdCheckpointState) Status(ctx context.Context) (string, error) {
240+
return "created", nil
241+
}
242+
234243
type runningState struct {
235244
p *Init
236245
}
@@ -248,6 +257,13 @@ func (s *runningState) transition(name string) error {
248257
}
249258

250259
func (s *runningState) Pause(ctx context.Context) error {
260+
s.p.pausing.set(true)
261+
// NOTE "pausing" will be returned in the short window
262+
// after `transition("paused")`, before `pausing` is reset
263+
// to false. That doesn't break the state machine, just
264+
// delays the "paused" state a little bit.
265+
defer s.p.pausing.set(false)
266+
251267
if err := s.p.runtime.Pause(ctx, s.p.id); err != nil {
252268
return s.p.runtimeError(err, "OCI runtime pause failed")
253269
}
@@ -295,6 +311,10 @@ func (s *runningState) Exec(ctx context.Context, path string, r *ExecConfig) (pr
295311
return s.p.exec(ctx, path, r)
296312
}
297313

314+
func (s *runningState) Status(ctx context.Context) (string, error) {
315+
return "running", nil
316+
}
317+
298318
type pausedState struct {
299319
p *Init
300320
}
@@ -363,6 +383,10 @@ func (s *pausedState) Exec(ctx context.Context, path string, r *ExecConfig) (pro
363383
return nil, errors.Errorf("cannot exec in a paused state")
364384
}
365385

386+
func (s *pausedState) Status(ctx context.Context) (string, error) {
387+
return "paused", nil
388+
}
389+
366390
type stoppedState struct {
367391
p *Init
368392
}
@@ -419,3 +443,7 @@ func (s *stoppedState) SetExited(status int) {
419443
func (s *stoppedState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
420444
return nil, errors.Errorf("cannot exec in a stopped state")
421445
}
446+
447+
func (s *stoppedState) Status(ctx context.Context) (string, error) {
448+
return "stopped", nil
449+
}

runtime/v1/linux/proc/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"strings"
2727
"sync"
28+
"sync/atomic"
2829
"time"
2930

3031
"github.com/containerd/containerd/errdefs"
@@ -51,6 +52,20 @@ func (s *safePid) set(pid int) {
5152
s.Unlock()
5253
}
5354

55+
type atomicBool int32
56+
57+
func (ab *atomicBool) set(b bool) {
58+
if b {
59+
atomic.StoreInt32((*int32)(ab), 1)
60+
} else {
61+
atomic.StoreInt32((*int32)(ab), 0)
62+
}
63+
}
64+
65+
func (ab *atomicBool) get() bool {
66+
return atomic.LoadInt32((*int32)(ab)) == 1
67+
}
68+
5469
// TODO(mlaventure): move to runc package?
5570
func getLastRuntimeError(r *runc.Runc) (string, error) {
5671
if r.Log == "" {

0 commit comments

Comments
 (0)