Search in sources :

Example 6 with ShardTbl

use of com.ctrip.xpipe.redis.console.model.ShardTbl in project x-pipe by ctripcorp.

the class ShardServiceImplTest2 method findOrCreateShardIfNotExist1.

/**
 *==========================================================================
 * no monitor name is posted
 *     ==========================================================================*
 */
// monitor name exist, no shard exist
@Test
public void findOrCreateShardIfNotExist1() throws Exception {
    ShardTbl proto = new ShardTbl().setShardName("shard1");
    String cluster = "cluster-test";
    when(shardDao.queryAllShardsByClusterName(anyString())).thenReturn(null);
    when(shardDao.queryAllShardMonitorNames()).thenReturn(Sets.newHashSet("shard1"));
    when(shardDao.insertShard(cluster, proto, Maps.newHashMap())).thenReturn(proto);
    ShardTbl shardTbl = shardService.findOrCreateShardIfNotExist(cluster, proto, Maps.newHashMap());
    Assert.assertEquals(cluster + "-" + proto.getShardName(), shardTbl.getSetinelMonitorName());
}
Also used : ShardTbl(com.ctrip.xpipe.redis.console.model.ShardTbl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 7 with ShardTbl

use of com.ctrip.xpipe.redis.console.model.ShardTbl in project x-pipe by ctripcorp.

the class ShardServiceImplTest2 method findOrCreateShardIfNotExist4.

// shard exist
@Test
public void findOrCreateShardIfNotExist4() throws Exception {
    ShardTbl proto = new ShardTbl().setShardName("shard1");
    String cluster = "cluster-test";
    ShardTbl expected = new ShardTbl().setShardName("shard1").setSetinelMonitorName("shard1");
    when(shardDao.queryAllShardsByClusterName(cluster)).thenReturn(Lists.newArrayList(expected));
    when(shardDao.queryAllShardMonitorNames()).thenReturn(Sets.newHashSet("shard1", cluster + "-" + proto.getShardName()));
    when(shardDao.insertShard(cluster, proto, Maps.newHashMap())).thenReturn(proto);
    ShardTbl shardTbl = shardService.findOrCreateShardIfNotExist(cluster, proto, Maps.newHashMap());
    Assert.assertTrue(expected == shardTbl);
}
Also used : ShardTbl(com.ctrip.xpipe.redis.console.model.ShardTbl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 8 with ShardTbl

use of com.ctrip.xpipe.redis.console.model.ShardTbl in project x-pipe by ctripcorp.

the class ShardServiceImplTest2 method findOrCreateShardIfNotExist7.

// shard not exist, but monitor name has been occupied by other shard
@Test(expected = java.lang.IllegalArgumentException.class)
public void findOrCreateShardIfNotExist7() throws Exception {
    String cluster = "cluster-test", shard = "shard1";
    ShardTbl proto = new ShardTbl().setShardName(shard).setSetinelMonitorName(shard);
    when(shardDao.queryAllShardsByClusterName(cluster)).thenReturn(Lists.newArrayList());
    when(shardDao.queryAllShardMonitorNames()).thenReturn(Sets.newHashSet("shard1", cluster + "-" + proto.getShardName()));
    when(shardDao.insertShard(cluster, proto, Maps.newHashMap())).thenReturn(proto);
    try {
        ShardTbl shardTbl = shardService.findOrCreateShardIfNotExist(cluster, proto, Maps.newHashMap());
    } catch (Exception e) {
        Assert.assertEquals(String.format("Shard monitor name %s already exist", shard), e.getMessage());
        throw e;
    }
}
Also used : ShardTbl(com.ctrip.xpipe.redis.console.model.ShardTbl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 9 with ShardTbl

use of com.ctrip.xpipe.redis.console.model.ShardTbl in project x-pipe by ctripcorp.

the class MetaUpdateTest3 method createShard1.

@Test
public void createShard1() throws Exception {
    ShardCreateInfo shardCreateInfo = new ShardCreateInfo();
    shardCreateInfo.setShardMonitorName(shardName);
    shardCreateInfo.setShardName(shardName);
    metaUpdate.createShards(clusterName, Lists.newArrayList(new ShardCreateInfo()));
    List<RedisCreateInfo> createInfo = createInfo(Lists.newArrayList("192.168.0.1:6379", "192.168.0.1:6380"), Lists.newArrayList("192.168.0.2:6379", "192.168.0.2:6380"));
    metaUpdate.createShard(clusterName, shardName, createInfo);
    ShardTbl shardTbl = shardService.find(clusterName, shardName);
    List<RedisTbl> keepers = redisService.findKeepersByDcClusterShard(activeDC, clusterName, shardName);
    List<RedisTbl> redisTbls = redisService.findRedisesByDcClusterShard(activeDC, clusterName, shardName);
    logger.info("{}", shardTbl);
    logger.info("{}", keepers);
    logger.info("{}", redisTbls);
    Assert.assertTrue(listEquals(Lists.newArrayList("192.168.0.1:6379", "192.168.0.1:6380"), redisTbls.stream().map((redisTbl) -> {
        return redisTbl.getRedisIp() + ":" + redisTbl.getRedisPort();
    }).collect(Collectors.toList())));
    Assert.assertEquals(2, keepers.size());
}
Also used : ShardCreateInfo(com.ctrip.xpipe.redis.console.controller.api.data.meta.ShardCreateInfo) RedisTbl(com.ctrip.xpipe.redis.console.model.RedisTbl) RedisCreateInfo(com.ctrip.xpipe.redis.console.controller.api.data.meta.RedisCreateInfo) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) java.util.concurrent(java.util.concurrent) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) RetMessage(com.ctrip.xpipe.redis.console.controller.api.RetMessage) Collectors(java.util.stream.Collectors) ShardTbl(com.ctrip.xpipe.redis.console.model.ShardTbl) com.ctrip.xpipe.redis.console.service(com.ctrip.xpipe.redis.console.service) List(java.util.List) Lists(com.google.common.collect.Lists) ClusterCreateInfo(com.ctrip.xpipe.redis.console.controller.api.data.meta.ClusterCreateInfo) StringUtil(com.ctrip.xpipe.utils.StringUtil) Assert(org.junit.Assert) Before(org.junit.Before) ShardTbl(com.ctrip.xpipe.redis.console.model.ShardTbl) RedisTbl(com.ctrip.xpipe.redis.console.model.RedisTbl) ShardCreateInfo(com.ctrip.xpipe.redis.console.controller.api.data.meta.ShardCreateInfo) RedisCreateInfo(com.ctrip.xpipe.redis.console.controller.api.data.meta.RedisCreateInfo) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) Test(org.junit.Test)

Example 10 with ShardTbl

use of com.ctrip.xpipe.redis.console.model.ShardTbl in project x-pipe by ctripcorp.

the class MetaUpdateTest3 method createShard6.

@Test
public void createShard6() throws Exception {
    int taskNum = 3;
    ThreadPoolExecutor executorService = new ThreadPoolExecutor(taskNum, taskNum, 1L, TimeUnit.SECONDS, new SynchronousQueue<>());
    executorService.prestartAllCoreThreads();
    executorService.allowsCoreThreadTimeOut();
    for (int i = 0; i < taskNum; i++) {
        executorService.execute(new Runnable() {

            @Override
            public void run() {
                ShardCreateInfo shardCreateInfo = new ShardCreateInfo();
                shardCreateInfo.setShardMonitorName(shardName);
                shardCreateInfo.setShardName(shardName);
                RetMessage result = metaUpdate.createShards(clusterName, Lists.newArrayList(shardCreateInfo));
                logger.info("{}", result);
            }
        });
    }
    waitConditionUntilTimeOut(() -> executorService.getCompletedTaskCount() == taskNum, 2000);
    System.out.println("=========================");
    List<ShardTbl> shards = shardService.findAllByClusterName(clusterName);
    logger.info("{}", shards);
    Assert.assertEquals(1, shards.size());
}
Also used : ShardTbl(com.ctrip.xpipe.redis.console.model.ShardTbl) ShardCreateInfo(com.ctrip.xpipe.redis.console.controller.api.data.meta.ShardCreateInfo) RetMessage(com.ctrip.xpipe.redis.console.controller.api.RetMessage) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) Test(org.junit.Test)

Aggregations

ShardTbl (com.ctrip.xpipe.redis.console.model.ShardTbl)17 Test (org.junit.Test)15 Matchers.anyString (org.mockito.Matchers.anyString)7 AbstractConsoleIntegrationTest (com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest)3 RetMessage (com.ctrip.xpipe.redis.console.controller.api.RetMessage)3 ShardCreateInfo (com.ctrip.xpipe.redis.console.controller.api.data.meta.ShardCreateInfo)3 AbstractConsoleTest (com.ctrip.xpipe.redis.console.AbstractConsoleTest)2 ClusterCreateInfo (com.ctrip.xpipe.redis.console.controller.api.data.meta.ClusterCreateInfo)2 RedisCreateInfo (com.ctrip.xpipe.redis.console.controller.api.data.meta.RedisCreateInfo)2 RedisTbl (com.ctrip.xpipe.redis.console.model.RedisTbl)2 SetinelTbl (com.ctrip.xpipe.redis.console.model.SetinelTbl)2 ShardDeleteEvent (com.ctrip.xpipe.redis.console.notifier.shard.ShardDeleteEvent)2 com.ctrip.xpipe.redis.console.service (com.ctrip.xpipe.redis.console.service)2 KeeperBasicInfo (com.ctrip.xpipe.redis.console.service.KeeperBasicInfo)2 StringUtil (com.ctrip.xpipe.utils.StringUtil)2 Lists (com.google.common.collect.Lists)2 List (java.util.List)2 java.util.concurrent (java.util.concurrent)2 Collectors (java.util.stream.Collectors)2 Assert (org.junit.Assert)2