Skip to content

Commit

Permalink
add api getting cluster with shards (#751)
Browse files Browse the repository at this point in the history
Co-authored-by: lishanglin <[email protected]>
  • Loading branch information
LanternLee and lishanglin authored Nov 23, 2023
1 parent 80b56f6 commit a6c247b
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import com.ctrip.xpipe.redis.checker.controller.result.RetMessage;
import com.ctrip.xpipe.redis.console.config.ConsoleConfig;
import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.CheckFailException;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.ClusterCreateInfo;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.ClusterExchangeNameInfo;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.ClusterRegionExchangeInfo;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.DcDetailInfo;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.RegionInfo;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.*;
import com.ctrip.xpipe.redis.console.dto.AzGroupDTO;
import com.ctrip.xpipe.redis.console.dto.ClusterCreateDTO;
import com.ctrip.xpipe.redis.console.dto.ClusterDTO;
Expand Down Expand Up @@ -42,11 +37,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

@RestController
Expand Down Expand Up @@ -173,6 +164,18 @@ public List<ClusterCreateInfo> getClusters(@RequestParam(required=false, default
return transformFromInner(clusterCreateInfoList);
}

@GetMapping(value = "/clusterWithShards")
public List<ClusterInfo> getClusterShards(@RequestParam(required=false, defaultValue = "one_way", name = "type") String clusterType) throws CheckFailException {
logger.info("[clusterWithShards]clusterType-{}", clusterType);
if (!ClusterType.isTypeValidate(clusterType)) {
throw new CheckFailException("unknown cluster type " + clusterType);
}

List<ClusterDTO> clusterDTOList = clusterService.getClusterWithShards(clusterType);
List<ClusterInfo> clusterInfos = clusterDTOList.stream().map(ClusterInfo::new).collect(Collectors.toList());
return clusterInfos;
}


@PutMapping(value = "/cluster/exchangename")
public RetMessage exchangeName(@RequestBody ClusterExchangeNameInfo exchangeNameInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,63 +41,6 @@ public class ClusterCreateInfo extends AbstractCreateInfo{
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<RegionInfo> regions = new LinkedList<>();

// public static String innerGroupType2OuterGroupType(String groupType) {
// if(DcGroupType.isSameGroupType(groupType, DcGroupType.DR_MASTER)){
// return "drMaster";
// }else if(DcGroupType.isSameGroupType(groupType, DcGroupType.MASTER)) {
// return "master";
// }
// return "drMaster";
// }

// public static DcGroupType outerGroupType2InnerGroupType(String groupType) {
// if(null == groupType || "drMaster".equals(groupType)){
// return DcGroupType.DR_MASTER;
// }else if("master".equals(groupType)){
// return DcGroupType.MASTER;
// }
// throw new IllegalArgumentException("unknown group type:"+groupType);
// }

// public static ClusterCreateInfo fromClusterTbl(ClusterTbl clusterTbl, List<DcTbl> clusterRelatedDc,
// Map<Long, List<DcClusterTbl>> clusterId2DcClusterTblsMap) {
// ClusterCreateInfo clusterCreateInfo = new ClusterCreateInfo();
//
// clusterCreateInfo.setClusterId(clusterTbl.getId());
// clusterCreateInfo.setDesc(clusterTbl.getClusterDescription());
// clusterCreateInfo.setClusterName(clusterTbl.getClusterName());
// clusterCreateInfo.setClusterType(clusterTbl.getClusterType());
// OrganizationTbl organizationTbl = clusterTbl.getOrganizationInfo();
// clusterCreateInfo.setOrganizationId(organizationTbl != null ? organizationTbl.getOrgId() : 0L);
// clusterCreateInfo.setClusterAdminEmails(clusterTbl.getClusterAdminEmails());
//
// Map<Long, String> dcId2DcNameMap = new HashMap<>();
// clusterRelatedDc.forEach(dcTbl -> {
// dcId2DcNameMap.put(dcTbl.getId(), dcTbl.getDcName());
// if (dcTbl.getId() == clusterTbl.getActivedcId()) {
// clusterCreateInfo.addFirstDc(dcTbl.getDcName());
// } else {
// clusterCreateInfo.addDc(dcTbl.getDcName());
// }
// });
//
// List<DcClusterTbl> dcClusterTbls = clusterId2DcClusterTblsMap.get(clusterTbl.getId());
// dcClusterTbls.forEach(dcClusterTbl -> {
// DcDetailInfo dcDetailInfo = new DcDetailInfo()
// .setDcId(dcId2DcNameMap.get(dcClusterTbl.getDcId()))
// .setDcGroupName(dcClusterTbl.getGroupName())
// .setDcGroupType(innerGroupType2OuterGroupType(dcClusterTbl.getGroupType()));
// if (dcClusterTbl.getDcId() == clusterTbl.getActivedcId()) {
// clusterCreateInfo.addFirstDcDetail(dcDetailInfo);
// } else {
// clusterCreateInfo.addDcDetail(dcDetailInfo);
// }
// });
//
//
// return clusterCreateInfo;
// }

public ClusterCreateInfo(){
}

Expand Down Expand Up @@ -135,30 +78,6 @@ public ClusterCreateInfo(ClusterDTO clusterDTO) {
}).collect(Collectors.toList());
}

// TODO:找DBA确认是否在使用dcDetails字段,若不使用,可不生成dcDetails信息
// if (!CollectionUtils.isEmpty(this.regions)) {
// for (RegionInfo regionInfo : this.regions) {
// String regionClusterType = regionInfo.getClusterType();
// if (ClusterType.isSameClusterType(regionClusterType, ClusterType.ONE_WAY)) {
// for (String az : regionInfo.getAzs()) {
// DcDetailInfo detailInfo = new DcDetailInfo();
// detailInfo.setDcId(az);
// detailInfo.setDcGroupType(innerGroupType2OuterGroupType(DcGroupType.DR_MASTER.name()));
// this.dcDetails.add(detailInfo);
// }
// } else if (ClusterType.isSameClusterType(regionClusterType, ClusterType.SINGLE_DC)) {
//
// }
// if (regionInfo.get)
// }
// this.dcs.forEach(dc -> {
// DcDetailInfo dcDetailInfo = new DcDetailInfo();
// dcDetailInfo.setDcId(dc);
// dcDetailInfo.setDcGroupName();
// dcDetailInfo.setDcGroupType();
//
// });
// }
}

public Long getClusterId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.ctrip.xpipe.redis.console.controller.api.data.meta;

import com.ctrip.xpipe.redis.console.dto.ClusterDTO;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author lishanglin
* date 2023/11/22
*/
public class ClusterInfo {

private long id;

private String clusterName;

private String clusterType;

private Set<ShardInfo> shards;

public ClusterInfo() {

}

public ClusterInfo(ClusterDTO clusterDTO) {
this.id = clusterDTO.getClusterId();
this.clusterName = clusterDTO.getClusterName();
this.clusterType = clusterDTO.getClusterType();
if (null != clusterDTO.getShards()) {
this.shards = clusterDTO.getShards().stream().map(ShardInfo::new).collect(Collectors.toSet());
}
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getClusterName() {
return clusterName;
}

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

public String getClusterType() {
return clusterType;
}

public void setClusterType(String clusterType) {
this.clusterType = clusterType;
}

public Set<ShardInfo> getShards() {
return shards;
}

public void setShards(Set<ShardInfo> shards) {
this.shards = shards;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClusterInfo that = (ClusterInfo) o;
return id == that.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ctrip.xpipe.redis.console.controller.api.data.meta;

import com.ctrip.xpipe.redis.console.dto.ShardDTO;

import java.util.Objects;

/**
* @author lishanglin
* date 2023/11/22
*/
public class ShardInfo {

private long id;

private String shardName;

public ShardInfo() {

}

public ShardInfo(ShardDTO shardDTO) {
this.id = shardDTO.getShardId();
this.shardName = shardDTO.getShardName();
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getShardName() {
return shardName;
}

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ShardInfo shardInfo = (ShardInfo) o;
return id == shardInfo.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.ctrip.xpipe.redis.console.model.ClusterTbl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ClusterDTO {

Expand All @@ -16,6 +18,7 @@ public class ClusterDTO {
private String adminEmails;
private List<String> azs;
private List<AzGroupDTO> azGroups;
private Set<ShardDTO> shards;

public ClusterDTO() {
}
Expand Down Expand Up @@ -107,4 +110,35 @@ public List<AzGroupDTO> getAzGroups() {
public void setAzGroups(List<AzGroupDTO> azGroups) {
this.azGroups = azGroups;
}

public Set<ShardDTO> getShards() {
return shards;
}

public ClusterDTO addShard(ShardDTO shard) {
if (null == this.shards) this.shards = new HashSet<>();
this.shards.add(shard);
return this;
}

public void setShards(Set<ShardDTO> shards) {
this.shards = shards;
}

@Override
public String toString() {
return "ClusterDTO{" +
"clusterId=" + clusterId +
", clusterName='" + clusterName + '\'' +
", clusterType='" + clusterType + '\'' +
", activeAz='" + activeAz + '\'' +
", description='" + description + '\'' +
", orgName='" + orgName + '\'' +
", cmsOrgId=" + cmsOrgId +
", adminEmails='" + adminEmails + '\'' +
", azs=" + azs +
", azGroups=" + azGroups +
", shards=" + shards +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.ctrip.xpipe.redis.console.dto;

import com.ctrip.xpipe.redis.console.model.ShardTbl;
import com.ctrip.xpipe.redis.core.entity.ShardMeta;

import java.util.Objects;

/**
* @author lishanglin
* date 2023/11/22
*/
public class ShardDTO {

private Long shardId;

private String shardName;

public ShardDTO() {

}

public ShardDTO(ShardTbl shardTbl) {
this.shardId = shardTbl.getId();
this.shardName = shardTbl.getShardName();
}

public ShardDTO(ShardMeta shardMeta) {
this.shardId = shardMeta.getDbId();
this.shardName = shardMeta.getId();
}

public Long getShardId() {
return shardId;
}

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

public String getShardName() {
return shardName;
}

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ShardDTO shardDTO = (ShardDTO) o;
return shardId.equals(shardDTO.shardId);
}

@Override
public int hashCode() {
return Objects.hash(shardId);
}

@Override
public String toString() {
return "ShardDTO{" +
"shardId=" + shardId +
", shardName='" + shardName + '\'' +
'}';
}
}
Loading

0 comments on commit a6c247b

Please sign in to comment.