Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UpdateDesiredStatus for task stop verification ACK #4240

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions agent/acs/session/task_manifest_responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ import (
)

const (
initialSeqNum = 11
nextSeqNum = 12
taskARN1 = "arn1"
taskARN2 = "arn2"
taskARN3 = "arn3"
initialSeqNum = 11
nextSeqNum = 12
taskARN1 = "arn1"
taskARN2 = "arn2"
taskARN3 = "arn3"
containerName1 = "name1"
containerName2 = "name2"
containerName3 = "name3"
)

var expectedTaskManifestAck = &ecsacs.AckRequest{
Expand Down
13 changes: 11 additions & 2 deletions agent/acs/session/task_stop_verification_ack_responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package session

import (
"github.com/aws/amazon-ecs-agent/agent/data"
"github.com/aws/amazon-ecs-agent/agent/engine"
apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
Expand All @@ -23,12 +24,14 @@ import (
// taskStopper implements the TaskStopper interface defined in ecs-agent module.
type taskStopper struct {
taskEngine engine.TaskEngine
dataClient data.Client
}

// NewTaskStopper creates a new taskStopper.
func NewTaskStopper(taskEngine engine.TaskEngine) *taskStopper {
func NewTaskStopper(taskEngine engine.TaskEngine, dataClient data.Client) *taskStopper {
return &taskStopper{
taskEngine: taskEngine,
dataClient: dataClient,
}
}

Expand All @@ -39,7 +42,13 @@ func (ts *taskStopper) StopTask(taskARN string) {
loggerfield.TaskARN: task.Arn,
})
task.SetDesiredStatus(apitaskstatus.TaskStopped)
ts.taskEngine.AddTask(task)
task.UpdateDesiredStatus()
if err := ts.dataClient.SaveTask(task); err != nil {
logger.Error("Failed to save data for task", logger.Fields{
loggerfield.TaskARN: task.Arn,
loggerfield.Error: err,
})
}
} else {
danehlim marked this conversation as resolved.
Show resolved Hide resolved
logger.Debug("Task from task stop verification ACK not found on the instance", logger.Fields{
loggerfield.TaskARN: taskARN,
Expand Down
59 changes: 38 additions & 21 deletions agent/acs/session/task_stop_verification_ack_responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package session
import (
"testing"

"github.com/aws/amazon-ecs-agent/agent/api/container"
"github.com/aws/amazon-ecs-agent/agent/api/task"
"github.com/aws/amazon-ecs-agent/agent/data"
mock_engine "github.com/aws/amazon-ecs-agent/agent/engine/mocks"
"github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs"
acssession "github.com/aws/amazon-ecs-agent/ecs-agent/acs/session"
"github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/testconst"
apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status"
apitaskstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/task/status"
"github.com/aws/amazon-ecs-agent/ecs-agent/metrics"
"github.com/aws/amazon-ecs-agent/ecs-agent/wsclient"
Expand All @@ -44,7 +47,7 @@ func setupTaskStopVerificationAckTest(t *testing.T) *taskStopVerificationAckTest
manifestMessageIDAccessor := NewManifestMessageIDAccessor()
manifestMessageIDAccessor.SetMessageID(testconst.MessageID)
taskStopVerificationAckResponder := acssession.NewTaskStopVerificationACKResponder(
NewTaskStopper(taskEngine),
NewTaskStopper(taskEngine, data.NewNoopClient()),
manifestMessageIDAccessor,
metrics.NewNopEntryFactory())

Expand All @@ -58,9 +61,27 @@ func setupTaskStopVerificationAckTest(t *testing.T) *taskStopVerificationAckTest
// defaultTasksOnInstance returns a baseline map of tasks that simulates/tracks the tasks on an instance.
func defaultTasksOnInstance() map[string]*task.Task {
return map[string]*task.Task{
taskARN1: {Arn: taskARN1, DesiredStatusUnsafe: apitaskstatus.TaskRunning},
taskARN2: {Arn: taskARN2, DesiredStatusUnsafe: apitaskstatus.TaskRunning},
taskARN3: {Arn: taskARN3, DesiredStatusUnsafe: apitaskstatus.TaskRunning},
taskARN1: {Arn: taskARN1, DesiredStatusUnsafe: apitaskstatus.TaskRunning,
Containers: []*container.Container{
{
Name: containerName1,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
},
}},
taskARN2: {Arn: taskARN2, DesiredStatusUnsafe: apitaskstatus.TaskRunning,
Containers: []*container.Container{
{
Name: containerName2,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
},
}},
taskARN3: {Arn: taskARN3, DesiredStatusUnsafe: apitaskstatus.TaskRunning,
Containers: []*container.Container{
{
Name: containerName3,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
},
}},
}
}

Expand Down Expand Up @@ -99,21 +120,24 @@ func TestTaskStopVerificationAckResponderStopsMultipleTasks(t *testing.T) {

tester.taskEngine.EXPECT().GetTaskByArn(taskARN2).Return(tasksOnInstance[taskARN2], true)
tester.taskEngine.EXPECT().GetTaskByArn(taskARN3).Return(tasksOnInstance[taskARN3], true)
tester.taskEngine.EXPECT().AddTask(tasksOnInstance[taskARN2]).Do(func(task *task.Task) {
task.SetDesiredStatus(apitaskstatus.TaskStopped)
})
tester.taskEngine.EXPECT().AddTask(tasksOnInstance[taskARN3]).Do(func(task *task.Task) {
task.SetDesiredStatus(apitaskstatus.TaskStopped)
})

handleTaskStopVerificationAck :=
tester.taskStopVerificationAckResponder.HandlerFunc().(func(message *ecsacs.TaskStopVerificationAck))
handleTaskStopVerificationAck(taskStopVerificationAck)

// Only task2 and task3 should be stopped.
// Only task2 and task3 and their containers should be stopped.
assert.Equal(t, apitaskstatus.TaskRunning, tasksOnInstance[taskARN1].GetDesiredStatus())
container1, ok := tasksOnInstance[taskARN1].ContainerByName(containerName1)
assert.True(t, ok)
assert.Equal(t, apicontainerstatus.ContainerRunning, container1.GetDesiredStatus())
assert.Equal(t, apitaskstatus.TaskStopped, tasksOnInstance[taskARN2].GetDesiredStatus())
container2, ok := tasksOnInstance[taskARN2].ContainerByName(containerName2)
assert.True(t, ok)
assert.Equal(t, apicontainerstatus.ContainerStopped, container2.GetDesiredStatus())
assert.Equal(t, apitaskstatus.TaskStopped, tasksOnInstance[taskARN3].GetDesiredStatus())
container3, ok := tasksOnInstance[taskARN3].ContainerByName(containerName3)
assert.True(t, ok)
assert.Equal(t, apicontainerstatus.ContainerStopped, container3.GetDesiredStatus())

}

Expand Down Expand Up @@ -149,22 +173,15 @@ func TestTaskStopVerificationAckResponderStopsAllTasks(t *testing.T) {
tester.taskEngine.EXPECT().GetTaskByArn(taskARN1).Return(tasksOnInstance[taskARN1], true)
tester.taskEngine.EXPECT().GetTaskByArn(taskARN2).Return(tasksOnInstance[taskARN2], true)
tester.taskEngine.EXPECT().GetTaskByArn(taskARN3).Return(tasksOnInstance[taskARN3], true)
tester.taskEngine.EXPECT().AddTask(tasksOnInstance[taskARN1]).Do(func(task *task.Task) {
task.SetDesiredStatus(apitaskstatus.TaskStopped)
})
tester.taskEngine.EXPECT().AddTask(tasksOnInstance[taskARN2]).Do(func(task *task.Task) {
task.SetDesiredStatus(apitaskstatus.TaskStopped)
})
tester.taskEngine.EXPECT().AddTask(tasksOnInstance[taskARN3]).Do(func(task *task.Task) {
task.SetDesiredStatus(apitaskstatus.TaskStopped)
})

handleTaskStopVerificationAck :=
tester.taskStopVerificationAckResponder.HandlerFunc().(func(message *ecsacs.TaskStopVerificationAck))
handleTaskStopVerificationAck(taskStopVerificationAck)

// All tasks on instance should be stopped.
// All tasks and containers on instance should be stopped.
for _, task := range tasksOnInstance {
assert.Equal(t, apitaskstatus.TaskStopped, task.GetDesiredStatus())
assert.Equal(t, 1, len(task.Containers))
assert.Equal(t, apicontainerstatus.ContainerStopped, task.Containers[0].GetDesiredStatus())
}
}
2 changes: 1 addition & 1 deletion agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ func (agent *ecsAgent) startACSSession(
manifestMessageIDAccessor := agentacs.NewManifestMessageIDAccessor()
sequenceNumberAccessor := agentacs.NewSequenceNumberAccessor(agent.latestSeqNumberTaskManifest, agent.dataClient)
taskComparer := agentacs.NewTaskComparer(taskEngine)
taskStopper := agentacs.NewTaskStopper(taskEngine)
taskStopper := agentacs.NewTaskStopper(taskEngine, agent.dataClient)

acsSession := session.NewSession(agent.containerInstanceARN,
agent.cfg.Cluster,
Expand Down
Loading