Use channel to propagate the stop info of sandbox
Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>
This commit is contained in:
@@ -129,7 +129,7 @@ func (c *criContainerdService) stopContainer(ctx context.Context, container cont
|
||||
return nil
|
||||
}
|
||||
|
||||
// waitContainerStop waits for container to be stopped until timeout exceeds or centext is cancelled.
|
||||
// waitContainerStop waits for container to be stopped until timeout exceeds or context is cancelled.
|
||||
func (c *criContainerdService) waitContainerStop(ctx context.Context, container containerstore.Container, timeout time.Duration) error {
|
||||
timeoutTimer := time.NewTimer(timeout)
|
||||
defer timeoutTimer.Stop()
|
||||
|
||||
@@ -68,10 +68,6 @@ func TestWaitContainerStop(t *testing.T) {
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, c.containerStore.Add(container))
|
||||
if test.status.FinishedAt != 0 {
|
||||
// Fake the TaskExit event
|
||||
container.Stop()
|
||||
}
|
||||
ctx := context.Background()
|
||||
if test.cancel {
|
||||
cancelledCtx, cancel := context.WithCancel(ctx)
|
||||
|
||||
@@ -235,4 +235,6 @@ func handleSandboxExit(e *eventtypes.TaskExit, sb sandboxstore.Sandbox) {
|
||||
// TODO(random-liu): [P0] Enqueue the event and retry.
|
||||
return
|
||||
}
|
||||
// Using channel to propagate the information of sandbox stop
|
||||
sb.Stop()
|
||||
}
|
||||
|
||||
@@ -31,10 +31,6 @@ import (
|
||||
sandboxstore "github.com/containerd/cri-containerd/pkg/store/sandbox"
|
||||
)
|
||||
|
||||
// stopCheckPollInterval is the the interval to check whether a sandbox
|
||||
// is stopped successfully.
|
||||
const stopCheckPollInterval = 100 * time.Millisecond
|
||||
|
||||
// StopPodSandbox stops the sandbox. If there are any running containers in the
|
||||
// sandbox, they should be forcibly terminated.
|
||||
func (c *criContainerdService) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandboxRequest) (*runtime.StopPodSandboxResponse, error) {
|
||||
@@ -125,25 +121,16 @@ func (c *criContainerdService) stopSandboxContainer(ctx context.Context, sandbox
|
||||
return c.waitSandboxStop(ctx, sandbox, killContainerTimeout)
|
||||
}
|
||||
|
||||
// waitSandboxStop polls sandbox state until timeout exceeds or sandbox is stopped.
|
||||
// waitSandboxStop waits for sandbox to be stopped until timeout exceeds or context is cancelled.
|
||||
func (c *criContainerdService) waitSandboxStop(ctx context.Context, sandbox sandboxstore.Sandbox, timeout time.Duration) error {
|
||||
ticker := time.NewTicker(stopCheckPollInterval)
|
||||
defer ticker.Stop()
|
||||
timeoutTimer := time.NewTimer(timeout)
|
||||
defer timeoutTimer.Stop()
|
||||
for {
|
||||
// Poll once before waiting for stopCheckPollInterval.
|
||||
// TODO(random-liu): Use channel with event handler instead of polling.
|
||||
if sandbox.Status.Get().State == sandboxstore.StateNotReady {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("wait sandbox container %q is cancelled", sandbox.ID)
|
||||
case <-timeoutTimer.C:
|
||||
return fmt.Errorf("wait sandbox container %q stop timeout", sandbox.ID)
|
||||
case <-ticker.C:
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("wait sandbox container %q is cancelled", sandbox.ID)
|
||||
case <-timeoutTimer.C:
|
||||
return fmt.Errorf("wait sandbox container %q stop timeout", sandbox.ID)
|
||||
case <-sandbox.Stopped():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestWaitSandboxStop(t *testing.T) {
|
||||
}{
|
||||
"should return error if timeout exceeds": {
|
||||
state: sandboxstore.StateReady,
|
||||
timeout: 2 * stopCheckPollInterval,
|
||||
timeout: 200 * time.Millisecond,
|
||||
expectErr: true,
|
||||
},
|
||||
"should return error if context is cancelled": {
|
||||
|
||||
Reference in New Issue
Block a user