Updating an entry succeeds but Java driver claims that n=0

30 views
Skip to first unread message

Patrick Peschlow

unread,
May 17, 2013, 9:56:42 AM5/17/13
to mongod...@googlegroups.com
Hi there,

we have a weird problem that sometimes occurs on our production system: Updating a database entry succeeds but the Java driver claims that n=0.

The setup is: Mongo 2.2.3, Java driver 2.11.1, sharding and replication. However, the database/collection in question is *not* sharded - I just mention this to highlight that a mongos is involved.

This is the sequence of events:
- In the Java code, we first successfully try to find a specific entry by its ID.
- Then we run an update using the same query. The ID is unique so we specify multi=false. The update uses WriteConcern.SAFE.
- The update call returns and the WriteResult claims that getN()==0.
- However, the database shows that the update succeeded.
- We check again if the entry is actually found by our clinet by finding it again, with success.

Does anyone have an idea how this can be possible?

The following code snippet (a bit simplified) experiences the issue, once in a while. It throws the IllegalStateException at the very end:

    // query the entry
    DBObject queryDbObject = new BasicDBObject("_id", id);
    DBObject entry = collection.findOne(queryDbObject);
    if (entry == null) {
        throw new IllegalStateException("Could not find entry with id " + id);
    }

    // update the entry
    DBObject updateDbObject = new BasicDBObject("$set", new BasicDBObject("status", "updated"));
    WriteResult writeResult = collection.update(queryDbObject, updateDbObject, false, false, WriteConcern.SAFE);

    if (writeResult.getError() != null) {
        throw new IllegalStateException("Got WriteResult error: " + writeResult.getError());
    }

    if (writeResult.getN() == 0) {
        // no entry was updated. just try to read the entry again
        if (collection.findOne(queryDbObject) != null) {
            throw new IllegalStateException("findOne, update n==0, findOne happened! How is this possible?");
        }
    }

Regards,
Patrick

Patrick Peschlow

unread,
May 17, 2013, 3:16:19 PM5/17/13
to mongod...@googlegroups.com
Here is an additional piece of information, WriteResult.toString(). This shows that "updatedExisting" = true but "n" = 0.

WriteResult: { "serverUsed" : "/127.0.0.1:27017" , "singleShard" : "my.replset02/node01:27016,node02:27016,node03:27016" , "n" : 0 , "lastOp" : { "$ts" : 1367820779 , "$inc" : 2} , "connectionId" : 7126 , "err" :  null  , "ok" : 1.0 , "writeback" : { "$oid" : "518749eb00000000000144c4"} , "writebackSince" : 30 , "instanceIdent" : "node01:27016" , "updatedExisting" : true , "writebackGLE" : { "singleShard" : "my.replset02/node01:27016,node02:27016,node03:27016" , "n" : 0 , "lastOp" : { "$ts" : 1367820779 , "$inc" : 2} , "connectionId" : 7126 , "err" :  null  , "ok" : 1.0} , "initialGLEHost" : "my.replset02/node01:27016,node02:27016,node03:27016"}

A similar situation was discussed in https://jira.mongodb.org/browse/SERVER-4532 for a sharded collection, but we don't have sharding here.

Asya Kamsky

unread,
May 18, 2013, 11:27:37 PM5/18/13
to mongod...@googlegroups.com
Do you have authentication turned on?

Asya Kamsky

unread,
May 18, 2013, 11:33:34 PM5/18/13
to mongod...@googlegroups.com
Also SERVER-4532 and all its relatives we knew about were fixed for 2.2.3 which is the version you are running, yes?  On all your mongod's and mongos's?


On Friday, May 17, 2013 12:16:19 PM UTC-7, Patrick Peschlow wrote:

Patrick Peschlow

unread,
May 19, 2013, 5:00:39 PM5/19/13
to mongod...@googlegroups.com
We have authentication turned on. All our mongod's  and mongos's are running on 2.2.3. I will open a server ticket for this one and put any additional information I can get in there.

Thanks,
Patrick
Reply all
Reply to author
Forward
0 new messages