Skip to content
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

useUnifiedTopology option causes HA to fail to reconnect #8224

Closed
stieg opened this issue Oct 7, 2019 · 1 comment
Closed

useUnifiedTopology option causes HA to fail to reconnect #8224

stieg opened this issue Oct 7, 2019 · 1 comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@stieg
Copy link
Contributor

stieg commented Oct 7, 2019

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
When using a mongodb URL that uses a replica set in combination with the useUnifiedTopology flag the replica set connection will fail to reestablish if one of the members in the collection goes offline. This is a regression from previous behavior where the following was noticed:

  • If a primary goes offline, the connection is severed for a couple of seconds, then recovers
  • If a secondary goes offline, no connection event occurs.

If the current behavior is a bug, please provide the steps to reproduce.
Setup a 3 instance mongod cluster with replication. I do this using docker with a small script I wrote. If you would like to use it I would be happy to attach it: just ask. Anyways I made the following script with hooks to demonstrate:

const assert = require('assert');
const mongoose = require('mongoose');
const process = require('process');

const opts = {
  autoIndex: false,
  bufferMaxEntries: 0, // Don't buffer on disconnect.  Fail fast instead.
  useNewUrlParser: true,
  useUnifiedTopology: true, // XXX: Breaks replica set
};

function setupNotifications(conn) {
  conn.on('connecting', () => console.log('Connecting to mongo server'));
  conn.on('connected', () => console.log('Connected to mongo server'));
  conn.on('reconnected', () => console.log('Reconnecting to mongo server'));
  conn.on('disconnected', () => console.log('Disconnected from mongo server'));
  conn.on('error', () => console.log('Error with mongo server'));
  conn.on('fullsetup', () => console.log('HA Full setup'));
  conn.on('all', () => console.log('HA completely connected'));
  conn.on('reconnectFailed', () => console.error('MongoDB reconnection failed'));
}

async function shutdown(code = 0) {
  if (typeof process.exitCode === 'number') return; // Already shutting down check.
  process.exitCode = code;

  console.log('Disconnecting from DB');
  await mongoose.disconnect();
  console.log('Disconnected from DB');
}

async function startup(url) {
  console.log('Connecting to DB');
  await mongoose.connect(url, opts);
  setupNotifications(mongoose.connection);
  console.log('Connected to DB');
}

function setupHandler(signal, handle) {
  console.log(`Registering signal handler for ${signal}`);
  process.on(signal, handle);
}

// Configure default migrator for db migration
async function main(url) {
  try {
    assert.ok(url, 'MongoDB URL not provided as argument');

    process.on('unhandledRejection', err => {
      console.error(`Uhandled Rejection:`, err);
      shutdown(1);
    });

    await startup(url);

    setupHandler('SIGHUP', shutdown);
    setupHandler('SIGINT', shutdown);
    setupHandler('SIGTERM', shutdown);
  } catch (err) {
    console.error(`Failed to connect to DB:`, err);
  }
}

main(process.argv[2]);

I invoke the script with the URL and disable or enable the useUnifiedTopology flag (either via the URL or via my options within the script) and then I proceed to shutdown one of the replicas unexpectedly. Below is the output:

With useUnifiedTopology enabled (does not matter if primary or secondary)

 node ./mongoose-connect-demo.js 'mongodb://mongo1:27117,mongo2:27217,mongo3:27317/test?replicaSet=rs0'
Connecting to DB
Connected to DB
Registering signal handler for SIGHUP
Registering signal handler for SIGINT
Registering signal handler for SIGTERM
Disconnected from mongo server
^CDisconnecting from DB
Disconnected from DB

bg
^Z
[1]  + 30741 suspended  node ./mongoose-connect-demo.js
➜  mongo-test bg
[1]  + 30741 continued  node ./mongoose-connect-demo.js
➜  mongo-test kill -9 30741
[1]  + 30741 killed     node ./mongoose-connect-demo.js

Mongoose sits in the disconnected state and never recovers automatically.

With useUnifiedTopology disabled (knocked out secondary)

➜  mongo-test node ./mongoose-connect-demo.js 'mongodb://mongo1:27117,mongo2:27217,mongo3:27317/test?replicaSet=rs0'
Connecting to DB
(node:9356) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Connected to DB
Registering signal handler for SIGHUP
Registering signal handler for SIGINT
Registering signal handler for SIGTERM

With useUnifiedTopology disabled (knocked out primary)

➜  mongo-test node ./mongoose-connect-demo.js 'mongodb://mongo1:27117,mongo2:27217,mongo3:27317/test?replicaSet=rs0'
Connecting to DB
(node:9559) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Connected to DB
Registering signal handler for SIGHUP
Registering signal handler for SIGINT
Registering signal handler for SIGTERM
Disconnected from mongo server
Connected to mongo server
Reconnecting to mongo server

What is the expected behavior?
HA topology handles failure gracefully and reconnects at an absolute minimum.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

➜  mongo-test node --version
v12.10.0
➜  mongo-test npm list mongoose
mongo-toJSON-failure@1.0.0 /home/stieg/Devel/airfordable/mongo-test
└── mongoose@5.7.3 
MongoD version 4.2.0 (via Docker)
@vkarpov15 vkarpov15 added this to the 5.7.5 milestone Oct 9, 2019
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Oct 9, 2019
@10kc-awright
Copy link

This looks like it is broken from the node driver (including not emitting the correct events) https://jira.mongodb.org/browse/NODE-2231?jql=text%20~%20%22useUnifiedTopology%22

esp: mongodb-js/mongodb-core#455

@vkarpov15 vkarpov15 modified the milestones: 5.7.5, 5.7.6 Oct 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

3 participants