Improve the logs when the heartbeat response indicates failure (#1303)
This commit is contained in:
parent
d07d17b4ca
commit
72bfe870cb
|
|
@ -15,15 +15,15 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.transport.config;
|
package com.alibaba.csp.sentinel.transport.config;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
import com.alibaba.csp.sentinel.util.HostNameUtil;
|
import com.alibaba.csp.sentinel.util.HostNameUtil;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author leyou
|
* @author leyou
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -15,20 +15,17 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.transport.heartbeat;
|
package com.alibaba.csp.sentinel.transport.heartbeat;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.Constants;
|
import com.alibaba.csp.sentinel.Constants;
|
||||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
import com.alibaba.csp.sentinel.spi.SpiOrder;
|
|
||||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
|
import com.alibaba.csp.sentinel.spi.SpiOrder;
|
||||||
|
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
||||||
|
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||||
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
||||||
import com.alibaba.csp.sentinel.util.HostNameUtil;
|
import com.alibaba.csp.sentinel.util.HostNameUtil;
|
||||||
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
|
||||||
import com.alibaba.csp.sentinel.util.PidUtil;
|
import com.alibaba.csp.sentinel.util.PidUtil;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
||||||
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
|
@ -36,6 +33,8 @@ import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Eric Zhao
|
* @author Eric Zhao
|
||||||
* @author leyou
|
* @author leyou
|
||||||
|
|
@ -45,6 +44,9 @@ public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
|
|
||||||
private final CloseableHttpClient client;
|
private final CloseableHttpClient client;
|
||||||
|
|
||||||
|
private static final int OK_STATUS = 200;
|
||||||
|
|
||||||
|
|
||||||
private final int timeoutMs = 3000;
|
private final int timeoutMs = 3000;
|
||||||
private final RequestConfig requestConfig = RequestConfig.custom()
|
private final RequestConfig requestConfig = RequestConfig.custom()
|
||||||
.setConnectionRequestTimeout(timeoutMs)
|
.setConnectionRequestTimeout(timeoutMs)
|
||||||
|
|
@ -89,11 +91,43 @@ public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
// Send heartbeat request.
|
// Send heartbeat request.
|
||||||
CloseableHttpResponse response = client.execute(request);
|
CloseableHttpResponse response = client.execute(request);
|
||||||
response.close();
|
response.close();
|
||||||
return true;
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if (statusCode == OK_STATUS) {
|
||||||
|
return true;
|
||||||
|
} else if (clientErrorCode(statusCode) || serverErrorCode(statusCode)) {
|
||||||
|
RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat to "
|
||||||
|
+ consoleHost + ":" + consolePort + ", http status code: {0}", statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long intervalMs() {
|
public long intervalMs() {
|
||||||
return 5000;
|
return 5000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 4XX Client Error
|
||||||
|
*
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean clientErrorCode(int code) {
|
||||||
|
return code > 399 && code < 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 5XX Server Error
|
||||||
|
*
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean serverErrorCode(int code) {
|
||||||
|
return code > 499 && code < 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,19 +15,17 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.transport.heartbeat;
|
package com.alibaba.csp.sentinel.transport.heartbeat;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
||||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||||
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpClient;
|
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpClient;
|
||||||
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest;
|
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest;
|
||||||
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse;
|
import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
|
||||||
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The heartbeat sender provides basic API for sending heartbeat request to provided target.
|
* The heartbeat sender provides basic API for sending heartbeat request to provided target.
|
||||||
* This implementation is based on a trivial HTTP client.
|
* This implementation is based on a trivial HTTP client.
|
||||||
|
|
@ -73,6 +71,8 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
||||||
SimpleHttpResponse response = httpClient.post(request);
|
SimpleHttpResponse response = httpClient.post(request);
|
||||||
if (response.getStatusCode() == OK_STATUS) {
|
if (response.getStatusCode() == OK_STATUS) {
|
||||||
return true;
|
return true;
|
||||||
|
} else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) {
|
||||||
|
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + ", http status code: {0}", response.getStatusCode());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr, e);
|
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr, e);
|
||||||
|
|
@ -96,4 +96,24 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
||||||
return addressList.get(index);
|
return addressList.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 4XX Client Error
|
||||||
|
*
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean clientErrorCode(int code) {
|
||||||
|
return code > 399 && code < 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 5XX Server Error
|
||||||
|
*
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean serverErrorCode(int code) {
|
||||||
|
return code > 499 && code < 600;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,11 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.transport.heartbeat.client;
|
package com.alibaba.csp.sentinel.transport.heartbeat.client;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple HTTP response representation.
|
* Simple HTTP response representation.
|
||||||
*
|
*
|
||||||
|
|
@ -112,10 +112,12 @@ public class SimpleHttpResponse {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
buf.append(statusLine)
|
buf.append(statusLine)
|
||||||
.append("\r\n");
|
|
||||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
||||||
buf.append(entry.getKey()).append(": ").append(entry.getValue())
|
|
||||||
.append("\r\n");
|
.append("\r\n");
|
||||||
|
if (headers != null) {
|
||||||
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
|
buf.append(entry.getKey()).append(": ").append(entry.getValue())
|
||||||
|
.append("\r\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
buf.append("\r\n");
|
buf.append("\r\n");
|
||||||
buf.append(getBodyAsString());
|
buf.append(getBodyAsString());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue