[CSHARP-544] Asynchronous I/O while inserting documents Created: 14/Aug/12  Updated: 05/Apr/19  Resolved: 14/Aug/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Zarian Waheed Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-138 Add asynchronous APIs Closed
related to SERVER-929 use async network layer Closed

 Description   

We need to know if there is a way to Asynchronously insert documents in mongo. We are currently using C# driver but if this feature is available in C++ driver then we can use that as well. We are currently calling



 Comments   
Comment by Mike McGranahan [ 05/Sep/12 ]

For a quick comparison of async/IOCP-based code vs blocking code, see http://stackoverflow.com/a/6230846/29805 . (Just substitute a MongoCollection.Find call for Thread.Sleep.)

Without IOCP, ASP.NET can only handle 9 simultaneous requests at once this benchmark. With IOCP, ASP.NET can start asynchronously processing all requests almost simultaneously, leading to a 97% decrease in overall execution time!

Comment by Mike McGranahan [ 05/Sep/12 ]

Just want to add that from my understanding of the OP, this sounds like a duplicate of CSHARP-138, and not something related to the server. As long as the server exposes a socket-based interface, Windows I/O completion ports can be leveraged by a driver to provide non-blocking consumption of it. .NET provides an easy-to-use API for IOCP. The C# driver, astonishingly, does not leverage this and fixing it is TBD.

This should be marked a duplicate if for no other reason than to properly represent the growing desire for this feature, especially given the recent release of C# 5.0, .NET 4.5, and their emphasis on asynchronicity.

Comment by Scott Hernandez (Inactive) [ 14/Aug/12 ]

Yep, it is on our server/networking roadmap. Feel free to vote for the issues I linked to. Let us know if you have any other questions/comments

Comment by Zarian Waheed [ 14/Aug/12 ]

Thanks. I think I have my answer. However it will be useful if you guys add support for true ASync I/O in the product. That way the client does not have to create additional threads for each insert request if they want the non blocking behavior. But thanks for your help.

Comment by Scott Hernandez (Inactive) [ 14/Aug/12 ]

The server does not support async io, and it processes requests sequentially on each socket. The client libraries which do support this just block on safe mode writes and use a different socket for future operations so they are non-blocking from the client perspective.

These are worth reading: CSHARP-138, SERVER-929

I'm not sure what you mean by light-weight; do you mean in terms of cpu/resource use/allocation while the client waits? Blocking on reading from network socket is pretty light-weight from the system perspective. If you want something which allows your program to continue you could use delegates and callback in C# (I'm sure there are common patterns for this that could be adopted in your code).

Comment by Zarian Waheed [ 14/Aug/12 ]

We don't use C++ right now, I just wanted to know if there was anything available in C++ for Async I/O. We use the C# driver. From your response it seems like mongo does not support Asynchronous I/O or callbacks as all. I understand that I can use safemode = false which will not block the thread for the actual write to happen but is there a light weight way to know the status of that write operation later.

Comment by Scott Hernandez (Inactive) [ 14/Aug/12 ]

What C++ calls are you using which you consider async? Is this using the c++ driver provided with the server?

The server doesn't support callbacks or async operations so I'm not sure how this works now. When you call a write operation it will be sent and there is no response, the getLastError call is used immediately after the write call to block and wait for completion. Using safemode:true does this under the covers.

If you don't want to get an acknowledgment of the write then the default mode of fire'n'forget in non-safe mode will do what you want.

Comment by Zarian Waheed [ 14/Aug/12 ]

Adding the complete question:

We need to know if there is a way to Asynchronously insert documents in mongo. We are currently using C# driver but if this feature is available in C++ driver then we can use that as well. We are currently calling MongoDB.Driver.MongoCollection<TDefaultDocument>.InsertBatch(..) with safemode = true. This function waits till all the documents are added in the collection before returning back to the caller. Is there a way to call this function Asynchronously and provide a callback that will be invoked when the call actually completes.

Generated at Wed Feb 07 21:37:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.