Skip to content

Commit

Permalink
support redis cluster dynamic deploy, for example 1 pod with 1 instance
Browse files Browse the repository at this point in the history
  • Loading branch information
githubname1024 committed Feb 1, 2024
1 parent ae0ac3a commit 00561c7
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 128 deletions.
10 changes: 5 additions & 5 deletions cachecloud-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@
<artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
Expand All @@ -105,6 +100,10 @@
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>HikariCP-java7</artifactId>
<groupId>com.zaxxer</groupId>
</exclusion>
</exclusions>
</dependency>

Expand Down Expand Up @@ -153,6 +152,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
/**
* @Author: zengyizhao
* @DateTime: 2021/9/3 13:38
* @Description: 报警记录
* @Description: 报警记录
*/
public interface AppAlertRecordDao {

/**
* 保存报警信息
* 保存报警信息
*
* @param appAlertRecord
* @return
*/
public int save(AppAlertRecord appAlertRecord);

/**
* 批量保存报警信息
* 批量保存报警信息
*
* @param appAlertRecordList
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public TaskFlowStatusEnum init() {

//masterPerMachine
masterPerMachine = MapUtils.getIntValue(paramMap, TaskConstants.MASTER_PER_MACHINE_KEY);
if (masterPerMachine <= 0 || masterPerMachine > TaskConstants.MAX_MASTER_PER_MACHINE) {
if (masterPerMachine < 0 || masterPerMachine > TaskConstants.MAX_MASTER_PER_MACHINE) {
logger.error(marker, "task {} masterPerMachine {} is wrong", taskId, masterPerMachine);
return TaskFlowStatusEnum.ABORT;
}
Expand Down Expand Up @@ -227,7 +227,12 @@ public TaskFlowStatusEnum checkResourceAllow() {
}
//兆
long memoryFree = NumberUtils.toLong(machineStats.getMemoryFree()) / 1024 / 1024;
long memoryNeed = Long.valueOf(masterPerMachine) * maxMemory;
long memoryNeed = 0L;
if (masterPerMachine == 0) {
memoryNeed = maxMemory;
} else {
memoryNeed = Long.valueOf(masterPerMachine) * maxMemory;
}
if (memoryNeed > memoryFree * 0.85) {
logger.error(marker, "{} need {} MB, but memoryFree is {} MB", redisServerIp, memoryNeed, memoryFree);
return TaskFlowStatusEnum.ABORT;
Expand Down Expand Up @@ -256,7 +261,7 @@ public TaskFlowStatusEnum checkMachineConnect() {
*/
public TaskFlowStatusEnum generateInstanceNodes() {

redisServerNodes = instancePortService.generateRedisServerNodeList(appId, appDeployInfoList, masterPerMachine, maxMemory);
redisServerNodes = instancePortService.generateRedisServerNodeListWithDeployInfo(appId, appDeployInfoList, masterPerMachine, maxMemory);
if (CollectionUtils.isEmpty(redisServerNodes)) {
logger.warn(marker, "redisServerNodes is empty, appId is {}, redisServerMachineList is {}, masterPerMachine is {}, maxMemory is {}",
appId, redisServerMachineList, masterPerMachine, maxMemory);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public interface InstancePortService {
public List<RedisServerNode> generateRedisServerNodeList(long appId, List<String> redisServerMachineList,
int masterPerMachine, int maxMemory);

/**
* 生成redis server 实例列表
* @param appId
* @param appDeployInfoList (masterIp:maxmemory:slaveIp)
* @param masterPerMachine
* @param maxMemory
* @return
*/
public List<RedisServerNode> generateRedisServerNodeListWithDeployInfo(long appId, List<String> appDeployInfoList,
int masterPerMachine, int maxMemory);

/**
* 生成redis sentinel 实例列表
* @param appId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ public void getResMachines(List<MachineMemStatInfo> machineCandi, Integer machin
/**
* @param machinelist 机器信息
* @param type 应用类型
* @param instanceNum 实例数量
* @param instanceNum 实例数量 实例数据为0时,则每个机器部署一个实例
* @param maxMemory 内存大小
* @param hasSalve 是否有从节点
* @return 添加部署Redis/Pika节点信息
Expand All @@ -956,6 +956,29 @@ public List<DeployInfo> generateInstanceInfo(List<String> machinelist, int type,
deployInfoList.add(deployInfo);
}
}
//每台机器仅部署一个实例处理逻辑
if (instanceNum == 0) {
for (int index = 0; index < machinelist.size() - 1; ) {
DeployInfo deployInfo = null;
if (DeployInfo.isRedisNode(type)) {
deployInfo = hasSalve == 1
? DeployInfo.getRedisInfo(appType, machinelist.get(index), maxMemory, machinelist.get(index + 1))
: DeployInfo.getRedisInfo(appType, machinelist.get(index), maxMemory, null);
} else {
deployInfo = hasSalve == 1
? DeployInfo.getPikaInfo(appType, machinelist.get(index), maxMemory, machinelist.get(index + 1))
: DeployInfo.getPikaInfo(appType, machinelist.get(index), maxMemory, null);
}
if (deployInfo != null) {
deployInfoList.add(deployInfo);
}
if(hasSalve == 1) {
index += 2;
} else {
index += 1;
}
}
}
}
return deployInfoList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public class InstancePortServiceImpl implements InstancePortService {
@Autowired
@Lazy
private RedisCenter redisCenter;
/**
* 1. 主从不同机器
* 2. 端口从起始端口开始自增1
*/
@Override
public List<RedisServerNode> generateRedisServerNodeList(long appId, List<String> appDeployInfoList,
int masterPerMachine, int maxMemory) {


/**
* 1. 主从不同机器
* 2. 端口从起始端口开始自增1
*/
@Override
public List<RedisServerNode> generateRedisServerNodeList(long appId, List<String> redisServerMachineList,
int masterPerMachine, int maxMemory) {

// 最终结果
List<RedisServerNode> redisServerNodeList = new ArrayList<RedisServerNode>();
Expand All @@ -83,29 +83,7 @@ public List<RedisServerNode> generateRedisServerNodeList(long appId, List<String

// 记录本次每个机器分配的port
Map<String, Set<Integer>> ipPortSetMap = new HashMap<String, Set<Integer>>();
for (int i=0; i < appDeployInfoList.size(); i++) {
String masterAndSlaveHost = appDeployInfoList.get(i);
String[] hostInfoArray = masterAndSlaveHost.split(":");
String masterHost = hostInfoArray[0];
maxMemory = Integer.parseInt(hostInfoArray[1]);
while (checkHostPortExist(ipPortSetMap, masterHost, masterPort)) {
masterPort++;
}
redisServerNodeList.add(new RedisServerNode(masterHost, masterPort, InstanceRoleEnum.MASTER.getRole(),
maxMemory, "", 0));

if (hostInfoArray.length >= 3) {
String slaveHost = hostInfoArray[2];
int slavePort = masterPort + ConstUtils.SLAVE_PORT_INCREASE;
while (checkHostPortExist(ipPortSetMap, slaveHost, slavePort)) {
slavePort++;
}
redisServerNodeList.add(new RedisServerNode(slaveHost, slavePort, InstanceRoleEnum.SLAVE.getRole(),
maxMemory, masterHost, masterPort));
}
masterPort += 1;
}
/*

for (int i = 0; i < redisServerMachineList.size(); i++) {

String masterHost = redisServerMachineList.get(i);
Expand All @@ -132,7 +110,47 @@ public List<RedisServerNode> generateRedisServerNodeList(long appId, List<String
masterPort += 1;
}
}
*/

return redisServerNodeList;
}

/**
* 1. 主从不同机器
* 2. 端口从起始端口开始自增1
*/
@Override
public List<RedisServerNode> generateRedisServerNodeListWithDeployInfo(long appId, List<String> appDeployInfoList,
int masterPerMachine, int maxMemory) {

// 最终结果
List<RedisServerNode> redisServerNodeList = new ArrayList<RedisServerNode>();
// 起始节点
int masterPort = (int) (ConstUtils.REDIS_SERVER_BASE_PORT + (appId % 10) * 10);

// 记录本次每个机器分配的port
Map<String, Set<Integer>> ipPortSetMap = new HashMap<String, Set<Integer>>();
for (int i=0; i < appDeployInfoList.size(); i++) {
String masterAndSlaveHost = appDeployInfoList.get(i);
String[] hostInfoArray = masterAndSlaveHost.split(":");
String masterHost = hostInfoArray[0];
maxMemory = Integer.parseInt(hostInfoArray[1]);
while (checkHostPortExist(ipPortSetMap, masterHost, masterPort)) {
masterPort++;
}
redisServerNodeList.add(new RedisServerNode(masterHost, masterPort, InstanceRoleEnum.MASTER.getRole(),
maxMemory, "", 0));

if (hostInfoArray.length >= 3) {
String slaveHost = hostInfoArray[2];
int slavePort = masterPort + ConstUtils.SLAVE_PORT_INCREASE;
while (checkHostPortExist(ipPortSetMap, slaveHost, slavePort)) {
slavePort++;
}
redisServerNodeList.add(new RedisServerNode(slaveHost, slavePort, InstanceRoleEnum.SLAVE.getRole(),
maxMemory, masterHost, masterPort));
}
masterPort += 1;
}
return redisServerNodeList;
}

Expand Down
2 changes: 1 addition & 1 deletion cachecloud-web/src/main/resources/spring/spring-quartz.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<props>
<prop key="org.quartz.scheduler.instanceName">CacheCloudScheduler</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<prop key="org.quartz.jobStore.class">org.springframework.scheduling.quartz.LocalDataSourceJobStore</prop>
<prop key="org.quartz.scheduler.batchTriggerAcquisitionMaxCount">200</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
<!-- 表名前缀 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ a.下载war包:

[3.1版本 cachecloud-web.war](http://43.137.44.6/redis-ecs/3.1/cachecloud-web.war)

[3.2版本 cachecloud-web.war](http://43.137.44.6/redis-ecs/3.2/cachecloud-web.war)

b.也可以clone源码,自行打包:

//clone项目
git clone https://github.com/sohutv/cachecloud.git
//切换到指定分支
git checkout 3.2
//修改指定应用和数据库配置
[参见 三、启动工程](#cc3)
//打包
mvn clean package

Expand All @@ -44,12 +50,13 @@ b.也可以clone源码,自行打包:
user: xxx
password: xxx

导入建表文件:/cachecloud-open/cachecloud-web/sql/cc2.0.sql
导入建表文件:/cachecloud-web/sql/cc3.2.sql
(根据指定版本导入,如为升级,请执行update {源版本} to {目标版本}.sql文件)

<a name="cc3"/>

### 三、启动工程
准备配置文件:application-open.yml,放到war包同目录下
准备配置文件:cachecloud-web/src/main/resources/application-open.yml,修改指定应用和数据库配置

#配置应用名称
spring:
Expand Down
Loading

0 comments on commit 00561c7

Please sign in to comment.