在Elasticsearch中模拟长时间运行的查询,也称为sleep或delay命令

7qhs6swi  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(67)

我看到Elasticsearch中的超时可能与查询或网络客户端相关。
我想模拟一个长时间运行的查询的行为,看看会发生什么。
类似于Elasticsearch中缺少的sleepdelay命令。

8wtpewkr

8wtpewkr1#

在Elasticsearch第7版上测试。

  • 你需要一个至少有一个文档的索引。

(Or使用系统索引,但在未来的主要版本中,默认情况下将阻止直接访问系统索引)

  • 或者你的查询应该返回至少一个文档。
    提示1此超时以“鹦鹉”为单位进行测量,哈哈。

参见parrotTimeout变量。它增加了进度中的真实的时间延迟。因此,5000个parrot可以很好地为您工作,但10000个可能会挂起您的Elasticsearch安装,直到重新启动。

注意2Elasticsearch在Macbook Pro中使用这种方法在Docker容器中运行时,在崩溃前大约存活8分钟。
顺便说一句,你可以使用这种方法来测试长时间运行的Elasticsearch任务,

?wait_for_completion=false也是。
使用以下代码。

GET /events/_search
{
  "timeout": "5m",
  "query": {
    "bool": {
      "should": [
        {
          "script": {
            "script": """
              // This heuristic timeout could only be <= 999999
              def parrotTimeout = 4000;
              
              def map1 = new HashMap();
              def map2 = new HashMap();
              
              for (int i = 0; i < parrotTimeout; ++i) {
                def key = i.toString();
                def val = new Random().nextInt(1000000000).toString();
                def noneExistingVal = "oppa la-la";
                
                map1.put(key, val);
                map1.containsValue(noneExistingVal);
                map2.put(key, val);
                map2.containsValue(noneExistingVal);
              }
            """
          }
        }
      ]
    }
  } 
}

字符串
如果你在脚本中更新一些随机字符串,你可以防止Elasticsearch中的“执行缓存”和查询时间减少。
在Nest lib和C#的情况下,这将像下面这样。参见下面的Guid.NewGuid()

var res = _client.Client.Search<T>(s => s
    .Query(q => q
        .Script(s => s
            .Script(ss => ss
                .Source($@"
def parrotTimeout = 4000;

def map1 = new HashMap();

for (int i = 0; i < parrotTimeout; ++i) {{
  def key = i.toString();
  def val = new Random().nextInt(1000000000).toString();
  def noneExistingVal = ""oppa {Guid.NewGuid()}"";

  map1.put(key, val);
  map1.containsValue(noneExistingVal);
}}
")
            )
        )
    )
);


它还可以处理“update”或“update by query”Elasticsearch查询。下面是一些C#示例。

$@"ctx._source.{targetFieldName}=ctx._source.{sourceFiledName}; ctx._source.remove('{sourceFiledName}');

def parrotTimeout = 4000;

def map1 = new HashMap();

for (int i = 0; i < parrotTimeout; ++i) {{
  def key = i.toString();
  def val = new Random().nextInt(1000000000).toString();
  def noneExistingVal = ""oppa {Guid.NewGuid()}"";
  
  map1.put(key, val);
  map1.containsValue(noneExistingVal);
}}
"

相关问题