Skip to content

Commit

Permalink
Refactor version representation and add client version to heartbeat
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Sep 25, 2018
1 parent f5b09a1 commit 6be07be
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,31 @@
* @author youji.zj
* @author jialiang.linjl
*/
public class Constants {
public final class Constants {

public static final String SENTINEL_VERSION = "0.1.2";

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

public final static DefaultNode ROOT = new EntranceNode(new StringResourceWrapper(ROOT_ID, EntryType.IN),
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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public MachineInfo toMachineInfo() {
machineInfo.setHostname(hostname);
machineInfo.setIp(ip);
machineInfo.setPort(port);
machineInfo.setVersion(timestamp);
machineInfo.setTimestamp(timestamp);

return machineInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
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();
Expand Down Expand Up @@ -66,12 +72,21 @@ public void setIp(String ip) {
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
Expand All @@ -95,7 +110,8 @@ public String toString() {
", hostname='" + hostname + '\'' +
", ip='" + ip + '\'' +
", port=" + port +
", version=" + version +
", timestamp=" + timestamp +
", version='" + version + '\'' +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void fetchOnce(String app, long startTime, long endTime, int maxWaitSeco
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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;
Expand All @@ -31,13 +33,15 @@
@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;
}
Expand All @@ -48,23 +52,23 @@ public Result<?> receiveHeartBeat(String app, Long version, String hostname, Str
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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
* @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) {
Expand All @@ -47,8 +50,9 @@ public static MachineInfoVo fromMachineInfo(MachineInfo machine) {
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;
Expand Down Expand Up @@ -86,12 +90,21 @@ public void setPort(Integer port) {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,6 @@ public class VersionCommandHandler implements CommandHandler<String> {

@Override
public CommandResponse<String> handle(CommandRequest request) {
return CommandResponse.ofSuccess("0.1.2");
return CommandResponse.ofSuccess(Constants.SENTINEL_VERSION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +82,7 @@ public boolean sendHeartbeat() throws Exception {
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -46,6 +47,9 @@ public HeartbeatMessage registerInformation(String key, String value) {
}

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;
Expand Down

0 comments on commit 6be07be

Please sign in to comment.