Add a write buffer and flush worker for prevented packages
What does this MR do and why?
Part of the Dependency Firewall dashboard stats series (work item https://gitlab.com/gitlab-org/gitlab/-/work_items/627538). The former Part 2 was split into three MRs: this one (write buffer and flush worker), MR B (rule evaluation, !257627 (merged)), and MR C (enforcement hook, !253944 (merged)). MR B is independent; MR C targets this branch and needs both.
There are no migrations, UI changes, or changelog entry: this MR is internal and unused until MR C merges. The worker is not behind a feature flag: it runs every 5 minutes and finds an empty buffer until then.
Security::DependencyFirewall::PreventedPackageWriteBuffer, a subclass of Analytics::DatabaseWriteBuffer, mirrors ActivityStatWriteBuffer's stage-then-drain design but merges instead of counting, with one Redis hash field per (project_id, rule_type, identifier). A Lua script keeps the earliest first-seen, latest blocked/warned timestamps, and highest severity, so replays converge and at-least-once delivery is safe. add rejects an Array and passes rule_type and severity through Integer(), so a wrong type raises at the call site.
Security::DependencyFirewall::FlushPreventedPackagesWriteBufferCronWorker runs every 5 minutes under an exclusive lease with a 200 s runtime limit, reading the staged buffer a page at a time with HSCAN and upserting it in batches of at most 1,000 rows via bulk_upsert!. Entries are cleared from Redis only after the write commits: a crash in between re-applies the batch on the next run, which the LEAST/GREATEST merge absorbs, whereas clearing first would lose a batch whose COMMIT fails.
- If a batch fails with a foreign-key violation, a CHECK violation, or an unmapped enum value (for example from a newer node mid-deploy), it is retried row by row and only the failing rows are discarded and logged.
- Everything else re-raises so Sidekiq retries with entries still staged, including other
ArgumentErrors.
The model includes Analytics::HasWriteBuffer; the Part 1 enum check in bulk_upsert! now raises InvalidEnumValueError (a subclass of ArgumentError), existing callers unaffected. Added to ee/config/schedule.yml and ee/app/workers/all_queues.yml; ee/spec/initializers/1_settings_spec.rb covers the new cron job.
Database review
Database review is requested here: batch size, transaction boundary, and discard-vs-retry handling. No schema changes; the only query is Part 1's bulk_upsert!, up to 1,000 rows, arbiter i_dep_fw_prevented_packages_unique. Batch size answers the Part 1 reviewer's question: 50,000 rows measured ~947 ms; 1,000 matches the activity-stats worker and ~1,000-per-bulk-insert guideline (~19 ms extrapolated, not measured). Rows are already unique per key (one Redis field each), so no pre-folding is needed. Query plans are in the Part 1 model MR description, !255353 (merged) (since-expired Database Lab clone, 4.7 million rows).
Raw SQL, captured from the test database with two rows (a real batch has up to 1,000 rows in VALUES):
INSERT INTO "dependency_firewall_prevented_packages"
("project_id","rule_type","identifier","first_seen_at","last_blocked_at","last_warned_at","severity","created_at","updated_at")
VALUES
(59, 1, 'pkg:npm/lodash@4.17.20', '2026-09-23 12:00:00', '2026-09-23 12:00:00', NULL, 7, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
(59, 0, 'pkg:npm/left-pad@1.3.0', '2026-09-23 12:00:00', NULL, '2026-09-23 12:00:00', NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT ("project_id","rule_type","identifier") DO UPDATE SET
first_seen_at = LEAST(dependency_firewall_prevented_packages.first_seen_at, excluded.first_seen_at),
last_blocked_at = GREATEST(dependency_firewall_prevented_packages.last_blocked_at, excluded.last_blocked_at),
last_warned_at = GREATEST(dependency_firewall_prevented_packages.last_warned_at, excluded.last_warned_at),
severity = GREATEST(dependency_firewall_prevented_packages.severity, excluded.severity),
updated_at = now()
RETURNING "id"Testing
63 examples (write buffer 32, flush worker 31) passing locally, plus the model and settings initializer specs; RuboCop clean.
References
- Work item: https://gitlab.com/gitlab-org/gitlab/-/work_items/627538
- Part 1 table: !253941 (merged)
- Part 1 model: !255353 (merged)
- MR B (rule evaluation changes): !257627 (merged)
- MR C (enforcement hook): !253944 (merged)
Screenshots or screen recordings
Not applicable, no UI changes.
How to set up and validate locally
- In
bin/rails console:Security::DependencyFirewallPreventedPackage.write_buffer.add(project_id: Project.first.id, rule_type: Security::DependencyFirewallPolicyRule.types['license'], identifier: 'pkg:npm/left-pad@1.3.0', seen_at: Time.current, blocked: true, warned: false, severity: nil) - Flush:
Security::DependencyFirewall::FlushPreventedPackagesWriteBufferCronWorker.new.perform - Check
Security::DependencyFirewallPreventedPackage.last:rule_typeislicense,last_blocked_atis set,last_warned_atandseverityare nil. - Repeat with
blocked: false, warned: true, flush again, and confirm the row now haslast_warned_atset whilefirst_seen_atis unchanged.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.