sql-server - 负载下 .Net Core 容器的 SQL Server DbCommand 超时

标签 sql-server asp.net-core entity-framework-core containers

我正在 Open Shift Enterprise V3 上运行一个指向 SQL Server 数据库的 .Net Core 容器。

我有一个带有 put 方法的 .Net Core REST API,用于添加或更新数据库中的记录。

我正在添加/更新的表有 3000 条记录并且有索引。

这在本地使用相同的数据库和容器工作正常。但是,当我开始通过容器加载时(与 JMeter 大约有 50 个并发 http 连接),我收到随机超时并显示以下错误消息。我没有得到本地运行的问题。

本地机器比容器强大得多,我增加了容器的 CPU 能力,但这似乎没有任何区别。

任何有关尝试的建议将不胜感激。

[10:45:36 ERR] Failed executing DbCommand (35,001ms) [Parameters=[@__get_Item_0='?' (Size = 255) (DbType = AnsiString)], CommandType='Text', CommandTimeout='30']
SELECT [e].[host_name], [e].[data_centre], [e].[Environment], [e].[is_physical_machine], [e].[mac_address], [e].[number_of_cores], [e].[number_of_sockets], [e].[number_of_v_cores], [e].[number_ofcpus], [e].[operating_system], [e].[operating_system_version], [e].[processor], [e].[uuid]
FROM [EntitlementServer].[host] AS [e]
WHERE [e].[host_name] = @__get_Item_0
System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (258): Unknown error 258
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
   at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
   at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
   at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:6ca037fc-9671-4d43-bebe-35879203c682
Error Number:-2,State:0,Class:11

更新 1

虽然我发现扩大容器上的 CPU 和内存对性能没有帮助。当我增加运行容器的数量时,这确实增加了吞吐量并且超时问题消失了。我不确定为什么会这样。

最佳答案

这个问题实际上是一个线程问题。数据库超时是一种症状。这就是增加容器数量停止问题的原因,因为http线程数量也增加了。

通过使代码异步以便释放线程,我能够在不必增加容器数量的情况下解决问题。

关于sql-server - 负载下 .Net Core 容器的 SQL Server DbCommand 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57270245/

相关文章:

c# - 在 ASP.NET Core 2 日志记录更新后将 ILogger 映射到 Serilog 的记录器

sql - 在冗长的 UPDATE 期间进行 SELECT - 不同事务隔离级别的 SELECT 和 SELECT WITH (NOLOCK) 会发生什么?

java - 我需要逻辑(JAVA)来避免在数据库中更新时 Excel 工作表中出现重复行

mysql - MySQL 和 MS SQL 语言之间的主要区别是什么?

asp.net-web-api - 验证用户是否存在于 Asp.net Web api core 的 Active Directory 中

asp.net-core - AspNetCore 中的 Controller 配置

c# - Entity Framework Core 中 TimeSpan 的总和

c# - 我如何在 EF Code First 中声明数据类型为 Medium Blob 的表上的字段

entity-framework-core - 包管理器控制台添加迁移命令不起作用

c# - 对数据库的查询返回空或 null 但保存数据确实有效