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

fix(ec2): Call describe-instance API concurrently #2047

Merged
merged 2 commits into from May 2, 2024

Conversation

max-melentyev
Copy link
Contributor

Description of your changes

Instance observation makes 4 AWS API calls. Running this calls concurrently reduces reconciliation time. Here is 0.99 quantile of reconciliation duration

image

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable test to ensure this PR is ready for review.

How has this code been tested

with existing unit tests

@MisterMX
Copy link
Collaborator

Adding multi-threading generally increases the complexity of the code and makes it harder to debug. Since we have a bunch of other controllers (i.e. S3) where we make multiple, consecutive calls that cause no problem so far, I am very reluctant to merge this.

Is there any issue that comes with the increased reconcile time for EC2 instances?

@max-melentyev
Copy link
Contributor Author

Having thousands of instances, this allowed us to reduce queue size and period of a single instance reconciliation from 10+ minutes down to several minutes.

Comment on lines +198 to +200
descAttr := func(attr types.InstanceAttributeName) (*awsec2.DescribeInstanceAttributeOutput, error) {
return e.client.DescribeInstanceAttribute(ctx, &awsec2.DescribeInstanceAttributeInput{
InstanceId: &instanceId,
Attribute: attr,
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if this could be done more elegantly by iterating over an array of attribute keys and start a Go routine in parallel. I would also prefer to use https://pkg.go.dev/golang.org/x/sync/errgroup instead of standard WaitGroup.

Something like this:

func (e *external) describeInstanceAttributes(ctx context.Context, cr *svcapitypes.Instance) (*awsec2.DescribeInstanceAttributeOutput, error) {
	eg := errgroup.Group{}
	out := &awsec2.DescribeInstanceAttributeOutput{}
	describeAttribute := func(attr types.InstanceAttributeName, onSuccess func(res *awsec2.DescribeInstanceAttributeOutput)) {
		eg.Go(func() error {
			res, err := e.client.DescribeInstanceAttribute(ctx, &awsec2.DescribeInstanceAttributeInput{
				InstanceId: aws.String(meta.GetExternalName(cr)),
				Attribute: attr,
			})
			if err != nil {
				err = errors.Wrap(err, string(attr))
				return err
			}
			onSuccess(res)
			return nil
		})
	}

	describeAttribute(types.InstanceAttributeNameDisableApiTermination, func(res *awsec2.DescribeInstanceAttributeOutput) {
		out.DisableApiTermination = res.DisableApiTermination
	})
	describeAttribute(types.InstanceAttributeNameInstanceInitiatedShutdownBehavior, func(res *awsec2.DescribeInstanceAttributeOutput) {
		out.InstanceInitiatedShutdownBehavior = res.InstanceInitiatedShutdownBehavior
	})
	describeAttribute(types.InstanceAttributeNameUserData, func(res *awsec2.DescribeInstanceAttributeOutput) {
		out.UserData = res.UserData
	})
	
	err := eg.Wait()
	return out, err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, cool. I didn't know about errgroup. It looks much better now.

Signed-off-by: Max Melentyev <max.melentyev@reddit.com>
@max-melentyev max-melentyev force-pushed the instance-api branch 3 times, most recently from ca04245 to 9482248 Compare April 29, 2024 18:13
Signed-off-by: Max Melentyev <max.melentyev@reddit.com>
@MisterMX MisterMX changed the title Call describe-instance API concurrently fix(ec2): Call describe-instance API concurrently May 2, 2024
Copy link
Collaborator

@MisterMX MisterMX left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you very much @max-melentyev!

@MisterMX MisterMX merged commit bddb183 into crossplane-contrib:master May 2, 2024
9 checks passed
@max-melentyev max-melentyev deleted the instance-api branch May 2, 2024 14:02
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

2 participants