[CSHARP-1970] "InvalidOperationException: Sequence contains no elements" in MongoDB.Driver.GridFS.GridFSForwardOnlyDownloadStream`1.GetNextBatchFromCursor(Boolean hasMore) Created: 25/Apr/17  Updated: 28/Oct/23  Resolved: 28/Apr/17

Status: Closed
Project: C# Driver
Component/s: GridFS
Affects Version/s: 2.4.2
Fix Version/s: 2.4.4

Type: Bug Priority: Critical - P2
Reporter: Ummer Irshad Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS : Windows Server 2008 R2
Sharding Environment



 Description   

We are running load test with MongoDB V3.2.11 and below mentioned error is thrown when reading the binary file from GridFS.
Size of binary file is 2 GB.

Type:InvalidOperationException,Message:Sequence contains no elements
   場所 System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
   場所 MongoDB.Driver.GridFS.GridFSForwardOnlyDownloadStream`1.GetNextBatchFromCursor(Boolean hasMore)
   場所 MongoDB.Driver.GridFS.GridFSForwardOnlyDownloadStream`1.GetFirstBatch(CancellationToken cancellationToken)
   場所 MongoDB.Driver.GridFS.GridFSForwardOnlyDownloadStream`1.GetSegment(CancellationToken cancellationToken)
   場所 MongoDB.Driver.GridFS.GridFSForwardOnlyDownloadStream`1.Read(Byte[] buffer, Int32 offset, Int32 count)

We are using below code for downloading the binary file

documentBucket.OpenDownloadStream(file.Id, downloadOptions)

Do you have any clue about above error?

Thanks in advance.



 Comments   
Comment by Ummer Irshad [ 02/May/17 ]

Dear Team,

Thank you very much for a quick fix

Please let us know the release date of MongoDB Driver 2.4.4. It will be of great help, if you can release the same within this week.

BR,
Irshad.

Comment by Githook User [ 28/Apr/17 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1970: Handle unexpected empty batches from server.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f3096260b139c9a73d2877f66ae04e39bb983606

Comment by Robert Stam [ 28/Apr/17 ]

That's great information thanks.

The only question I forgot to ask was what the shard key was for the fs.chunks collection.

I may not be able to reproduce this, but I see an easy code change to handle unexpected empty batches returned from the server (the code can just keeping asking for the next batch until it either reaches the end of the cursor or a non-empty batch is received).

Comment by Ummer Irshad [ 28/Apr/17 ]

Thank you very much Robert Stam for taking up this issue.

We are not able to reproduce this issue with MongoDB 2.6.11 and Mongocsharpdriver 1.11.0. But, when we tried with MongoDB 3.2.9 and Mongocsharpdriver 1.11.0, it got reproduced.

Pls. find the clarification which you requested below:

1. Whether you are using a custom chunksize and if so what the value is
Yes, we are using custom chunksize. Chunksize we are using is 262144 bytes.

2. The exact file size in bytes (I'm not sure whether MB means 1,000,000 or 1024 x 1024)
File size is 2102109 bytes and 2101971 bytes. Issue is there with both sizes.

3. What download options you are using
We are using only “CheckMD5 = true” download options

4. the values of the offset and count parameters to Read
Value of “offset” is 0 and “count” is length of chunksize, in our case it 262144 bytes.

I'm assuming this is the first call to Read after OpenDownloadStream?
After “OpenDownloadStream”, we are opening a “FileStream” for writing the read chunks.
Code is as below:

using (var srcStream = documentBucket.OpenDownloadStream(file.Id, downloadOptions))
            {
                try
                {
                    using (var dstStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                    {
                        while (true)
                        {
                            int count = srcStream.Read(buffer, 0, buffer.Length);
                            if (count == 0) break;
 
                            dstStream.Write(buffer, 0, count);
                        }
 
                        dstStream.Flush();
                    }
                }
                catch
                {
 
                }
                finally
                {
                    srcStream.Close();
                }
            }

5. Standalone, replica set or sharded cluster?
We are using sharded cluster

6. Any ReadPreference set on the database or GridFS bucket?
We are using “SecondaryPreferred” in Connection String and the same as ReadPreference for GridFS bucket.

7. Any custom indexes on the fs.chunks collection?
We have only default “id” index and “files_id_1_n_1” index in fs.chunks

8. Is the fs.chunks collection sharded?
Yes, fs.chunks is sharded.

Pls. let me know if any more info required.

Comment by Robert Stam [ 27/Apr/17 ]

And also some information about your server configuration:

5. Standalone, replica set or sharded cluster?
6. Any ReadPreference set on the database or GridFS bucket?
7. Any custom indexes on the fs.chunks collection?
8. Is the fs.chunks collection sharded?

Comment by Robert Stam [ 27/Apr/17 ]

It would also be helpful to know:

4. the values of the offset and count parameters to Read

I'm assuming this is the first call to Read after OpenDownloadStream?

Comment by Robert Stam [ 27/Apr/17 ]

I am unable to reproduce this, but looking at the driver code I can see how this would happen if the server unexpectedly returned an empty final batch for the query used to read the chunks.

Can you provide some more information to help me reproduce this?

1. Whether you are using a custom chunksize and if so what the value is
2. The exact file size in bytes (I'm not sure whether MB means 1,000,000 or 1024 x 1024)
3. What download options you are using

Comment by Ummer Irshad [ 25/Apr/17 ]

Update:
Size of binary file is 2 MB and MongoDB version is V3.2.12

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