-
-
Notifications
You must be signed in to change notification settings - Fork 10
Convert MongoDB from standalone to replica set #4184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
8
commits into
master
Choose a base branch
from
copilot/convert-standalone-to-replica-set
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5b2ea66
Initial plan
Copilot ca558a9
Convert MongoDB from standalone to replica set
Copilot dbfb84c
Add startDatabase.js to .gitignore exceptions
Copilot 34a4308
Remove setupMongo.js and its .gitignore reference
Copilot 46d3f10
Fix prettier formatting in startDatabase.js and tasks.json
Copilot d0942b0
Update run-mongo VSCode task to use npm run database
Copilot 2c9afee
Make startDatabase.js failure modes fatal with proper exit codes
Copilot 36d0f2f
Fix exit code logic: use ?? instead of || to preserve clean exit (0)
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Initialize the replica set on first startup. | ||
| // See: https://www.mongodb.com/docs/manual/tutorial/convert-standalone-to-replica-set/ | ||
| // | ||
| // MONGO_INITDB_REPLICA_HOST can be set to the resolvable hostname:port | ||
| // used to advertise this member (e.g. the Kubernetes Service name "database:27017"). | ||
| // It defaults to "localhost:27017" for local development. | ||
| try { | ||
| rs.status(); | ||
| } catch (e) { | ||
| print(`Replica set not yet initialized (${e}), initializing now...`); | ||
| const host = process.env.MONGO_INITDB_REPLICA_HOST || "localhost:27017"; | ||
| rs.initiate({ _id: "rs0", members: [{ _id: 0, host: host }] }); | ||
| // Wait for replica set to reach PRIMARY state before other init scripts run. | ||
| const maxWaitMs = 30000; | ||
| const intervalMs = 500; | ||
| let waited = 0; | ||
| let isPrimary = false; | ||
| while (!isPrimary && waited < maxWaitMs) { | ||
| sleep(intervalMs); | ||
| waited += intervalMs; | ||
| const status = rs.status(); | ||
| isPrimary = | ||
| status.members !== undefined && | ||
| status.members.some((m) => m.stateStr === "PRIMARY"); | ||
| } | ||
| if (!isPrimary) { | ||
| throw new Error( | ||
| `Replica set did not reach PRIMARY state after ${maxWaitMs}ms` | ||
| ); | ||
| } | ||
| } |
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
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
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| "use strict"; | ||
|
|
||
| const { spawn, spawnSync } = require("child_process"); | ||
| const { ensureDir } = require("fs-extra"); | ||
|
|
||
| const dbPath = "./mongo_database"; | ||
| const replSetName = "rs0"; | ||
| const maxAttempts = 30; | ||
| const retryInterval = 1000; // ms | ||
|
|
||
| async function waitForMongo() { | ||
| for (let i = 0; i < maxAttempts; i++) { | ||
| const result = spawnSync("mongosh", [ | ||
| "--eval", | ||
| "db.adminCommand('ping')", | ||
| "--quiet", | ||
| ]); | ||
| if (result.status === 0) { | ||
| return true; | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, retryInterval)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| async function initReplicaSet() { | ||
| const result = spawnSync( | ||
| "mongosh", | ||
| ["--eval", "try { rs.status() } catch(e) { rs.initiate() }", "--quiet"], | ||
| { stdio: "inherit" } | ||
| ); | ||
| return result.status === 0; | ||
| } | ||
|
|
||
| async function main() { | ||
| await ensureDir(dbPath); | ||
|
|
||
| const mongod = spawn( | ||
| "mongod", | ||
| [`--dbpath=${dbPath}`, "--replSet", replSetName], | ||
| { | ||
| stdio: "inherit", | ||
| } | ||
| ); | ||
|
|
||
| mongod.on("error", (err) => { | ||
| console.error(`mongod error: ${err.message}`); | ||
| process.exit(1); | ||
| }); | ||
|
|
||
| const ready = await waitForMongo(); | ||
| if (ready) { | ||
| await initReplicaSet(); | ||
| } else { | ||
| console.error("MongoDB did not start in time"); | ||
| } | ||
|
|
||
| process.on("SIGINT", () => { | ||
| mongod.kill("SIGINT"); | ||
| }); | ||
| process.on("SIGTERM", () => { | ||
| mongod.kill("SIGTERM"); | ||
| }); | ||
|
|
||
| await new Promise((resolve) => mongod.on("close", (code) => resolve(code))); | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.