c# - 使用 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
附加信息
- API 规范
- 应用服务部署在美国西部、西欧、东南亚和巴西南部地区
- .NET 核心 2.2
- Microsoft.Azure.Cosmos v3.5.0 SDK
- CosmosDB 客户端连接
- 连接方式:直接
- 应用地区:美国西部
- 其余的默认值
- 写入区域 -> 美国西部
- 阅读地区 -> 美国西部、西欧、东南亚和巴西南部
- 正如文档所暗示的,对于整个应用程序,我只有一个与 Cosmos DB 的连接
- 要检索文档,我使用以下实现:
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);
}
解决方案
推荐阅读
- javascript - 需要在页面加载前显示 div
- ios - 模拟器人脸ID快捷方式
- python - 烧瓶错误:db:“scoped_session”的 scoped_session 实例没有“commit”memberpylint(no-member)
- java - 当给定类型是数组时如何使用给定类型实例化泛型类
- javascript - ES6,任何 Babel-plugins (Version6) 来解构新的 Set?
- python - 我如何重新处理它以获得布局良好的 csv 文件,如附图所示?
- bash - ssh echo 命令及其执行结果
- postgresql - Kotlin JPA 一对多 @ElementCollection 尝试保存重复项导致违反约束
- ksqldb - ksqldb - select * from stream' 结果导致 io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor 找不到
- python-3.x - 使用 Count Vectorizer 获取 1 x N 相似度矩阵而不是 N x N 一个