Skip to content

Commit ca558a9

Browse files
Copilotimnasnainaec
andcommitted
Convert MongoDB from standalone to replica set
Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
1 parent 5b2ea66 commit ca558a9

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"label": "run-mongo",
3131
"command": "mongod",
3232
"type": "process",
33-
"args": ["--dbpath", "${workspaceFolder}/mongo_database"],
33+
"args": ["--dbpath", "${workspaceFolder}/mongo_database", "--replSet", "rs0"],
3434
"problemMatcher": "$tsc"
3535
}
3636
]

Backend/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"MongoDB": {
3-
"ConnectionString": "mongodb://localhost:27017",
4-
"ContainerConnectionString": "mongodb://database:27017",
3+
"ConnectionString": "mongodb://localhost:27017/?replicaSet=rs0",
4+
"ContainerConnectionString": "mongodb://database:27017/?replicaSet=rs0",
55
"CombineDatabase": "CombineDatabase"
66
},
77
"Logging": {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ npm run license-report-frontend
473473

474474
To browse the database locally during development, open [MongoDB Compass](https://www.mongodb.com/try/download/compass).
475475

476-
1. Under New Connection, enter `mongodb://localhost:27017`
476+
1. Under New Connection, enter `mongodb://localhost:27017/?replicaSet=rs0`
477477
2. Under Databases, select CombineDatabase
478478

479479
### Add or Update Dictionary Files

database/init/00-replica-set.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Initialize the replica set on first startup.
2+
// See: https://www.mongodb.com/docs/manual/tutorial/convert-standalone-to-replica-set/
3+
//
4+
// MONGO_INITDB_REPLICA_HOST can be set to the resolvable hostname:port
5+
// used to advertise this member (e.g. the Kubernetes Service name "database:27017").
6+
// It defaults to "localhost:27017" for local development.
7+
try {
8+
rs.status();
9+
} catch (e) {
10+
print(`Replica set not yet initialized (${e}), initializing now...`);
11+
const host = process.env.MONGO_INITDB_REPLICA_HOST || "localhost:27017";
12+
rs.initiate({ _id: "rs0", members: [{ _id: 0, host: host }] });
13+
// Wait for replica set to reach PRIMARY state before other init scripts run.
14+
const maxWaitMs = 30000;
15+
const intervalMs = 500;
16+
let waited = 0;
17+
let isPrimary = false;
18+
while (!isPrimary && waited < maxWaitMs) {
19+
sleep(intervalMs);
20+
waited += intervalMs;
21+
const status = rs.status();
22+
isPrimary =
23+
status.members !== undefined &&
24+
status.members.some((m) => m.stateStr === "PRIMARY");
25+
}
26+
if (!isPrimary) {
27+
throw new Error(
28+
`Replica set did not reach PRIMARY state after ${maxWaitMs}ms`
29+
);
30+
}
31+
}

deploy/helm/thecombine/charts/database/templates/database.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ spec:
4646
- image: {{ include "database.containerImage" . }}
4747
imagePullPolicy: {{ .Values.global.imagePullPolicy }}
4848
name: database
49+
args:
50+
- "--replSet"
51+
- "rs0"
52+
env:
53+
- name: MONGO_INITDB_REPLICA_HOST
54+
value: "database:27017"
4955
ports:
5056
- containerPort: 27017
5157
resources:

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
"backend": "dotnet watch --project Backend/BackendFramework.csproj",
99
"build": "parcel build",
1010
"build:analyze": "npm run build -- --reporter @parcel/reporter-bundle-analyzer",
11-
"predatabase": "node scripts/setupMongo.js",
12-
"database": "mongod --dbpath=./mongo_database",
11+
"database": "node scripts/startDatabase.js",
1312
"drop-database": "tsc scripts/dropDB.ts && node scripts/dropDB.js",
1413
"find-circular-deps": "npx --ignore-scripts -y madge -c src/index.tsx --ts-config tsconfig.json",
1514
"fmt-backend": " dotnet format && dotnet format Backend.Tests",

0 commit comments

Comments
 (0)