码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • ES High Level Rest Client 超时问题排查及解决


    (1048条消息) ES异常:Connection reset by peer_浊酒入清梦的博客-CSDN博客https://blog.csdn.net/m0_37862405/article/details/108324096

    ES High Level Rest Client 超时问题排查及解决 - 墨天轮 (modb.pro)https://www.modb.pro/db/388569记一次elasticsearch client的conncection reset 异常 - 小专栏 (xiaozhuanlan.com)https://xiaozhuanlan.com/topic/7350912846(1049条消息) Es 超时设置 high-level-client_小檗的博客-CSDN博客_es 设置超时时间https://blog.csdn.net/xiaobozhi1993/article/details/114260115

    ES的high level查询超时设置失效问题 - 简书 (jianshu.com)icon-default.png?t=M5H6https://www.jianshu.com/p/f781e38b3bb8

    问题描述
    es7.4.1客户端查询时报异常:

    1. Caused by: java.io.IOException: Connection reset by peer
    2. at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:793)
    3. at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218)
    4. at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205)
    5. at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1454)
    6. at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424)
    7. at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394)
    8. at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:930)
    9. ...
    10. Caused by: java.io.IOException: Connection reset by peer
    11. at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    12. at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
    13. at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    14. at sun.nio.ch.IOUtil.read(IOUtil.java:197)
    15. at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    16. at org.apache.http.impl.nio.reactor.SessionInputBufferImpl.fill(SessionInputBufferImpl.java:231)
    17. ...


    原因排查

    es7.4.1客户端(RestHighLevelClient)使用的apache httpclient 版本为4.1.3,keepAlive默认为-1(即无限)。在某些特殊情况下,ES服务端的keepAlive短于ES客户端的keepAlive,进而导致:ES服务端已经关闭了连接,但是客户端还继续复用该连接,从而抛出上述异常。

    解决方法

    手动设置RestHighLevelClient的keepAlive,通过KeepAliveStrategy手动配置keepAlive代码如下:

    1. public static RestHighLevelClient createRestHighLevelClient(String esUrl, Long keepAlive) {
    2.     RestClientBuilder clientBuilder = RestClient.builder(createHttpHost(URI.create(esUrl)))
    3.             .setHttpClientConfigCallback(requestConfig -> requestConfig.setKeepAliveStrategy(
    4.                     (response, context) -> keepAlive));
    5.     return new RestHighLevelClient(clientBuilder);
    6. }
    7.     private static HttpHost createHttpHost(URI uri) {
    8.         if (StringUtils.isEmpty(uri.getUserInfo())) {
    9.             return HttpHost.create(uri.toString());
    10.         }
    11.         try {
    12.             return HttpHost.create(new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(),
    13.                     uri.getQuery(), uri.getFragment()).toString());
    14.         } catch (URISyntaxException ex) {
    15.             throw new IllegalStateException(ex);
    16.         }
    17.     }


    源码可参考

    原理

    RestHighLevelClient设置http配置的源码:

    1. public RestClientBuilder setHttpClientConfigCallback(HttpClientConfigCallback httpClientConfigCallback) {
    2.     Objects.requireNonNull(httpClientConfigCallback, "httpClientConfigCallback must not be null");
    3.     this.httpClientConfigCallback = httpClientConfigCallback;
    4.     return this;
    5. }
    6. public interface HttpClientConfigCallback {
    7.         HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder);
    8.     }

    根据HttpAsyncClientBuilder中的部分源码可知,RestHighLevelClient默认keepAliveStrategy为DefaultConnectionKeepAliveStrategy:

    1. ConnectionKeepAliveStrategy keepAliveStrategy = this.keepAliveStrategy;
    2. if (keepAliveStrategy == null) {
    3.     keepAliveStrategy = DefaultConnectionKeepAliveStrategy.INSTANCE;
    4. }

    DefaultConnectionKeepAliveStrategy源码,如果没有在http表头中设置,默认返回-1:

    1. @Contract(threading = ThreadingBehavior.IMMUTABLE)
    2. public class DefaultConnectionKeepAliveStrategy implements ConnectionKeepAliveStrategy {
    3.     public static final DefaultConnectionKeepAliveStrategy INSTANCE = new DefaultConnectionKeepAliveStrategy();
    4.     @Override
    5.     public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
    6.         Args.notNull(response, "HTTP response");
    7.         final HeaderElementIterator it = new BasicHeaderElementIterator(
    8.                 response.headerIterator(HTTP.CONN_KEEP_ALIVE));
    9.         while (it.hasNext()) {
    10.             final HeaderElement he = it.nextElement();
    11.             final String param = he.getName();
    12.             final String value = he.getValue();
    13.             if (value != null && param.equalsIgnoreCase("timeout")) {
    14.                 try {
    15.                     return Long.parseLong(value) * 1000;
    16.                 } catch(final NumberFormatException ignore) {
    17.                 }
    18.             }
    19.         }
    20.         return -1;
    21.     }
    22. }


    参考文档:

    https://juejin.im/post/6844904069442568205

    https://stackoverflow.com/questions/52997697/how-to-get-around-connection-reset-by-peer-when-using-elasticsearchs-restclie

  • 相关阅读:
    Linux 安装jenkins
    MySQL数据库SSL连接测试
    关于yii2 hasOne()、hasMany()用法,参数说明,注意事项
    【机器学习-周志华】学习笔记-第六章
    多标签页文件管理器 - Win系统
    Ubuntu上使用SSH连接到CentOS系统
    【OpenVINO™】在 C# 中使用OpenVINO™ 部署PP-YOLOE实现物体检测
    Spark 中数据结果传输到 Driver 端
    Thread线程类基本使用(下)
    flink原理源码分析(一) 集群与资源@k8s
  • 原文地址:https://blog.csdn.net/z69183787/article/details/125500612
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号