ClusterMetaServiceImpl extends AbstractMetaService implements ClusterMetaService { @Override public long getClusterMetaCurrentPrimaryDc(DcTbl dcInfo, ClusterTbl clusterInfo) { if (ClusterStatus.isSameClusterStatus(clusterInfo.getStatus(), ClusterStatus.Migrating)) { MigrationClusterTbl migrationCluster = migrationService.findLatestUnfinishedMigrationCluster(clusterInfo.getId()); if(migrationCluster != null && dcInfo.getId() == migrationCluster.getDestinationDcId()) { return migrationCluster.getDestinationDcId(); } } return clusterInfo.getActivedcId(); }  @Override ClusterMeta loadClusterMeta(DcMeta dcMeta, ClusterTbl clusterTbl, DcMetaQueryVO dcMetaQueryVO); @Override ClusterMeta getClusterMeta(final String dcName, final String clusterName); @Override long getClusterMetaCurrentPrimaryDc(DcTbl dcInfo, ClusterTbl clusterInfo); void setMigrationService(MigrationService migrationService);  }
@Test public void testGetClusterMetaCurrentPrimaryDcMigrating(){ long currentActiveDcId = randomInt(); long clusterId = randomInt(); long destinationDcId = currentActiveDcId + 1; DcTbl dcTbl = new DcTbl(); ClusterTbl clusterTbl = new ClusterTbl(); clusterTbl.setId(clusterId); clusterTbl.setActivedcId(currentActiveDcId); clusterTbl.setStatus(ClusterStatus.Migrating.toString()); when(migrationService.findLatestUnfinishedMigrationCluster(clusterId)).thenReturn(new MigrationClusterTbl().setDestinationDcId(destinationDcId)); dcTbl.setId(destinationDcId); Assert.assertEquals(destinationDcId, clusterMetaServiceImpl.getClusterMetaCurrentPrimaryDc(dcTbl, clusterTbl)); dcTbl.setId(destinationDcId + 1); Assert.assertEquals(currentActiveDcId, clusterMetaServiceImpl.getClusterMetaCurrentPrimaryDc(dcTbl, clusterTbl)); dcTbl.setId(currentActiveDcId); Assert.assertEquals(currentActiveDcId, clusterMetaServiceImpl.getClusterMetaCurrentPrimaryDc(dcTbl, clusterTbl)); }
@Test public void testGetClusterMetaCurrentPrimaryDcNotMigrating(){ long currentActiveDcId = randomInt(); long clusterId = randomInt(); long destinationDcId = currentActiveDcId + 1; DcTbl dcTbl = new DcTbl(); ClusterTbl clusterTbl = new ClusterTbl(); clusterTbl.setId(clusterId); clusterTbl.setActivedcId(currentActiveDcId); when(migrationService.findLatestUnfinishedMigrationCluster(clusterId)).thenReturn(new MigrationClusterTbl().setDestinationDcId(destinationDcId)); dcTbl.setId(destinationDcId); for(ClusterStatus clusterStatus : ClusterStatus.values()){ if(clusterStatus == ClusterStatus.Migrating){ continue; } clusterTbl.setStatus(clusterStatus.toString()); Assert.assertEquals(currentActiveDcId, clusterMetaServiceImpl.getClusterMetaCurrentPrimaryDc(dcTbl, clusterTbl)); } }
@Test public void testCurrentPrimaryDcPublish() { long currentActiveDcId = 1; long clusterId = randomInt(); long destinationDcId = 2; DcTbl dcTbl = new DcTbl(); dcTbl.setId(destinationDcId); ClusterTbl clusterTbl = new ClusterTbl(); clusterTbl.setId(clusterId); clusterTbl.setActivedcId(currentActiveDcId); clusterTbl.setStatus(ClusterStatus.Migrating.name()); when(migrationService.findLatestUnfinishedMigrationCluster(clusterId)).thenReturn(new MigrationClusterTbl().setDestinationDcId(destinationDcId)); long target = clusterMetaServiceImpl.getClusterMetaCurrentPrimaryDc(dcTbl, clusterTbl); Assert.assertEquals(destinationDcId, target); }