What am I doing wrong? C# driver throwing.

142 views
Skip to first unread message

Splash

unread,
Feb 9, 2011, 4:06:36 AM2/9/11
to mongodb-user
Hi,

I've recently put together a small app which constantly receives data
via a serial port, transforms this into a nice set of documents and
logs them into several collections. I am also periodically running a
couple of map-reduce operations over this data in order to calculate
averages for some larger time periods.

There is new data to be logged to the two major collections ('energy-
instant' and 'temperatures') every six seconds. The two map-reduce
operations are calculating the average energy per minute and
temperature per 10 minutes respectively. However every so often I am
seeing crashes occurring in my app originating in the C# driver. I'm
not sure if running two large map-reduce operations one after the
other is causing this, as I understood it ought to be fine. It is also
very sporadic... it can go a good several hours without any problems
and then crash out several times within a few minutes.

I'm using the 'official' C# driver, and am seeing this issue with
MongoDB 1.6.5 and 1.7.5.

I've attached the stack trace I'm seeing (sorry, it's in release mode
so no line numbers - if this is a problem, I'll try and reproduce it
in debug) at the bottom. All the code in the 'MarkEmbling.Home.Logger'
namespace are of course my code - the 'MapReduceAverageInstantEnergy'
method basically just runs one map-reduce method and then the other
straight afterwards. Both are being asked to save to a permanent
collection.

I've tried changing the app to open a new connection (and close it
again) for each chunk of functionality - this made no difference (and
seems like a bad decision, so will revert this) - even doing this, I'm
doing both map-reduces on the same connection.

I guess my question is this: am I doing something fundamentally wrong
here? Since the exception is coming from down in System.Net.Sockets
from inside the driver, I cant help thinking that either me or it is
causing a situation nobody really foresaw occurring. Any help would be
greatly appreciated, thanks :)


Unhandled Exception: System.IO.IOException: Unable to read data from
the transport connection: A connection attempt fail
ed because the connected party did not properly respond after a period
of time, or established connection failed because
connected host has failed to respond. --->
System.Net.Sockets.SocketException: A connection attempt failed
because the
connected party did not properly respond after a period of time, or
established connection failed because connected host
has failed to respond
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32
offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32
offset, Int32 size)
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream, Int32 count)
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream)
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument]
(MongoServer server)
at
MongoDB.Driver.Internal.MongoCursorEnumerator`1.GetReply(MongoRequestMessage
message)
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.GetFirst()
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1
source)
at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](IMongoQuery
query)
at MongoDB.Driver.MongoCollection`1.FindOne(IMongoQuery query)
at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult]
(IMongoCommand command)
at MongoDB.Driver.MongoCollection.MapReduce(BsonJavaScript map,
BsonJavaScript reduce, IMongoMapReduceOptions options
)
at
MarkEmbling.Home.Logger.Brains.Runner.MapReduceAverageInstantEnergy(MongoDatabase
db)
at MarkEmbling.Home.Logger.Brains.Runner.CalculateAverages()
at MarkEmbling.Home.Logger.Brains.Runner.Start()
at MarkEmbling.Home.Logger.Console.Program.Run()
at MarkEmbling.Home.Logger.Console.Program.Main(String[] args)

Nat

unread,
Feb 9, 2011, 5:47:27 AM2/9/11
to mongodb-user
It means that your M/R takes longer than 30 seconds. Did you do MR
with inline output?

Mark Embling

unread,
Feb 9, 2011, 6:16:53 AM2/9/11
to mongod...@googlegroups.com
Taking longer than 30 seconds is expected. But I have told it to output to a collection, not inline... (I assume that's what you meant). I was of the understanding that M/R was not blocking in this case...?

Nat

unread,
Feb 9, 2011, 6:56:12 AM2/9/11
to mongodb-user
BTW, which version of driver do you use? I looked at the current code.
It didn't seem to set the timeout which means it should be infinite.

Mark Embling

unread,
Feb 9, 2011, 7:44:35 AM2/9/11
to mongod...@googlegroups.com
I only downloaded it a week or so ago, version 0.11.0.4042. I also find it strange that it only happens intermittently. The map-reduce operation ought to always take a fair bit of time (actually, as time goes on it'll take longer - something I'm yet to look at) and yet it only occasionally does this, in no discernible pattern.

For now, I've worked around it by removing all the MR stuff from my code and am running it separately from a ruby script. Not exactly a long-term solution, but the ruby driver is coping more than adequately so far (seems to rule out the case whereby it's what I'm doing causing this).

Nat

unread,
Feb 9, 2011, 7:57:24 AM2/9/11
to mongodb-user
You already have the latest version. I can confirm that the socket
timeout is set to infinity. So ideally you shouldn't get that
exception.

BTW, some more questions:

- which platform are you on?
- Do you have firewall turn on?
- Do you see anything error from mongod log file?
- Are you using sharding/mongos?

On Feb 9, 8:44 pm, Mark Embling <markembl...@gmail.com> wrote:
> I only downloaded it a week or so ago, version *0.11.0.4042*. I also find it

Mark Embling

unread,
Feb 9, 2011, 8:04:21 AM2/9/11
to mongod...@googlegroups.com
- which platform are you on? 
Windows Server 2008 R2. Tried with MongoDB 1.6.5 and 1.7.5 - same issue. The application is built with .NET 4.

- Do you have firewall turn on? 
Yes, but MongoDB has an exception (and it's connecting to localhost anyway...)

- Do you see anything error from mongod log file? 
Unfortunately not. That was my first port of call to get any clue as to what might be wrong. Looking at that, it's all fine.

- Are you using sharding/mongos? 

No, just plain old mongod.

Robert Stam

unread,
Feb 9, 2011, 8:23:27 AM2/9/11
to mongodb-user
You can change the socket timeout from the connection string. For
example, to set a 5 minute timeout use:

var connectionString = "mongodb://localhost/?sockettimeout=5m"

the default is 30 seconds. You can also change the default for all
servers by setting the SocketTimeout static property of MongoDefaults:

MongoDefaults.SocketTimeout = TimeSpan.FromMinutes(5);

but you would have to make sure to do this BEFORE you call
MongoServer.Create for the first time.

The timeout setting applies to all operations on a server. It is not
currently possible to set a longer timeout for just one individual
operation.

On Feb 9, 8:04 am, Mark Embling <markembl...@gmail.com> wrote:
> *- which platform are you on? *
> Windows Server 2008 R2. Tried with MongoDB 1.6.5 and 1.7.5 - same issue. The
> application is built with .NET 4.
>
> *- Do you have firewall turn on? *
> Yes, but MongoDB has an exception (and it's connecting to localhost
> anyway...)
>
> *- Do you see anything error from mongod log file? *
> Unfortunately not. That was my first port of call to get any clue as to what
> might be wrong. Looking at that, it's all fine.
>
> *- Are you using sharding/mongos? *

Mark Embling

unread,
Feb 9, 2011, 8:30:02 AM2/9/11
to mongod...@googlegroups.com
Awesome, thanks for that. I'll try out that later on and see how things fare.

Is the 30sec default also the case with the Ruby driver? I only ask because so far the ruby version seems to be working fine, and of course that's doing the same map-reduce operations, using the same connection criteria.

Nat

unread,
Feb 9, 2011, 8:37:37 AM2/9/11
to mongodb-user
yup.. only for C# driver. Sorry I was checking the code from the wrong
branch so I couldn't find the setting. (-:
Reply all
Reply to author
Forward
0 new messages