Huge increase in connections crashed the Mongo Server and blocked the system
Rocket.Chat Version: 0.54.1 Running Instances: 1 Oplog: disabled Node Version: 4.8.1
Yesterday we suffered a shutdown of our services. According to MongoDB logs in almost 30 seconds the number of connections raised to 620. The Server status reported:
2017-03-31T01:23:52.314+0200 I COMMAND [ftdc] serverStatus was very slow: { after basic: 250, after asserts: 470, after connections: 620, after extra_info: 1060, after globalLock: 1320, after locks: 1520, after network: 1570, after opcounters: 1680, after opcountersRepl: 1680, after storageEngine: 1940, after tcmalloc: 2430, after wiredTiger: 3310, at end: 4620 }
Some seconds later the report shows a reduction in the connections number, but in that moment the system was already blocked:
2017-03-31T01:24:00.360+0200 I COMMAND [ftdc] serverStatus was very slow: { after basic: 120, after asserts: 220, after connections: 370, after extra_info: 810, after globalLock: 1060, after locks: 1160, after network: 1220, after opcounters: 1300, after opcountersRepl: 1300, after storageEngine: 1520, after tcmalloc: 1940, after wiredTiger: 3320, at end: 4600 }
That made that no one could be able to connect after that, and the previous connections were completely blocked. RocketChat logs presented the next data:
Exception in setInterval callback: Error: read ECONNRESET
at Object.Future.wait (/var/www/rocketchat/programs/server/node_modules/fibers/future.js:449:15)
at [object Object]._.extend._nextObject (/var/www/rocketchat/programs/server/packages/mongo.js:1054:47)
at [object Object]._.extend.forEach (/var/www/rocketchat/programs/server/packages/mongo.js:1086:22)
at [object Object]._.extend.map (/var/www/rocketchat/programs/server/packages/mongo.js:1096:10)
at [object Object]._.extend.fetch (/var/www/rocketchat/programs/server/packages/mongo.js:1116:17)
at [object Object].Cursor.(anonymous function) [as fetch] (/var/www/rocketchat/programs/server/packages/mongo.js:948:44)
at Object.<anonymous> (packages/matb33_collection-hooks.js:445:81)
at Object.collection.(anonymous function) [as update] (packages/matb33_collection-hooks.js:146:21)
at [object Object].update (/var/www/rocketchat/programs/server/packages/mongo.js:3882:31)
at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/dispatch_run-as-user.js:325:19)
at Object.InstanceStatus.ping (packages/konecty_multiple-instances-status.js:110:24)
at packages/konecty_multiple-instances-status.js:98:19
at [object Object]._.extend.withValue (packages/meteor.js:1122:17)
at packages/meteor.js:445:45
at runWithEnvironment (packages/meteor.js:1176:24)
A little bit before of that, it looks there is a problem with SMTP, but I don't know if that is affecting:
Exception in defer callback: Error: read ECONNRESET
at Object.Future.wait (/var/www/rocketchat/programs/server/node_modules/fibers/future.js:449:15)
at smtpSend (packages/email.js:108:37)
at Object.Email.send (packages/email.js:211:5)
at /var/www/rocketchat/programs/server/packages/rocketchat_lib.js:1687:14
at [object Object]._.extend.withValue (packages/meteor.js:1122:17)
at packages/meteor.js:445:45
at runWithEnvironment (packages/meteor.js:1176:24)
- - - - -
at exports._errnoException (util.js:907:11)
at TLSWrap.onread (net.js:561:26)
Those exception are being repeated a lot of times, and after that there is a lot of EPIPE errors:
Exception in setInterval callback: Error: write EPIPE
at Object.Future.wait (/var/www/rocketchat/programs/server/node_modules/fibers/future.js:449:15)
at [object Object]._.extend._nextObject (/var/www/rocketchat/programs/server/packages/mongo.js:1054:47)
at [object Object]._.extend.forEach (/var/www/rocketchat/programs/server/packages/mongo.js:1086:22)
at [object Object]._.extend.map (/var/www/rocketchat/programs/server/packages/mongo.js:1096:10)
at [object Object]._.extend.fetch (/var/www/rocketchat/programs/server/packages/mongo.js:1116:17)
at [object Object].Cursor.(anonymous function) [as fetch] (/var/www/rocketchat/programs/server/packages/mongo.js:948:44)
at Object.<anonymous> (packages/matb33_collection-hooks.js:445:81)
at Object.collection.(anonymous function) [as update] (packages/matb33_collection-hooks.js:146:21)
at [object Object].update (/var/www/rocketchat/programs/server/packages/mongo.js:3882:31)
at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/dispatch_run-as-user.js:325:19)
at Object.InstanceStatus.ping (packages/konecty_multiple-instances-status.js:110:24)
at packages/konecty_multiple-instances-status.js:98:19
at [object Object]._.extend.withValue (packages/meteor.js:1122:17)
at packages/meteor.js:445:45
at runWithEnvironment (packages/meteor.js:1176:24)
- - - - -
at exports._errnoException (util.js:907:11)
at WriteWrap.afterWrite (net.js:790:14)
Ideas to solve it? Isolated case?
@juanrhino I see you say oplog is disabled? If you aren't using oplog Rocket.Chat has to open more queries to mongodb. I would try using oplog. You should notice a pretty large drop in number of connections.
To use oplog you'll have to start using replicaset.
But is weird, I've been checking MongoDB connections and so far the average is 5, so it is strange that only for a moment it had more than 600 connections. I understand the oplog theory, but in our case I don't know if it is an overkill, cause we only have 110 users, and maybe our maximum is going to be 300 once we deliver the product to the complete company (but with an average of 1r/s to 3 r/s). What can cause that increment in the connections? I'm going to put a little bit of my infrastructure (maybe that helps):
- DMZ Network I have a t2.micro Amazon Linux machine with NGINX as reverse proxy/Load Balancer and HTTP server
- Backend network I have a t2.small Amazon Linux box with Rocketchat
- Database network I have a t2.medium Amazon Linux with MongoDB and 3 EBS SSD volumes: root, logs and data/journal
@juanrhino nothing seems wrong with your setup. I would recommend oplog for a couple of reasons. While growing lets say you decide you need another Rocket.Chat instance running to help share the load. Or you want a more fault tolerant setup. Oplog will be required for that. Also, we've noticed performance gains by using it.
As to why the increase in connections. I would assume the connections are building up over time as activity picks up on the server. Since Rocket.Chat doesn't have the stream of events being delivered to it via oplog, it has to fall back on polling the database to make sure things haven't changed. I have a hard time seeing it needing 600 connections... i'll check into this.
I'm sure you have done this, but just in case... Make sure mongo is only listening on a private network address and not binding to all interfaces.
Thanks @geekgonecrazy I think we will discuss it here at the company. To answer your last question, that MongoDB on listens the Backend network, that means other external connections are not possible. Given into account the actual architecture, the 600 connections are really weird and I've been checking the complete week the connections numbers and it's always 5 (actually 6 with the monitor connection)
@juanrhino one more thing that might contribute is if you have a lot of file uploads and are using GridFS as the file store. I believe it opens a new connection or two for file uploads. As well as a connection when retrieving a file.