From 9e012d2e20d8e5828b4f83d53c8b03c05678689c Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Tue, 25 Sep 2018 16:52:28 +0800 Subject: [PATCH] Refactor version representation and add client version to heartbeat Signed-off-by: Eric Zhao --- .../com/alibaba/csp/sentinel/Constants.java | 14 ++++++---- .../datasource/entity/MachineEntity.java | 2 +- .../dashboard/discovery/MachineInfo.java | 24 ++++++++++++++--- .../dashboard/metric/MetricFetcher.java | 2 +- .../view/MachineRegistryController.java | 20 ++++++++------ .../dashboard/view/vo/MachineInfoVo.java | 21 ++++++++++++--- .../webapp/resources/app/views/machine.html | 26 +++++++------------ .../handler/VersionCommandHandler.java | 3 ++- .../heartbeat/HttpHeartbeatSender.java | 2 ++ .../transport/heartbeat/HeartbeatMessage.java | 4 +++ 10 files changed, 77 insertions(+), 41 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java index 5e0a104b..7ea0c4e2 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java @@ -26,10 +26,13 @@ import com.alibaba.csp.sentinel.slots.system.SystemRule; * @author youji.zj * @author jialiang.linjl */ -public class Constants { +public final class Constants { + + public static final String SENTINEL_VERSION = "0.2.0"; public final static int MAX_CONTEXT_NAME_SIZE = 2000; public final static int MAX_SLOT_CHAIN_SIZE = 6000; + public final static String ROOT_ID = "machine-root"; public final static String CONTEXT_DEFAULT_NAME = "sentinel_default_context"; @@ -37,16 +40,17 @@ public class Constants { Env.nodeBuilder.buildClusterNode()); /** - * statistics for {@link SystemRule} checking. + * Statistics for {@link SystemRule} checking. */ public final static ClusterNode ENTRY_NODE = new ClusterNode(); /** - * 超过这个时间的请求不作为平均时间计算 + * Response time that exceeds TIME_DROP_VALVE will be calculated as TIME_DROP_VALVE. */ public final static int TIME_DROP_VALVE = 4900; - /*** 框架功能打开或者关闭的开关 ***/ + /** + * The global switch for Sentinel. + */ public static volatile boolean ON = true; - } \ No newline at end of file diff --git a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/datasource/entity/MachineEntity.java b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/datasource/entity/MachineEntity.java index f0cba232..682e4c78 100755 --- a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/datasource/entity/MachineEntity.java +++ b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/datasource/entity/MachineEntity.java @@ -103,7 +103,7 @@ public class MachineEntity { machineInfo.setHostname(hostname); machineInfo.setIp(ip); machineInfo.setPort(port); - machineInfo.setVersion(timestamp); + machineInfo.setTimestamp(timestamp); return machineInfo; } diff --git a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/MachineInfo.java b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/MachineInfo.java index b3aea6e2..dc2723b7 100755 --- a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/MachineInfo.java +++ b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/MachineInfo.java @@ -20,11 +20,17 @@ import java.util.Date; import com.alibaba.csp.sentinel.util.StringUtil; public class MachineInfo implements Comparable { + private String app = ""; private String hostname = ""; private String ip = ""; private Integer port = -1; - private Date version; + private Date timestamp; + + /** + * Indicates the version of Sentinel client (since 0.2.0). + */ + private String version; public static MachineInfo of(String app, String ip, Integer port) { MachineInfo machineInfo = new MachineInfo(); @@ -66,12 +72,21 @@ public class MachineInfo implements Comparable { this.ip = ip; } - public Date getVersion() { + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + public String getVersion() { return version; } - public void setVersion(Date version) { + public MachineInfo setVersion(String version) { this.version = version; + return this; } @Override @@ -95,7 +110,8 @@ public class MachineInfo implements Comparable { ", hostname='" + hostname + '\'' + ", ip='" + ip + '\'' + ", port=" + port + - ", version=" + version + + ", timestamp=" + timestamp + + ", version='" + version + '\'' + '}'; } diff --git a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/metric/MetricFetcher.java b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/metric/MetricFetcher.java index ba93100f..dce727f0 100755 --- a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/metric/MetricFetcher.java +++ b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/metric/MetricFetcher.java @@ -184,7 +184,7 @@ public class MetricFetcher { final CountDownLatch latch = new CountDownLatch(machines.size()); for (final MachineInfo machine : machines) { // dead - if (System.currentTimeMillis() - machine.getVersion().getTime() > MAX_CLIENT_LIVE_TIME_MS) { + if (System.currentTimeMillis() - machine.getTimestamp().getTime() > MAX_CLIENT_LIVE_TIME_MS) { latch.countDown(); dead.incrementAndGet(); continue; diff --git a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/MachineRegistryController.java b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/MachineRegistryController.java index f37ff928..5a73f5ec 100755 --- a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/MachineRegistryController.java +++ b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/MachineRegistryController.java @@ -17,6 +17,8 @@ package com.taobao.csp.sentinel.dashboard.view; import java.util.Date; +import com.alibaba.csp.sentinel.util.StringUtil; + import com.taobao.csp.sentinel.dashboard.discovery.AppManagement; import com.taobao.csp.sentinel.dashboard.discovery.MachineDiscovery; import com.taobao.csp.sentinel.dashboard.discovery.MachineInfo; @@ -31,13 +33,15 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/registry", produces = MediaType.APPLICATION_JSON_VALUE) public class MachineRegistryController { - Logger logger = LoggerFactory.getLogger(MachineRegistryController.class); + + private final Logger logger = LoggerFactory.getLogger(MachineRegistryController.class); + @Autowired private AppManagement appManagement; @ResponseBody @RequestMapping("/machine") - public Result receiveHeartBeat(String app, Long version, String hostname, String ip, Integer port) { + public Result receiveHeartBeat(String app, Long version, String v, String hostname, String ip, Integer port) { if (app == null) { app = MachineDiscovery.UNKNOWN_APP_NAME; } @@ -48,23 +52,23 @@ public class MachineRegistryController { return Result.ofFail(-1, "port can't be null"); } if (port == -1) { - logger.info("receive heartbeat from " + ip + " but port not set yet"); + logger.info("Receive heartbeat from " + ip + " but port not set yet"); return Result.ofFail(-1, "your port not set yet"); } - if (version == null) { - version = System.currentTimeMillis(); - } + String sentinelVersion = StringUtil.isEmpty(v) ? "unknown" : v; + long timestamp = version == null ? System.currentTimeMillis() : version; try { MachineInfo machineInfo = new MachineInfo(); machineInfo.setApp(app); machineInfo.setHostname(hostname); machineInfo.setIp(ip); machineInfo.setPort(port); - machineInfo.setVersion(new Date(version)); + machineInfo.setTimestamp(new Date(timestamp)); + machineInfo.setVersion(sentinelVersion); appManagement.addMachine(machineInfo); return Result.ofSuccessMsg("success"); } catch (Exception e) { - logger.error("receive heartbeat error:", e); + logger.error("Receive heartbeat error", e); return Result.ofFail(-1, e.getMessage()); } } diff --git a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/vo/MachineInfoVo.java b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/vo/MachineInfoVo.java index 60145f11..4e9af367 100755 --- a/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/vo/MachineInfoVo.java +++ b/sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/vo/MachineInfoVo.java @@ -26,13 +26,16 @@ import com.taobao.csp.sentinel.dashboard.discovery.MachineInfo; * @author leyou */ public class MachineInfoVo { + private String app; private String hostname; private String ip; private Integer port; - private Date version; + private Date timestamp; private boolean health; + private String version; + public static List fromMachineInfoList(List machines) { List list = new ArrayList<>(); for (MachineInfo machine : machines) { @@ -47,8 +50,9 @@ public class MachineInfoVo { vo.setHostname(machine.getHostname()); vo.setIp(machine.getIp()); vo.setPort(machine.getPort()); + vo.setTimestamp(machine.getTimestamp()); vo.setVersion(machine.getVersion()); - if (System.currentTimeMillis() - machine.getVersion().getTime() < MachineDiscovery.MAX_CLIENT_LIVE_TIME_MS) { + if (System.currentTimeMillis() - machine.getTimestamp().getTime() < MachineDiscovery.MAX_CLIENT_LIVE_TIME_MS) { vo.setHealth(true); } return vo; @@ -86,12 +90,21 @@ public class MachineInfoVo { this.port = port; } - public Date getVersion() { + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + public String getVersion() { return version; } - public void setVersion(Date version) { + public MachineInfoVo setVersion(String version) { this.version = version; + return this; } public boolean isHealth() { diff --git a/sentinel-dashboard/src/main/webapp/resources/app/views/machine.html b/sentinel-dashboard/src/main/webapp/resources/app/views/machine.html index 1e9fa5ab..ca6fd791 100755 --- a/sentinel-dashboard/src/main/webapp/resources/app/views/machine.html +++ b/sentinel-dashboard/src/main/webapp/resources/app/views/machine.html @@ -37,21 +37,12 @@ - - - - - + + + + + + @@ -60,10 +51,11 @@ + - - + +
- 机器名 - - IP地址 - - 端口号 - - 健康状态 - - 心跳时间 - 机器名IP 地址端口号Sentinel 客户端版本健康状态心跳时间
{{entry.hostname}} {{entry.ip}} {{entry.port}} {{entry.version}} 健康 失联{{entry.version | date: 'yyyy/MM/dd HH:mm:ss'}}{{entry.timestamp | date: 'yyyy/MM/dd HH:mm:ss'}}
diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/VersionCommandHandler.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/VersionCommandHandler.java index 1b1f0f11..3895626f 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/VersionCommandHandler.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/VersionCommandHandler.java @@ -15,6 +15,7 @@ */ package com.alibaba.csp.sentinel.command.handler; +import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.command.CommandHandler; import com.alibaba.csp.sentinel.command.CommandRequest; import com.alibaba.csp.sentinel.command.CommandResponse; @@ -29,6 +30,6 @@ public class VersionCommandHandler implements CommandHandler { @Override public CommandResponse handle(CommandRequest request) { - return CommandResponse.ofSuccess("0.2.0"); + return CommandResponse.ofSuccess(Constants.SENTINEL_VERSION); } } diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java index 976f26bb..f92e4744 100755 --- a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java @@ -15,6 +15,7 @@ */ package com.alibaba.csp.sentinel.transport.heartbeat; +import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.AppNameUtil; @@ -81,6 +82,7 @@ public class HttpHeartbeatSender implements HeartbeatSender { uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort) .setPath("/registry/machine") .setParameter("app", AppNameUtil.getAppName()) + .setParameter("v", Constants.SENTINEL_VERSION) .setParameter("version", String.valueOf(System.currentTimeMillis())) .setParameter("hostname", HostNameUtil.getHostName()) .setParameter("ip", HostNameUtil.getIp()) diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java index d6fc597f..8292c7a3 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java @@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.transport.heartbeat; import java.util.HashMap; import java.util.Map; +import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.util.AppNameUtil; import com.alibaba.csp.sentinel.util.HostNameUtil; @@ -46,6 +47,9 @@ public class HeartbeatMessage { } public Map generateCurrentMessage() { + // Version of Sentinel. + message.put("v", Constants.SENTINEL_VERSION); + // Actually timestamp. message.put("version", String.valueOf(TimeUtil.currentTimeMillis())); message.put("port", String.valueOf(TransportConfig.getPort())); return message;