2023-08-03 11:06:53 +00:00
|
|
|
const { MongoClient, MongoServerError} = require("mongodb");
|
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
|
|
|
const { preprocessMongoDBURI } = require("./utils");
|
2022-03-10 04:46:16 +00:00
|
|
|
|
|
|
|
|
async function exec() {
|
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
|
|
|
const client = new MongoClient(preprocessMongoDBURI(process.env.APPSMITH_MONGODB_URI), {
|
2022-03-10 04:46:16 +00:00
|
|
|
useNewUrlParser: true,
|
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-21 08:17:29 +00:00
|
|
|
let isReplicaSetEnabled = false;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
isReplicaSetEnabled = await checkReplicaSet(client);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error trying to check replicaset", err);
|
|
|
|
|
} finally {
|
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
|
|
|
await client.close();
|
2022-03-21 08:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.exit(isReplicaSetEnabled ? 0 : 1);
|
2022-03-10 04:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkReplicaSet(client) {
|
|
|
|
|
await client.connect();
|
|
|
|
|
return await new Promise((resolve) => {
|
|
|
|
|
try {
|
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
|
|
|
const changeStream = client
|
2022-03-21 08:17:29 +00:00
|
|
|
.db()
|
|
|
|
|
.collection("user")
|
2022-03-10 04:46:16 +00:00
|
|
|
.watch()
|
|
|
|
|
.on("change", (change) => console.log(change))
|
2022-03-21 08:17:29 +00:00
|
|
|
.on("error", (err) => {
|
2023-08-03 11:06:53 +00:00
|
|
|
if (err instanceof MongoServerError && err.toString() == "MongoServerError: The $changeStream stage is only supported on replica sets") {
|
|
|
|
|
console.log("Replica set not enabled");
|
|
|
|
|
} else {
|
|
|
|
|
console.error("Error even from changeStream", err);
|
|
|
|
|
}
|
2022-03-21 08:17:29 +00:00
|
|
|
resolve(false);
|
|
|
|
|
});
|
2022-03-10 04:46:16 +00:00
|
|
|
|
|
|
|
|
// setTimeout so the error event can kick-in first
|
|
|
|
|
setTimeout(() => {
|
2022-03-21 08:17:29 +00:00
|
|
|
resolve(true);
|
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
|
|
|
changeStream.close();
|
2022-03-10 04:46:16 +00:00
|
|
|
}, 1000);
|
|
|
|
|
} catch (err) {
|
2022-03-21 08:17:29 +00:00
|
|
|
console.error("Error thrown when checking replicaset", err);
|
|
|
|
|
resolve(false);
|
2022-03-10 04:46:16 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
exec,
|
|
|
|
|
};
|