RejectedSoftware Forums

Sign up

Pages: 1 2

Strange error when trying to use MongoDB

Hi,

I am trying to use the MongoDB driver, but I am having some issues. I have the following piece of code:

  auto logins = client.getCollection("logins.level2");
  auto user   = logins.find(["ip" : ip]);

  if (user.empty)
  {
    logins.insert(serializeToBson(
      [
        "ip"       : serializeToBson(ip),
        "attempts" : serializeToBson(1),
        "time"     : serializeToBson(to!int(Clock.currTime().toUnixTime()))
      ]
    ));
  }

This code is executed, when a user attempts to enter authenticated part of server. The data is successfully inserted into the DB (verified using the mongo client), but the user get this in the browser:

500 - Internal Server Error

Internal Server Error

Internal error information:
vibe.db.mongo.connection.MongoDriverException@../../.dub/packages/vibe-d-0.7.17/source/vibe/db/mongo/connection.d(302): BSON value is type 'Null', expected to be one of [Int]
----------------
/tmp/dub/4191021397/webserver(void vibe.db.mongo.connection.MongoConnection.checkForError(immutable(char)[])+0x84) [0x616e44]
/tmp/dub/4191021397/webserver(void vibe.db.mongo.connection.MongoConnection.insert(immutable(char)[], vibe.db.mongo.connection.InsertFlags, vibe.data.bson.Bson[])+0x251) [0x615881]
/tmp/dub/4191021397/webserver(void vibe.db.mongo.collection.MongoCollection.insert!(vibe.data.bson.Bson).insert(vibe.data.bson.Bson, vibe.db.mongo.connection.InsertFlags)+0x176) [0x61afae]
/tmp/dub/4191021397/webserver(void app.timedAuth(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)+0x24c) [0x576a9c]
/tmp/dub/4191021397/webserver(void std.functional.DelegateFaker!(void function(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)*).DelegateFaker.doIt(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)+0x68) [0x5b0f20]
/tmp/dub/4191021397/webserver(void vibe.http.router.URLRouter.handleRequest(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)+0x179) [0x639479]
/tmp/dub/4191021397/webserver(bool vibe.http.server.handleRequest(vibe.core.stream.Stream, immutable(char)[], vibe.http.server.HTTPServerListener, ref vibe.http.server.HTTPServerSettings, ref bool)+0x162e) [0x640c56]
/tmp/dub/4191021397/webserver(void vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener)+0x143) [0x63f563]
/tmp/dub/4191021397/webserver(void vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings, void delegate(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)).void doListen(vibe.http.server.HTTPServerSettings, vibe.http.server.HTTPServerListener, immutable(char)[]).void __lambda110(vibe.core.net.TCPConnection)+0x2c) [0x63bb44]
/tmp/dub/4191021397/webserver(extern (C) nothrow void vibe.core.drivers.libevent2_tcp.onConnect(int, short, void*).void ClientTask.execute()+0x2d6) [0x5e6506]
/tmp/dub/4191021397/webserver(void vibe.core.core.CoreTask.run()+0xf2) [0x606c6a]
/tmp/dub/4191021397/webserver(void core.thread.Fiber.run()+0x2a) [0x6ac9f2]
/tmp/dub/4191021397/webserver(fiber_entryPoint+0x61) [0x6ac8fd]
[(nil)]

Do you have any idea, why this is happening?

Thanks

Re: Strange error when trying to use MongoDB

Am 11.11.2013 11:52, schrieb Drasha:

Hi,

I am trying to use the MongoDB driver, but I am having some issues. I have the following piece of code:

...

Do you have any idea, why this is happening?

Thanks

It seems like the MongoDB server doesn't send one of the expected fields
for the getLastError query. If you could enable verbose logging in
your MongoDB server and see what exactly it sends in response to the
last getLastError request, that would be great. I can then adjust the
code appropriately.

Re: Strange error when trying to use MongoDB

On Mon, 11 Nov 2013 12:59:19 +0100, Sönke Ludwig wrote:

It seems like the MongoDB server doesn't send one of the expected fields
for the getLastError query. If you could enable verbose logging in
your MongoDB server and see what exactly it sends in response to the
last getLastError request, that would be great. I can then adjust the
code appropriately.

Ok, I had a quick and dirty fight with my mongodb instance and was not able to properly configure the verbosity. Or rather I was, but not to a point to debug my problem. So I instead launched Wireshark and captured the reply. Following is the dissected packet. On first glance it seems that mongo does not recognize getLastError command.

Btw, my mongo instance is a default debian instalation:
$ mongod --version
db version v1.4.4, pdfile version 4.5
Mon Nov 11 20:04:40 git version: nogitversion

Thanks,
Drasha

Frame 9: 179 bytes on wire (1432 bits), 179 bytes captured (1432 bits)
Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)
Internet Protocol Version 4, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0.1)
Transmission Control Protocol, Src Port: 27017 (27017), Dst Port: 39548 (39548), Seq: 37, Ack: 239, Len: 113
Mongo Wire Protocol
    Message Length: 113
    Request ID: 0x223c71d6 (574386646)
    Response To: 0x00000009 (9)
    OpCode: Reply (1)
    Reply Flags
        .... .... .... .... .... .... .... ...0 = Cursor Not Found: No
        .... .... .... .... .... .... .... ..0. = Query Failure: No
        .... .... .... .... .... .... .... .0.. = Shared Config Stale: No
        .... .... .... .... .... .... .... 0... = Await Capable: No
    Cursor ID: 0
    Starting From: 0
    Number Returned: 1
    Document
        Document length: 77
        Elements
            Element: errmsg
                Type: String (0x02)
                Length: 12
                Value: no such cmd
            Element: bad cmd
                Type: Document (0x03)
                Document
                    Document length: 27
                    Elements
                        Element: getLastError
                            Type: Double (0x01)
                            Value: 1
            Element: ok
                Type: Double (0x01)
                Value: 0

Re: Strange error when trying to use MongoDB

On Mon, 11 Nov 2013 19:05:49 GMT, Drasha wrote:

On Mon, 11 Nov 2013 12:59:19 +0100, Sönke Ludwig wrote:

It seems like the MongoDB server doesn't send one of the expected fields
for the getLastError query. If you could enable verbose logging in
your MongoDB server and see what exactly it sends in response to the
last getLastError request, that would be great. I can then adjust the
code appropriately.

Ok, I had a quick and dirty fight with my mongodb instance and was not able to properly configure the verbosity. Or rather I was, but not to a point to debug my problem. So I instead launched Wireshark and captured the reply. Following is the dissected packet. On first glance it seems that mongo does not recognize getLastError command.

(...)

Thanks, got it. Should be fixed on latest git master by c633db9.

Re: Strange error when trying to use MongoDB

On Mon, 11 Nov 2013 19:35:36 GMT, Sönke Ludwig wrote:

Thanks, got it. Should be fixed on latest git master by c633db9.

Thanks. I am afraid that it is not enough though. The error is still there, although it changed a bit (like a very little bit). Here is the new error output:

500 - Internal Server Error

Internal Server Error

Internal error information:
vibe.db.mongo.connection.MongoDriverException@../../.dub/packages/vibe-d-master/source/vibe/db/mongo/connection.d(302): BSON value is type 'null_', expected to be one of [int_]
----------------
/tmp/dub/3216937015/webserver(void vibe.db.mongo.connection.MongoConnection.checkForError(immutable(char)[])+0x84) [0x6a19d8]
/tmp/dub/3216937015/webserver(void vibe.db.mongo.connection.MongoConnection.insert(immutable(char)[], vibe.db.mongo.connection.InsertFlags, vibe.data.bson.Bson[])+0x251) [0x6a0415]
/tmp/dub/3216937015/webserver(void vibe.db.mongo.collection.MongoCollection.insert!(vibe.data.bson.Bson).insert(vibe.data.bson.Bson, vibe.db.mongo.connection.InsertFlags)+0x176) [0x6a57e2]
/tmp/dub/3216937015/webserver(void app.timedAuth(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)+0x24c) [0x5bbbb8]
/tmp/dub/3216937015/webserver(void std.functional.DelegateFaker!(void function(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)*).DelegateFaker.doIt(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)+0x68) [0x633b0c]
/tmp/dub/3216937015/webserver(void vibe.http.router.URLRouter.handleRequest(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)+0x179) [0x6c4a0d]
/tmp/dub/3216937015/webserver(bool vibe.http.server.handleRequest(vibe.core.stream.Stream, immutable(char)[], vibe.core.net.NetworkAddress, vibe.http.server.HTTPServerListener, ref vibe.http.server.HTTPServerSettings, ref bool)+0x172f) [0x6cc6eb]
/tmp/dub/3216937015/webserver(void vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection, vibe.http.server.HTTPServerListener)+0x1a2) [0x6caef6]
/tmp/dub/3216937015/webserver(void vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings, void delegate(vibe.http.server.HTTPServerRequest, vibe.http.server.HTTPServerResponse)).void doListen(vibe.http.server.HTTPServerSettings, vibe.http.server.HTTPServerListener, immutable(char)[]).void __lambda115(vibe.core.net.TCPConnection)+0x2c) [0x6c71b0]
/tmp/dub/3216937015/webserver(extern (C) nothrow void vibe.core.drivers.libevent2_tcp.onConnect(int, short, void*).void ClientTask.execute()+0x2d6) [0x66fb6a]
/tmp/dub/3216937015/webserver(void vibe.core.core.CoreTask.run()+0xf2) [0x691706]
/tmp/dub/3216937015/webserver(void core.thread.Fiber.run()+0x2a) [0x7432a6]
/tmp/dub/3216937015/webserver(fiber_entryPoint+0x61) [0x7431b1]
[(nil)]

This is what I have in mongodb.log

Tue Nov 12 08:09:04 query logins.level2 ntoreturn:0 reslen:36 nreturned:0 0ms
Tue Nov 12 08:09:04 insert logins.level2 0ms
Tue Nov 12 08:09:04 run command logins.$cmd { getLastError: 1.0 }
Tue Nov 12 08:09:04 query logins.$cmd ntoreturn:1 command  reslen:113 0ms
Tue Nov 12 08:09:04 end connection 127.0.0.1:46259

The dissected packet looks the same as before.

If there is anything I can do on my end, just tell me...

Thanks,
Drasha

Re: Strange error when trying to use MongoDB

Am 12.11.2013 08:18, schrieb Drasha:> On Mon, 11 Nov 2013 19:35:36 GMT, Sönke Ludwig wrote:

Thanks, got it. Should be fixed on latest git master by c633db9.

Thanks. I am afraid that it is not enough though. The error is still there, although it changed a bit (like a very little bit). Here is the new error output:
(...)
The dissected packet looks the same as before.

If there is anything I can do on my end, just tell me...

Thanks,
Drasha

Can you try to insert a logInfo("getLastError: %s", reply.toJson().toString(); right after the query line in source/vibe/db/mongo/connection.d:262? The query itself looks OK so far, but maybe the reply reveals the issue.

Re: Strange error when trying to use MongoDB

On Tue, 12 Nov 2013 10:50:18 GMT, Sönke Ludwig wrote:

Am 12.11.2013 08:18, schrieb Drasha:> On Mon, 11 Nov 2013 19:35:36 GMT, Sönke Ludwig wrote:

Can you try to insert a logInfo("getLastError: %s", reply.toJson().toString(); right after the query line in source/vibe/db/mongo/connection.d:262? The query itself looks OK so far, but maybe the reply reveals the issue.

This is what I get:

getLastError: {"documents":[{"errmsg":"no such cmd","ok":0,"bad cmd":{"getLastError":1}}],"cursor":0,"flags":0,"firstDocument":0}

Btw... I had to write

serializeToJson(reply).toString()

your version did not work. I got the "Error: no property 'toJson' for type 'vibe.db.mongo.connection.Reply'". I have dmd 2.063.2 and vibe.d master;

Martin

Re: Strange error when trying to use MongoDB

On Tue, 12 Nov 2013 13:51:14 GMT, Drasha wrote:

On Tue, 12 Nov 2013 10:50:18 GMT, Sönke Ludwig wrote:

Am 12.11.2013 08:18, schrieb Drasha:> On Mon, 11 Nov 2013 19:35:36 GMT, Sönke Ludwig wrote:

Can you try to insert a logInfo("getLastError: %s", reply.toJson().toString(); right after the query line in source/vibe/db/mongo/connection.d:262? The query itself looks OK so far, but maybe the reply reveals the issue.

This is what I get:

getLastError: {"documents":[{"errmsg":"no such cmd","ok":0,"bad cmd":{"getLastError":1}}],"cursor":0,"flags":0,"firstDocument":0}

That's really strange. What log entry is produced by the following when entered in the mongo shell?

use logins
db.getLastError()

For me it produces exactly the same ouput, except that it doesn't print "query", but "command" in the second row (and of course it's a different DB name):

Tue Nov 12 15:07:25 [conn16] run command vibenews.$cmd { getLastError: 1.0 }
Tue Nov 12 15:07:25 [conn16] command vibenews.$cmd command: { getLastError: 1.0 } ntoreturn:1 keyUpdates:0  reslen:67 0ms

But maybe it's just the really old version of the server (1.4), which doesn't yet support the command. Can you try to use their repository with the latest version? I'd personally recommend that as the pre 2.0 versions have been pretty unsable in my experience.

Btw... I had to write

serializeToJson(reply).toString()

your version did not work. I got the "Error: no property 'toJson' for type 'vibe.db.mongo.connection.Reply'". I have dmd 2.063.2 and vibe.d master;

Oh sorry, I sloppily assumed that reply was of type Bson.

Re: Strange error when trying to use MongoDB

On Tue, 12 Nov 2013 14:18:13 GMT, Sönke Ludwig wrote:

But maybe it's just the really old version of the server (1.4), which doesn't yet support the command. Can you try to use their repository with the latest version? I'd personally recommend that as the pre 2.0 versions have been pretty unsable in my experience.

Done that, fixed it. You are probably right with the version being old. If you want, I can help you tracking the bug with 1.4, or we can let it be and have it as resolved.

Btw... I had to write

serializeToJson(reply).toString()

your version did not work. I got the "Error: no property 'toJson' for type 'vibe.db.mongo.connection.Reply'". I have dmd 2.063.2 and vibe.d master;

Oh sorry, I sloppily assumed that reply was of type Bson.

No problem, I was not sure if it was an overlook or has to do something with e.g. UFCS in new dmd.

Thanks,
Drasha

Re: Strange error when trying to use MongoDB

Am 12.11.2013 18:20, schrieb Drasha:

On Tue, 12 Nov 2013 14:18:13 GMT, Sönke Ludwig wrote:

But maybe it's just the really old version of the server (1.4), which doesn't yet support the command. Can you try to use their repository with the latest version? I'd personally recommend that as the pre 2.0 versions have been pretty unsable in my experience.

Done that, fixed it. You are probably right with the version being old. If you want, I can help you tracking the bug with 1.4, or we can let it be and have it as resolved.

If there is possibly a simple alias for "getLastError", then a fix might
be worth it. But since 1.4.4 is already more than three years old, I'm
inclined to simply say that 1.8 is the minimum supported version.

Pages: 1 2