Reduced database contention during the vulnerability cron#41667
Reduced database contention during the vulnerability cron#41667
Conversation
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
WalkthroughThis PR reduces database contention during the vulnerability cron by introducing a database index on the Possibly related PRs
🚥 Pre-merge checks | ✅ 2 | ❌ 3❌ Failed checks (3 warnings)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
server/datastore/mysql/software.go (1)
2800-2817:⚠️ Potential issue | 🟠 MajorAdd the 100ms pause between cleanup batches.
Line 2800 onward still iterates continuously. That misses the contention-relief requirement from issue
#41664and can keep pressure on concurrent host writes.💡 Proposed patch
func (ds *Datastore) cleanupUnusedSoftware(ctx context.Context) error { @@ for range cleanupMaxIterations { var ids []uint if err := sqlx.SelectContext(ctx, db, &ids, findUnusedSoftwareStmt, cleanupBatchSize); err != nil { return ctxerr.Wrap(ctx, err, "find unused software for cleanup") } @@ if _, err := ds.writer(ctx).ExecContext(ctx, stmt, args...); err != nil { return ctxerr.Wrap(ctx, err, "delete unused software batch") } db = ds.writer(ctx) + + // Brief pause between batches to reduce lock contention with concurrent writers. + select { + case <-ctx.Done(): + return ctxerr.Wrap(ctx, ctx.Err(), "cleanup unused software canceled") + case <-time.After(100 * time.Millisecond): + } } return nil }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@server/datastore/mysql/software.go` around lines 2800 - 2817, The cleanup loop over cleanupMaxIterations in the function that queries findUnusedSoftwareStmt and deletes batches using cleanupBatchSize and ds.writer(ctx) needs a 100ms pause between iterations to reduce contention; after successfully deleting a non-empty batch (i.e., after the ExecContext call and before the next loop iteration), insert a context-aware 100ms delay (use time.After with select on ctx.Done or a context-aware sleep) so the code yields to concurrent host writes and respects cancellation; ensure you add the time import if missing.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@server/datastore/mysql/software.go`:
- Around line 2800-2817: The cleanup loop over cleanupMaxIterations in the
function that queries findUnusedSoftwareStmt and deletes batches using
cleanupBatchSize and ds.writer(ctx) needs a 100ms pause between iterations to
reduce contention; after successfully deleting a non-empty batch (i.e., after
the ExecContext call and before the next loop iteration), insert a context-aware
100ms delay (use time.After with select on ctx.Done or a context-aware sleep) so
the code yields to concurrent host writes and respects cancellation; ensure you
add the time import if missing.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 67d904d4-1d03-4894-a05a-dead5a510ca4
📒 Files selected for processing (4)
changes/41664-vulnerability-cron-db-contentionserver/datastore/mysql/migrations/tables/20260313120000_AddHostSoftwareSoftwareIDIndex.goserver/datastore/mysql/schema.sqlserver/datastore/mysql/software.go
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #41667 +/- ##
==========================================
+ Coverage 66.38% 66.42% +0.03%
==========================================
Files 2499 2495 -4
Lines 199767 199903 +136
Branches 9002 8899 -103
==========================================
+ Hits 132618 132782 +164
+ Misses 55172 55110 -62
- Partials 11977 12011 +34
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
# Conflicts: # server/datastore/mysql/schema.sql
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #41664 If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - [x] QA'd all new/changed functionality manually For unreleased bug fixes in a release candidate, one of: - [x] Alerted the release DRI if additional load testing is needed <!-- This is an auto-generated comment: release notes by coderabbit.ai --> * **Chores** * Optimized database performance for vulnerability processing to reduce contention during routine operations. * Improved query efficiency for software cleanup processes. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Related issue: Resolves #41664
Checklist for submitter
If some of the following don't apply, delete the relevant line.
changes/,orbit/changes/oree/fleetd-chrome/changes.Testing
For unreleased bug fixes in a release candidate, one of:
Summary by CodeRabbit