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
openUri behaviour changed in v6 #10948
Comments
I've made some progress with this, if I pass the following to "openUri" I get further:
This allows the connection to succeed. However, because the admin command initialises the replica-set (as shown in the original post), the "serverDescriptionChanged" event is fired, and that marks the connection as "disconnected". This can be fixed by updating the following code (in connection.js - line 850):
OR Reinstate the following code that was in mongoose v5:
It's actually this "serverHeartbeatSucceeded" code that means v5 works for us - because we never get the "newDescription.type" as "Standalone", it always ends up being "RSPrimary". Our database tends to go through the following states: "RSGhost", "RSOther", "RSSecondary", "RSPrimary" - which it settles on and becomes usable. These description changes happen after calling:
|
We will add a note about this to the docs. The |
Ok - I'm not sure I understand the response. I'd like to know how to connect to an uninitiated replica-set and programmatically initiate it via an admin command? Once initiated, mongoose connection should report that it is connected, i.e. the "readyState" should be "connected". Thanks in advance. |
As you suggested:
An alternative approach;
|
I get that directConnection works - and solves the first issue I was having around connecting to an uninitiated replicaSet. The follow-up question is more around the bug that mongoose does not correctly set the "readyState" of the connection after telling the relica-set to initiate via the admin command.
The above call causes a series of "serverDescriptionChanged" events. Once these have finished firing, the connection to MongoDB is active - but mongoose doesn't think it is (i.e. the "readyState" is still reporting disconnected) - because the "serverDescriptionChanged" callback is not correctly detecting the change from RSGhost to RSPrimary. The fix is in my original post. |
We use mongoose for a number of high profile games - and unfortunately this is a blocking bug, meaning we can not update from v5 to v6. I'd rather avoid forking the repo to apply the fix. This is the fix. connection.js - line ~850 If you connect to a replica-set using "directConnection: true" then the server description does not use "Standalone", it uses "RSPrimary" (among other RS identifiers - like RSGhost). |
Hi - I've created a minimal repo case to highlight the issue. Before running the code, initialise a new MongoDB database using:
Do not initiate the database - that will be done in code.
Run the code without my fix, you will see the following output (note, I'm using mongoose v6.0.14):
Notice the document "create" failed. With the following one line fix in connection.js:
You get the following output instead:
Notice that the document creation is successful. I hope this helps. |
Fix should be in v6.0.15, thanks for your patience 👍 |
Thanks for the heads-up. I can confirm that the fix has worked. Thanks! |
Also had the problem with version 6.0.14 that the connection fails and we had to set the directConnection option to true. Upgrading to 6.0.15 didn't solve it for us. So we continue to set this option. MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => {
return {
uri: DBUtil.getMongoDBConnectionUrl(config),
dbName: config.get('AICOBE_MONGODB_DATABASE'),
socketTimeoutMS: 15000,
};
},
inject: [ConfigService],
}) While uri was "mongodb://user:password@ip:port" Error log is: {
"context":"ExceptionHandler",
"level":"error",
"message":"getaddrinfo EAI_AGAIN mongodb_stndln",
"stack":
[
"
MongooseServerSelectionError: getaddrinfo EAI_AGAIN mongodb_stndln
at NativeConnection.Connection.openUri (/home/marcus/projects/aicosy/node_modules/mongoose/lib/connection.js:797:32)
at Mongoose.createConnection (/home/marcus/projects/aicosy/node_modules/mongoose/lib/index.js:276:10)
at Function.<anonymous> (/home/marcus/projects/aicosy/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:82:63)
at Generator.next (<anonymous>)
at /home/marcus/projects/aicosy/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (/home/marcus/projects/aicosy/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:16:12)
at /home/marcus/projects/aicosy/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:81:80
at Observable._subscribe (/home/marcus/projects/aicosy/node_modules/rxjs/src/internal/observable/defer.ts:53:15)
at Observable._trySubscribe (/home/marcus/projects/aicosy/node_modules/rxjs/src/internal/Observable.ts:244:19)
"
],
"timestamp":"2021-12-08T10:57:27.467Z"} |
My understand is: under the hood, "directConnection" used to default to true in mongoose v5, now it defaults to false in mongoose v6. So in my code I now always pass "directConnection: true" to the "openUri" API. The bug I reported was not to do with having to turn "directConnection" on (that to me was an acceptable change as node-modules with major version number changes will always have breaking changes), but more to do with how mongoose handled the connection readyStatus...it did not correctly report a valid connection to MongoDB. This bug is now fixed. |
@MarcusElevait this looks like a separate issue, the getaddrinfo error makes it look like you're running into the replica set hostnames issue |
@vkarpov15 Thanks I will check that. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
"openUri" behaviour changed from mongoose v5 to v6. It used to be able to connect to RSGhost, now it waits 30s then reports an error because it can't find the RSPrimary. This means it is no longer possible to programatically call "rs.initiate()" which our code relies on.
i.e.
If the current behavior is a bug, please provide the steps to reproduce.
Create an uninitialised replica-set database (i.e. one where rs.initiate has not been called). Try to connect to it via "openUri" - wait 30s to get the following error:
You can see in the error above it is aware of RSGhost.
What is the expected behavior?
Allow connection to RSGhost like mongoose v5 allowed.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 16.13.0
Mongoose 6.0.12
MongoDB 4.4.8
The text was updated successfully, but these errors were encountered: