Skip to content

perf(MongoDB): reduce allocations and improve robustness in lock hot paths#276

Open
joesdu wants to merge 4 commits intomadelson:masterfrom
joesdu:master
Open

perf(MongoDB): reduce allocations and improve robustness in lock hot paths#276
joesdu wants to merge 4 commits intomadelson:masterfrom
joesdu:master

Conversation

@joesdu
Copy link
Copy Markdown
Contributor

@joesdu joesdu commented Mar 27, 2026

  • Cache IMongoCollection reference in constructor instead of calling GetCollection on every TryAcquireAsync invocation
  • Promote immutable BsonDocument sub-expressions (expiredOrMissing, newFencingToken) to static readonly fields; cache newExpiresAt as an instance field (depends on expiry) to avoid rebuilding ~10 BsonDocument objects per busy-wait iteration
  • Move TTL index initialization to the start of TryAcquireAsync so it is triggered on every acquisition attempt, not only on success
  • Cache ownerFilter and renewUpdate in InnerHandle constructor; lease renewal (~every 10 s) no longer allocates new filter/update objects
  • Wrap ReleaseLockAsync in try-catch so network failures during Dispose do not surface unexpected exceptions to callers (TTL index provides eventual cleanup)
  • Add internal constructor accepting pre-parsed MongoDistributedLockOptions so MongoDistributedSynchronizationProvider can parse options once and reuse the result across all CreateLock calls
  • Fix stale comment "foo" → "expiresAt" in CheckIfIndexExists
  • Upgraded Microsoft.SourceLink.GitHub, Microsoft.Build.Tasks.Git, and Microsoft.SourceLink.Common to version 10.0.201 in DistributedLock.ZooKeeper, DistributedLock, and DistributedLockTaker.
  • Updated System.IO.Hashing to version 10.0.5 and added its dependencies.
  • Updated MongoDB.Bson and MongoDB.Driver versions to 3.7.1.
  • Updated System.Diagnostics.DiagnosticSource to version 10.0.5.
  • Adjusted DistributedLock.Oracle and DistributedLock.SqlServer versions to 1.0.5 and 1.0.7 respectively.
  • Made various transitive dependency updates to ensure compatibility and stability.

…paths

- Cache IMongoCollection<T> reference in constructor instead of calling GetCollection on every TryAcquireAsync invocation
- Promote immutable BsonDocument sub-expressions (expiredOrMissing, newFencingToken) to static readonly fields; cache newExpiresAt as an instance field (depends on expiry) to avoid rebuilding ~10 BsonDocument objects per busy-wait iteration
- Move TTL index initialization to the start of TryAcquireAsync so it is triggered on every acquisition attempt, not only on success
- Cache ownerFilter and renewUpdate in InnerHandle constructor; lease renewal (~every 10 s) no longer allocates new filter/update objects
- Wrap ReleaseLockAsync in try-catch so network failures during Dispose do not surface unexpected exceptions to callers (TTL index provides eventual cleanup)
- Add internal constructor accepting pre-parsed MongoDistributedLockOptions so MongoDistributedSynchronizationProvider can parse options once and reuse the result across all CreateLock calls
- Fix stale comment "foo" → "expiresAt" in CheckIfIndexExists
- Upgraded Microsoft.SourceLink.GitHub, Microsoft.Build.Tasks.Git, and Microsoft.SourceLink.Common to version 10.0.201 in DistributedLock.ZooKeeper, DistributedLock, and DistributedLockTaker.
- Updated System.IO.Hashing to version 10.0.5 and added its dependencies.
- Increased MongoDB.Bson and MongoDB.Driver versions to 3.7.1.
- Updated System.Diagnostics.DiagnosticSource to version 10.0.5.
- Adjusted DistributedLock.Oracle and DistributedLock.SqlServer versions to 1.0.5 and 1.0.7 respectively.
- Made various transitive dependency updates to ensure compatibility and stability.
Copilot AI review requested due to automatic review settings March 27, 2026 08:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the MongoDB-based distributed lock implementation to reduce per-acquire/renew allocations and improve operational robustness (TTL index creation timing, safer dispose behavior), while also updating several NuGet dependencies across the solution.

Changes:

  • MongoDB lock hot-path optimizations: cache IMongoCollection, reuse BSON sub-expressions, and cache renewal filter/update definitions.
  • Robustness adjustments: trigger TTL index initialization on every acquisition attempt; make release-on-dispose resilient to transient failures.
  • Dependency refresh: bump MongoDB driver/BSON, SourceLink/build tooling, DiagnosticSource, System.IO.Hashing, and related transitive pins across projects.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/DistributedLock.MongoDB/MongoDistributedLock.cs Caches collection/expressions and reduces allocations in acquire/renew; adjusts TTL init timing; swallows release failures on dispose.
src/DistributedLock.MongoDB/MongoIndexInitializer.cs Makes index-creation helper static and fixes TTL index existence check logic.
src/DistributedLock.MongoDB/MongoDistributedSynchronizationProvider.cs Parses Mongo lock options once in provider and reuses them when creating locks.
src/Directory.Packages.props Central version bumps (e.g., MongoDB.Driver, SourceLink, DiagnosticSource).
src/DistributedLock.MongoDB/packages.lock.json Updates locked dependency graph for MongoDB package changes.
src/DistributedLock/packages.lock.json Updates locked dependency graph for core package changes (SourceLink/System.IO.Hashing/etc.).
src/DistributedLock.Azure/packages.lock.json Updates locked dependency graph for Azure package changes and transitive updates.
src/DistributedLock.Core/packages.lock.json Updates locked dependency graph (notably SourceLink/System.IO.Hashing changes).
src/DistributedLock.FileSystem/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.MySql/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Oracle/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Postgres/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Redis/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.SqlServer/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Tests/packages.lock.json Updates locked dependency graph for tests (now only contains net8.0 graph).
src/DistributedLock.WaitHandles/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.ZooKeeper/packages.lock.json Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLockTaker/packages.lock.json Updates locked dependency graph (MongoDB driver and other dependency pins).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants