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

acs: add an empty task in TaskStateChange for unrecognized tasks #1467

Merged
merged 1 commit into from
Aug 14, 2018
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.21.0-dev
* Bug - Fixed a bug where unrecognized task cannot be stopped [#1467](https://github.com/aws/amazon-ecs-agent/pull/1467)

## 1.20.1
* Bug - Fixed a bug where the agent couldn't be upgraded if there are tasks that
use volumes in the task definition on the instance
Expand Down
3 changes: 1 addition & 2 deletions agent/acs/handler/acs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
package handler

import (
"context"
"io"
"net/url"
"strconv"
"strings"
"time"

"context"

acsclient "github.com/aws/amazon-ecs-agent/agent/acs/client"
"github.com/aws/amazon-ecs-agent/agent/acs/model/ecsacs"
"github.com/aws/amazon-ecs-agent/agent/acs/update_handler"
Expand Down
4 changes: 4 additions & 0 deletions agent/acs/handler/payload_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ func (payloadHandler *payloadRequestHandler) handleUnrecognizedTask(task *ecsacs
TaskARN: *task.Arn,
Status: apitaskstatus.TaskStopped,
Reason: UnrecognizedTaskError{err}.Error(),
// The real task cannot be extracted from payload message, so we send an empty task.
// This is necessary because the task handler will not send an event whose
// Task is nil.
Task: &apitask.Task{},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do a nil check here, as the task isn't always nil here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified offline.

}

payloadHandler.taskHandler.AddStateChangeEvent(taskEvent, payloadHandler.ecsClient)
Expand Down
32 changes: 30 additions & 2 deletions agent/acs/handler/payload_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package handler

import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"testing"

"context"

"github.com/aws/amazon-ecs-agent/agent/acs/model/ecsacs"
"github.com/aws/amazon-ecs-agent/agent/api"
"github.com/aws/amazon-ecs-agent/agent/api/mocks"
Expand Down Expand Up @@ -754,3 +755,30 @@ func TestPayloadHandlerAddedASMAuthData(t *testing.T) {
assert.Equal(t, aws.StringValue(expected.AsmAuthData.Region), actual.ASMAuthData.Region)
assert.Equal(t, aws.StringValue(expected.AsmAuthData.CredentialsParameter), actual.ASMAuthData.CredentialsParameter)
}

func TestHandleUnrecognizedTask(t *testing.T) {
tester := setup(t)
defer tester.ctrl.Finish()

arn := "arn"
ecsacsTask := &ecsacs.Task{Arn: &arn}
payloadMessage := &ecsacs.PayloadMessage{
Tasks: []*ecsacs.Task{ecsacsTask},
MessageId: aws.String(payloadMessageId),
}

mockECSACSClient := mock_api.NewMockECSClient(tester.ctrl)
taskHandler := eventhandler.NewTaskHandler(tester.ctx, tester.payloadHandler.saver, nil, mockECSACSClient)
tester.payloadHandler.taskHandler = taskHandler

wait := &sync.WaitGroup{}
wait.Add(1)

mockECSACSClient.EXPECT().SubmitTaskStateChange(gomock.Any()).Do(func(change api.TaskStateChange) {
assert.NotNil(t, change.Task)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we allow unrecognized tasks to run and complete? If so, how do we know that the task state changes after this are handled ok?

i.e. Is the empty task passed on to another component? And if so, we we creating a new untested code path by creating the empty task?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't allow unrecognized tasks to run or complete. If it's unrecognized, we simply drop it and tell backend that this task is stopped because it's unrecognized.

To stop a task, we don't even need to specify a task struct when calling SubmitTaskStateChange API, The only purpose I added an empty task here is to make this task change not dropped by Agent, so that it will be sent to backend via SubmitTaskStateChange API.

wait.Done()
})

tester.payloadHandler.handleUnrecognizedTask(ecsacsTask, errors.New("test error"), payloadMessage)
wait.Wait()
}