Azure / azure-cosmos-dotnet-v2

Contains samples and utilities relating to the Azure Cosmos DB .NET SDK
MIT License
578 stars 836 forks source link

Parallelism and MaxItemCount causing Memory spike/uncancelled background tasks #588

Open nukefusion opened 6 years ago

nukefusion commented 6 years ago

Running a query against a reasonable size dataset (3000 documents) using FeedOptions.MaxDegreeOfParallelism = -1 and FeedOptions.MaxItemCount = 50.

The query is constructed and executed within an MVC controller and only the first batch of 50 is requested (i.e. ExecuteNextAsync is only called once). 50 records are returned as expected.

However, after the ASP.NET web request completes memory on the server increases and multiple parallel background requests and throughput can be observed still running against DocumentDB and continuing to execute for some time.

Is it necessary to manually dispose of the query? Or call some sort of cancellation? Or shouldn't the parallel tasks cease execution once the initial batch has been fulfilled?

ausfeldt commented 6 years ago

If MaxDegreeOfParallelism is set to a values other than 1 then it will pre-fetch the data. You can set the maximum number of items that can be buffered during a parallel execution by setting MaxBufferedItemCount. Also when finished with DocumentQuery make sure you dispose it.

nukefusion commented 6 years ago

In this scenario we're using a continuation token and returning to the client, so pre-fetching any more data after the first 50 records is unnecessary. If I understand you correctly, setting MaxBufferedItemCount to 50 should solve this problem and only retrieve the first 50 documents - unfortunately this doesn't seem to make any difference, requests still execute and memory still increases steadily until an out of memory exception occurs.

The fact remains though that given a poorly constructed query, parallel execution can potentially consume all of the available memory on the machine. Is there no provision for this built in to the library, e.g. a maximum upper limit to MaxBufferedItemCount?