fix(database): cannot overwrite view bug#1464
Open
ChrisPdgn wants to merge 7 commits into
Open
Conversation
ioanniskemerlis
approved these changes
May 6, 2026
Avoid serial Redlock handoff after the writer finishes by trying a short lock acquire first, then polling durable view state before falling back to the exclusive writer path. Co-authored-by: Cursor <cursoragent@cursor.com>
a5ea081 to
b98d4ae
Compare
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Concurrent calls to
createViewwith the sameviewNamecaused intermittent failures:This is a check-then-act race. Two or more callers both see
this.views[viewName]as unset, then both try to register the same Mongoose model and create the same MongoDB view. The second caller fails because:mongoose.model(name, schema)throwsOverwriteModelErrorif the name is already registered.createCollection({ viewOn })fails withNamespaceExistsif the view already exists in MongoDB.Typical triggers:
CreateResourceAccessListcalls that resolve to the same hashedviewName.database:create:viewbus events simultaneously.recoverViewsFromDatabaserunning manycreateViewcalls in parallel.The original code also had a second, subtler race in metadata insertion: a check-then-create pattern (
findOne->create) that could produce duplicateViewsdocuments.Solution
The fix introduces three complementary layers of concurrency protection and separates the tightly coupled creation logic into distinct steps.
1. In-process promise coalescing (
pendingViewCreations)A
Map<string, Promise<void>>tracks in-flight work perviewName. When a second caller within the same Node.js process requests the same view while creation is already running, itawaits the existing promise instead of starting duplicate work. After the promise resolves, the caller re-checks local state before proceeding; it does not blindly assume the first caller succeeded.2. Cross-instance distributed locking via Redlock
For multi-replica deployments (multiple database module instances sharing the same MongoDB and Redis), a Redlock-based distributed lock is acquired per view name.
Two methods were added to
StateManager:tryAcquireLock(resource, ttl): attempts to acquire the lock exactly once (retryCount: 0). Returns the lock on success, ornullon contention. Only swallows true lock contention; Redis/infrastructure failures are re-thrown.withLock(resource, ttl, fn): acquires the lock, executesfn, and releases in afinallyblock.The
isLockContentionhelper inspects Redlock'sExecutionError.attemptsto distinguish true lock contention (all vote-against errors areResourceLockedError) from infrastructure failures (for example, Redis unavailable), ensuring real errors surface instead of being treated as normal contention.The
runCreateViewLockedflow is:tryAcquireLock. If acquired, run the creation logic and release.withLockcall that waits for the lock and then creates the view.3. Readiness polling for lock losers (
waitForViewReadiness)Instead of every lock loser serially queuing to acquire the same lock, they poll for the view's existence in parallel, checking every 50ms for up to 5 seconds:
this.viewscache with a matching query?Viewsmetadata document exist in MongoDB (read from primary) with the correct query?viewOn+pipeline) match what we expect?If all checks pass, the lock loser registers the Mongoose model locally via
buildViewModeland returns without acquiring the lock. If something doesn't match, such as a query mismatch or a namespace that exists but is not a view, a descriptiveGrpcErroris thrown rather than silently producing corrupt state.4. Separation of creation concerns
The old code mixed Mongoose model registration with MongoDB view creation in one path. The new code separates this into focused private methods:
buildViewModel: registers the Mongoose model in memory (this.views[viewName]) and clears stale models first.createPhysicalMongoView: callsdb.createCollection(viewName, { viewOn, pipeline }).upsertViewMetadata: usesupdateOnewithupsert: true, replacing the old race-prone check-then-create.createViewLocked: orchestrates the full locked creation path with state checks.Summary of changes
viewName.Testing
Verified with two database instances deployed concurrently. The
OverwriteModelErrorno longer occurs under the previously failing scenario.What kind of change does this PR introduce?
Does this PR introduce a breaking change?
The PR fulfills these requirements:
mainbranchfix #xxx, where "xxx" is the issue number)