Skip to content

Commit

Permalink
Revert "Remove task serialization and use host resource manager for t…
Browse files Browse the repository at this point in the history
…ask resources (#3723)"

This reverts commit 0a4673a.
  • Loading branch information
sparrc committed Jul 5, 2023
1 parent 61ad010 commit cb54139
Show file tree
Hide file tree
Showing 23 changed files with 404 additions and 280 deletions.
1 change: 1 addition & 0 deletions agent/acs/handler/acs_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,7 @@ func validateAddedTask(expectedTask apitask.Task, addedTask apitask.Task) error
Family: addedTask.Family,
Version: addedTask.Version,
DesiredStatusUnsafe: addedTask.GetDesiredStatus(),
StartSequenceNumber: addedTask.StartSequenceNumber,
}

if !reflect.DeepEqual(expectedTask, taskToCompareFromAdded) {
Expand Down
4 changes: 0 additions & 4 deletions agent/api/ecsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,6 @@ func (client *APIECSClient) GetHostResources() (map[string]*ecs.Resource, error)
}
resourceMap := make(map[string]*ecs.Resource)
for _, resource := range resources {
if *resource.Name == "PORTS" {
// Except for RCI, TCP Ports are named as PORTS_TCP in agent for Host Resources purpose
resource.Name = utils.Strptr("PORTS_TCP")
}
resourceMap[*resource.Name] = resource
}
return resourceMap, nil
Expand Down
40 changes: 29 additions & 11 deletions agent/api/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/agent/taskresource"
"github.com/aws/amazon-ecs-agent/agent/taskresource/asmauth"
"github.com/aws/amazon-ecs-agent/agent/taskresource/asmsecret"
Expand All @@ -48,7 +49,6 @@ import (
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/field"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/arn"
Expand Down Expand Up @@ -235,6 +235,9 @@ type Task struct {
// is handled properly so that the state storage continues to work.
SentStatusUnsafe apitaskstatus.TaskStatus `json:"SentStatus"`

StartSequenceNumber int64
StopSequenceNumber int64

// ExecutionCredentialsID is the ID of credentials that are used by agent to
// perform some action at the task level, such as pulling image from ECR
ExecutionCredentialsID string `json:"executionCredentialsID"`
Expand Down Expand Up @@ -310,6 +313,11 @@ func TaskFromACS(acsTask *ecsacs.Task, envelope *ecsacs.PayloadMessage) (*Task,
if err := json.Unmarshal(data, task); err != nil {
return nil, err
}
if task.GetDesiredStatus() == apitaskstatus.TaskRunning && envelope.SeqNum != nil {
task.StartSequenceNumber = *envelope.SeqNum
} else if task.GetDesiredStatus() == apitaskstatus.TaskStopped && envelope.SeqNum != nil {
task.StopSequenceNumber = *envelope.SeqNum
}

// Overrides the container command if it's set
for _, container := range task.Containers {
Expand Down Expand Up @@ -2818,6 +2826,22 @@ func (task *Task) GetAppMesh() *apiappmesh.AppMesh {
return task.AppMesh
}

// GetStopSequenceNumber returns the stop sequence number of a task
func (task *Task) GetStopSequenceNumber() int64 {
task.lock.RLock()
defer task.lock.RUnlock()

return task.StopSequenceNumber
}

// SetStopSequenceNumber sets the stop seqence number of a task
func (task *Task) SetStopSequenceNumber(seqnum int64) {
task.lock.Lock()
defer task.lock.Unlock()

task.StopSequenceNumber = seqnum
}

// SetPullStartedAt sets the task pullstartedat timestamp and returns whether
// this field was updated or not
func (task *Task) SetPullStartedAt(timestamp time.Time) bool {
Expand Down Expand Up @@ -3521,6 +3545,10 @@ func (task *Task) IsServiceConnectConnectionDraining() bool {
//
// * GPU
// - Return num of gpus requested (len of GPUIDs field)
//
// TODO remove this once ToHostResources is used
//
//lint:file-ignore U1000 Ignore all unused code
func (task *Task) ToHostResources() map[string]*ecs.Resource {
resources := make(map[string]*ecs.Resource)
// CPU
Expand Down Expand Up @@ -3634,13 +3662,3 @@ func (task *Task) ToHostResources() map[string]*ecs.Resource {
})
return resources
}

func (task *Task) HasActiveContainers() bool {
for _, container := range task.Containers {
containerStatus := container.GetKnownStatus()
if containerStatus >= apicontainerstatus.ContainerPulled && containerStatus <= apicontainerstatus.ContainerResourcesProvisioned {
return true
}
}
return false
}
9 changes: 5 additions & 4 deletions agent/api/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
mock_dockerapi "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi/mocks"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
mock_s3_factory "github.com/aws/amazon-ecs-agent/agent/s3/factory/mocks"
mock_ssm_factory "github.com/aws/amazon-ecs-agent/agent/ssm/factory/mocks"
"github.com/aws/amazon-ecs-agent/agent/taskresource"
Expand All @@ -53,7 +54,6 @@ import (
apieni "github.com/aws/amazon-ecs-agent/ecs-agent/api/eni"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
mock_credentials "github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/aws-sdk-go/service/secretsmanager"

"github.com/aws/amazon-ecs-agent/agent/taskresource/asmsecret"
Expand Down Expand Up @@ -1861,9 +1861,10 @@ func TestTaskFromACS(t *testing.T) {
Type: "elastic-inference",
},
},
CPU: 2.0,
Memory: 512,
ResourcesMapUnsafe: make(map[string][]taskresource.TaskResource),
StartSequenceNumber: 42,
CPU: 2.0,
Memory: 512,
ResourcesMapUnsafe: make(map[string][]taskresource.TaskResource),
}

seqNum := int64(42)
Expand Down
4 changes: 4 additions & 0 deletions agent/api/task/taskvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func TestMarshalTaskVolumesEFS(t *testing.T) {
"PullStoppedAt": "0001-01-01T00:00:00Z",
"ExecutionStoppedAt": "0001-01-01T00:00:00Z",
"SentStatus": "NONE",
"StartSequenceNumber": 0,
"StopSequenceNumber": 0,
"executionCredentialsID": "",
"ENI": null,
"AppMesh": null,
Expand Down Expand Up @@ -166,6 +168,8 @@ func TestUnmarshalTaskVolumesEFS(t *testing.T) {
"PullStoppedAt": "0001-01-01T00:00:00Z",
"ExecutionStoppedAt": "0001-01-01T00:00:00Z",
"SentStatus": "NONE",
"StartSequenceNumber": 0,
"StopSequenceNumber": 0,
"executionCredentialsID": "",
"ENI": null,
"AppMesh": null,
Expand Down
4 changes: 4 additions & 0 deletions agent/api/task/taskvolume_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func TestMarshalTaskVolumeFSxWindowsFileServer(t *testing.T) {
"PullStoppedAt": "0001-01-01T00:00:00Z",
"ExecutionStoppedAt": "0001-01-01T00:00:00Z",
"SentStatus": "NONE",
"StartSequenceNumber": 0,
"StopSequenceNumber": 0,
"executionCredentialsID": "",
"ENI": null,
"AppMesh": null,
Expand Down Expand Up @@ -116,6 +118,8 @@ func TestUnmarshalTaskVolumeFSxWindowsFileServer(t *testing.T) {
"PullStoppedAt": "0001-01-01T00:00:00Z",
"ExecutionStoppedAt": "0001-01-01T00:00:00Z",
"SentStatus": "NONE",
"StartSequenceNumber": 0,
"StopSequenceNumber": 0,
"executionCredentialsID": "",
"ENI": null,
"AppMesh": null,
Expand Down
2 changes: 0 additions & 2 deletions agent/app/agent_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ func TestDoStartCgroupInitHappyPath(t *testing.T) {
state.EXPECT().AllImageStates().Return(nil),
state.EXPECT().AllENIAttachments().Return(nil),
state.EXPECT().AllTasks().Return(nil),
state.EXPECT().AllTasks().Return(nil),
client.EXPECT().DiscoverPollEndpoint(gomock.Any()).Do(func(x interface{}) {
// Ensures that the test waits until acs session has bee started
discoverEndpointsInvoked.Done()
Expand Down Expand Up @@ -647,7 +646,6 @@ func TestDoStartGPUManagerHappyPath(t *testing.T) {
state.EXPECT().AllImageStates().Return(nil),
state.EXPECT().AllENIAttachments().Return(nil),
state.EXPECT().AllTasks().Return(nil),
state.EXPECT().AllTasks().Return(nil),
client.EXPECT().DiscoverPollEndpoint(gomock.Any()).Do(func(x interface{}) {
// Ensures that the test waits until acs session has been started
discoverEndpointsInvoked.Done()
Expand Down
2 changes: 1 addition & 1 deletion agent/app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"strings"

"github.com/aws/amazon-ecs-agent/agent/data"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/agent/engine"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/engine/execcmd"
"github.com/aws/amazon-ecs-agent/agent/engine/serviceconnect"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/eventstream"

"github.com/pkg/errors"
Expand Down
4 changes: 1 addition & 3 deletions agent/engine/common_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,14 @@ func skipIntegTestIfApplicable(t *testing.T) {
}
}

// Values in host resources from getTestHostResources() should be looked at and CPU/Memory assigned
// accordingly
func createTestContainerWithImageAndName(image string, name string) *apicontainer.Container {
return &apicontainer.Container{
Name: name,
Image: image,
Command: []string{},
Essential: true,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 1024,
Memory: 128,
}
}
Expand Down
4 changes: 2 additions & 2 deletions agent/engine/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import (
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
mock_dockerapi "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi/mocks"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/agent/engine/execcmd"
mock_engine "github.com/aws/amazon-ecs-agent/agent/engine/mocks"
"github.com/aws/amazon-ecs-agent/agent/statechange"
"github.com/aws/amazon-ecs-agent/agent/utils"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
mock_ttime "github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime/mocks"
"github.com/aws/amazon-ecs-agent/agent/utils"
"github.com/cihub/seelog"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/golang/mock/gomock"
Expand Down
2 changes: 1 addition & 1 deletion agent/engine/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/engine/execcmd"
"github.com/aws/amazon-ecs-agent/agent/engine/serviceconnect"
"github.com/aws/amazon-ecs-agent/agent/taskresource"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/eventstream"
)

Expand Down
12 changes: 6 additions & 6 deletions agent/engine/docker_image_manager_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,23 +568,23 @@ func createImageCleanupHappyTestTask(taskName string) *apitask.Task {
Image: test1Image1Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 512,
Memory: 256,
},
{
Name: "test2",
Image: test1Image2Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 512,
Memory: 256,
},
{
Name: "test3",
Image: test1Image3Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 512,
Memory: 256,
},
},
Expand All @@ -603,23 +603,23 @@ func createImageCleanupThresholdTestTask(taskName string) *apitask.Task {
Image: test2Image1Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 512,
Memory: 256,
},
{
Name: "test2",
Image: test2Image2Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 512,
Memory: 256,
},
{
Name: "test3",
Image: test2Image3Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 256,
CPU: 512,
Memory: 256,
},
},
Expand Down

0 comments on commit cb54139

Please sign in to comment.