Skip to content

Commit

Permalink
Merge pull request #2273 from wangyu096/issue_2272
Browse files Browse the repository at this point in the history
perf: 优化 job-manage 批量获取主机 API 性能 #2272
  • Loading branch information
jsonwan authored Jul 25, 2023
2 parents edd95bf + 5bb12db commit 08ea1fc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class JobRedisAutoConfiguration {

@Bean("jsonRedisTemplate")
@Primary
public RedisTemplate<Object, Object> jsonRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
public RedisTemplate<String, Object> jsonRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
public class ApplicationCache {

private final ApplicationDAO applicationDAO;
private final RedisTemplate<Object, Object> redisTemplate;
private final RedisTemplate<String, Object> redisTemplate;
private final String APP_HASH_KEY = "job:manage:apps";
private final String SCOPE_HASH_KEY = "job:manage:scopes";

@Autowired
public ApplicationCache(ApplicationDAO applicationDAO,
@Qualifier("jsonRedisTemplate") RedisTemplate<Object, Object> redisTemplate) {
@Qualifier("jsonRedisTemplate") RedisTemplate<String, Object> redisTemplate) {
this.applicationDAO = applicationDAO;
this.redisTemplate = redisTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class DangerousRuleCache {
public static final String DANGEROUS_RULE_HASH_KEY = "job:manage:dangerousRules";
private final DSLContext dslContext;
private final DangerousRuleDAO dangerousRuleDAO;
private final RedisTemplate redisTemplate;
private final RedisTemplate<String, Object> redisTemplate;

@Autowired
public DangerousRuleCache(DSLContext dslContext,
DangerousRuleDAO dangerousRuleDAO,
@Qualifier("jsonRedisTemplate") RedisTemplate redisTemplate) {
@Qualifier("jsonRedisTemplate") RedisTemplate<String, Object> redisTemplate) {
this.dslContext = dslContext;
this.dangerousRuleDAO = dangerousRuleDAO;
this.redisTemplate = redisTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
package com.tencent.bk.job.manage.manager.host;

import com.tencent.bk.job.common.model.dto.ApplicationHostDTO;
import com.tencent.bk.job.common.service.AppScopeMappingService;
import com.tencent.bk.job.manage.model.db.CacheHostDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -49,11 +52,13 @@
@Component
public class HostCache {

private final RedisTemplate redisTemplate;
private final RedisTemplate<String, Object> redisTemplate;

// 1天过期
private static final int EXPIRE_DAYS = 1;

@Autowired
public HostCache(@Qualifier("jsonRedisTemplate") RedisTemplate<Object, Object> redisTemplate,
AppScopeMappingService appScopeMappingService) {
public HostCache(@Qualifier("jsonRedisTemplate") RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}

Expand Down Expand Up @@ -81,7 +86,12 @@ public List<CacheHostDO> batchGetHostsByHostIds(List<Long> hostIds) {

private List<CacheHostDO> getHostsByKeys(List<String> hostKeys) {
try {
return (List<CacheHostDO>) redisTemplate.opsForValue().multiGet(hostKeys);
List<Object> results = redisTemplate.opsForValue().multiGet(hostKeys);
if (CollectionUtils.isEmpty(results)) {
return Collections.emptyList();
}
// 通过 Object 间接强制转换 List
return (List<CacheHostDO>) (Object) results;
} catch (Exception e) {
log.warn("Batch get host in cache exception", e);
return Collections.emptyList();
Expand Down Expand Up @@ -128,17 +138,42 @@ public void addOrUpdateHost(ApplicationHostDTO applicationHostDTO) {
CacheHostDO cacheHost = CacheHostDO.fromApplicationHostDTO(applicationHostDTO);
String hostIpKey = buildHostIpKey(applicationHostDTO);
String hostIdKey = buildHostIdKey(applicationHostDTO);
redisTemplate.opsForValue().set(hostIpKey, cacheHost, 1, TimeUnit.DAYS);
redisTemplate.opsForValue().set(hostIdKey, cacheHost, 1, TimeUnit.DAYS);
redisTemplate.opsForValue().set(hostIpKey, cacheHost, EXPIRE_DAYS, TimeUnit.DAYS);
redisTemplate.opsForValue().set(hostIdKey, cacheHost, EXPIRE_DAYS, TimeUnit.DAYS);
}

/**
* 批量更新缓存中的主机
*
* @param hosts 主机列表
*/
public void addOrUpdateHosts(List<ApplicationHostDTO> hosts) {
hosts.forEach(this::addOrUpdateHost);
public void batchAddOrUpdateHosts(List<ApplicationHostDTO> hosts) {
if (CollectionUtils.isEmpty(hosts)) {
return;
}
if (hosts.size() == 1) {
addOrUpdateHost(hosts.get(0));
return;
}

long start = System.currentTimeMillis();
redisTemplate.executePipelined(new SessionCallback<Object>() {
@Override
public Object execute(@NotNull RedisOperations operations) throws DataAccessException {
hosts.forEach(host -> {
CacheHostDO cacheHost = CacheHostDO.fromApplicationHostDTO(host);
String hostIpKey = buildHostIpKey(host);
String hostIdKey = buildHostIdKey(host);
operations.opsForValue().set(hostIpKey, cacheHost, EXPIRE_DAYS, TimeUnit.DAYS);
operations.opsForValue().set(hostIdKey, cacheHost, EXPIRE_DAYS, TimeUnit.DAYS);
});
return null;
}
});
long cost = System.currentTimeMillis() - start;
if (cost > 1000) {
log.info("BatchAddOrUpdateHosts slow, hostSize: {}, cost: {}", hosts.size(), cost);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public int deleteByBasicHost(List<BasicHostDTO> basicHostList) {
hostCache.batchDeleteHost(hostList);
hostList = applicationHostDAO.listHostInfoByHostIds(hostIdList);
// 未成功从DB删除的主机重新加入缓存
hostCache.addOrUpdateHosts(hostList);
hostCache.batchAddOrUpdateHosts(hostList);
return deletedNum;
}

Expand Down Expand Up @@ -1642,11 +1642,13 @@ public Pair<List<String>, List<ApplicationHostDTO>> listHostsFromDb(List<String>
// DB中缓存的主机可能没有业务信息(依赖的主机事件还没有处理),那么暂时跳过该主机
continue;
}
hostCache.addOrUpdateHost(appHost);
notExistCloudIps.remove(appHost.getCloudIp());
appHosts.add(appHost);
}
}
if (CollectionUtils.isNotEmpty(appHosts)) {
hostCache.batchAddOrUpdateHosts(appHosts);
}

long cost = System.currentTimeMillis() - start;
if (cost > 1000) {
Expand All @@ -1669,7 +1671,7 @@ public Pair<List<String>, List<ApplicationHostDTO>> listHostsFromCmdb(List<Strin
notExistCloudIps.removeAll(cmdbExistHostIds);
log.info("sync new hosts from cmdb, hosts:{}", cmdbExistHosts);

hostCache.addOrUpdateHosts(cmdbExistHosts);
hostCache.batchAddOrUpdateHosts(cmdbExistHosts);
}

long cost = System.currentTimeMillis() - start;
Expand Down Expand Up @@ -1716,12 +1718,14 @@ public Pair<List<Long>, List<ApplicationHostDTO>> listHostsFromDb(List<Long> hos
// DB中缓存的主机可能没有业务信息(依赖的主机事件还没有处理),那么暂时跳过该主机
continue;
}
hostCache.addOrUpdateHost(appHost);
notExistHostIds.remove(appHost.getHostId());
appHosts.add(appHost);
}
}

if (CollectionUtils.isNotEmpty(appHosts)) {
hostCache.batchAddOrUpdateHosts(appHosts);
}
long cost = System.currentTimeMillis() - start;
if (cost > 1000) {
log.warn("ListHostsFromMySQL slow, hostSize: {}, cost: {}", hostIds.size(), cost);
Expand All @@ -1741,7 +1745,7 @@ public Pair<List<Long>, List<ApplicationHostDTO>> listHostsFromCmdb(List<Long> h
notExistHostIds.removeAll(cmdbExistHostIds);
log.info("sync new hosts from cmdb, hosts:{}", cmdbExistHosts);

hostCache.addOrUpdateHosts(cmdbExistHosts);
hostCache.batchAddOrUpdateHosts(cmdbExistHosts);
}

long cost = System.currentTimeMillis() - start;
Expand Down

0 comments on commit 08ea1fc

Please sign in to comment.