Elasticsearch PowerShell:快照运行时执行enrich policy的等待方法
问题描述
在PowerShell中执行Elasticsearch enrich policy时,因快照正在运行触发以下错误:
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
系统提示无法删除正在被快照的索引,需等待快照完成或取消快照。但重试时又遇到锁冲突错误:
{"error":{"root_cause":[{"type":"es_rejected_execution_exception","reason":"Could not obtain lock because policy execution for
请问是否有办法让enrich policy等待锁释放后自动执行?
解决方案
轮询检查快照状态后手动重试
先通过Elasticsearch API查询当前运行的快照状态:Invoke-RestMethod -Uri "http://<es-host>:<port>/_snapshot/<repo-name>/_current" -Method Get当返回结果显示无运行中快照后,再重新执行enrich policy命令。
编写PowerShell脚本自动等待并重试
用循环脚本定期检查快照和enrich policy锁状态,直到可以执行为止:$esHost = "http://your-es-host:9200" $enrichPolicyName = "your-policy-name" $waitInterval = 60 # 每次检查间隔60秒 while ($true) { # 检查是否有运行中的快照 $snapshotStatus = Invoke-RestMethod -Uri "$esHost/_snapshot/_all/_current" -Method Get if ($snapshotStatus.running.Count -eq 0) { # 尝试执行enrich policy try { Invoke-RestMethod -Uri "$esHost/_enrich/policy/$enrichPolicyName/_execute" -Method Post Write-Host "Enrich policy执行成功" break } catch { # 检查是否是锁冲突错误 if ($_.ErrorDetails.Message -match "es_rejected_execution_exception.*Could not obtain lock") { Write-Host "仍无法获取锁,等待$waitInterval秒后重试..." Start-Sleep -Seconds $waitInterval } else { # 其他错误直接抛出 throw $_ } } } else { Write-Host "有快照正在运行,等待$waitInterval秒后重试..." Start-Sleep -Seconds $waitInterval } }该脚本会持续检查快照状态,快照结束后尝试执行enrich policy,若遇锁冲突则继续等待重试,直到执行成功。
调整enrich policy锁超时参数(谨慎操作)
若频繁遇到锁冲突,可在执行enrich policy时添加后台等待参数,让请求在后台等待锁释放:Invoke-RestMethod -Uri "$esHost/_enrich/policy/$enrichPolicyName/_execute?wait_for_completion=false&timeout=30m" -Method Post请求会立即返回任务ID,之后可通过
_tasksAPI查询任务状态:Invoke-RestMethod -Uri "$esHost/_tasks/<task-id>" -Method Get注意:过长的超时时间可能占用更多集群资源,需根据实际情况调整。
内容的提问来源于stack exchange,提问作者Naveen Kumar
相关产品推荐
相关产品推荐

