Trying to create too many scroll contexts. Must be less than or equal to 500

背景

elasticsearch7.x 开始加上了软限制,max_open_scroll_context默认为500个,这个参数可以理解为数据库可用活跃连接数

报错是因为虽然scroll id有一个存活时限,过期后会自动被清理,但在高并发情况下还是会有可能达到限制,从而报错

Trying to create too many scroll contexts. Must be less than or equal to 500

可以发送请求来查看es集群的open_context使用情况

[GET] _nodes/stats/indices/search

参数解释

        "search" : {
           "open_contexts" : 0,         //正在打开的查询上下文个数      
           "query_total" : 0,           //查询出来的总数据条数  
           "query_time_in_millis" : 0,  //查询阶段所耗费的时间          
           "query_current" : 0,         //当前正在查询个数 
           "fetch_total" : 0,           //Fetch操作的次数
           "fetch_time_in_millis" : 0,  //Fetch阶段总耗时时间            
           "fetch_current" : 0,         //正在fetch的次数 
           "scroll_total" : 0,          //通过scroll api查询数据总条数
           "scroll_time_in_millis" : 0, //通过scroll api总耗时时间
           "scroll_current" : 0,        //当前滚动API调用次
           "suggest_total" : 0,         //通过suggest api获取的推荐总数量
           "suggest_time_in_millis" : 0,//suggest总耗费时间      
           "suggest_current" : 0        //正在执行suggest api的个数 
         },

更多参数解释请点击此处

解决方法

百度到的方法是将这个参数调大

curl -X PUT http://ip:9200/_cluster/settings -H 'Content-Type: application/json' -d'{
    "persistent" : {
        "search.max_open_scroll_context": 5000
    },
    "transient": {
        "search.max_open_scroll_context": 5000
    }
}
'

但是这个方法治标不治本,正确的解决办法是在使用scorll id查询后,手动将scroll id清除

xpack sql

在使用xpack sql中,如果sql语句查询的结果大于fetch_size,就会生成cursor,其实也就是scroll id

这里将整个流程完整走一遍,cursor较长,这里为了方便观察,简化了长度

  1. 发送请求
curl -d "{\"query\":\"select * from sample where name = 'John Smith' limit 1 \",\"fetch_size\":2}" -X POST -H 'Content-Type: application/json' http://ip:9200/_xpack/sql?format=json
  1. 得到的结果如下
{"rows":[[33,1,"TestD",null,"John Smith","Test Street No. 1",1,"TestO","Developer"],[33,1,"TestD",null,"John Smith","Test Street No. 1",1,"TestO","Developer"]],"cursor":"04qtAwFz5"}
  1. 根据获取到的游标重新发送请求,查下一页
curl -d  "{\"cursor\":\"04qtAwFz5\"}" -X POST -H 'Content-Type: application/json' http://ip:9200/_xpack/sql?format=json
  1. 当所有的结果都查完后,再发送请求会得到
{"rows":[]}

这时如果再继续发送请求,就会报错

  1. 判断数据已经全部查完后,发送请求,清除游标
curl -d  "{\"cursor\":\"04qtAwFz5\"}" -X POST -H 'Content-Type: application/json' http://ip:9200/_xpack/sql/close?format=json

#注意这里的地址为/_xpack/sql/close

transportClient

具体查询的代码我就不贴了,这里只贴删除游标的

        //清除游标
        if (searchResponse.getScrollId()!=null && !searchResponse.getScrollId().equals("")){
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(searchResponse.getScrollId());
            ActionFuture<ClearScrollResponse> actionFuture =  transportClient.clearScroll(clearScrollRequest);
            if (!actionFuture.actionGet().isSucceeded()){
                logger.error(actionFuture.toString());
            }
        }

restHighLevelClient

        if (searchResponse.getScrollId() != null && !searchResponse.getScrollId().equals("")){
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(searchResponse.getScrollId());
            ClearScrollResponse clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest,RequestOptions.DEFAULT);
            if (!clearScrollResponse.isSucceeded()){
                logger.info(clearScrollResponse.toString());
            }
        }

查询时,每次返回的游标都是一样的,所以只需要最后删除一次就可以

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,121评论 0 0
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,656评论 0 4
  • -- 来源于网络 -- 更详细的介结参考联机帮助文档 xp_cmdshell--*执行DOS各种命令,结果以文本行...
    overad阅读 2,415评论 0 13
  • 游标概念 由select语句返回的结果集包括满足该语句的where子句中条件的所有行。但是有时候应用程序并不总能将...
    不知名的蛋挞阅读 2,066评论 0 6
  • 2019年上半年,这学期的课只有10节课,周一、周五两天上课,中间多了很多空余的时间,买了很多书,参加了阅读营。利...
    Athena1211阅读 474评论 0 1