首页 > 解决方案 > 使用 SDK v3 来自 Cosmos DB 的 408 状态代码

问题描述

我有一个 API (.NET Core 2.2),它使用 SDK v3.5.0 从 Cosmos DB 检索文档。当前,由于对 Cosmos DB 的请求超时,某些请求会引发异常 - 响应是408 状态代码。值得一提的是,90% 的请求都被成功处理了。

使用遥测记录 API 的活动,我注意到在依赖关系表(记录对 Cosmos DB 的每个请求)中,持续时间超过 60 秒的请求是由名为“创建/查询文档”的命令启动的。这些是超时的请求。

另一方面,所有其他请求都使用“查询文档”命令,该命令在 < 5 秒内响应并且请求成功完成。为了分享更多上下文,我在下面详细介绍了错误堆栈跟踪、一般规范以及我如何查询文档。

堆栈跟踪

    Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: 408 Substatus: 0 Reason: (Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: 408 Substatus: 0 Reason: (Microsoft.Azure.Documents.RequestTimeoutException: GatewayStoreClient Request Timeout. Start Time:1/30/2020 4:18:00 AM; Total Duration:00:01:05.0332130; Http Client Timeout:00:01:05; Activity id: 7498789a-8e09-4c3e-96a6-31c32e4dc2d7; Inner Message: The operation was canceled.;, Request URI: /dbs/production/colls/Announcement/docs, RequestStats: , SDK: Windows/10.0.14393 cosmos-netstandard-sdk/3.4.2 ---> System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. ---> System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request
       --- End of inner exception stack trace ---
       at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
       at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
       at System.Net.Security.SslStreamInternal.<FillBufferAsync>g__InternalFillBufferAsync|38_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
       at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
       at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
       --- End of inner exception stack trace ---
       at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
       at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
       at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
       at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       at Microsoft.Azure.Cosmos.DocumentClient.HttpRequestMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
       at Microsoft.Azure.Cosmos.GatewayStoreClient.<>c__DisplayClass14_0.<<InvokeClientAsync>b__0>d.MoveNext()
       --- End of inner exception stack trace ---
       at Microsoft.Azure.Cosmos.GatewayStoreClient.<>c__DisplayClass14_0.<<InvokeClientAsync>b__0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)
       at Microsoft.Azure.Documents.ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException)
       at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)
       at Microsoft.Azure.Cosmos.GatewayStoreClient.InvokeClientAsync(DocumentServiceRequest request, ResourceType resourceType, Uri physicalAddress, CancellationToken cancellationToken)
       at Microsoft.Azure.Cosmos.GatewayStoreClient.InvokeAsync(DocumentServiceRequest request, ResourceType resourceType, Uri physicalAddress, CancellationToken cancellationToken)
       at Microsoft.Azure.Cosmos.GatewayStoreModel.ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken)
       at Microsoft.Azure.Cosmos.Handlers.TransportHandler.SendAsync(RequestMessage request, CancellationToken cancellationToken)).
    StatusCode = RequestTimeout;
    SubStatusCode = 0;
    ActivityId = 7498789a-8e09-4c3e-96a6-31c32e4dc2d7;
    RequestCharge = 0;
    {"ActivityId":"7498789a-8e09-4c3e-96a6-31c32e4dc2d7","StatusCode":408,"SubStatusCode":0,"RequestCharge":0.0,"ErrorMessage":"Microsoft.Azure.Documents.RequestTimeoutException: GatewayStoreClient Request Timeout. Start Time:1/30/2020 4:18:00 AM; Total Duration:00:01:05.0332130; Http Client Timeout:00:01:05; Activity id: 7498789a-8e09-4c3e-96a6-31c32e4dc2d7; Inner Message: The operation was canceled.;, Request URI: /dbs/production/colls/Announcement/docs, RequestStats: , SDK: Windows/10.0.14393 cosmos-netstandard-sdk/3.4.2 ---> System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. ---> System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request\r\n   --- End of inner exception stack trace ---\r\n   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)\r\n   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)\r\n   at System.Net.Security.SslStreamInternal.<FillBufferAsync>g__InternalFillBufferAsync|38_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)\r\n   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   --- End of inner exception stack trace ---\r\n   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at Microsoft.Azure.Cosmos.DocumentClient.HttpRequestMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)\r\n   at Microsoft.Azure.Cosmos.GatewayStoreClient.<>c__DisplayClass14_0.<<InvokeClientAsync>b__0>d.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at Microsoft.Azure.Cosmos.GatewayStoreClient.<>c__DisplayClass14_0.<<InvokeClientAsync>b__0>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)\r\n   at Microsoft.Azure.Documents.ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException)\r\n   at Microsoft.Azure.Documents.BackoffRetryUtility`1.ExecuteRetryAsync(Func`1 callbackMethod, Func`3 callShouldRetry, Func`1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action`1 preRetryCallback)\r\n   at Microsoft.Azure.Cosmos.GatewayStoreClient.InvokeClientAsync(DocumentServiceRequest request, ResourceType resourceType, Uri physicalAddress, CancellationToken cancel

附加信息

    var feed = container.GetItemLinqQueryable<T>(false, null, queryRequestOptions).Where(predicate).ToFeedIterator();
    var batches = new List<FeedResponse<T>>();
    while (feed.HasMoreResults)
    {
        var batch = await feed.ReadNextAsync();
        batches.Add(batch);
    }

标签: c#.net-coreazure-cosmosdbazure-cosmosdb-sqlapi

解决方案


推荐阅读