Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GatewayStoreClient buffers entire response body and makes another copy of it for later use #1828

Closed
magole opened this issue Sep 2, 2020 · 5 comments · Fixed by #1835
Closed

Comments

@magole
Copy link

magole commented Sep 2, 2020

Describe the bug
GatewayStoreClient buffers entire response body and makes another copy of it for later use. With large number of docs in the response, this doesn't scale well on a busy server.

To Reproduce

var client = new HttpClient()
{
    MaxResponseContentBufferSize = 64 * 1024, // 64 KiB
};

Expected behavior
Cosmos client should use streaming and never buffer entire response in memory.
Cosmos client should always use HttpCompletionOption.ResponseHeadersRead for all HTTP calls.

Actual behavior

Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: InternalServerError (500); Substatus: 0; ActivityId: 00000000-0000-0000-0000-000000000000; Reason: (Cannot write more bytes to the buffer than the configured maximum buffer size: 65536.);
 ---> System.Net.Http.HttpRequestException: Cannot write more bytes to the buffer than the configured maximum buffer size: 65536.
   at System.Net.Http.HttpContent.LimitMemoryStream.CheckSize(Int32 countToAdd)
   at System.Net.Http.HttpContent.LimitMemoryStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.CopyToAsyncCore(Stream destination, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionResponseContent.SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
   at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

Environment summary
Linux, NET Core 3.1

Additional context
https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/GatewayStoreClient.cs

Line 369

return this.httpClient.SendHttpAsync(
                () => this.PrepareRequestMessageAsync(request, physicalAddress),
                resourceType,
                diagnosticsContext,
                cancellationToken);

https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs

Line 201

        public override Task<HttpResponseMessage> SendHttpAsync(
            Func<ValueTask<HttpRequestMessage>> createRequestMessageAsync,
            ResourceType resourceType,
            CosmosDiagnosticsContext diagnosticsContext,
            CancellationToken cancellationToken)
        {
            return this.SendHttpAsync(
                createRequestMessageAsync,
                HttpCompletionOption.ResponseContentRead,
                resourceType,
                diagnosticsContext,
                cancellationToken);
        }
@j82w
Copy link
Contributor

j82w commented Sep 2, 2020

I agree that it is an issue, but we mainly optimize for Direct mode as it is the recommend mode for performance scenarios. Is there a reason you are using gateway mode?

@magole
Copy link
Author

magole commented Sep 2, 2020

We run on Kubernetes and have limited number of connections for containers. With large number of pods on each node and Azure limits per node we quickly run out of connections. We tried Direct mode and had to revert back to gateway mode. Also using Gateway mode provides better insights/logs for HTTP calls out of the box.

@ealsur
Copy link
Member

ealsur commented Sep 3, 2020

Question: Have you tried the reducing the connections with PortReuseMode and IdleConnectionTimeout? That would reduce connections when you have sporadic usage.

@j82w
Copy link
Contributor

j82w commented Sep 23, 2020

@magole please try the latest 3.13.0

@ghost
Copy link

ghost commented Dec 15, 2021

Closing due to in-activity, pease feel free to re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants