Skip to content

Commit 5b9bb23

Browse files
Jay.H.ZouJay.H.Zou
authored andcommitted
Merge branch 'feature-sentinel'
2 parents 7d841f2 + 471c71e commit 5b9bb23

File tree

52 files changed

+2189
-179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2189
-179
lines changed

redis-manager-dashboard/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.newegg.ec</groupId>
88
<artifactId>redis-manager-dashboard</artifactId>
9-
<version>2.1.0-RELEASE</version>
9+
<version>2.2.0-RELEASE</version>
1010
<packaging>jar</packaging>
1111

1212
<parent>

redis-manager-dashboard/src/main/java/com/newegg/ec/redis/client/IRedisClient.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.newegg.ec.redis.entity.NodeRole;
55
import com.newegg.ec.redis.entity.RedisNode;
66
import redis.clients.jedis.ClusterReset;
7+
import redis.clients.jedis.HostAndPort;
78
import redis.clients.jedis.Jedis;
89
import redis.clients.jedis.params.MigrateParams;
910
import redis.clients.jedis.util.Slowlog;
@@ -72,6 +73,8 @@ public interface IRedisClient extends IDatabaseCommand {
7273

7374
List<RedisNode> clusterNodes() throws Exception;
7475

76+
List<RedisNode> sentinelNodes(Set<HostAndPort> hostAndPorts) throws Exception;
77+
7578
Long dbSize();
7679

7780
/**
@@ -203,8 +206,6 @@ public interface IRedisClient extends IDatabaseCommand {
203206
String migrate(String host, int port, int destinationDB,
204207
int timeout, MigrateParams params, String... keys);
205208

206-
// String clusterSlaves(String nodeId);
207-
208209
/**
209210
* old name: slaveOf
210211
*
@@ -223,10 +224,27 @@ String migrate(String host, int port, int destinationDB,
223224

224225
String memoryPurge();
225226

227+
List<Map<String, String>> getSentinelMasters();
228+
229+
List<String> getMasterAddrByName(String masterName);
230+
231+
List<Map<String, String>> sentinelSlaves(String masterName);
232+
233+
boolean monitorMaster(String masterName, String ip, int port, int quorum);
234+
235+
boolean failoverMaster(String masterName);
236+
237+
boolean sentinelRemove(String masterName);
238+
239+
Long resetConfig(String pattern);
240+
241+
boolean sentinelSet(String masterName, Map<String, String> parameterMap);
242+
226243
/**
227244
* Close client
228245
*
229246
* @return
230247
*/
231248
void close();
249+
232250
}

redis-manager-dashboard/src/main/java/com/newegg/ec/redis/client/RedisClient.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Strings;
44
import com.newegg.ec.redis.entity.*;
5+
import com.newegg.ec.redis.util.NetworkUtil;
56
import com.newegg.ec.redis.util.RedisUtil;
67
import com.newegg.ec.redis.util.SignUtil;
78
import redis.clients.jedis.*;
@@ -39,6 +40,8 @@ public class RedisClient implements IRedisClient {
3940

4041
public static final String REPLICATION = "replication";
4142

43+
public static final String SENTINEL = "sentinel";
44+
4245
private Jedis jedis;
4346

4447
private RedisURI redisURI;
@@ -137,8 +140,8 @@ public List<RedisNode> nodes() throws Exception {
137140
List<RedisNode> nodeList = new ArrayList<>();
138141
Map<String, String> infoMap = getInfo(REPLICATION);
139142
String role = infoMap.get(ROLE);
140-
String masterHost = null;
141-
Integer masterPort = null;
143+
String masterHost;
144+
Integer masterPort;
142145
// 使用 master node 进行连接
143146
if (Objects.equals(role, NodeRole.SLAVE.getValue())) {
144147
close();
@@ -255,6 +258,20 @@ public List<RedisNode> clusterNodes() throws Exception {
255258
return redisNodeList;
256259
}
257260

261+
@Override
262+
public List<RedisNode> sentinelNodes(Set<HostAndPort> hostAndPorts) {
263+
List<RedisNode> redisNodeList = new ArrayList<>();
264+
hostAndPorts.forEach(hostAndPort -> {
265+
RedisNode redisNode = RedisNode.masterRedisNode(hostAndPort);
266+
boolean run = NetworkUtil.telnet(redisNode.getHost(), redisNode.getPort());
267+
redisNode.setLinkState(run ? "connected" : "unconnected");
268+
redisNode.setNodeId(hostAndPort.toString());
269+
redisNode.setFlags("master");
270+
redisNodeList.add(redisNode);
271+
});
272+
return redisNodeList;
273+
}
274+
258275
@Override
259276
public boolean exists(String key) {
260277
return jedis.exists(key);
@@ -367,12 +384,12 @@ public Object list(DataCommandsParam dataCommandsParam) {
367384
} else if (cmd.startsWith(RPUSH)) {
368385
result = jedis.rpush(key, items);
369386
} else if (cmd.startsWith(LINDEX)) {
370-
result = jedis.lindex(key, Integer.valueOf(list[2]));
387+
result = jedis.lindex(key, Integer.parseInt(list[2]));
371388
} else if (cmd.startsWith(LLEN)) {
372389
result = jedis.llen(key);
373390
} else if (cmd.startsWith(LRANGE)) {
374-
int start = Integer.valueOf(list[2]);
375-
int stop = Integer.valueOf(list[3]);
391+
int start = Integer.parseInt(list[2]);
392+
int stop = Integer.parseInt(list[3]);
376393
result = jedis.lrange(key, start, stop);
377394
}
378395
return result;
@@ -395,7 +412,7 @@ public Object set(DataCommandsParam dataCommandsParam) {
395412
} else if (cmd.startsWith(SRANDMEMBER)) {
396413
int count = 1;
397414
if (list.length > 2) {
398-
count = Integer.valueOf(list[2]);
415+
count = Integer.parseInt(list[2]);
399416
}
400417
result = jedis.srandmember(key, count);
401418
}
@@ -419,8 +436,8 @@ public Object zset(DataCommandsParam dataCommandsParam) {
419436
} else if (cmd.startsWith(ZCOUNT)) {
420437
result = jedis.zcount(key, param1, param2);
421438
} else if (cmd.startsWith(ZRANGE)) {
422-
int start = Integer.valueOf(param1);
423-
int stop = Integer.valueOf(param2);
439+
int start = Integer.parseInt(param1);
440+
int stop = Integer.parseInt(param2);
424441
if (list.length > 4) {
425442
result = jedis.zrangeWithScores(key, start, stop);
426443
} else {
@@ -629,14 +646,53 @@ public String memoryPurge() {
629646
return null;
630647
}
631648

649+
@Override
650+
public List<Map<String, String>> getSentinelMasters() {
651+
return jedis.sentinelMasters();
652+
}
653+
654+
@Override
655+
public List<String> getMasterAddrByName(String masterName) {
656+
return jedis.sentinelGetMasterAddrByName(masterName);
657+
}
658+
659+
@Override
660+
public List<Map<String, String>> sentinelSlaves(String masterName) {
661+
return jedis.sentinelSlaves(masterName);
662+
}
663+
664+
@Override
665+
public boolean monitorMaster(String masterName, String ip, int port, int quorum) {
666+
return Objects.equals(jedis.sentinelMonitor(masterName, ip, port, quorum), OK);
667+
}
668+
669+
@Override
670+
public boolean failoverMaster(String masterName) {
671+
return Objects.equals(jedis.sentinelFailover(masterName), OK);
672+
}
673+
674+
@Override
675+
public boolean sentinelRemove(String masterName) {
676+
return Objects.equals(jedis.sentinelRemove(masterName), OK);
677+
}
678+
679+
@Override
680+
public Long resetConfig(String pattern) {
681+
return jedis.sentinelReset(pattern);
682+
}
683+
684+
@Override
685+
public boolean sentinelSet(String masterName, Map<String, String> parameterMap) {
686+
return Objects.equals(jedis.sentinelSet(masterName, parameterMap), OK);
687+
}
688+
632689
@Override
633690
public void close() {
634691
try {
635692
if (jedis != null) {
636693
jedis.close();
637694
}
638695
} catch (Exception ignored) {
639-
640696
}
641697
}
642698
}

redis-manager-dashboard/src/main/java/com/newegg/ec/redis/client/RedisClientFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import redis.clients.jedis.HostAndPort;
55
import redis.clients.jedis.exceptions.JedisConnectionException;
66

7+
import java.util.Set;
8+
79
/**
810
* Build redis client
911
*
@@ -43,4 +45,16 @@ public static RedisClient buildRedisClient(HostAndPort hostAndPort, String requi
4345
return buildRedisClient(redisURI);
4446
}
4547

48+
public static RedisClient buildRedisClient(HostAndPort hostAndPort) {
49+
return buildRedisClient(hostAndPort, null);
50+
}
51+
52+
public static RedisClient buildRedisClient(String host, int port) {
53+
return buildRedisClient(new HostAndPort(host, port));
54+
}
55+
56+
public static RedisClient buildRedisClient(Set<HostAndPort> hostAndPorts) {
57+
return buildRedisClient(hostAndPorts.iterator().next());
58+
}
59+
4660
}

redis-manager-dashboard/src/main/java/com/newegg/ec/redis/config/InitializeConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class InitializeConfiguration implements ApplicationListener<ContextRefre
5252
@Autowired
5353
private IOperationLogDao operationLogDao;
5454

55+
@Autowired
56+
private ISentinelMastersDao sentinelMastersDao;
57+
5558
@Value("${redis-manager.auth.user-name:admin}")
5659
private String userName;
5760

@@ -75,6 +78,7 @@ private void createTables() {
7578
alertRecordDao.createAlertRecordTable();
7679
redisNodeDao.createRedisNodeTable();
7780
operationLogDao.createLogTable();
81+
sentinelMastersDao.createSentinelMastersTable();
7882
}
7983

8084
@Transactional

redis-manager-dashboard/src/main/java/com/newegg/ec/redis/controller/ClusterController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.springframework.stereotype.Controller;
1515
import org.springframework.web.bind.annotation.*;
1616

17-
import javax.ws.rs.PathParam;
1817
import java.util.*;
1918

2019
/**

0 commit comments

Comments
 (0)