Polish code of transport command centers and heartbeat senders
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
0536fb6846
commit
22df09b427
|
|
@ -25,7 +25,8 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author leyou
|
* @author Carpenter Lee
|
||||||
|
* @author Jason Joo
|
||||||
*/
|
*/
|
||||||
public class TransportConfig {
|
public class TransportConfig {
|
||||||
|
|
||||||
|
|
@ -66,7 +67,6 @@ public class TransportConfig {
|
||||||
String config = SentinelConfig.getConfig(CONSOLE_SERVER);
|
String config = SentinelConfig.getConfig(CONSOLE_SERVER);
|
||||||
List<Tuple2<String, Integer>> list = new ArrayList<Tuple2<String, Integer>>();
|
List<Tuple2<String, Integer>> list = new ArrayList<Tuple2<String, Integer>>();
|
||||||
if (StringUtil.isBlank(config)) {
|
if (StringUtil.isBlank(config)) {
|
||||||
RecordLog.warn("Dashboard server address is not configured");
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ import com.alibaba.csp.sentinel.util.HostNameUtil;
|
||||||
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;
|
||||||
|
|
@ -37,7 +38,7 @@ import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Eric Zhao
|
* @author Eric Zhao
|
||||||
* @author leyou
|
* @author Carpenter Lee
|
||||||
*/
|
*/
|
||||||
@SpiOrder(SpiOrder.LOWEST_PRECEDENCE - 100)
|
@SpiOrder(SpiOrder.LOWEST_PRECEDENCE - 100)
|
||||||
public class HttpHeartbeatSender implements HeartbeatSender {
|
public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
|
|
@ -46,7 +47,6 @@ public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
|
|
||||||
private static final int OK_STATUS = 200;
|
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)
|
||||||
|
|
@ -54,18 +54,21 @@ public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
.setSocketTimeout(timeoutMs)
|
.setSocketTimeout(timeoutMs)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private String consoleHost;
|
private final String consoleHost;
|
||||||
private int consolePort;
|
private final int consolePort;
|
||||||
|
|
||||||
public HttpHeartbeatSender() {
|
public HttpHeartbeatSender() {
|
||||||
this.client = HttpClients.createDefault();
|
this.client = HttpClients.createDefault();
|
||||||
List<Tuple2<String, Integer>> dashboardList = TransportConfig.getConsoleServerList();
|
List<Tuple2<String, Integer>> dashboardList = TransportConfig.getConsoleServerList();
|
||||||
if (dashboardList == null || dashboardList.isEmpty()) {
|
if (dashboardList == null || dashboardList.isEmpty()) {
|
||||||
RecordLog.info("[NettyHttpHeartbeatSender] No dashboard available");
|
RecordLog.info("[NettyHttpHeartbeatSender] No dashboard server available");
|
||||||
|
consoleHost = null;
|
||||||
|
consolePort = -1;
|
||||||
} else {
|
} else {
|
||||||
consoleHost = dashboardList.get(0).r1;
|
consoleHost = dashboardList.get(0).r1;
|
||||||
consolePort = dashboardList.get(0).r2;
|
consolePort = dashboardList.get(0).r2;
|
||||||
RecordLog.info("[NettyHttpHeartbeatSender] Dashboard address parsed: <" + consoleHost + ':' + consolePort + ">");
|
RecordLog.info(
|
||||||
|
"[NettyHttpHeartbeatSender] Dashboard address parsed: <" + consoleHost + ':' + consolePort + ">");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,12 +99,10 @@ public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
return true;
|
return true;
|
||||||
} else if (clientErrorCode(statusCode) || serverErrorCode(statusCode)) {
|
} else if (clientErrorCode(statusCode) || serverErrorCode(statusCode)) {
|
||||||
RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat to "
|
RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat to "
|
||||||
+ consoleHost + ":" + consolePort + ", http status code: {0}", statusCode);
|
+ consoleHost + ":" + consolePort + ", http status code: " + statusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -109,25 +110,11 @@ public class HttpHeartbeatSender implements HeartbeatSender {
|
||||||
return 5000;
|
return 5000;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 4XX Client Error
|
|
||||||
*
|
|
||||||
* @param code
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private boolean clientErrorCode(int code) {
|
private boolean clientErrorCode(int code) {
|
||||||
return code > 399 && code < 500;
|
return code > 399 && code < 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 5XX Server Error
|
|
||||||
*
|
|
||||||
* @param code
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private boolean serverErrorCode(int code) {
|
private boolean serverErrorCode(int code) {
|
||||||
return code > 499 && code < 600;
|
return code > 499 && code < 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import com.alibaba.csp.sentinel.command.CommandRequest;
|
||||||
import com.alibaba.csp.sentinel.command.CommandResponse;
|
import com.alibaba.csp.sentinel.command.CommandResponse;
|
||||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
import com.alibaba.csp.sentinel.log.CommandCenterLog;
|
import com.alibaba.csp.sentinel.log.CommandCenterLog;
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
|
||||||
import com.alibaba.csp.sentinel.transport.command.SimpleHttpCommandCenter;
|
import com.alibaba.csp.sentinel.transport.command.SimpleHttpCommandCenter;
|
||||||
import com.alibaba.csp.sentinel.transport.command.exception.RequestException;
|
import com.alibaba.csp.sentinel.transport.command.exception.RequestException;
|
||||||
import com.alibaba.csp.sentinel.transport.util.HttpCommandUtils;
|
import com.alibaba.csp.sentinel.transport.util.HttpCommandUtils;
|
||||||
|
|
@ -41,15 +40,17 @@ import java.nio.charset.Charset;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
/***
|
|
||||||
* The task handles incoming command request in HTTP protocol.
|
* The task handles incoming command request in HTTP protocol.
|
||||||
*
|
*
|
||||||
* @author youji.zj
|
* @author youji.zj
|
||||||
* @author Eric Zhao
|
* @author Eric Zhao
|
||||||
|
* @author Jason Joo
|
||||||
*/
|
*/
|
||||||
public class HttpEventTask implements Runnable {
|
public class HttpEventTask implements Runnable {
|
||||||
private static final String SERVER_ERROR_MESSAGE = "Command server error";
|
|
||||||
|
public static final String SERVER_ERROR_MESSAGE = "Command server error";
|
||||||
|
public static final String INVALID_COMMAND_MESSAGE = "Invalid command";
|
||||||
|
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
|
|
||||||
|
|
@ -92,7 +93,7 @@ public class HttpEventTask implements Runnable {
|
||||||
// Validate the target command.
|
// Validate the target command.
|
||||||
String commandName = HttpCommandUtils.getTarget(request);
|
String commandName = HttpCommandUtils.getTarget(request);
|
||||||
if (StringUtil.isBlank(commandName)) {
|
if (StringUtil.isBlank(commandName)) {
|
||||||
writeResponse(printWriter, StatusCode.BAD_REQUEST, "Invalid command");
|
writeResponse(printWriter, StatusCode.BAD_REQUEST, INVALID_COMMAND_MESSAGE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -168,13 +169,13 @@ public class HttpEventTask implements Runnable {
|
||||||
|
|
||||||
if (headerMap == null) {
|
if (headerMap == null) {
|
||||||
// illegal request
|
// illegal request
|
||||||
RecordLog.warn("Illegal request read");
|
CommandCenterLog.warn("Illegal request read: null headerMap");
|
||||||
throw new RequestException(StatusCode.BAD_REQUEST, "");
|
throw new RequestException(StatusCode.BAD_REQUEST, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headerMap.containsKey("content-type") && !checkSupport(headerMap.get("content-type"))) {
|
if (headerMap.containsKey("content-type") && !checkContentTypeSupported(headerMap.get("content-type"))) {
|
||||||
// not support Content-type
|
// not supported Content-type
|
||||||
RecordLog.warn("Not supported Content-Type: {}", headerMap.get("content-type"));
|
CommandCenterLog.warn("Request not supported: unsupported Content-Type: " + headerMap.get("content-type"));
|
||||||
throw new RequestException(StatusCode.UNSUPPORTED_MEDIA_TYPE,
|
throw new RequestException(StatusCode.UNSUPPORTED_MEDIA_TYPE,
|
||||||
"Only form-encoded post request is supported");
|
"Only form-encoded post request is supported");
|
||||||
}
|
}
|
||||||
|
|
@ -186,7 +187,7 @@ public class HttpEventTask implements Runnable {
|
||||||
}
|
}
|
||||||
if (bodyLength < 1) {
|
if (bodyLength < 1) {
|
||||||
// illegal request without Content-length header
|
// illegal request without Content-length header
|
||||||
RecordLog.warn("No available Content-Length in headers");
|
CommandCenterLog.warn("Request not supported: no available Content-Length in headers");
|
||||||
throw new RequestException(StatusCode.LENGTH_REQUIRED, "No legal Content-Length");
|
throw new RequestException(StatusCode.LENGTH_REQUIRED, "No legal Content-Length");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -222,7 +223,7 @@ public class HttpEventTask implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean checkSupport(String contentType) {
|
private static boolean checkContentTypeSupported(String contentType) {
|
||||||
int idx = contentType.indexOf(";");
|
int idx = contentType.indexOf(";");
|
||||||
String type;
|
String type;
|
||||||
if (idx > 0) {
|
if (idx > 0) {
|
||||||
|
|
@ -234,7 +235,6 @@ public class HttpEventTask implements Runnable {
|
||||||
// But some library do add it. So we will be compatible with that but force to
|
// But some library do add it. So we will be compatible with that but force to
|
||||||
// encoding specified in configuration as legacy processing will do.
|
// encoding specified in configuration as legacy processing will do.
|
||||||
if (!type.contains("application/x-www-form-urlencoded")) {
|
if (!type.contains("application/x-www-form-urlencoded")) {
|
||||||
CommandCenterLog.warn("Content-Type not supported: " + contentType);
|
|
||||||
// Not supported request type
|
// Not supported request type
|
||||||
// Now simple-http only support form-encoded post request.
|
// Now simple-http only support form-encoded post request.
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,13 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.transport.command.http;
|
package com.alibaba.csp.sentinel.transport.command.http;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jason Joo
|
||||||
|
*/
|
||||||
public enum StatusCode {
|
public enum StatusCode {
|
||||||
|
/**
|
||||||
|
* 200 OK.
|
||||||
|
*/
|
||||||
OK(200, "OK"),
|
OK(200, "OK"),
|
||||||
BAD_REQUEST(400, "Bad Request"),
|
BAD_REQUEST(400, "Bad Request"),
|
||||||
REQUEST_TIMEOUT(408, "Request Timeout"),
|
REQUEST_TIMEOUT(408, "Request Timeout"),
|
||||||
|
|
@ -27,7 +33,7 @@ public enum StatusCode {
|
||||||
private String desc;
|
private String desc;
|
||||||
private String representation;
|
private String representation;
|
||||||
|
|
||||||
private StatusCode(int code, String desc) {
|
StatusCode(int code, String desc) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.desc = desc;
|
this.desc = desc;
|
||||||
this.representation = code + " " + desc;
|
this.representation = code + " " + desc;
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ import java.util.List;
|
||||||
* This implementation is based on a trivial HTTP client.
|
* This implementation is based on a trivial HTTP client.
|
||||||
*
|
*
|
||||||
* @author Eric Zhao
|
* @author Eric Zhao
|
||||||
* @author leyou
|
* @author Carpenter Lee
|
||||||
*/
|
*/
|
||||||
public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
||||||
|
|
||||||
|
|
@ -49,14 +49,18 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
||||||
public SimpleHttpHeartbeatSender() {
|
public SimpleHttpHeartbeatSender() {
|
||||||
// Retrieve the list of default addresses.
|
// Retrieve the list of default addresses.
|
||||||
List<Tuple2<String, Integer>> newAddrs = TransportConfig.getConsoleServerList();
|
List<Tuple2<String, Integer>> newAddrs = TransportConfig.getConsoleServerList();
|
||||||
|
if (newAddrs.isEmpty()) {
|
||||||
|
RecordLog.warn("[SimpleHttpHeartbeatSender] Dashboard server address not configured or not available");
|
||||||
|
} else {
|
||||||
RecordLog.info("[SimpleHttpHeartbeatSender] Default console address list retrieved: " + newAddrs);
|
RecordLog.info("[SimpleHttpHeartbeatSender] Default console address list retrieved: " + newAddrs);
|
||||||
|
}
|
||||||
this.addressList = newAddrs;
|
this.addressList = newAddrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean sendHeartbeat() throws Exception {
|
public boolean sendHeartbeat() throws Exception {
|
||||||
if (TransportConfig.getRuntimePort() <= 0) {
|
if (TransportConfig.getRuntimePort() <= 0) {
|
||||||
RecordLog.info("[SimpleHttpHeartbeatSender] Runtime port not initialized, won't send heartbeat");
|
RecordLog.info("[SimpleHttpHeartbeatSender] Command server port not initialized, won't send heartbeat");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Tuple2<String, Integer> addrInfo = getAvailableAddress();
|
Tuple2<String, Integer> addrInfo = getAvailableAddress();
|
||||||
|
|
@ -72,7 +76,8 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
||||||
if (response.getStatusCode() == OK_STATUS) {
|
if (response.getStatusCode() == OK_STATUS) {
|
||||||
return true;
|
return true;
|
||||||
} else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) {
|
} else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) {
|
||||||
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + ", http status code: {0}", response.getStatusCode());
|
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr
|
||||||
|
+ ", http status code: " + 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,23 +101,10 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
|
||||||
return addressList.get(index);
|
return addressList.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 4XX Client Error
|
|
||||||
*
|
|
||||||
* @param code
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private boolean clientErrorCode(int code) {
|
private boolean clientErrorCode(int code) {
|
||||||
return code > 399 && code < 500;
|
return code > 399 && code < 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 5XX Server Error
|
|
||||||
*
|
|
||||||
* @param code
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private boolean serverErrorCode(int code) {
|
private boolean serverErrorCode(int code) {
|
||||||
return code > 499 && code < 600;
|
return code > 499 && code < 600;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue