Skip to content

Commit

Permalink
Merge pull request #892 from ctripcorp/bugfix/mig_records
Browse files Browse the repository at this point in the history
fix query mig clusters fail
  • Loading branch information
LanternLee authored Oct 8, 2024
2 parents a181ca9 + 2499db8 commit af7b1ac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,30 @@ public Map<String, List<ClusterMigrationStatusV2>> getClusterMigrationHistoryV2(

long from = TimeUnit.SECONDS.toMillis(req.from);
long to = TimeUnit.SECONDS.toMillis(req.to);
List<MigrationClusterTbl> migrationClusterTbls = migrationService.fetchMigrationClusters(oneWayClusters.keySet(), from, to);
List<MigrationBiClusterEntity> biMigrationRecords = biMigrationRepository.selectAllByClusterIdAndOpTime(biDirectionClusters.keySet(),
new Date(from), new Date(to));

Map<String, List<ClusterMigrationStatusV2>> resp = new HashMap<>();
for (MigrationClusterTbl migrationClusterTbl: migrationClusterTbls) {
String clusterName = migrationClusterTbl.getCluster().getClusterName();
ClusterEntity cluster = oneWayClusters.get(clusterName);
if (!resp.containsKey(clusterName)) resp.put(clusterName, new ArrayList<>());
ClusterMigrationStatusV2 clusterMigrationStatus = ClusterMigrationStatusV2.from(cluster, migrationClusterTbl, dcCache);
resp.get(clusterName).add(clusterMigrationStatus);

if (!oneWayClusters.isEmpty()) {
List<MigrationClusterTbl> migrationClusterTbls = migrationService.fetchMigrationClusters(oneWayClusters.keySet(), from, to);
for (MigrationClusterTbl migrationClusterTbl: migrationClusterTbls) {
String clusterName = migrationClusterTbl.getCluster().getClusterName();
ClusterEntity cluster = oneWayClusters.get(clusterName);
if (!resp.containsKey(clusterName)) resp.put(clusterName, new ArrayList<>());
ClusterMigrationStatusV2 clusterMigrationStatus = ClusterMigrationStatusV2.from(cluster, migrationClusterTbl, dcCache);
resp.get(clusterName).add(clusterMigrationStatus);
}
}

for (MigrationBiClusterEntity biMigrationRecord: biMigrationRecords) {
Long clusterId = biMigrationRecord.getClusterId();
ClusterEntity cluster = biDirectionClusters.get(clusterId);
String clusterName = cluster.getClusterName();
if (!resp.containsKey(clusterName)) resp.put(clusterName, new ArrayList<>());
ClusterMigrationStatusV2 clusterMigrationStatus = ClusterMigrationStatusV2.from(cluster, biMigrationRecord, getBiRelatedDcs(clusterName));
resp.get(clusterName).add(clusterMigrationStatus);
if (!biDirectionClusters.isEmpty()) {
List<MigrationBiClusterEntity> biMigrationRecords = biMigrationRepository.selectAllByClusterIdAndOpTime(biDirectionClusters.keySet(),
new Date(from), new Date(to));
for (MigrationBiClusterEntity biMigrationRecord: biMigrationRecords) {
Long clusterId = biMigrationRecord.getClusterId();
ClusterEntity cluster = biDirectionClusters.get(clusterId);
String clusterName = cluster.getClusterName();
if (!resp.containsKey(clusterName)) resp.put(clusterName, new ArrayList<>());
ClusterMigrationStatusV2 clusterMigrationStatus = ClusterMigrationStatusV2.from(cluster, biMigrationRecord, getBiRelatedDcs(clusterName));
resp.get(clusterName).add(clusterMigrationStatus);
}
}

return resp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void testGetClusterMigrationHistoryV2() {
req.to = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600;
req.clusters = new HashSet<>(Arrays.asList("cluster2", "bi_cluster1"));

getXpipeMeta();
Map<String, List<ClusterMigrationStatusV2>> resp = migrationApi.getClusterMigrationHistoryV2(req);
logger.info("[testGetClusterMigrationHistoryV2] {}", resp);
Assert.assertTrue(resp.containsKey("cluster2"));
Expand All @@ -82,6 +81,39 @@ public void testGetClusterMigrationHistoryV2() {
Assert.assertEquals(Collections.singleton("oy"), resp.get("bi_cluster1").get(0).destDcs);
}

@Test
public void testOnlyGetOneWayClusterMigrationHistoryV2() {
MigrationHistoryReq req = new MigrationHistoryReq();
req.from = 0;
req.to = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600;
req.clusters = new HashSet<>(Arrays.asList("cluster2"));

Map<String, List<ClusterMigrationStatusV2>> resp = migrationApi.getClusterMigrationHistoryV2(req);
Assert.assertEquals(1, resp.size());
logger.info("[testGetClusterMigrationHistoryV2] {}", resp);
Assert.assertTrue(resp.containsKey("cluster2"));
Assert.assertEquals(1, resp.get("cluster2").size());
Assert.assertEquals("Processing", resp.get("cluster2").get(0).status);
Assert.assertEquals(Collections.singleton("jq"), resp.get("cluster2").get(0).sourceDcs);
Assert.assertEquals(Collections.singleton("oy"), resp.get("cluster2").get(0).destDcs);
}

@Test
public void testOnlyGetBiClusterMigrationHistoryV2() {
MigrationHistoryReq req = new MigrationHistoryReq();
req.from = 0;
req.to = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600;
req.clusters = new HashSet<>(Arrays.asList("bi_cluster1"));

Map<String, List<ClusterMigrationStatusV2>> resp = migrationApi.getClusterMigrationHistoryV2(req);
Assert.assertEquals(1, resp.size());
Assert.assertTrue(resp.containsKey("bi_cluster1"));
Assert.assertEquals(1, resp.get("bi_cluster1").size());
Assert.assertEquals("Success", resp.get("bi_cluster1").get(0).status);
Assert.assertEquals(new HashSet<>(Arrays.asList("jq", "oy")), resp.get("bi_cluster1").get(0).sourceDcs);
Assert.assertEquals(Collections.singleton("oy"), resp.get("bi_cluster1").get(0).destDcs);
}

protected String prepareDatas() throws IOException {
return prepareDatasFromFile("src/test/resources/migration-test.sql");
}
Expand Down

0 comments on commit af7b1ac

Please sign in to comment.