Search in sources :

Example 1 with DefaultMigrationShard

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

the class MigrationEventDao method loadMigrationEvent.

private MigrationEvent loadMigrationEvent(List<MigrationEventTbl> details) {
    if (!CollectionUtils.isEmpty(details)) {
        MigrationEvent event = new DefaultMigrationEvent(details.get(0));
        for (MigrationEventTbl detail : details) {
            MigrationClusterTbl cluster = detail.getRedundantClusters();
            MigrationShardTbl shard = detail.getRedundantShards();
            if (null == event.getMigrationCluster(cluster.getClusterId())) {
                event.addMigrationCluster(new DefaultMigrationCluster(executors, scheduled, event, detail.getRedundantClusters(), dcService, clusterService, shardService, redisService, migrationService));
            }
            MigrationCluster migrationCluster = event.getMigrationCluster(cluster.getClusterId());
            migrationCluster.addNewMigrationShard(new DefaultMigrationShard(migrationCluster, shard, migrationCluster.getClusterShards().get(shard.getShardId()), migrationCluster.getClusterDcs(), migrationService));
        }
        return event;
    }
    throw new BadRequestException("Cannot load migration event from null.");
}
Also used : DefaultMigrationEvent(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationEvent) DefaultMigrationCluster(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationCluster) MigrationCluster(com.ctrip.xpipe.redis.console.migration.model.MigrationCluster) DefaultMigrationCluster(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationCluster) BadRequestException(com.ctrip.xpipe.redis.console.exception.BadRequestException) DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) MigrationEvent(com.ctrip.xpipe.redis.console.migration.model.MigrationEvent) DefaultMigrationEvent(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationEvent)

Example 2 with DefaultMigrationShard

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

the class DefaultMigrationShardTest method prepareMockData.

private void prepareMockData() {
    String clusterName = "test-cluster";
    when(mockedMigrationCluster.getCurrentCluster()).thenReturn((new ClusterTbl()).setId(1).setClusterName(clusterName).setActivedcId(1L));
    when(mockedMigrationCluster.clusterName()).thenReturn(clusterName);
    when(mockedMigrationCluster.getMigrationCluster()).thenReturn((new MigrationClusterTbl()).setClusterId(1).setDestinationDcId(2L));
    when(mockedMigrationCluster.getRedisService()).thenReturn(mockedRedisService);
    final AtomicInteger cnt = new AtomicInteger(0);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            System.out.println(args[0]);
            if (cnt.incrementAndGet() == 3) {
                if (migrationShard.getShardMigrationResult().stepSuccess(ShardMigrationStep.MIGRATE_NEW_PRIMARY_DC)) {
                    migrationShard.doMigrateOtherDc();
                }
            }
            ;
            return null;
        }
    }).when(mockedMigrationCluster).update(anyObject(), anyObject());
    mockedMigrationShard = (new MigrationShardTbl()).setId(1).setKeyId(1).setShardId(1).setMigrationClusterId(1);
    mockedCurrentShard = (new ShardTbl()).setId(1).setKeyId(1).setShardName("test-shard").setClusterId(1).setSetinelMonitorName("test-shard-monitor");
    mockedDcs.put(1L, (new DcTbl()).setId(1).setKeyId(1).setDcName("dc-a"));
    mockedDcs.put(2L, (new DcTbl()).setId(2).setKeyId(2).setDcName("dc-b"));
    migrationShard = new DefaultMigrationShard(mockedMigrationCluster, mockedMigrationShard, mockedCurrentShard, mockedDcs, mockedMigrationService, mockedCommandBuilder);
}
Also used : DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 3 with DefaultMigrationShard

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

the class MultiShardMigrationTest method prepare.

@Before
public void prepare() {
    MockitoAnnotations.initMocks(this);
    Map<Long, DcTbl> dcs = new HashMap<>();
    for (DcTbl dc : dcService.findClusterRelatedDc(clusterName)) {
        dcs.put(dc.getId(), dc);
    }
    createShards();
    MigrationClusterTbl migrationClusterTbl = migrationService.findMigrationCluster(1L, clusterId);
    migrationCluster = new DefaultMigrationCluster(executors, scheduled, migrationEvent, migrationClusterTbl, dcService, clusterService, shardService, redisService, migrationService);
    for (int cnt = 1; cnt != TEST_SHARD_CNT + 1; ++cnt) {
        MigrationShardTbl migrationShardTbl = new MigrationShardTbl();
        migrationShardTbl.setId(cnt).setMigrationClusterId(1).setShardId(cnt).setLog("");
        ShardTbl shardTbl = new ShardTbl();
        shardTbl.setId(cnt).setClusterId(clusterId).setShardName(getShardName(cnt)).setSetinelMonitorName("cluster1-" + getShardName(cnt));
        MigrationShard migrationShard = new DefaultMigrationShard(migrationCluster, migrationShardTbl, shardTbl, dcs, migrationService, migrationCommandBuilder);
        migrationCluster.addNewMigrationShard(migrationShard);
    }
}
Also used : DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) HashMap(java.util.HashMap) DefaultMigrationCluster(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationCluster) DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) Before(org.junit.Before)

Example 4 with DefaultMigrationShard

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

the class DefaultMigrationClusterTest method prepare.

@Before
public void prepare() {
    MockitoAnnotations.initMocks(this);
    dcA = dcNames[0];
    dcB = dcNames[1];
    MigrationClusterTbl migrationClusterTbl = migrationService.findMigrationCluster(1L, 1L);
    migrationCluster = new DefaultMigrationCluster(executors, scheduled, migrationEvent, migrationClusterTbl, dcService, clusterService, shardService, redisService, migrationService);
    Map<Long, DcTbl> dcs = new HashMap<>();
    for (DcTbl dc : dcService.findClusterRelatedDc("cluster1")) {
        dcs.put(dc.getId(), dc);
    }
    migrationShard = new DefaultMigrationShard(migrationCluster, migrationService.findMigrationShards(1).get(0), shardService.find(1), dcs, migrationService, migrationCommandBuilder);
    migrationCluster.addNewMigrationShard(migrationShard);
}
Also used : MigrationClusterTbl(com.ctrip.xpipe.redis.console.model.MigrationClusterTbl) DcTbl(com.ctrip.xpipe.redis.console.model.DcTbl) HashMap(java.util.HashMap) DefaultMigrationCluster(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationCluster) DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) Before(org.junit.Before)

Example 5 with DefaultMigrationShard

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

the class MigrationEventDaoTest method testShardResult.

@Test
@DirtiesContext
public void testShardResult() {
    MigrationEvent event = migrationEventDao.buildMigrationEvent(2);
    // update check
    MigrationCluster migrationCluster = event.getMigrationClusters().get(0);
    List<MigrationShard> migrationShards = migrationCluster.getMigrationShards();
    migrationShards.forEach(migrationShard -> {
        ShardMigrationResult result = randomResult();
        ((DefaultMigrationShard) migrationShard).updateShardMigrationResult(result);
        migrationShard.update(null, null);
    });
    MigrationEvent eventNew = migrationEventDao.buildMigrationEvent(2);
    MigrationCluster migrationClusterNew = eventNew.getMigrationClusters().get(0);
    Assert.assertEquals(migrationCluster.clusterName(), migrationClusterNew.clusterName());
    List<MigrationShard> migrationShardsNew = migrationClusterNew.getMigrationShards();
    Assert.assertEquals(migrationShards.size(), migrationShardsNew.size());
    for (int i = 0; i < migrationShards.size(); i++) {
        ShardMigrationResult result = migrationShards.get(i).getShardMigrationResult();
        ShardMigrationResult resultNew = migrationShardsNew.get(i).getShardMigrationResult();
        Assert.assertFalse(result == resultNew);
        Assert.assertEquals(result, resultNew);
    }
}
Also used : DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) DefaultMigrationShard(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard) DefaultShardMigrationResult(com.ctrip.xpipe.redis.console.migration.model.impl.DefaultShardMigrationResult) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Aggregations

DefaultMigrationShard (com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationShard)6 DefaultMigrationCluster (com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationCluster)4 HashMap (java.util.HashMap)3 Before (org.junit.Before)3 DcTbl (com.ctrip.xpipe.redis.console.model.DcTbl)2 MigrationClusterTbl (com.ctrip.xpipe.redis.console.model.MigrationClusterTbl)2 AbstractConsoleIntegrationTest (com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest)1 BadRequestException (com.ctrip.xpipe.redis.console.exception.BadRequestException)1 MigrationCluster (com.ctrip.xpipe.redis.console.migration.model.MigrationCluster)1 MigrationEvent (com.ctrip.xpipe.redis.console.migration.model.MigrationEvent)1 DefaultMigrationEvent (com.ctrip.xpipe.redis.console.migration.model.impl.DefaultMigrationEvent)1 DefaultShardMigrationResult (com.ctrip.xpipe.redis.console.migration.model.impl.DefaultShardMigrationResult)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 DirtiesContext (org.springframework.test.annotation.DirtiesContext)1