背景:原系统是通过transport连接的ES,最新的系统是通过RestHighLevelClient去连接的系统
transport 恢复可方便设置超时失败时间
RestoreSnapshotRequest restoreSnapshotRequest = new RestoreSnapshotRequest().repository("repositoryName").snapshot("snapshotName").waitForCompletion(true);
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().restoreSnapshot(restoreSnapshotRequest).actionGet(1L,
TimeUnit.HOURS); //可以设置超时时间
RestStatus restStatus = restoreSnapshotResponse.status();
RestHighLevelClient恢复不能设置超时
RestoreSnapshotRequest restoreSnapshotRequest =
new RestoreSnapshotRequest().repository("repositoryName").snapshot("snapshotName")
.waitForCompletion(true);
//restoreSnapshotRequest.masterNodeTimeout(100000L) 沒任何作用
RestoreSnapshotResponse restoreSnapshotResponse =
client.snapshot().restore(restoreSnapshotRequest, RequestOptions.DEFAULT); //不能设置超时时间
//改为异步调用仍未解决该问题
报Caused by: java.net.SocketTimeoutException: 30,000 milliseconds timeout on connection http-outgoing-0 [ACTIVE]
解决思路:
报错很明显 SocketTimeoutException,但是创建连接时设置全局的超时时间不合理其他的不需要这么久的超时。
尝试1:设置masterNodeTimeout发现无用
尝试2:改为异步的调用,仍旧报改错误
尝试3:设置自定义的RequestOptions解决问题(看文档的时候不仔细,未看到可以单个自定义超时时间)
看下面的地址很容易解决该问题
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.12/_timeouts.html
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(60000)
.build();
RequestOptions options = RequestOptions.DEFAULT.toBuilder()
.setRequestConfig(requestConfig)
.build();
引深
RequestConfig 查看所在的包竟然是httpclient的并不是elasticsearch的,然后就又查了查相关的超时含义
RequestConfig有三个超时如下
ConnectTimeout | ConnectionRequestTimeout | SocketTimeout |
---|---|---|
设置连接超时 | 请求获取数据超时 | connect Manager获取Connection 超时 |
ConnectTimeout
设置连接超时时间,单位毫秒。指的是连接一个url的连接等待时间。比如连google.报错如下
org.apache.http.conn.ConnectTimeoutException: Connect to www.google.com:80 [www.google.com/203.98.7.65] failed: connect timed out
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:132)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at Test.main(Test.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
... 15 more
SocketTimeout
请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。指的是连接上一个url,获取response的返回等待时间。
ConnectionRequestTimeout
设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。