WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

When returning IQueryable to OData, all results are drained into memory before the first is returned to the client.

Open nickkin-msft opened this issue 10 years ago • 11 comments

We are returning an IEnumerator (via the implementation of IQueryable), which does properly implement "yield return" and thus lazy enumeration to OData:

WebApi:

   [EnableQuery(
       EnsureStableOrdering = false,
       AllowedQueryOptions = (AllowedQueryOptions)(AllowedQueryOptions.Supported - AllowedQueryOptions.Skip - AllowedQueryOptions.Count))]
   public async Task<IQueryable<ScenarioDto>> Get()

IQueryable implementation (wraps DocumentDB's IQueryable):

   public override IEnumerator<TElement> ExecuteQuery<TElement>(Expression expression)
   {
       IQueryable<TElement> query = underlyingProvider.CreateQuery<TElement>(expression);
       var enumerable = DocumentCollectionClient<Document>.StreamQueryWithContinuationAndRetryStatic(query, maxTime, maxRetries);
       return enumerable.GetEnumerator(); // <-- this ****DOES stream via "yield return"****
   }

However, experiments show that even as we stream data out of the IQueryable (via the IEnumerator) back into OData, nothing is returned to the client until the last record has been read out of the IEnumerator.

Our data set is > 2 GB in size so this poses problems for us as OData is caching this entire dataset in memory instead of streaming it out to the client as expected.

nickkin-msft avatar Jan 27 '16 02:01 nickkin-msft

It's like in this scenario we need a writer.flush() after

            entrySerializer.WriteObjectInline(entry, elementType, writer, writeContext);

in ODataFeedSerializer.

VikingsFan avatar Feb 02 '16 02:02 VikingsFan

Thanks Fan, I will try to make another pull request soon.

nickkin-msft avatar Feb 02 '16 02:02 nickkin-msft

@nickkin-msft can you use page size to get this work around?

VikingsFan avatar Feb 03 '16 01:02 VikingsFan

Haven’t tried it. That might work for well-behaved callers, but it wouldn’t prevent anyone (malicious, or who simply don’t know about the bug) from being able to make harmful calls into our service.

nickkin-msft avatar Feb 03 '16 01:02 nickkin-msft

@nickkin-msft pageSize is set by server side, maybe you can try it.

[EnableQuery(PageSize=10)]

VikingsFan avatar Feb 03 '16 01:02 VikingsFan

It appears this is not a valid workaround for us as paging seems to try to apply operations to the IQueryable (in our case supplied by DocumentDB) which it does not support.

nickkin-msft avatar Feb 03 '16 02:02 nickkin-msft

Any further word on this? I have an OData endpoint hanging because of about 20MB of data in the IQueryable I want to return from the endpoint.

TehWardy avatar Jan 17 '17 17:01 TehWardy

I have a table with + 1Milion records. I will need to abort OData usage? (OMG)... Using Entity Framework 6

iquirino avatar Jan 27 '17 20:01 iquirino

Only if you want to return 1+ million records in a single call? I haven't had time to commit a fix for this as a consumer myself yet, but it should be very simple and is open for contribution.

nickkin-msft avatar Jan 27 '17 21:01 nickkin-msft

This issue could be related https://github.com/OData/WebApi/issues/898 ? I dont know how to do.

iquirino avatar Jan 27 '17 22:01 iquirino

I am in a situation where OData has become the required protocol for data retrieval from web clients. I work with large amounts of data so paging of the data is performed in stored procedures. Due to performance and resource concerns, I refuse to over fetch data from the procedures to play nicely with the built in paging functionality in the MS OData library.

I have tried returning an instance of PageResultOfT with the "count" set manually but the runtime appears to be overriding the value and applying the $skip/$top paging logic to the already paged values. This results in every page beyond the first returing no values because the runtime skips them all.

I have found a way to completely control the paging after several maddening days of testing.

The root cause of my issue was the EnableQuery attribute. It appears that this attribute instructs the runtime to perform paging operations, including total count override, on the PageResultOfT instance returned from the API method. Removing the EnableQuery attribute from the controller resolved the issue.

Note: The "Global Query Setting" heading in section 13.1 of the OData documentation only applies to methods that use the EnableQuery attribute, including times when it is inherited from the controller.

JSanford42 avatar Jan 31 '18 18:01 JSanford42