-
-
Notifications
You must be signed in to change notification settings - Fork 122
Getting duplicate messages using Mongodb as mosca backend. #131
Comments
I'm +1 with the fix. That each() is supposed to be sequential, so something
|
Hi, @mcollina. I've create a PR. Since we are using mosca in a production environment, could you please push a new release to the npm once it is merged? I did some experiment to locate the reason of this problem. The function executed by each() is fine. I thought the cause may be inside the mongodb driver, or even the mongodb it self, until I saw this on mongodb's document http://docs.mongodb.org/master/tutorial/create-tailable-cursor/ .
Actually by setting tailable of this cursor to false on https://github.com/mcollina/ascoltatori/blob/master/lib/mongo_ascoltatore.js#L260 did solve this problem. However, I do not dare to use this in production as I'm not fully into ascoltatori and not certain how many changes will this modification bring. |
Fix a problem related to Issues #131 Getting duplicate messages using Mongodb as mosca backend.
Setting A completely different solution might be to replace This is relevant also: #90. I've merged your fix and I will release soon! However, we should look for a more stable fix. I think we should just remove the checks on |
Thank you very much for the fix . I'm wondering if it is important to guarantee the read order. If this is an important feature of ascoltatori, I agree with your fix, as building index on a field will break the read order guarantee. However, if ascoltatori does not promise read order, then maybe indexing timestamp and max(latest, document.timestamp) is a better option. The ObjectID may cause some confusion in a multi-server environment. |
Read order is not really important. The problem with indexing is speed: objects are inserted and removed from the capped collection at all times. Insertion speed is an issue I would like to avoid. The order is already guaranteed by the fact of using a capped collection + tailable cursor. |
In that case, use timestamp without index is the only practical way. Initialization time is acceptable, given that the capped collection is not actually that large. |
👍. Would you mind sending a PR through? :) Thanks so much. |
I can have a try. |
Is this still a problem? Would you mind sending a PR? |
Hi @mcollina , I'm using ascolatatori as part of mosca. Recently I found that as long as I send three messages to a channel which has a client subscribed to very very quickly, the client will eventually receive four messages. I lookup into the sources and found the root cause may be in the ascoltatori.
How to reproduce
Have a client connected to a mosca server, subscribe to a channel, and send three consecutive messages very very quickly. Then at the client side, you will see three messages arrived, and yet another one after some time (this time gap equals the wait param you set for mongo backend ascoltatori).
This has been verified on mongodb 3.0.6 and 2.4.10
I tried to reproduce the problem in the unit test but I cannot always reproduce it in the unit test. I'm not quite sure why. Sometimes it can pass and sometimes it cannot. Here is my test code
to replace the original code in https://github.com/mcollina/ascoltatori/blob/master/test/mongo_ascoltatore_spec.js#L110 from line 110 to line 124.
Root cause
I've looked into the sources. Each message sent to a channel will create two entries in the capped collection that the mongo ascoltatori uses. In other words, the _id of entries is supposed to increase all the time. But if you send messages through mosca, and mosca calls mongodb backend, the order of fifth and sixth document is not preserved. I added a line to print out the value of latest here: https://github.com/mcollina/ascoltatori/blob/master/lib/mongo_ascoltatore.js#L295. And this is what I got.
You see there is a gap between the ids since I'm using Mongo as backend of mosca persistence. Each qos1 message will consume an id. But the result is same without qos1.
I think the reason might be either:
Temporary fix
I suggest to change https://github.com/mcollina/ascoltatori/blob/master/lib/mongo_ascoltatore.js#L295 to following to temporarily get rid of this problem.
From:
To:
The text was updated successfully, but these errors were encountered: