Skip to content

Commit 2ff94eb

Browse files
support appoint maxmemory policy
and make it work through app lifecycle
1 parent 0ea9763 commit 2ff94eb

File tree

25 files changed

+1540
-25
lines changed

25 files changed

+1540
-25
lines changed

cachecloud-web/sql/3.1.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ CREATE TABLE `system_resource` (
11361136
-- Records of `system_resource`
11371137
-- ----------------------------
11381138
BEGIN;
1139-
INSERT INTO `system_resource1` VALUES (1,'cachecloud-init.sh','容器初始化脚本',2,'2020-07-15 18:35:41','/script','',0,1,NULL,NULL,NULL,0),(2,'x.x.x.x',NULL,1,'2020-08-10 10:31:51','/opt/download/software/cachecloud/resource','http://x.x.x.x/software/cachecloud/resource',0,1,'admin',0,NULL,0),(4,'cachecloud-env.sh','宿主环境脚本',2,'2020-07-15 18:36:28','/script','',0,1,NULL,NULL,NULL,0),(5,'id_rsa','私钥文件',4,'2020-07-07 10:45:39','/ssh','',0,1,NULL,NULL,NULL,0),(6,'id_rsa.pub','公钥文件',4,'2020-07-07 10:45:45','/ssh','',0,1,NULL,NULL,NULL,0),(12,'redis-4.0.14','redis 4.0.14资源包',3,'2020-08-10 09:52:41','/redis','http://download.redis.io/releases/redis-4.0.14.tar.gz',0,1,'admin',532,NULL,0),(21,'/script','脚本目录管理',6,'2020-08-10 10:51:34','',NULL,0,1,'admin',0,NULL,0),(28,'/ssh','ssh目录',6,'2020-07-20 17:55:03',NULL,NULL,0,1,'admin',0,NULL,0),(29,'redis-3.0.7','redis3.0.7 资源包',3,'2020-08-10 09:53:32','/redis','http://download.redis.io/releases/redis-3.0.7.tar.gz',0,1,'admin',529,NULL,0),(31,'redis-3.2.12','redis 3.2.12 资源包',3,'2020-08-10 15:08:21','/redis','http://download.redis.io/releases/redis-3.2.12.tar.gz',0,1,'admin',530,NULL,0),(32,'/redis','redis资源包管理',6,'2020-07-20 17:54:59',NULL,NULL,0,1,'admin',0,NULL,0),(33,'/tool','迁移工具资源包',6,'2020-07-20 17:54:53',NULL,NULL,0,1,'admin',0,NULL,0),(37,'redis-5.0.9','redis5.0.9 资源包',3,'2020-08-10 09:51:41','/redis','http://download.redis.io/releases/redis-5.0.9.tar.gz',0,1,'admin',533,NULL,0),(40,'redis-shake-2.0.3','redis 2.0.3\n修复fix 5.0迁移类型问题',7,'2020-08-11 10:53:26','/tool','https://github.com/alibaba/RedisShake/releases/download/release-v2.0.3-20200724/redis-shake-v2.0.3.tar.gz',0,1,'admin',518,NULL,0);
1139+
INSERT INTO `system_resource` VALUES (1,'cachecloud-init.sh','容器初始化脚本',2,'2020-07-15 18:35:41','/script','',0,1,NULL,NULL,NULL,0),(2,'x.x.x.x',NULL,1,'2020-08-10 10:31:51','/opt/download/software/cachecloud/resource','http://x.x.x.x/software/cachecloud/resource',0,1,'admin',0,NULL,0),(4,'cachecloud-env.sh','宿主环境脚本',2,'2020-07-15 18:36:28','/script','',0,1,NULL,NULL,NULL,0),(5,'id_rsa','私钥文件',4,'2020-07-07 10:45:39','/ssh','',0,1,NULL,NULL,NULL,0),(6,'id_rsa.pub','公钥文件',4,'2020-07-07 10:45:45','/ssh','',0,1,NULL,NULL,NULL,0),(12,'redis-4.0.14','redis 4.0.14资源包',3,'2020-08-10 09:52:41','/redis','http://download.redis.io/releases/redis-4.0.14.tar.gz',0,1,'admin',532,NULL,0),(21,'/script','脚本目录管理',6,'2020-08-10 10:51:34','',NULL,0,1,'admin',0,NULL,0),(28,'/ssh','ssh目录',6,'2020-07-20 17:55:03',NULL,NULL,0,1,'admin',0,NULL,0),(29,'redis-3.0.7','redis3.0.7 资源包',3,'2020-08-10 09:53:32','/redis','http://download.redis.io/releases/redis-3.0.7.tar.gz',0,1,'admin',529,NULL,0),(31,'redis-3.2.12','redis 3.2.12 资源包',3,'2020-08-10 15:08:21','/redis','http://download.redis.io/releases/redis-3.2.12.tar.gz',0,1,'admin',530,NULL,0),(32,'/redis','redis资源包管理',6,'2020-07-20 17:54:59',NULL,NULL,0,1,'admin',0,NULL,0),(33,'/tool','迁移工具资源包',6,'2020-07-20 17:54:53',NULL,NULL,0,1,'admin',0,NULL,0),(37,'redis-5.0.9','redis5.0.9 资源包',3,'2020-08-10 09:51:41','/redis','http://download.redis.io/releases/redis-5.0.9.tar.gz',0,1,'admin',533,NULL,0),(40,'redis-shake-2.0.3','redis 2.0.3\n修复fix 5.0迁移类型问题',7,'2020-08-11 10:53:26','/tool','https://github.com/alibaba/RedisShake/releases/download/release-v2.0.3-20200724/redis-shake-v2.0.3.tar.gz',0,1,'admin',518,NULL,0);
11401140
COMMIT;
11411141

11421142
--

cachecloud-web/sql/3.2.sql

Lines changed: 1353 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- app_desc change
2+
ALTER TABLE app_desc ADD maxmemory_policy tinyint(4) DEFAULT NULL COMMENT '淘汰策略(0:noeviction; 1:allkeys-lru;2:allkeys-lfu;3:volatile-lru;4:volatile-lfu;5:allkeys-random;6:volatile-random;7:volatile-ttl)';

cachecloud-web/src/main/java/com/sohu/cache/constant/AppDescEnum.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.sohu.cache.constant;
22

3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
37
/**
48
* app详情枚举(以后有关app详情的都往这里面迁)
59
*
@@ -58,4 +62,55 @@ public String getInfo() {
5862
}
5963

6064

65+
/**
66+
* 应用内存淘汰策略
67+
*/
68+
public static enum MaxmemoryPolicyType {
69+
NOEVICTION(0, "noeviction","不淘汰,占满写入失败"),
70+
ALLKEYSLRU(1, "allkeys-lru","所有键-最近最少使用"),
71+
ALLKEYSLFU(2, "allkeys-lfu","所有键-最少频率使用"),
72+
VOLATILELRU(3, "volatile-lru","有过期时间的键-最近最少使用"),
73+
VOLATILELFU(4, "volatile-lfu","有过期时间的键-最少频率使用"),
74+
ALLKEYSRANDOM(5, "allkeys-random","所有键-随机"),
75+
VOLATILERANDOM(6, "volatile-random","有过期时间的键-随机"),
76+
VOLATILETTL(7, "volatile-ttl","有过期时间的键-剩余时间最短");
77+
78+
private int type;
79+
80+
private String name;
81+
82+
private String desc;
83+
84+
private MaxmemoryPolicyType(int type, String name, String desc) {
85+
this.type = type;
86+
this.name = name;
87+
this.desc = desc;
88+
}
89+
90+
public int getType(){
91+
return type;
92+
}
93+
94+
public String getName(){
95+
return name;
96+
}
97+
98+
public String getDesc(){
99+
return desc;
100+
}
101+
102+
public static MaxmemoryPolicyType getByType(int type){
103+
Optional<MaxmemoryPolicyType> policyTypeOptional = Arrays.asList(MaxmemoryPolicyType.values()).stream().filter(maxmemoryPolicyType -> maxmemoryPolicyType.type == type).findFirst();
104+
if(policyTypeOptional.isPresent()){
105+
return policyTypeOptional.get();
106+
}
107+
return null;
108+
}
109+
110+
public static List<MaxmemoryPolicyType> getAll(){
111+
MaxmemoryPolicyType[] values = MaxmemoryPolicyType.values();
112+
return Arrays.asList(values);
113+
}
114+
}
115+
61116
}

cachecloud-web/src/main/java/com/sohu/cache/entity/AppDesc.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ public class AppDesc implements Serializable {
162162
*/
163163
private String customPassword;
164164

165+
/**
166+
* 最大内存淘汰策略
167+
* 见AppDescEnum.MaxmemoryPolicyType
168+
*/
169+
private Integer maxmemoryPolicy;
165170
public String getAuthPassword() {
166171
if(StringUtils.isNotBlank(customPassword)){
167172
return customPassword;
@@ -309,4 +314,15 @@ public Date getPassedTime() {
309314
public void setPassedTime(Date passedTime) {
310315
this.passedTime = (Date) passedTime.clone();
311316
}
317+
318+
public String getMaxmemoryPolicyDesc() {
319+
if(maxmemoryPolicy != null){
320+
AppDescEnum.MaxmemoryPolicyType policyType = AppDescEnum.MaxmemoryPolicyType.getByType(maxmemoryPolicy);
321+
if(policyType != null){
322+
return policyType.getName();
323+
324+
}
325+
}
326+
return null;
327+
}
312328
}

cachecloud-web/src/main/java/com/sohu/cache/log/CustLogAppenderInit.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
import ch.qos.logback.classic.LoggerContext;
55
import org.slf4j.LoggerFactory;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.ApplicationArguments;
8+
import org.springframework.boot.ApplicationRunner;
79
import org.springframework.stereotype.Component;
810

9-
import javax.annotation.PostConstruct;
10-
1111
@Component
12-
public class CustLogAppenderInit {
12+
public class CustLogAppenderInit implements ApplicationRunner{
1313

1414
@Autowired
1515
private TaskFlowRecordAppender taskFlowRecordAppender;
1616

1717
private final org.slf4j.Logger logger = LoggerFactory.getLogger(CustLogAppenderInit.class);
1818

19-
@PostConstruct
20-
public void init() {
19+
@Override
20+
public void run(ApplicationArguments args) throws Exception {
2121
logger.warn("custLogAppender init begin!");
2222
if (LoggerFactory.getILoggerFactory() instanceof LoggerContext) {
2323
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
@@ -32,5 +32,4 @@ public void init() {
3232
LoggerFactory.getILoggerFactory());
3333
}
3434
}
35-
3635
}

cachecloud-web/src/main/java/com/sohu/cache/redis/RedisConfigTemplateService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ public interface RedisConfigTemplateService {
8787
* 普通节点配置
8888
* @param port
8989
* @param maxMemory
90+
* @param maxMemoryPolicy
9091
* @param versionId Redis版本
9192
* @return 普通配置列表
9293
*/
93-
List<String> handleCommonConfig(String host, int port, int maxMemory, int versionId);
94+
List<String> handleCommonConfig(String host, int port, int maxMemory, String maxMemoryPolicy, int versionId);
9495

9596
/**
9697
* sentinel节点配置(兼容k8s)

cachecloud-web/src/main/java/com/sohu/cache/redis/impl/RedisCenterImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,9 @@ public Object executeAdminCommand(AppDesc appDesc, ProtocolCommand command, Stri
15631563
if(instanceInfo.getType() == ConstUtils.CACHE_REDIS_SENTINEL){
15641564
continue;
15651565
}
1566+
if(instanceInfo.isOffline()){
1567+
continue;
1568+
}
15661569
host = instanceInfo.getIp();
15671570
port = instanceInfo.getPort();
15681571
BooleanEnum isMaster = this.isMaster(appId, host, port);

cachecloud-web/src/main/java/com/sohu/cache/redis/impl/RedisConfigTemplateServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public int updateStatus(long id, int status) {
163163

164164

165165
@Override
166-
public List<String> handleCommonConfig(String host, int port, int maxMemory, int versionId) {
166+
public List<String> handleCommonConfig(String host, int port, int maxMemory, String maxMemoryPolicy, int versionId) {
167167
//2018-08-24
168168
List<InstanceConfig> instanceConfigList = getByVesionAndType(ConstUtils.CACHE_REDIS_STANDALONE, versionId);
169169
if (CollectionUtils.isEmpty(instanceConfigList)) {
@@ -193,6 +193,10 @@ public List<String> handleCommonConfig(String host, int port, int maxMemory, int
193193
configValue = String.format(configValue, percent);
194194
} else if (RedisConfigEnum.BIND.getKey().equals(configKey)) {
195195
configValue = String.format(configValue, host);
196+
} else if (RedisConfigEnum.MAXMEMORY_POLICY.getKey().equals(configKey)) {
197+
if(StringUtils.isNotEmpty(maxMemoryPolicy)){
198+
configValue = maxMemoryPolicy;
199+
}
196200
}
197201
configs.add(combineConfigKeyValue(configKey, configValue));
198202
}

cachecloud-web/src/main/java/com/sohu/cache/redis/impl/RedisDeployCenterImpl.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sohu.cache.redis.impl;
22

33
import com.google.common.collect.Lists;
4+
import com.sohu.cache.constant.AppDescEnum;
45
import com.sohu.cache.constant.ClusterOperateResult;
56
import com.sohu.cache.constant.InstanceStatusEnum;
67
import com.sohu.cache.dao.AppDao;
@@ -570,7 +571,7 @@ private boolean runInstanceWithDefaultConfig(AppDesc appDesc, String host, Integ
570571
throw new RuntimeException(String.format("machine: %s version :%s is installed %s", host, redisResource, installStatus));
571572
}
572573
// 生成配置
573-
List<String> configs = handleCommonConfig(host, port, maxMemory, appDesc.getVersionId());
574+
List<String> configs = handleCommonConfig(host, port, maxMemory, appDesc.getMaxmemoryPolicy(), appDesc.getVersionId());
574575
if (isCluster) {
575576
configs.addAll(handleClusterConfig(port, appDesc.getVersionId()));
576577
}
@@ -633,7 +634,7 @@ public boolean bornConfigAndRunNode(AppDesc appDesc, InstanceInfo instanceInfo,
633634
SystemResource redisResource = resourceService.getResourceById(appDesc.getVersionId());
634635
String redisDir = redisResource == null ? ConstUtils.REDIS_DEFAULT_DIR : ConstUtils.getRedisDir(redisResource.getName());
635636
// 生成配置
636-
List<String> configs = handleCommonConfig(host, port, maxMemory, appDesc.getVersionId());
637+
List<String> configs = handleCommonConfig(host, port, maxMemory, appDesc.getMaxmemoryPolicy(), appDesc.getVersionId());
637638
if (isCluster) {
638639
configs.addAll(handleClusterConfig(port, appDesc.getVersionId()));
639640
}
@@ -757,10 +758,17 @@ private boolean runSentinel(AppDesc appDesc, String sentinelHost, String masterN
757758
* @param maxMemory
758759
* @return
759760
*/
760-
public List<String> handleCommonConfig(String host, int port, int maxMemory, int versionId) {
761+
public List<String> handleCommonConfig(String host, int port, int maxMemory, Integer maxMemoryPolicyType, int versionId) {
761762
List<String> configs = null;
762763
try {
763-
configs = redisConfigTemplateService.handleCommonConfig(host, port, maxMemory, versionId);
764+
String maxMemoryPolicy = null;
765+
if(maxMemoryPolicyType != null){
766+
AppDescEnum.MaxmemoryPolicyType policyType = AppDescEnum.MaxmemoryPolicyType.getByType(maxMemoryPolicyType);
767+
if(policyType != null){
768+
maxMemoryPolicy = policyType.getName();
769+
}
770+
}
771+
configs = redisConfigTemplateService.handleCommonConfig(host, port, maxMemory, maxMemoryPolicy, versionId);
764772
} catch (Exception e) {
765773
logger.error(e.getMessage(), e);
766774
}

0 commit comments

Comments
 (0)