gpt4 book ai didi

.net - Powershell Runspaces 并行脚本执行 : Is Set-AzureRmSqlDatabaseTransparentDataEncryption commandlet threadsafe?

转载 作者:行者123 更新时间:2023-12-03 07:05:30 26 4
gpt4 key购买 nike

运行 powershell commandlet 时:设置-AzureRmSqlDatabaseTransparentDataEncryption

在并行和单独的运行空间中,我们看到堆栈跟踪出现瞬时/间歇性故障,如下所示。但是,当我们连续运行相同的命令行开关时,我没有看到此问题发生。

到目前为止我想到的可能原因:

  • 是否知道此命令行开关不是线程安全的? (故意还是错误?)
  • 争用/锁定文件{ReleaseUser}\AppData\Roaming\Windows Azure Powershell\ErrorRecords\Set-AzureRmSqlDatabaseTransparentDataEncryption_YYYY-MM-DD-THH-MM-SS-PPP.log,其中命令行开关出现记录与解析 token 相关的信息(此文件中没有实际错误)。

运行此命令的脚本之一的堆栈跟踪:

[Exception:System.NullReferenceException: Object reference not set to an instance of an object.\r\n
at System.Collections.Specialized.OrderedDictionary.OrderedDictionaryEnumerator.get_Value()\r\n
at Microsoft.Azure.Commands.Common.Authentication.Factories.ClientFactory.GetCustomHandlers()\r\n
at Microsoft.Azure.Commands.Common.Authentication.Factories.ClientFactory.CreateClient[TClient](AzureContext context, Endpoint endpoint)\r\n
at Microsoft.Azure.Commands.Sql.TransparentDataEncryption.Services.AzureSqlDatabaseTransparentDataEncryptionCommunicator.GetCurrentSqlClient(String clientRequestId)\r\n
at Microsoft.Azure.Commands.Sql.TransparentDataEncryption.Adapter.AzureSqlDatabaseTransparentDataEncryptionAdapter.GetTransparentDataEncryption(String resourceGroupName, String serverName, String databaseName)\r\n
at Microsoft.Azure.Commands.Sql.TransparentDataEncryption.Cmdlet.SetAzureSqlDatabaseTransparentDataEncryption.GetEntity()\r\n
at Microsoft.Azure.Commands.Sql.Common.AzureSqlCmdletBase'2.ExecuteCmdlet()\r\n
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()]\

堆栈跟踪 对于运行相同命令行开关的第二个线程:

Message: "Object reference not set to an instance of an object.", Source: "System", StackTrace: " at System.Collections.Specialized.OrderedDictionary.IndexOfKey(Object key)\r\n at System.Collections.Specialized.OrderedDictionary.set_Item(Object key, Object value)\r\n at Microsoft.Azure.Commands.Common.Authentication.Factories.ClientFactory.AddHandler[T](T handler)\r\n at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.BeginProcessing()\r\n at System.Management.Automation.Cmdlet.DoBeginProcessing()\r\n at System.Management.Automation.CommandProcessorBase.DoBegin()"`

可疑:根据堆栈跟踪枚举/访问System.Collections.Specialized.OrderedDictionary时,这两个命令都会崩溃。

意义:该命令的两个实例是否访问SAME字典?

最佳答案

Will 的回答似乎支持了我们的怀疑,即这个(和其他)命令行开关不是线程安全的。为了解决这个问题,我们使用互斥锁作为协调机制来“锁定”调用此类命令行开关。

以此线程为灵感,在 powershell 中构建了进程锁定/协调机制(使用互斥体)的简单实现:

function Wait-OnMutex
{
param(
[parameter(Mandatory = $true)][string] $MutexId
)

try
{
$MutexInstance = New-Object System.Threading.Mutex -ArgumentList 'false', $MutexId

while (-not $MutexInstance.WaitOne(1000))
{
Start-Sleep -m 500;
}

return $MutexInstance
}
catch [System.Threading.AbandonedMutexException]
{
$MutexInstance = New-Object System.Threading.Mutex -ArgumentList 'false', $MutexId
return Wait-OnMutex -MutexId $MutexId
}

}

##
## example script calling unsafe commandlet
##

$MutexInstance = Wait-OnMutex -MutexId 'SomeMutexId12345'

## this is where you do work inside the "lock"
## call the commandlet needing to be single-threaded
Set-AzureRmSqlDatabaseTransparentDataEncryption -arg1 value

$MutexInstance.ReleaseMutex()

希望这对某人有帮助。

关于.net - Powershell Runspaces 并行脚本执行 : Is Set-AzureRmSqlDatabaseTransparentDataEncryption commandlet threadsafe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com