Skip to content

Commit

Permalink
Remove task serialization and use host resource manager for task reso…
Browse files Browse the repository at this point in the history
…urces (#3723)
  • Loading branch information
prateekchaudhry committed Jul 12, 2023
1 parent f26b79b commit 6bc7b20
Show file tree
Hide file tree
Showing 23 changed files with 280 additions and 404 deletions.
1 change: 0 additions & 1 deletion agent/acs/handler/acs_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,6 @@ 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: 4 additions & 0 deletions agent/api/ecsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ 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: 11 additions & 29 deletions agent/api/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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 @@ -49,6 +48,7 @@ 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,9 +235,6 @@ 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 @@ -313,11 +310,6 @@ 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 @@ -2826,22 +2818,6 @@ 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 @@ -3545,10 +3521,6 @@ 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 @@ -3662,3 +3634,13 @@ 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: 4 additions & 5 deletions agent/api/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ 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 @@ -54,6 +53,7 @@ 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,10 +1861,9 @@ func TestTaskFromACS(t *testing.T) {
Type: "elastic-inference",
},
},
StartSequenceNumber: 42,
CPU: 2.0,
Memory: 512,
ResourcesMapUnsafe: make(map[string][]taskresource.TaskResource),
CPU: 2.0,
Memory: 512,
ResourcesMapUnsafe: make(map[string][]taskresource.TaskResource),
}

seqNum := int64(42)
Expand Down
4 changes: 0 additions & 4 deletions agent/api/task/taskvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ 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 @@ -168,8 +166,6 @@ 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: 0 additions & 4 deletions agent/api/task/taskvolume_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ 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 @@ -118,8 +116,6 @@ 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: 2 additions & 0 deletions agent/app/agent_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ 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 @@ -646,6 +647,7 @@ 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: 3 additions & 1 deletion agent/engine/common_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,16 @@ 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: 1024,
CPU: 256,
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: 512,
CPU: 256,
Memory: 256,
},
{
Name: "test2",
Image: test1Image2Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 512,
CPU: 256,
Memory: 256,
},
{
Name: "test3",
Image: test1Image3Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 512,
CPU: 256,
Memory: 256,
},
},
Expand All @@ -603,23 +603,23 @@ func createImageCleanupThresholdTestTask(taskName string) *apitask.Task {
Image: test2Image1Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 512,
CPU: 256,
Memory: 256,
},
{
Name: "test2",
Image: test2Image2Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 512,
CPU: 256,
Memory: 256,
},
{
Name: "test3",
Image: test2Image3Name,
Essential: false,
DesiredStatusUnsafe: apicontainerstatus.ContainerRunning,
CPU: 512,
CPU: 256,
Memory: 256,
},
},
Expand Down
Loading

0 comments on commit 6bc7b20

Please sign in to comment.