英文:
create/query in cosmos throws cancelled exception
问题
我正在使用.NET 6隔离架构运行v4函数。在依赖注入中使用以下代码实现我的cosmosClient。
var cosmosClient = CosmosClient.CreateAndInitializeAsync(cosmosSettings?.Endpoint, cosmosSettings?.Key, containers).Result;
services.AddSingleton(cosmosClient);
在我的Repository
类构造函数中:
public CosmosRepo(CosmosClient client, ILogger<CosmosRepo> logger,
IOptions<MyConfig> config)
{
_logger = logger;
_config = config.Value;
_container = client.GetContainer(_config.CosmosDb.DatabaseId, _collectionName);
}
这个类中只有一个对cosmos的调用:
private async Task<ItemDbo> Save(ItemDbo itemDbo)
{
if (itemDbo is null)
{
throw new ArgumentNullException(nameof(itemDbo));
}
try
{
var result = await _container.UpsertItemAsync(itemDbo);
return result.Resource;
}
catch (CosmosException e)
{
_logger.LogCritical($"itemDbo upsert失败,实体:{itemDbo.Id},错误信息:" + e.Message);
throw;
}
}
我发现有成千上万次的创建/查询操作被取消。我的数据正常传入,但却完全混淆了我的问题洞察和理解。Upsert是从任务列表创建的,每次迭代的函数中有大约25个任务。
List<Task> tasklist;
循环25次
tasklist.Add(Save(itemDbo)).ContinueWith(i =>
{
if (!i.IsCompletedSuccessfully)
{
_logger.LogCritical($"无法upsert数据,itemDbo.Id:{itemDbo.Id}");
}
}));
await Task.WhenAll(updateAndInsertTasks);
异常/错误似乎只在这个路径上发生,但这是没有道理的:...dbs/cosmosdb/container/colls/itemDbo/docs
在我的日志中,我看到操作的数量巨大,而且RU成本为零。
我的应用程序依赖项故障非常严重。
尤其让人沮丧的是,我的try/catch块中的跟踪为空。我的异常也为空。我每小时处理大约250万个请求。
英文:
I'm running v4 functions using .net 6 isolated architecture. implementing my cosmosClient in the injection using following.
var cosmosClient = CosmosClient.CreateAndInitializeAsync(cosmosSettings?.Endpoint, cosmosSettings?.Key, containers).Result;
services.AddSingleton(cosmosClient);
in my repositoryclass constructor I have "
public CosmosRepo(CosmosClient client, ILogger<CosmosRepo> logger,
IOptions<MyConfig> config)
{
_logger = logger;
_config = config.Value;
_container = client.GetContainer(_config.CosmosDb.DatabaseId, _collectionName);
}
there is only one call to cosmos in this class
private async Task<ItemDbo> Save(ItemDbo itemDbo)
{
if (itemDbo is null)
{
throw new ArgumentNullException(nameof(itemDbo));
}
try
{
var result = await _container.UpsertItemAsync(itemDbo);
return result.Resource;
}
catch (CosmosException e)
{
_logger.LogCritical($"itemDbo upsert is failing for entity {itemDbo.Id} " + e.Message);
throw;
}
}
For some reason I am seeing thousands of create/query operations happening where the result is cancelled. My data is coming in but completely dirtying my insights and understanding of the issue.
The upsert is created from a list of tasks.. about 25 tasks per iteration of function.
its just
List<Task> tasklist;
loop 25X
tasklist.add(Save(itemDbo)).ContinueWith(i =>
{
if (!i.IsCompletedSuccessfully)
{
_logger.LogCritical($"Issue upserting the itemDbo data {itemDbo.Id}");
}
}));
await Task.WhenAll(updateAndInsertTasks);`
The exception/error seems 100% only on this path, which doesn't make sense
...dbs/cosmosdb/container/colls/itemDbo/docs
In my logs I see the number of operations is enormous of zero ru cost.
My app insights dependencies failures is crazy.
This is especially frustrating as my traces are empty for any of my try/catches.. my exceptions are null as well. I am running about 2.5 million requests per hour.
答案1
得分: 0
请参阅 https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3032#issuecomment-1227434296 获取更多详细信息。
您的应用程序 Insight 截图中的详细信息并不是来自 UpsertItem
调用,而是来自 查询。您的代码的某些其他部分正在执行查询 (GetQueryItemIterator
和 ReadNextAsync
)。
在 Application Insights 中的 Canceled
指的是超时的 HTTP 请求。这意味着您的应用程序在 HTTP 层面上遇到了连接问题,SDK 在进行重试,最终成功,但从应用程序的角度来看,重要的是您要调查超时发生的原因。
请参阅 https://learn.microsoft.com/azure/cosmos-db/nosql/troubleshoot-dotnet-sdk-request-timeout 以获取可能原因的列表。如果您的应用程序正在运行在 App Service 上,它还有一些诊断工具可能有助于缩小问题的范围。
有一点是确定的,请避免使用 .Result
,.GetAwaiter().GetResult()
或者任何会阻塞异步调用的地方。
英文:
Please see https://github.com/Azure/azure-cosmos-dotnet-v3/issues/3032#issuecomment-1227434296 for more details.
The details in your App Insight screenshot are not from an UpsertItem
call, they are from a Query. Some other part of your code is executing queries (GetQueryItemIterator
and ReadNextAsync
).
Canceled
in Application Insights refers to timed out HTTP requests. This means your application is experiencing connectivity issues on the HTTP layer, the SDK is retrying and eventually succeeding but from the application perspective, it is important that you investigated why the timeouts are happening.
Please refer to https://learn.microsoft.com/azure/cosmos-db/nosql/troubleshoot-dotnet-sdk-request-timeout for a list of potential reasons. If this is running on App Service, it also has some Diagnostics tools that might help narrow down the cause.
One thing is for sure, please avoid using .Result
, .GetAwaiter().GetResult()
or any place where you are blocking async calls.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论