diff --git a/redis-manager-dashboard/pom.xml b/redis-manager-dashboard/pom.xml index 9e58138d..89509eda 100644 --- a/redis-manager-dashboard/pom.xml +++ b/redis-manager-dashboard/pom.xml @@ -6,7 +6,7 @@ com.newegg.ec redis-manager-dashboard - 2.3.2.1-RELEASE + 2.3.2.2-RELEASE jar diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/controller/NodeManageController.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/controller/NodeManageController.java index 201f7c8d..c8717755 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/controller/NodeManageController.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/controller/NodeManageController.java @@ -307,47 +307,8 @@ public Result importNode(@RequestBody List redisNodeList) { try { RedisNode firstRedisNode = redisNodeList.get(0); final Cluster cluster = getCluster(firstRedisNode.getClusterId()); - String redisMode = cluster.getRedisMode(); - StringBuilder message = new StringBuilder(); - List redisMasterNodeList = redisService.getRedisMasterNodeList(cluster); - redisNodeList.forEach(redisNode -> { - Iterator 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 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()); diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/dao/IClusterDao.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/dao/IClusterDao.java index 53bd8f7e..c096b402 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/dao/IClusterDao.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/dao/IClusterDao.java @@ -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); diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/schedule/ClusterUpdateSchedule.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/schedule/ClusterUpdateSchedule.java index 81b65747..24aab330 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/schedule/ClusterUpdateSchedule.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/schedule/ClusterUpdateSchedule.java @@ -55,9 +55,6 @@ public class ClusterUpdateSchedule implements IDataCollection, ApplicationListen @Autowired private IClusterService clusterService; - @Autowired - private IClusterDao clusterDao; - @Autowired private IRedisService redisService; @@ -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); } diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IClusterService.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IClusterService.java index 6561c1b0..79aebbc1 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IClusterService.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IClusterService.java @@ -27,7 +27,7 @@ public interface IClusterService { boolean updateClusterMeta(Cluster cluster); - boolean updateClusterState(Cluster cluster); + boolean updateCluster(Cluster cluster); boolean updateNodes(Cluster cluster); diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IRedisService.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IRedisService.java index f6ef77d1..28c15f49 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IRedisService.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/IRedisService.java @@ -68,6 +68,10 @@ public interface IRedisService { boolean clusterForget(Cluster cluster, RedisNode forgetNode); + String clusterImport(Cluster cluster, RedisNode newRedisNode); + + String clusterImport(Cluster cluster, List newRedisNodeList); + /** * change master * diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/ClusterService.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/ClusterService.java index cc23b8a0..e0b62dcd 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/ClusterService.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/ClusterService.java @@ -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; diff --git a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/RedisService.java b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/RedisService.java index e6adec31..19423ea9 100644 --- a/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/RedisService.java +++ b/redis-manager-dashboard/src/main/java/com/newegg/ec/redis/service/impl/RedisService.java @@ -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; @@ -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 newRedisNodeList) { + List redisMasterNodeList = getRedisMasterNodeList(cluster); + String clusterName = cluster.getClusterName(); + if (redisMasterNodeList.isEmpty()) { + return clusterName + " no masters, cluster is not healthy."; + } + String redisMode = cluster.getRedisMode(); + Iterator 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(); @@ -456,9 +493,7 @@ public String clusterMeet(Cluster cluster, RedisNode seed, List redis @Override public String clusterMeet(Cluster cluster, RedisNode seed, RedisNode redisNode) { - List redisNodeList = new ArrayList<>(); - redisNodeList.add(redisNode); - return clusterMeet(cluster, seed, redisNodeList); + return clusterMeet(cluster, seed, Lists.newArrayList(redisNode)); } @Override