|
| 1 | +package com.ctrip.xpipe.redis.console.service.impl; |
| 2 | + |
| 3 | +import com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest; |
| 4 | +import com.ctrip.xpipe.redis.console.model.*; |
| 5 | +import com.ctrip.xpipe.redis.console.service.ClusterService; |
| 6 | +import com.ctrip.xpipe.redis.console.service.DcClusterService; |
| 7 | +import com.ctrip.xpipe.redis.console.service.RedisService; |
| 8 | +import com.ctrip.xpipe.redis.console.service.ShardService; |
| 9 | +import com.google.common.collect.Lists; |
| 10 | +import org.junit.Before; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | + |
| 13 | +import java.util.LinkedList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.concurrent.atomic.AtomicLong; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author wenchao.meng |
| 19 | + * <p> |
| 20 | + * Mar 31, 2017 |
| 21 | + */ |
| 22 | +public abstract class AbstractServiceImplTest extends AbstractConsoleIntegrationTest{ |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private ClusterService clusterService; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private DcClusterService dcClusterService; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private ShardService shardService; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private RedisService redisService; |
| 35 | + |
| 36 | + protected String []dcNames = new String[]{"A", "B"}; |
| 37 | + private Long []dcIds = new Long[]{1L, 2L}; |
| 38 | + protected String clusterName = "cluster1"; |
| 39 | + protected String []shardNames = new String[]{"shard1", "shard2"}; |
| 40 | + |
| 41 | + private final int initRedisPort = 6379; |
| 42 | + private final int initKeeperPort = 8000; |
| 43 | + |
| 44 | + //TODO auto generate by h2 |
| 45 | + private AtomicLong redisId = new AtomicLong(1); |
| 46 | + |
| 47 | + @Before |
| 48 | + public void beforeAbstractServiceImpl(){ |
| 49 | + |
| 50 | + ClusterModel clusterModel = new ClusterModel(); |
| 51 | + |
| 52 | + clusterModel.setClusterTbl(new ClusterTbl(). |
| 53 | + setClusterName(clusterName) |
| 54 | + .setActivedcId(dcIds[0]) |
| 55 | + .setClusterDescription("desc")); |
| 56 | + |
| 57 | + |
| 58 | + clusterModel.setShards(createShards(shardNames)); |
| 59 | + |
| 60 | + DcTbl slaveDc = new DcTbl().setDcName(dcNames[1]); |
| 61 | + clusterModel.setSlaveDcs(Lists.newArrayList(slaveDc)); |
| 62 | + |
| 63 | + clusterService.createCluster(clusterModel); |
| 64 | + |
| 65 | + createRedisAndKeepers(); |
| 66 | + } |
| 67 | + |
| 68 | + private void createRedisAndKeepers() { |
| 69 | + |
| 70 | + int redisPort = initRedisPort; |
| 71 | + int keeperPort = initKeeperPort; |
| 72 | + |
| 73 | + for(String dcName : dcNames){ |
| 74 | + |
| 75 | + String clusterName = this.clusterName; |
| 76 | + for(String shardName : shardNames){ |
| 77 | + |
| 78 | + ShardModel shardModel = new ShardModel(); |
| 79 | + shardModel.addRedis(new RedisTbl().setId(redisId.incrementAndGet()).setRedisIp("127.0.0.1").setRedisPort(redisPort++).setMaster(true)); |
| 80 | + shardModel.addRedis(new RedisTbl().setId(redisId.incrementAndGet()).setRedisIp("127.0.0.1").setRedisPort(redisPort++)); |
| 81 | + shardModel.addKeeper(new RedisTbl().setId(redisId.incrementAndGet()).setKeepercontainerId(1).setRedisIp("127.0.0.1").setRedisPort(keeperPort++)); |
| 82 | + shardModel.addKeeper(new RedisTbl().setId(redisId.incrementAndGet()).setKeepercontainerId(2).setRedisIp("127.0.0.1").setRedisPort(keeperPort++)); |
| 83 | + redisService.updateRedises(clusterName, dcName, shardName, shardModel); |
| 84 | + |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + private List<ShardModel> createShards(String[] shardNames) { |
| 91 | + |
| 92 | + List<ShardModel> shards = new LinkedList<>(); |
| 93 | + |
| 94 | + for(String shardName : shardNames){ |
| 95 | + ShardModel shardModel = new ShardModel(); |
| 96 | + shardModel.setShardTbl(new ShardTbl().setShardName(shardName).setSetinelMonitorName(shardName)); |
| 97 | + shards.add(shardModel); |
| 98 | + } |
| 99 | + |
| 100 | + return shards; |
| 101 | + } |
| 102 | +} |
0 commit comments