Skip to content

Commit

Permalink
Merge pull request #923 from ctripcorp/feature/keeper_locate
Browse files Browse the repository at this point in the history
新增keepe详情页面,shard查询页面
  • Loading branch information
LanternLee authored Dec 17, 2024
2 parents 0170e35 + b277c78 commit 59b2822
Show file tree
Hide file tree
Showing 25 changed files with 577 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.ctrip.xpipe.redis.console.controller.consoleportal;

import com.ctrip.xpipe.redis.checker.controller.result.RetMessage;
import com.ctrip.xpipe.redis.checker.model.DcClusterShard;
import com.ctrip.xpipe.redis.checker.model.KeeperContainerUsedInfoModel;
import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController;
import com.ctrip.xpipe.redis.console.keeper.KeeperContainerUsedInfoAnalyzer;
import com.ctrip.xpipe.redis.console.keeper.entity.KeeperContainerDiskType;
import com.ctrip.xpipe.redis.console.model.ConfigModel;
import com.ctrip.xpipe.redis.console.model.KeeperContainerInfoModel;
import com.ctrip.xpipe.redis.console.model.KeepercontainerTbl;
import com.ctrip.xpipe.redis.console.model.KeeperMsgModel;
import com.ctrip.xpipe.redis.console.model.MigrationKeeperContainerDetailModel;
import com.ctrip.xpipe.redis.console.service.ConfigService;
import com.ctrip.xpipe.redis.console.service.KeeperContainerMigrationService;
Expand Down Expand Up @@ -132,4 +131,13 @@ public Set<String> getAllDiskTypeName() {
}
}

@RequestMapping(value = "/keepercontainer/keepers/{ip}")
public List<KeeperMsgModel> getLocateKeeperContainerByIpAndPort(@PathVariable String ip) {
try {
return keeperContainerService.getAllKeepers(ip);
} catch (Exception e) {
return Collections.emptyList();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ctrip.xpipe.redis.console.controller.consoleportal;

import com.ctrip.xpipe.redis.checker.controller.result.GenericRetMessage;
import com.ctrip.xpipe.redis.checker.controller.result.RetMessage;
import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController;
import com.ctrip.xpipe.redis.console.model.ShardModel;
import com.ctrip.xpipe.redis.console.model.ShardTbl;
Expand Down Expand Up @@ -62,4 +64,24 @@ public List<ShardListModel> findAllUnhealthyShards() {
return valueOrEmptySet(ShardListModel.class, shardService.findAllUnhealthy());
}

@RequestMapping(value = "/shards/allById/{id}", method = RequestMethod.GET)
public RetMessage findAllShardsById(@PathVariable long id) {
try {
List<ShardListModel> result = new ArrayList<>();
result.add(shardService.findByReplId(id));
return GenericRetMessage.createGenericRetMessage(result);
} catch (Exception e) {
return RetMessage.createFailMessage(e.getMessage());
}
}

@RequestMapping(value = "/shards/allByName/{shardName}", method = RequestMethod.GET)
public RetMessage findAllShardsByShardName(@PathVariable String shardName) {
try {
return GenericRetMessage.createGenericRetMessage(shardService.findAllByShardName(shardName));
} catch (Exception e) {
return RetMessage.createFailMessage(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.ctrip.xpipe.redis.console.model;

public class KeeperMsgModel {

private String ip;
private int port;
private String clusterName;
private String shardName;
private boolean active;
private String role;
private String err;

public KeeperMsgModel(String ip, int port, String clusterName, String shardName) {
this.ip = ip;
this.port = port;
this.clusterName = clusterName;
this.shardName = shardName;
}

public KeeperMsgModel(String clusterName, String shardName) {
this.clusterName = clusterName;
this.shardName = shardName;
}

public KeeperMsgModel(String err) {
this.err = err;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getClusterName() {
return clusterName;
}

public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}

public String getShardName() {
return shardName;
}

public void setShardName(String shardName) {
this.shardName = shardName;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

public String getErr() {
return err;
}

public void setErr(String err) {
this.err = err;
}

public void addErr(String err) {
this.err += err;
}

@Override
public String toString() {
return "KeeperMsgModel{" +
"ip='" + ip + '\'' +
", port=" + port +
", clusterName='" + clusterName + '\'' +
", shardName='" + shardName + '\'' +
", active=" + active +
", role='" + role + '\'' +
", err='" + err + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class ShardListModel extends AbstractClusterModel {

private String shardName;

private Long shardId;

private String clusterType;

private List<String> dcNames = new ArrayList<>();
Expand All @@ -22,6 +24,15 @@ public ShardListModel setShardName(String shardName) {
return this;
}

public Long getShardId() {
return shardId;
}

public ShardListModel setShardId(Long shardId) {
this.shardId = shardId;
return this;
}

public List<String> getDcNames() {
return dcNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public interface DcClusterService {
DcClusterTbl find(long dcId, long clusterId);
DcClusterTbl find(String dcName, String clusterName);
DcClusterTbl findByPK(long keyDcClusterId);
DcClusterCreateInfo findDcClusterCreateInfo(final String dcName, final String clusterName);
void updateDcCluster(DcClusterCreateInfo dcClusterCreateInfo);
List<DcClusterTbl> findAllDcClusters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.ctrip.xpipe.redis.console.controller.api.data.meta.KeeperContainerCreateInfo;
import com.ctrip.xpipe.redis.console.model.KeeperContainerInfoModel;
import com.ctrip.xpipe.redis.console.model.KeeperMsgModel;
import com.ctrip.xpipe.redis.console.model.KeepercontainerTbl;
import com.ctrip.xpipe.redis.core.entity.KeeperInstanceMeta;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -41,4 +41,6 @@ public interface KeeperContainerService {
void updateKeeperContainerByInfoModel(KeeperContainerInfoModel keeperContainerInfoModel);

Map<Long, Long> keeperContainerIdDcMap();

List<KeeperMsgModel> getAllKeepers(String keeperIp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface RedisService {

List<RedisTbl> findAllRedisWithSameIP(String ip);

List<RedisTbl> findAllRedisByIp(String ip);

List<RedisTbl> findAllByDcClusterShard(long dcClusterShardId);

List<RedisTbl> findAllRedisesByDcClusterName(String dcId, String clusterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface ShardService {
ShardTbl find(String clusterName, String shardName);
List<ShardTbl> findAllByClusterName(String clusterName);
List<ShardTbl> findAllShardNamesByClusterName(String clusterName);
List<ShardListModel> findAllByShardName(String shardName);
ShardListModel findByReplId(long replId);
ShardTbl createShard(String clusterName, ShardTbl shard, Map<Long, SentinelGroupModel> sentinels);
ShardTbl findOrCreateShardIfNotExist(String clusterName, ShardTbl shard, List<DcClusterTbl> dcClusterTbls, Map<Long, SentinelGroupModel> sentinels);
void deleteShard(String clusterName, String shardName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public DcClusterTbl doQuery() throws DalException {
});
}

@Override
public DcClusterTbl findByPK(long keyDcClusterId) {
return queryHandler.handleQuery(new DalQuery<DcClusterTbl>() {
@Override
public DcClusterTbl doQuery() throws DalException {
return dao.findByPK(keyDcClusterId, DcClusterTblEntity.READSET_FULL);
}
});
}

@Override
public DcClusterCreateInfo findDcClusterCreateInfo(final String dcName, final String clusterName) {
DcClusterTbl dcClusterTbl = find(dcName, clusterName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ctrip.xpipe.redis.console.service.impl;

import com.ctrip.xpipe.command.AbstractCommand;
import com.ctrip.xpipe.endpoint.HostPort;
import com.ctrip.xpipe.exception.XpipeRuntimeException;
import com.ctrip.xpipe.redis.checker.model.KeeperContainerUsedInfoModel;
Expand Down Expand Up @@ -32,10 +33,14 @@
import org.springframework.web.client.RestOperations;
import org.unidal.dal.jdbc.DalException;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.ctrip.xpipe.spring.AbstractSpringConfigContext.SCHEDULED_EXECUTOR;

@Service
@Conditional(ConsoleDisableDbCondition.class)
@DisableDbMode(false)
Expand All @@ -54,11 +59,21 @@ public class KeeperContainerServiceImpl extends AbstractConsoleService<Keepercon
@Autowired
private RedisService redisService;

@Autowired
private DcClusterShardService dcClusterShardService;

@Autowired
private DcClusterService dcClusterService;

@Autowired
private AzService azService;

@Autowired
private ShardService shardService;

private RestOperations restTemplate;


@Override
public KeepercontainerTbl find(final long id) {
return queryHandler.handleQuery(new DalQuery<KeepercontainerTbl>() {
Expand Down Expand Up @@ -470,6 +485,56 @@ public Map<Long, Long> keeperContainerIdDcMap() {
return keeperContainerIdDcMap;
}

@Override
public List<KeeperMsgModel> getAllKeepers(String keeperIp) {
List<RedisTbl> keepers = redisService.findAllRedisByIp(keeperIp);
if (keepers.isEmpty()) return Collections.emptyList();
List<KeeperMsgModel> result = new ArrayList<>();
keepers.forEach(keeper -> {
try {
KeeperMsgModel keeperMsg = getKeeperMsg(keeper.getDcClusterShardId());
keeperMsg.setIp(keeper.getRedisIp());
keeperMsg.setPort(keeper.getRedisPort());
keeperMsg.setRole(keeper.getRedisRole());
if (XPipeConsoleConstant.ROLE_KEEPER.equals(keeper.getRedisRole())) {
keeperMsg.setActive(keeper.isKeeperActive());
} else if(XPipeConsoleConstant.ROLE_REDIS.equals(keeper.getRedisRole())) {
keeperMsg.setActive(keeper.isMaster());
} else {
keeperMsg.setErr("Instance role not in redis and keeper!");
}
result.add(keeperMsg);
} catch (Exception e) {
KeeperMsgModel keeperMsg = new KeeperMsgModel(e.getMessage());
keeperMsg.setIp(keeper.getRedisIp());
keeperMsg.setPort(keeper.getRedisPort());
keeperMsg.setRole(keeper.getRedisRole());
if (XPipeConsoleConstant.ROLE_KEEPER.equals(keeper.getRedisRole())) {
keeperMsg.setActive(keeper.isKeeperActive());
} else if(XPipeConsoleConstant.ROLE_REDIS.equals(keeper.getRedisRole())) {
keeperMsg.setActive(keeper.isMaster());
} else {
keeperMsg.addErr(" and Instance role not in redis and keeper!");
}
result.add(keeperMsg);
}
});

return result;
}

private KeeperMsgModel getKeeperMsg(long dcClusterShardId) {
DcClusterShardTbl dcClusterShardTbl = dcClusterShardService.findByPk(dcClusterShardId);
if (dcClusterShardTbl == null) return new KeeperMsgModel("Can't find dcClusterShardTbl by dcClusterShardId:" + dcClusterShardId);
DcClusterTbl dcClusterTbl = dcClusterService.findByPK(dcClusterShardTbl.getDcClusterId());
if (dcClusterTbl == null) return new KeeperMsgModel("Can't find dcClusterTbl by dcClusterId:" + dcClusterShardTbl.getDcClusterId());
ClusterTbl clusterTbl = clusterService.find(dcClusterTbl.getClusterId());
if (clusterTbl == null) return new KeeperMsgModel("Can't find clusterTbl by clusterId:" + dcClusterTbl.getClusterId());
ShardTbl shardTbl = shardService.find(dcClusterShardTbl.getShardId());
if (shardTbl == null) return new KeeperMsgModel("Can't find shardTbl by shardId:" + dcClusterShardTbl.getShardId());
return new KeeperMsgModel(clusterTbl.getClusterName(), shardTbl.getShardName());
}

@Override
public List<Set<Long>> divideKeeperContainers(int partsCount) {
List<KeepercontainerTbl> all = findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ctrip.xpipe.redis.console.config.ConsoleConfig;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.KeeperContainerCreateInfo;
import com.ctrip.xpipe.redis.console.model.KeeperContainerInfoModel;
import com.ctrip.xpipe.redis.console.model.KeeperMsgModel;
import com.ctrip.xpipe.redis.console.model.KeepercontainerTbl;
import com.ctrip.xpipe.redis.console.resources.ConsolePortalService;
import com.ctrip.xpipe.redis.console.service.KeeperContainerService;
Expand Down Expand Up @@ -191,4 +192,9 @@ public Map<Long, Long> keeperContainerIdDcMap() {
return keeperContainerIdDcMap;
}

@Override
public List<KeeperMsgModel> getAllKeepers(String keeperIp) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public List<RedisTbl> doQuery() throws DalException {
});
}

@Override
public List<RedisTbl> findAllRedisByIp(String ip) {
return queryHandler.handleQuery(new DalQuery<List<RedisTbl>>() {
@Override
public List<RedisTbl> doQuery() throws DalException {
return dao.findByIp(ip, RedisTblEntity.READSET_REDIS_MSG_INFO);
}
});
}

@Override
public List<RedisTbl> findAllByDcClusterShard(final long dcClusterShardId) {
return queryHandler.handleQuery(new DalQuery<List<RedisTbl>>() {
Expand Down
Loading

0 comments on commit 59b2822

Please sign in to comment.