-
Type:
Bug
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: 2.3
-
Component/s: Connectivity
-
None
-
Environment:Dev env: Windows 10 x64, MongoDB 3.2.4 as Windows Service;
Prod env: Windows Server 2012 R3 x64, MongoDB hosted by Ubuntu VM
Hi, I'm trying to use MongoDB in my project but faced with the following problem. I have some amount of tasks. Simple Find() operation inside of a task throws "A timeout occured after 30000ms selecting a server using CompositeServerSelector ...".
I write a small console app to catch the error. I have MongoDB instance hosted locally by my Windows dev machine. The database is really small (1 collection, 21 objects, 95 bytes average). It means network or size could not be the reason. Moreover I found out the following:
- If I run the Find() method 40 one after another (no tasks) - it takes less than a second to finish.
- If I run just a few tasks simultaneously (like 5) - it takes a few seconds to finish
- But if I run 40 tasks simultaneously without using async-await pattern - some of them end up with timeout exception, some of them return the data. Average task exection time is 18 seconds.
- If I run 40 tasks and use async-await pattern it works a little bit slowly then continuous execution and do not throw timeout exception.
The following code throws the timeout exception:
Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
static void Main() { int iterations = 40; var tasks = new Task<TimeSpan>[iterations]; for (int i = 0; i < iterations; i++) { var tempI = i; tasks[i] = new Task<TimeSpan>(() => { var stopwatch = new Stopwatch(); stopwatch.Start(); var integrationId = INTEGRATION_IDS[i]; try { var metadataCollection = database.GetCollection<CacheMetadata>(METADATA_COLLECTION_NAME); CacheMetadata metadata = metadataCollection .Find(m => m.IntegrationId == integrationId) .FirstOrDefault(); // If FirstOrDefaultAsync().Result is used it throws the exception as well stopwatch.Stop(); // Write metadata to console } catch (Exception ex) { // Write exception to console } return stopwatch.Elapsed; }); } for (int i = 0; i < iterations; i++) { tasks[i].Start(); } Task.WaitAll(tasks); }
However the following code works:
Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
static void Main() { int iterations = 40; var tasks = new Task<TimeSpan>[iterations]; for (int i = 0; i < iterations; i++) { var integrationId = INTEGRATION_IDS[tempI]; tasks[i] = runMongoQuery(integrationId); } Task.WaitAll(tasks); } private static async Task<TimeSpan> runMongoQuery(Guid _integrationId) { var stopwatch = new Stopwatch(); stopwatch.Start(); try { var metadataCollection = database.GetCollection<CacheMetadata>(METADATA_COLLECTION_NAME); CacheMetadata metadata = await metadataCollection .Find(m => m.IntegrationId == _integrationId) .FirstOrDefaultAsync(); stopwatch.Stop(); // Write metadata to console } catch (Exception ex) { // Write exception to console } return stopwatch.Elapsed; }
Is it a bug or I'm doing something wrong?
Thanks.