Refactor version representation and add client version to heartbeat

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2018-09-25 16:52:28 +08:00
parent 7c179bb592
commit 9e012d2e20
10 changed files with 77 additions and 41 deletions

View File

@ -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;
}

View File

@ -103,7 +103,7 @@ public class MachineEntity {
machineInfo.setHostname(hostname);
machineInfo.setIp(ip);
machineInfo.setPort(port);
machineInfo.setVersion(timestamp);
machineInfo.setTimestamp(timestamp);
return machineInfo;
}

View File

@ -20,11 +20,17 @@ import java.util.Date;
import com.alibaba.csp.sentinel.util.StringUtil;
public class MachineInfo implements Comparable<MachineInfo> {
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<MachineInfo> {
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<MachineInfo> {
", hostname='" + hostname + '\'' +
", ip='" + ip + '\'' +
", port=" + port +
", version=" + version +
", timestamp=" + timestamp +
", version='" + version + '\'' +
'}';
}

View File

@ -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;

View File

@ -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());
}
}

View File

@ -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<MachineInfoVo> fromMachineInfoList(List<MachineInfo> machines) {
List<MachineInfoVo> 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() {

View File

@ -37,21 +37,12 @@
<table class="table" style="border-left: none; border-right:none;margin-top: 10px;">
<thead>
<tr style="background: #F3F5F7;">
<td>
机器名
</td>
<td>
IP地址
</td>
<td>
端口号
</td>
<td>
健康状态
</td>
<td>
心跳时间
</td>
<td>机器名</td>
<td>IP 地址</td>
<td>端口号</td>
<td>Sentinel 客户端版本</td>
<td>健康状态</td>
<td>心跳时间</td>
</tr>
</thead>
<tbody>
@ -60,10 +51,11 @@
<td style="word-wrap:break-word;word-break:break-all;">{{entry.hostname}}</td>
<td style="word-wrap:break-word;word-break:break-all;">{{entry.ip}}</td>
<td> {{entry.port}} </td>
<td> {{entry.version}} </td>
<td ng-if="entry.health">健康</td>
<td ng-if="!entry.health" style="color: red">失联</td>
<td>{{entry.version | date: 'yyyy/MM/dd HH:mm:ss'}}</td>
<!--<td ng-if="!entry.health" style="color: grey">{{entry.version | date: 'yyyy/MM/dd HH:mm:ss'}}</td>-->
<td>{{entry.timestamp | date: 'yyyy/MM/dd HH:mm:ss'}}</td>
<!--<td ng-if="!entry.health" style="color: grey">{{entry.timestamp | date: 'yyyy/MM/dd HH:mm:ss'}}</td>-->
</tr>
</tbody>
</table>

View File

@ -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<String> {
@Override
public CommandResponse<String> handle(CommandRequest request) {
return CommandResponse.ofSuccess("0.2.0");
return CommandResponse.ofSuccess(Constants.SENTINEL_VERSION);
}
}

View File

@ -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())

View File

@ -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<String, String> 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;