Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 74d0743

Browse files
committed
Better handle unknown state.
Signed-off-by: Lantao Liu <[email protected]>
1 parent b1bef15 commit 74d0743

5 files changed

Lines changed: 24 additions & 1 deletion

File tree

pkg/server/events.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ func handleContainerExit(ctx context.Context, e *eventtypes.TaskExit, cntr conta
333333
status.Pid = 0
334334
status.FinishedAt = e.ExitedAt.UnixNano()
335335
status.ExitCode = int32(e.ExitStatus)
336+
// Unknown state can only transit to EXITED state, so we need
337+
// to handle unknown state here.
338+
if status.Unknown {
339+
logrus.Debugf("Container %q transited from UNKNOWN to EXITED", cntr.ID)
340+
status.Unknown = false
341+
}
336342
return status, nil
337343
})
338344
if err != nil {

pkg/server/helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ func unknownContainerStatus() containerstore.Status {
465465
FinishedAt: 0,
466466
ExitCode: unknownExitCode,
467467
Reason: unknownExitReason,
468+
Unknown: true,
468469
}
469470
}
470471

pkg/server/restart.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ func (c *criService) loadContainer(ctx context.Context, cntr containerd.Containe
307307
}()
308308
if err != nil {
309309
log.G(ctx).WithError(err).Errorf("Failed to load container status for %q", id)
310-
status = unknownContainerStatus()
310+
// Only set the unknown field in this case, because other fields may
311+
// contain useful information loaded from the checkpoint.
312+
status.Unknown = true
311313
}
312314
opts := []containerstore.Opts{
313315
containerstore.WithStatus(status, containerDir),

pkg/store/container/status.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ type Status struct {
9494
// Removing indicates that the container is in removing state.
9595
// This field doesn't need to be checkpointed.
9696
Removing bool `json:"-"`
97+
// Unknown indicates that the container status is not fully loaded.
98+
// This field doesn't need to be checkpointed.
99+
Unknown bool `json:"-"`
97100
}
98101

99102
// State returns current state of the container based on the container status.
100103
func (s Status) State() runtime.ContainerState {
104+
if s.Unknown {
105+
return runtime.ContainerState_CONTAINER_UNKNOWN
106+
}
101107
if s.FinishedAt != 0 {
102108
return runtime.ContainerState_CONTAINER_EXITED
103109
}

pkg/store/container/status_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func TestContainerState(t *testing.T) {
3636
state runtime.ContainerState
3737
}{
3838
"unknown state": {
39+
status: Status{
40+
Unknown: true,
41+
},
42+
state: runtime.ContainerState_CONTAINER_UNKNOWN,
43+
},
44+
"unknown state because there is no timestamp set": {
3945
status: Status{},
4046
state: runtime.ContainerState_CONTAINER_UNKNOWN,
4147
},
@@ -76,6 +82,7 @@ func TestStatusEncodeDecode(t *testing.T) {
7682
Message: "test-message",
7783
Removing: true,
7884
Starting: true,
85+
Unknown: true,
7986
}
8087
assert := assertlib.New(t)
8188
data, err := s.encode()
@@ -84,6 +91,7 @@ func TestStatusEncodeDecode(t *testing.T) {
8491
assert.NoError(newS.decode(data))
8592
s.Removing = false // Removing should not be encoded.
8693
s.Starting = false // Starting should not be encoded.
94+
s.Unknown = false // Unknown should not be encoded.
8795
assert.Equal(s, newS)
8896

8997
unsupported, err := json.Marshal(&versionedStatus{

0 commit comments

Comments
 (0)