Skip to content

Commit

Permalink
merge fix-import-node
Browse files Browse the repository at this point in the history
  • Loading branch information
zouhuajian committed Apr 20, 2020
2 parents 69988e9 + 15aa520 commit de2b769
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 55 deletions.
2 changes: 1 addition & 1 deletion redis-manager-dashboard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.newegg.ec</groupId>
<artifactId>redis-manager-dashboard</artifactId>
<version>2.3.2.1-RELEASE</version>
<version>2.3.2.2-RELEASE</version>
<packaging>jar</packaging>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,47 +307,8 @@ public Result importNode(@RequestBody List<RedisNode> redisNodeList) {
try {
RedisNode firstRedisNode = redisNodeList.get(0);
final Cluster cluster = getCluster(firstRedisNode.getClusterId());
String redisMode = cluster.getRedisMode();
StringBuilder message = new StringBuilder();
List<RedisNode> redisMasterNodeList = redisService.getRedisMasterNodeList(cluster);
redisNodeList.forEach(redisNode -> {
Iterator<RedisNode> iterator = redisMasterNodeList.iterator();
RedisNode seed = null;
while (iterator.hasNext()) {
RedisNode masterNode = iterator.next();
if (Objects.equals(redisNode, masterNode)) {
continue;
}
seed = masterNode;
break;
}
if (seed != null) {
String oneResult = null;
if (Objects.equals(redisMode, REDIS_MODE_CLUSTER)) {
oneResult = redisService.clusterMeet(cluster, seed, redisNode);
} else {
redisService.standaloneReplicaOf(cluster, seed, redisNode);
}
if (Strings.isNullOrEmpty(oneResult)) {
boolean exist = redisNodeService.existRedisNode(redisNode);
if (!exist) {
try {
Thread.sleep(FIVE_SECONDS);
} catch (InterruptedException e) {
}
List<RedisNode> redisNodes = redisService.getRealRedisNodeList(cluster);
redisNodes.forEach(node -> {
if (RedisNodeUtil.equals(node, redisNode)) {
redisNodeService.addRedisNode(node);
}
});
}
} else {
message.append(oneResult);
}
}
});
return Strings.isNullOrEmpty(message.toString()) ? Result.successResult() : Result.failResult().setMessage(message.toString());
String result = redisService.clusterImport(cluster, redisNodeList);
return Strings.isNullOrEmpty(result) ? Result.successResult() : Result.failResult().setMessage(result);
} catch (Exception e) {
e.printStackTrace();
return Result.failResult().setMessage(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public interface IClusterDao {
"WHERE cluster_id = #{clusterId}")
int updateCluster(Cluster cluster);

@Update("UPDATE cluster SET cluster_state = #{clusterState}, update_time = NOW() WHERE cluster_id = #{clusterId}")
int updateClusterState(Cluster cluster);

@Update("UPDATE cluster SET nodes = #{nodes}, update_time = NOW() WHERE cluster_id = #{clusterId}")
int updateNodes(@Param("clusterId") Integer clusterId, @Param("nodes") String nodes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public class ClusterUpdateSchedule implements IDataCollection, ApplicationListen
@Autowired
private IClusterService clusterService;

@Autowired
private IClusterDao clusterDao;

@Autowired
private IRedisService redisService;

Expand Down Expand Up @@ -125,7 +122,7 @@ private void updateCluster(Cluster.ClusterState clusterState, Cluster cluster) {
}
try {
completedCluster.setClusterState(clusterState);
clusterDao.updateCluster(completedCluster);
clusterService.updateCluster(completedCluster);
} catch (Exception e) {
logger.error("Update cluster completed info failed, cluster name = " + cluster.getClusterName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IClusterService {

boolean updateClusterMeta(Cluster cluster);

boolean updateClusterState(Cluster cluster);
boolean updateCluster(Cluster cluster);

boolean updateNodes(Cluster cluster);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public interface IRedisService {

boolean clusterForget(Cluster cluster, RedisNode forgetNode);

String clusterImport(Cluster cluster, RedisNode newRedisNode);

String clusterImport(Cluster cluster, List<RedisNode> newRedisNodeList);

/**
* change master
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ public boolean updateClusterMeta(Cluster cluster) {
}

@Override
public boolean updateClusterState(Cluster cluster) {
public boolean updateCluster(Cluster cluster) {
try {
return clusterDao.updateClusterState(cluster) > 1;
return clusterDao.updateCluster(cluster) > 1;
} catch (Exception e) {
logger.error("Update cluster state failed, cluster name = " + cluster.getClusterName(), e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.CaseFormat;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.newegg.ec.redis.client.*;
import com.newegg.ec.redis.entity.*;
import com.newegg.ec.redis.service.IClusterService;
Expand Down Expand Up @@ -388,6 +389,42 @@ public boolean clusterForget(Cluster cluster, RedisNode forgetNode) {
}
}

@Override
public String clusterImport(Cluster cluster, RedisNode newRedisNode) {
return clusterImport(cluster, Lists.newArrayList(newRedisNode));
}

@Override
public String clusterImport(Cluster cluster, List<RedisNode> newRedisNodeList) {
List<RedisNode> redisMasterNodeList = getRedisMasterNodeList(cluster);
String clusterName = cluster.getClusterName();
if (redisMasterNodeList.isEmpty()) {
return clusterName + " no masters, cluster is not healthy.";
}
String redisMode = cluster.getRedisMode();
Iterator<RedisNode> newNodeIterator = newRedisNodeList.iterator();
while (newNodeIterator.hasNext()) {
RedisNode newNode = newNodeIterator.next();
for (RedisNode masterNode : redisMasterNodeList) {
if (RedisNodeUtil.equals(newNode, masterNode)) {
newNodeIterator.remove();
}
}
}
StringBuilder result = new StringBuilder();
RedisNode seedMasterNode = redisMasterNodeList.get(0);
for (RedisNode newNode : newRedisNodeList) {
if (Objects.equals(redisMode, REDIS_MODE_CLUSTER)) {
result.append(clusterMeet(cluster, seedMasterNode, newNode));
} else if (Objects.equals(redisMode, REDIS_MODE_STANDALONE)) {
result.append(standaloneReplicaOf(cluster, seedMasterNode, newNode));
} else {
return clusterName + " import node failed, redis mode is " + redisMode;
}
}
return result.toString();
}

@Override
public boolean clusterReplicate(Cluster cluster, String masterId, RedisNode slaveNode) {
String redisPassword = cluster.getRedisPassword();
Expand Down Expand Up @@ -456,9 +493,7 @@ public String clusterMeet(Cluster cluster, RedisNode seed, List<RedisNode> redis

@Override
public String clusterMeet(Cluster cluster, RedisNode seed, RedisNode redisNode) {
List<RedisNode> redisNodeList = new ArrayList<>();
redisNodeList.add(redisNode);
return clusterMeet(cluster, seed, redisNodeList);
return clusterMeet(cluster, seed, Lists.newArrayList(redisNode));
}

@Override
Expand Down

0 comments on commit de2b769

Please sign in to comment.