Skip to content

Commit 08cd41a

Browse files
jasonjoo2010jason-joo
authored andcommitted
dashboard supports removing of unhealth machines
1 parent 3b60958 commit 08cd41a

File tree

11 files changed

+157
-27
lines changed

11 files changed

+157
-27
lines changed

sentinel-dashboard/pom.xml

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<dependency>
2323
<groupId>com.alibaba.csp</groupId>
2424
<artifactId>sentinel-core</artifactId>
25-
<version>${project.version}</version>
2625
</dependency>
2726
<dependency>
2827
<groupId>com.alibaba.csp</groupId>
@@ -32,7 +31,6 @@
3231
<dependency>
3332
<groupId>com.alibaba.csp</groupId>
3433
<artifactId>sentinel-transport-simple-http</artifactId>
35-
<version>${project.version}</version>
3634
</dependency>
3735
<dependency>
3836
<groupId>com.alibaba.csp</groupId>

sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/AppInfo.java

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.HashSet;
1919
import java.util.Optional;
20+
import java.util.Iterator;
2021
import java.util.Set;
2122
import java.util.concurrent.ConcurrentHashMap;
2223

@@ -59,6 +60,18 @@ public boolean addMachine(MachineInfo machineInfo) {
5960
machines.remove(machineInfo);
6061
return machines.add(machineInfo);
6162
}
63+
64+
public synchronized boolean removeMachine(String ip, int port) {
65+
Iterator<MachineInfo> it = machines.iterator();
66+
while (it.hasNext()) {
67+
MachineInfo machine = it.next();
68+
if (machine.getIp().equals(ip) && machine.getPort() == port) {
69+
it.remove();
70+
return true;
71+
}
72+
}
73+
return false;
74+
}
6275

6376
public Optional<MachineInfo> getMachine(String ip, int port) {
6477
return machines.stream()

sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/AppManagement.java

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public Set<AppInfo> getBriefApps() {
5252
public long addMachine(MachineInfo machineInfo) {
5353
return machineDiscovery.addMachine(machineInfo);
5454
}
55+
56+
@Override
57+
public boolean removeMachine(String app, String ip, int port) {
58+
return machineDiscovery.removeMachine(app, ip, port);
59+
}
5560

5661
@Override
5762
public List<String> getAppNames() {

sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/MachineDiscovery.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ public interface MachineDiscovery {
3030
AppInfo getDetailApp(String app);
3131

3232
long addMachine(MachineInfo machineInfo);
33+
34+
boolean removeMachine(String app, String ip, int port);
3335
}

sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/discovery/SimpleMachineDiscovery.java

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public long addMachine(MachineInfo machineInfo) {
3636
appInfo.addMachine(machineInfo);
3737
return 1;
3838
}
39+
40+
@Override
41+
public boolean removeMachine(String app, String ip, int port) {
42+
AppInfo appInfo = apps.get(app);
43+
if (appInfo != null) {
44+
return appInfo.removeMachine(ip, port);
45+
}
46+
return false;
47+
}
3948

4049
@Override
4150
public List<String> getAppNames() {

sentinel-dashboard/src/main/java/com/taobao/csp/sentinel/dashboard/view/AppController.java

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.stereotype.Controller;
3232
import org.springframework.web.bind.annotation.PathVariable;
3333
import org.springframework.web.bind.annotation.RequestMapping;
34+
import org.springframework.web.bind.annotation.RequestParam;
3435
import org.springframework.web.bind.annotation.ResponseBody;
3536

3637
/**
@@ -78,4 +79,21 @@ Result<List<MachineInfoVo>> getMachinesByApp(@PathVariable("app") String app) {
7879
});
7980
return Result.ofSuccess(MachineInfoVo.fromMachineInfoList(list));
8081
}
82+
83+
@ResponseBody
84+
@RequestMapping(value = "/{app}/machine/remove.json")
85+
Result<String> removeMachineById(
86+
@PathVariable("app") String app,
87+
@RequestParam(name = "ip") String ip,
88+
@RequestParam(name = "port") int port) {
89+
AppInfo appInfo = appManagement.getDetailApp(app);
90+
if (appInfo == null) {
91+
return Result.ofSuccess(null);
92+
}
93+
if (appManagement.removeMachine(app, ip, port)) {
94+
return Result.ofSuccessMsg("success");
95+
} else {
96+
return Result.ofFail(1, "remove failed");
97+
}
98+
}
8199
}

sentinel-dashboard/src/main/webapp/resources/app/scripts/controllers/machine.js

+41-21
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,47 @@ app.controller('MachineCtl', ['$scope', '$stateParams', 'MachineService',
1919
$scope.reverse = ($scope.propertyName === propertyName) ? !$scope.reverse : false;
2020
$scope.propertyName = propertyName;
2121
};
22-
23-
MachineService.getAppMachines($scope.app).success(
24-
function (data) {
25-
// console.log('get machines: ' + data.data[0].hostname)
26-
if (data.code == 0 && data.data) {
27-
$scope.machines = data.data;
28-
var health = 0;
29-
$scope.machines.forEach(function (item) {
30-
if (item.health) {
31-
health++;
32-
}
33-
if (!item.hostname) {
34-
item.hostname = '未知'
35-
}
36-
})
37-
$scope.healthCount = health;
38-
$scope.machinesPageConfig.totalCount = $scope.machines.length;
39-
} else {
40-
$scope.machines = [];
41-
$scope.healthCount = 0;
22+
23+
$scope.reloadMachines = function() {
24+
MachineService.getAppMachines($scope.app).success(
25+
function (data) {
26+
// console.log('get machines: ' + data.data[0].hostname)
27+
if (data.code == 0 && data.data) {
28+
$scope.machines = data.data;
29+
var health = 0;
30+
$scope.machines.forEach(function (item) {
31+
if (item.health) {
32+
health++;
33+
}
34+
if (!item.hostname) {
35+
item.hostname = '未知'
36+
}
37+
})
38+
$scope.healthCount = health;
39+
$scope.machinesPageConfig.totalCount = $scope.machines.length;
40+
} else {
41+
$scope.machines = [];
42+
$scope.healthCount = 0;
43+
}
4244
}
45+
);
46+
};
47+
48+
$scope.removeMachine = function(ip, port) {
49+
if (!confirm("confirm to remove machine [" + ip + ":" + port + "]?")) {
50+
return;
4351
}
44-
);
52+
MachineService.removeAppMachine($scope.app, ip, port).success(
53+
function(data) {
54+
if (data.code == 0) {
55+
$scope.reloadMachines();
56+
} else {
57+
alert("remove failed");
58+
}
59+
}
60+
);
61+
};
62+
63+
$scope.reloadMachines();
64+
4565
}]);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
var app = angular.module('sentinelDashboardApp');
22

3-
app.service('MachineService', ['$http', function ($http) {
3+
app.service('MachineService', ['$http', '$httpParamSerializerJQLike', function ($http, $httpParamSerializerJQLike) {
44
this.getAppMachines = function (app) {
55
return $http({
66
url: 'app/' + app + '/machines.json',
77
method: 'GET'
88
});
99
};
10+
this.removeAppMachine = function (app, ip, port) {
11+
return $http({
12+
url: 'app/' + app + '/machine/remove.json',
13+
method: 'POST',
14+
headers: {
15+
"Content-type": 'application/x-www-form-urlencoded; charset=UTF-8'
16+
},
17+
data: $httpParamSerializerJQLike({
18+
ip: ip,
19+
port: port
20+
})
21+
});
22+
};
1023
}]);

sentinel-dashboard/src/main/webapp/resources/app/views/machine.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<td>Sentinel 客户端版本</td>
4444
<td>健康状态</td>
4545
<td>心跳时间</td>
46+
<td>操作</td>
4647
</tr>
4748
</thead>
4849
<tbody>
@@ -55,7 +56,9 @@
5556
<td ng-if="entry.health">健康</td>
5657
<td ng-if="!entry.health" style="color: red">失联</td>
5758
<td>{{entry.timestamp | date: 'yyyy/MM/dd HH:mm:ss'}}</td>
58-
<!--<td ng-if="!entry.health" style="color: grey">{{entry.timestamp | date: 'yyyy/MM/dd HH:mm:ss'}}</td>-->
59+
<td>
60+
<button ng-if="!entry.health" class="btn btn-xs btn-default" style="height: 25px; font-size: 12px;" ng-click="removeMachine(entry.ip, entry.port)">REMOVE</button>
61+
</td>
5962
</tr>
6063
</tbody>
6164
</table>

sentinel-dashboard/src/main/webapp/resources/dist/js/app.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentinel-dashboard/src/test/java/com/taobao/csp/sentinel/dashboard/discovery/AppInfoTest.java

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.taobao.csp.sentinel.dashboard.discovery;
22

33
import java.util.ConcurrentModificationException;
4+
import java.util.Date;
45
import java.util.Set;
56

67
import org.junit.Test;
@@ -49,4 +50,52 @@ private MachineInfo genMachineInfo(String hostName, String ip) {
4950
return machine;
5051
}
5152

52-
}
53+
@Test
54+
public void addRemoveMachineTest() {
55+
AppInfo appInfo = new AppInfo("default");
56+
assertEquals("default", appInfo.getApp());
57+
assertEquals(0, appInfo.getMachines().size());
58+
//add one
59+
{
60+
MachineInfo machineInfo = new MachineInfo();
61+
machineInfo.setApp("default");
62+
machineInfo.setHostname("bogon");
63+
machineInfo.setIp("127.0.0.1");
64+
machineInfo.setPort(3389);
65+
machineInfo.setTimestamp(new Date());
66+
machineInfo.setVersion("0.4.1");
67+
appInfo.addMachine(machineInfo);
68+
}
69+
assertEquals(1, appInfo.getMachines().size());
70+
//add duplicated one
71+
{
72+
MachineInfo machineInfo = new MachineInfo();
73+
machineInfo.setApp("default");
74+
machineInfo.setHostname("bogon");
75+
machineInfo.setIp("127.0.0.1");
76+
machineInfo.setPort(3389);
77+
machineInfo.setTimestamp(new Date());
78+
machineInfo.setVersion("0.4.2");
79+
appInfo.addMachine(machineInfo);
80+
}
81+
assertEquals(1, appInfo.getMachines().size());
82+
//add different one
83+
{
84+
MachineInfo machineInfo = new MachineInfo();
85+
machineInfo.setApp("default");
86+
machineInfo.setHostname("bogon");
87+
machineInfo.setIp("127.0.0.1");
88+
machineInfo.setPort(3390);
89+
machineInfo.setTimestamp(new Date());
90+
machineInfo.setVersion("0.4.3");
91+
appInfo.addMachine(machineInfo);
92+
}
93+
assertEquals(2, appInfo.getMachines().size());
94+
appInfo.removeMachine("127.0.0.1", 3389);
95+
assertEquals(1, appInfo.getMachines().size());
96+
appInfo.removeMachine("127.0.0.1", 3390);
97+
assertEquals(0, appInfo.getMachines().size());
98+
}
99+
100+
}
101+

0 commit comments

Comments
 (0)