-
Notifications
You must be signed in to change notification settings - Fork 473
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
Update NewLabel method to use more efficient update mechanism #25777
Conversation
@jacobshandling some failing tests, i'll ping you for review once they're resolved. |
@jacobshandling all set, I just had to do the thing I said I would do 🙄 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #25777 +/- ##
==========================================
- Coverage 63.61% 63.60% -0.02%
==========================================
Files 1623 1623
Lines 155465 155456 -9
Branches 4077 4077
==========================================
- Hits 98905 98883 -22
- Misses 48759 48769 +10
- Partials 7801 7804 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good.
Can we also update this comment to keep it accurate to the new matching functionality?
@@ -165,8 +165,11 @@ VALUES ` + strings.Join(placeholders, ", ") | |||
} | |||
return nil | |||
}) | |||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary? Seems like every possible err
is being handled already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the error returned from the retry function above. Errors are handled inside the function e.g. here but we still need to check whether the retry function failed so we can return out of UpdateLabelMembershipByHostIDs
early. We were previously doing this implicitly with
return ctxerr.Wrap(ctx, err, "UpdateLabelMembershipByHostIDs transaction")
where err
would usually be nil
.
|
||
return ctxerr.Wrap(ctx, err, "UpdateLabelMembershipByHostIDs transaction") | ||
return ds.labelDB(ctx, labelID, teamFilter, ds.writer(ctx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about ds.reader(ctx)
, or even using
this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with both of those is that they use the read replica (ds.reader()
), which doesn't have the updates yet.
For #25555
Checklist for submitter
changes/
,orbit/changes/
oree/fleetd-chrome/changes
.See Changes files for more information.
SELECT *
is avoided, SQL injection is prevented (using placeholders for values in statements)This PR updates the
NewLabel
service to use theUpdateLabelMembershipByHostIDs
method previously added by @jacobshandling rather than usingApplyLabels
. The latter method has performance issues when adding large numbers of hosts at once to a manual label (see #25555) because it does an expensive lookup of host names before transforming those into Fleet host IDs. The new code skips the middleman and transforms host identifiers directly to Fleet host IDs, and does so using a batching strategy to ensure the queries don't get too large.This PR does update
UpdateLabelMembershipByHostIDs
slightly to return an updated Label object and host IDs array, as this is the expected return value forNewLabel
. I update the method's tests accordingly. I don't think any new tests forNewLabel
are needed as it should have the same functionality and return values.Manual Testing
On the main branch, I launched my local MySQL with the thread stack size set to the minimal allowed, and used the API to try and create a new label with 5,000 hosts attached, and received a 422 response from the server. Server logs showed:
On this branch, I kept the same MySQL settings and tried my API request again and it was successful:
![image](https://private-user-images.githubusercontent.com/553428/407376553-c4f0f52b-4d09-457b-8096-4dd3a747b1f4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MTI1MTAsIm5iZiI6MTczODkxMjIxMCwicGF0aCI6Ii81NTM0MjgvNDA3Mzc2NTUzLWM0ZjBmNTJiLTRkMDktNDU3Yi04MDk2LTRkZDNhNzQ3YjFmNC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwN1QwNzEwMTBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05OTA5ZGRhODhmYjAxOTQwNGU4YWU4Y2UyYTJkYTQ3NjQ1ZDVmNGQ2ZTFiMDAzYTA4Y2I5NjIxNmYwMzE0ZmE1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.YLxQA8kC9j42vc8D3Kx-y9pdt5Hvy7rWg8EIxb9Odks)
QA
The script I used to create a new manual label with lots of hosts is at: https://gist.github.com/sgress454/84f12064c437da456c456e25c26d9069
To run it, first grab a bearer token from any API request by opening the network tab, clicking a Fleet API request, and in the headers tab scrolling down to Authorization:
![image](https://private-user-images.githubusercontent.com/553428/407422935-5680f3bf-8db8-469a-9f03-000b86622c04.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MTI1MTAsIm5iZiI6MTczODkxMjIxMCwicGF0aCI6Ii81NTM0MjgvNDA3NDIyOTM1LTU2ODBmM2JmLThkYjgtNDY5YS05ZjAzLTAwMGI4NjYyMmMwNC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwN1QwNzEwMTBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01MGYxZjE1NDY3MGExOWYyYzgxMDExNTA0NDI3MDU4YmJjOGRhODBmYmNhNzQ4ODU4M2EyMjE3OTc4N2JmNzFmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.DKgza6Tf-04BUV3Gjuf7JI2SDMDMGY0ATbzjlzxM-qQ)
(only take the part after "Bearer")
Then download the script from that gist and in its folder run:
e.g.
This will invoke the API on https://localhost:8080 and try to add 5000 hosts a new label "some test label".
If you need to change the # of hosts or the url of the server, there are additional arguments:
e.g.