MongoDB Replicaset support#16698
Open
artiomchi wants to merge 6 commits intomicrosoft:mainfrom
Open
Conversation
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 16698Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 16698" |
Minor cleanup
66c95f0 to
67db131
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds single-node replica set support for hosted MongoDB resources so transaction-dependent scenarios can run against the Aspire MongoDB container, and introduces tests for the new API and connection string behavior.
Changes:
- Add
WithReplicaSet(...)to configure MongoDB as a single-node replica set and exposeReplicaSetName. - Replace the default MongoDB health check with a custom health check that initializes the replica set and waits for primary election.
- Add functional and unit tests covering the new API, container args, and connection string updates.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Aspire.Hosting.MongoDB.Tests/MongoDbFunctionalTests.cs | Adds a functional test that verifies transactions succeed against a replica-set-enabled MongoDB resource. |
| tests/Aspire.Hosting.MongoDB.Tests/MongoDBPublicApiTests.cs | Adds public API validation tests for WithReplicaSet and ReplicaSetName. |
| tests/Aspire.Hosting.MongoDB.Tests/AddMongoDBTests.cs | Adds unit tests for replica set defaults, args, annotations, and connection string behavior. |
| src/Aspire.Hosting.MongoDB/api/Aspire.Hosting.MongoDB.cs | Updates the shipped public API surface with WithReplicaSet and ReplicaSetName. |
| src/Aspire.Hosting.MongoDB/MongoDBServerResource.cs | Extends the resource model and connection string generation for replica set mode. |
| src/Aspire.Hosting.MongoDB/MongoDBServerHealthCheck.cs | Introduces a custom health check that initializes the replica set and checks primary readiness. |
| src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs | Registers the new health check and implements WithReplicaSet. |
…ead safe way Disposing of the mongo cursor in the health check
…tensions.CreateGeneratedParameter
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.
Description
This PR modifies the MongoDB Builder to add support for building a single node replica set. This will allow applications using transactions or change streams to use MongoDB in Aspire.
I'm using the same approach as I used when building the Testcontainers.MongoDb library. Instead of building a separate container in a dockerfile, the standard mongo container is used. To start as a replicaset, a keyfile is generated and added to to the container. The healthcheck is used to ensure the replicaset is initialised on server startup. This keeps the implementation clean and doesn't require an additional dockerfile.
WithReplicaSet()Extension Method on the builderreplSetInitiatecommand runsdirectConnection=trueparameter when replica set is enabledFixes #5238
Decision points
New health check
The new health check replaces the simple original health check and will only return healthy once the replica set is initialised. It is also used to call the
rs.initiate()call in Mongo to initialise the replicaset.Alternatives considered:
OnResourceReadycallback fires after the health check passes - this would mark the resource as healthy before the replica set is readydocker-entrypoint-initdb.d- tried in the past, this won't work as that script runs against a standalonemongodinstance, and won't affect the main instanceDirect connection in connection string
Specifying the replicaset name in the connection string is explicitly discouraged by the MongoDB driver configuration, and can cause behavioural conflicts. Specifying
directConnection=truein the connection string tells the MongoDB driver to connect directly to the specified host bypassing topology discovery. Since we only have a single node, this is more than enough. Additionally, the topology discovery would return the internal host address within the container, which wouldn't be accessible by the application.Note
There may be an issue in Mongo 8.0 - 8.2 that causes Mongo to crash every ~30 seconds if replica set is initiated. Seems to be a compatibility issue with certain linux kernels. Switching to older Mongo versions (7.0) seems to completely bypass the issue. Not related to this PR or Aspire, purely a MongoDB regression.
ref:
bluewave-labs/Checkmate#3444
https://www.mongodb.com/community/forums/t/mongodb-8-x-and-linux-kernel-6-19/337547
stoatchat/self-hosted#268
Checklist
<remarks />and<code />elements on your triple slash comments?aspire.devissue: