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

Adding EBS watcher implementation #3866

Merged
merged 3 commits into from Sep 12, 2023
Merged

Adding EBS watcher implementation #3866

merged 3 commits into from Sep 12, 2023

Conversation

mye956
Copy link
Contributor

@mye956 mye956 commented Aug 23, 2023

Summary

This PR will introduce the implementation for the EBS watcher which is responsible for detecting and validating that an EBS volume is attached onto the host.

Implementation details

The general workflow is upon agent start up, the EBS watcher gets initialized and will start to periodically check whether we have any pending EBS attachments to find on the host. If there is then it'll scan for all pending EBS volumes within the agent state and try to find if they're attached via lsblk. Otherwise it's a noop. The watcher will always be running and will stop whenever agent stops.

EBS volumes are added to the agent state whenever we receive and handle an attachment message.

  • Introduces the EBS watcher
    • Added a watcher.go into agent/ebs
      • Introduce new EBSWatcher struct
      • Added new functionality to:
        • NewWatcher: Create a new instance of a EBSWatcher struct
        • Start: Start the periodic scanning process of the EBSWatcher for all pending EBS attachments saved to the agent state
        • Stop: Stop the EBSWatcher
        • HandleResourceAttachment: Handle a new ResourceAttachment object from the Attachment Message Responder/Handler and save it into the agent state if it does not exists.
        • notifyFoundEBS: Mark the pending ResourceAttachment as no longer pending as well as stops the ACK timer
        • RemoveAttachment: Removes a ResourceAttachment object from the list of attachments within the agent state
        • scanEBSVolumes: Iterates through the list of pending ResourceAttachment objects within the agent state and checks if it's attached within the host via ConfirmEBSVolumeIsAttached
        • addEBSAttachmentToState: Starts the ACK timer of a ResourceAttachment object as well as add it to the list of attachments within the agent state
        • handleEBSAckTimer: Handles the when a ResourceAttachment object once its ACK timer expires
  • Minor changes to the log statements within Docker task engine state when adding and removing ResourceAttachments from the list
  • Added new functionality in ecs-agent/api/resource/resource_attachment.go to safely read ResourceAttachment objects within logs
  • Introduces the implementation of EBS volume discovery and validation
    • Added a new interface called EBSDiscovery in ecs-agent/api/resource/interfaces.go
    • Added a new struct calledEBSDiscoveryClient in ecs-agent/api/resource/ebs_discovery.go which implements the EBSDiscovery interface
    • ebs_discovery_linux.go
      • ConfirmEBSVolumeIsAttached: Calls upon lsblk -o NAME,SERIAL -J to obtain all attached volumes on the host and validates whether the given device name and volume ID is within the output.
      • parseLsblkOutput: Parses the output of lsblk and then returns the volume ID of a specific device name from the output
    • ebs_discovery_windows.go
      • ConfirmEBSVolumeIsAttached: The windows version to find and validate whether a EBS volume is attached on the host
    • ecs-agent/api/resource/generate_mocks.go: Generate new mocks for the EBSDiscovery interface for testing

Note: There will be a follow up PR for the ACS Attachment message handler/responder that'll also include additional changes to the EBS watcher.

This PR will also include changes made in the following PR mye956#49.

Testing

New tests cover the changes: yes

Unit tests

Description for the changelog

Adding implementation for EBS watcher and EBS volume discovery.

Licensing

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@mye956 mye956 changed the title WIP Adding EBS watcher implementation Adding EBS watcher implementation Aug 23, 2023
@mye956 mye956 marked this pull request as ready for review August 23, 2023 22:29
@mye956 mye956 requested a review from a team as a code owner August 23, 2023 22:29
@mye956 mye956 force-pushed the ebsWatcher branch 6 times, most recently from 5266fda to 2e9d9cd Compare August 25, 2023 23:23
ecs-agent/api/resource/ebs_discovery.go Outdated Show resolved Hide resolved
ecs-agent/api/resource/ebs_discovery.go Outdated Show resolved Hide resolved
ecs-agent/api/resource/interfaces.go Show resolved Hide resolved
ecs-agent/api/resource/resource_attachment.go Show resolved Hide resolved
agent/ebs/watcher_test.go Outdated Show resolved Hide resolved
agent/ebs/watcher.go Outdated Show resolved Hide resolved
agent/ebs/watcher.go Outdated Show resolved Hide resolved
agent/ebs/watcher_test.go Outdated Show resolved Hide resolved
agent/ebs/watcher_test.go Show resolved Hide resolved
wg.Add(1)
w.mailbox <- func() {
defer wg.Done()
empty := len(w.agentState.GetAllPendingEBSAttachments()) == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rename empty to something more specific?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call out, I renamed it to wasEmpty as well as added some comments to better explain the use case. Thanks!

@mye956 mye956 force-pushed the ebsWatcher branch 2 times, most recently from b80b2c6 to bab80b4 Compare September 1, 2023 22:30
* Added the implementation for EBS volume discovery on Windows

* incorporated the review comments. Also fixed the name of the timeout variable.

* incorporated the review comments. Also fixed the name of the timeout variable.
agent/ebs/watcher.go Outdated Show resolved Hide resolved
amogh09
amogh09 previously approved these changes Sep 7, 2023

// TestHandleExpiredEBSAttachment tests acknowledging an expired resource attachment of type Elastic Block Stores
// The resource attachment object should not be saved to the agent state since the expiration date is in the past.
func TestHandleExpiredEBSAttachment(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Nice to see you catch this in your tests!

Copy link
Contributor

@Realmonia Realmonia left a comment

Choose a reason for hiding this comment

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

Thanks for making this change! One question: since Xen and Nitro are both amazon internal instance types, is this feature specifically for ec2 ecosystem, how are we planning to prevent ecsanywhere instances running this as it is embedded into agent state?

ecs-agent/api/resource/resource_attachment.go Show resolved Hide resolved
ecs-agent/api/resource/ebs_discovery_linux.go Outdated Show resolved Hide resolved
ecs-agent/api/resource/ebs_discovery_linux.go Outdated Show resolved Hide resolved
agent/ebs/watcher.go Show resolved Hide resolved
Realmonia
Realmonia previously approved these changes Sep 11, 2023
)

const (
ebsVolumeDiscoveryTimeout = 5 * time.Second
Copy link
Member

Choose a reason for hiding this comment

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

Where did this timeout come from? Based on our data, 5 seconds is not enough to always give EBS time to handle the attachment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!


// parseLsblkOutput will parse the `lsblk` output and search for a EBS volume with a specific device name.
// Once found we return the volume ID, otherwise we return an empty string along with an error
// The output of the "lsblk -o NAME,SERIAL -J" command looks like the following:
Copy link
Member

Choose a reason for hiding this comment

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

thank you overall for the thorough code comments!!

Realmonia
Realmonia previously approved these changes Sep 12, 2023
@mye956 mye956 merged commit e7e9eca into aws:dev Sep 12, 2023
40 checks passed
@mye956 mye956 mentioned this pull request Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants