Skip to content

Conversation

@ngjaying
Copy link
Collaborator

@ngjaying ngjaying commented Jan 8, 2026

Summary

Add version-based conflict resolution for concurrent rule creation via upsert. When multiple requests try to create the same rule concurrently (rule doesn't exist yet), the request with the higher version will win.

Note: Version-based conflict resolution already worked correctly for updates to existing rules.

Changes

  • save(): Persist to DB first, then update memory to ensure atomicity for new rules
  • Added comments to clarify the flow

Test

Includes FVT test TestConcurrentUpdateRule that verifies concurrent upserts result in highest version (3.0.0) winning.

@ngjaying ngjaying force-pushed the fix/rule-upsert-race-condition branch from 7fc5a66 to d2f6dc1 Compare January 8, 2026 00:47
@ngjaying ngjaying changed the title fix(server): fix race condition in UpsertRule feat(server): ensure higher version wins in concurrent rule upsert Jan 8, 2026
@ngjaying ngjaying changed the title feat(server): ensure higher version wins in concurrent rule upsert feat(server): version wins in concurrent upsert Jan 8, 2026
@ngjaying ngjaying force-pushed the fix/rule-upsert-race-condition branch 2 times, most recently from bfd36e0 to d5228ca Compare January 8, 2026 01:19
@codecov
Copy link

codecov bot commented Jan 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.95%. Comparing base (4078f62) to head (c3fc654).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3970      +/-   ##
==========================================
+ Coverage   71.93%   71.95%   +0.03%     
==========================================
  Files         456      456              
  Lines       52603    52612       +9     
==========================================
+ Hits        37835    37855      +20     
+ Misses      11850    11844       -6     
+ Partials     2918     2913       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Hold RuleRegistry lock for entire UpsertRule operation to ensure
atomic version check, ValidateAndRun, and persist. This prevents
race conditions where lower versions could win.

Refactored to use ExecUpsert directly and removed unused private methods.
Added unit tests to cover temp rules and DB persistence failures.
Includes FVT test to verify the fix.

Signed-off-by: Jiyong Huang <[email protected]>
@ngjaying ngjaying force-pushed the fix/rule-upsert-race-condition branch from d5228ca to ac11429 Compare January 8, 2026 02:01
- Use mutex to protect 'final' variable update in TestMulShared/async_run in fvt/rulestate_test.go
- Sor imports in internal/server/rule_manager_test.go

Signed-off-by: Jiyong Huang <[email protected]>
@ngjaying ngjaying force-pushed the fix/rule-upsert-race-condition branch from 26544a9 to c3fc654 Compare January 8, 2026 02:28
@ngjaying ngjaying requested a review from Yisaer January 8, 2026 02:45
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.

1 participant