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

Commit 5420c6f

Browse files
authored
Merge pull request #1354 from Random-Liu/cherrypick-#1351-release-1.2
[release/1.2] Better handle unknown state.
2 parents 57022a5 + 12b0943 commit 5420c6f

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
@@ -337,6 +337,12 @@ func handleContainerExit(ctx context.Context, e *eventtypes.TaskExit, cntr conta
337337
status.Pid = 0
338338
status.FinishedAt = e.ExitedAt.UnixNano()
339339
status.ExitCode = int32(e.ExitStatus)
340+
// Unknown state can only transit to EXITED state, so we need
341+
// to handle unknown state here.
342+
if status.Unknown {
343+
logrus.Debugf("Container %q transited from UNKNOWN to EXITED", cntr.ID)
344+
status.Unknown = false
345+
}
340346
return status, nil
341347
})
342348
if err != nil {

pkg/server/helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ func unknownContainerStatus() containerstore.Status {
576576
FinishedAt: 0,
577577
ExitCode: unknownExitCode,
578578
Reason: unknownExitReason,
579+
Unknown: true,
579580
}
580581
}
581582

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
logrus.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
@@ -95,10 +95,16 @@ type Status struct {
9595
// Removing indicates that the container is in removing state.
9696
// This field doesn't need to be checkpointed.
9797
Removing bool `json:"-"`
98+
// Unknown indicates that the container status is not fully loaded.
99+
// This field doesn't need to be checkpointed.
100+
Unknown bool `json:"-"`
98101
}
99102

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

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)