RedisServiceImpl extends AbstractConsoleService<RedisTblDao> implements RedisService { @Override public void updateRedises(String dcName, String clusterName, String shardName, ShardModel shardModel) { if (null == shardModel) { throw new BadRequestException("RequestBody cannot be null."); } final DcClusterShardTbl dcClusterShard = dcClusterShardService.find(dcName, clusterName, shardName); if (null == dcClusterShard) { throw new BadRequestException("Cannot find related dc-cluster-shard."); } List<RedisTbl> originRedises = findAllByDcClusterShard(dcClusterShard.getDcClusterShardId()); List<RedisTbl> toUpdateRedises = formatRedisesFromShardModel(dcClusterShard, shardModel); updateRedises(originRedises, toUpdateRedises); ClusterTbl clusterInfo = clusterService.find(clusterName); if (null != clusterInfo && !clusterInfo.isIsXpipeInterested()) { clusterInfo.setIsXpipeInterested(true); clusterService.update(clusterInfo); } notifyClusterUpdate(dcName, clusterName); }  @Override RedisTbl find(final long id); @Override List<RedisTbl> findAllRedisWithSameIP(String ip); @Override List<RedisTbl> findAllByDcClusterShard(final long dcClusterShardId); @Override List<RedisTbl> findAllRedisesByDcClusterName(String dcId, String clusterId); @Override List<RedisTbl> findAllByDcClusterShard(String dcId, String clusterId, String shardId); @Override List<RedisTbl> findRedisesByDcClusterShard(String dcId, String clusterId, String shardId); @Override List<RedisTbl> findKeepersByDcClusterShard(String dcId, String clusterId, String shardId); @Override synchronized void insertRedises(String dcId, String clusterId, String shardId,
                                           List<Pair<String, Integer>> redisAddresses); @Override synchronized int insertKeepers(String dcId, String clusterId, String shardId,
                                          List<KeeperBasicInfo> keepers); @Override List<RedisTbl> deleteKeepers(String dcId, String clusterId, String shardId); @Override void deleteRedises(String dcId, String clusterId, String shardId, List<Pair<String, Integer>> redisAddresses); @Override void updateByPK(final RedisTbl redis); @Override void updateBatchMaster(List<RedisTbl> redises); @Override void updateBatchKeeperActive(List<RedisTbl> redises); @Override void updateRedises(String dcName, String clusterName, String shardName, ShardModel shardModel); @Override List<RedisTbl> findAllKeeperContainerCountInfo(); @Override List<Long> findClusterIdsByKeeperContainer(long keeperContainerId); void insertInstancesToDb(long dcClusterShardId, String role, Pair<String, Integer>... addrs);  }
@Test public void testUpdateRedises() throws IOException, ResourceNotFoundException { List<RedisTbl> allByDcClusterShard = redisService.findAllByDcClusterShard(dcName, clusterName, shardName); checkAllInstances(allByDcClusterShard); boolean firstSlave = true; RedisTbl newMaster = null; for (RedisTbl redisTbl : allByDcClusterShard) { if (redisTbl.getRedisRole().equals(XPipeConsoleConstant.ROLE_REDIS)) { if (redisTbl.isMaster()) { redisTbl.setMaster(false); } else if (!redisTbl.isMaster() && firstSlave) { redisTbl.setMaster(true); newMaster = redisTbl; firstSlave = false; } } } checkAllInstances(allByDcClusterShard); ShardModel shardModel = new ShardModel(allByDcClusterShard); redisService.updateRedises(dcName, clusterName, shardName, shardModel); allByDcClusterShard = redisService.findAllByDcClusterShard(dcName, clusterName, shardName); checkAllInstances(allByDcClusterShard); Stream<RedisTbl> redisTblStream = allByDcClusterShard.stream().filter(instance -> instance.isMaster()); RedisTbl currentMaster = redisTblStream.findFirst().get(); Assert.assertEquals(newMaster.getId(), currentMaster.getId()); }