]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18466 reindexing a project should not delete activities of other projects
authorPierre <pierre.guillot@sonarsource.com>
Thu, 9 Feb 2023 16:35:43 +0000 (17:35 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 10 Feb 2023 20:02:45 +0000 (20:02 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java

index 8918d48bd2a61cfc3dd98d7402b1e1fed50613c6..fd0c1f63ccd1cc8b796745eac5b23d1c10263c8b 100644 (file)
@@ -144,38 +144,41 @@ public class AsyncIssueIndexingImpl implements AsyncIssueIndexing {
   }
 
   private void removeExistingIndexationTasks(DbSession dbSession) {
-    removeIndexationTasks(dbSession, dbClient.ceQueueDao().selectAllInAscOrder(dbSession));
+    Set<String> ceQueueUuids = dbClient.ceQueueDao().selectAllInAscOrder(dbSession)
+      .stream().filter(p -> p.getTaskType().equals(BRANCH_ISSUE_SYNC))
+      .map(CeQueueDto::getUuid).collect(Collectors.toSet());
+    Set<String> ceActivityUuids = dbClient.ceActivityDao().selectByTaskType(dbSession, BRANCH_ISSUE_SYNC)
+      .stream().map(CeActivityDto::getUuid).collect(Collectors.toSet());
+    removeIndexationTasks(dbSession, ceQueueUuids, ceActivityUuids);
   }
 
   private void removeExistingIndexationTasksForProject(DbSession dbSession, String projectUuid) {
-    removeIndexationTasks(dbSession, dbClient.ceQueueDao().selectByMainComponentUuid(dbSession, projectUuid));
+    Set<String> ceQueueUuidsForProject = dbClient.ceQueueDao().selectByMainComponentUuid(dbSession, projectUuid)
+      .stream().filter(p -> p.getTaskType().equals(BRANCH_ISSUE_SYNC))
+      .map(CeQueueDto::getUuid).collect(Collectors.toSet());
+    Set<String> ceActivityUuidsForProject = dbClient.ceActivityDao().selectByTaskType(dbSession, BRANCH_ISSUE_SYNC)
+      .stream()
+      .filter(ceActivityDto -> projectUuid.equals(ceActivityDto.getMainComponentUuid()))
+      .map(CeActivityDto::getUuid).collect(Collectors.toSet());
+    removeIndexationTasks(dbSession, ceQueueUuidsForProject, ceActivityUuidsForProject);
   }
 
-  private void removeIndexationTasks(DbSession dbSession, List<CeQueueDto> ceQueueDtos) {
-    List<String> uuids = ceQueueDtos.stream()
-      .filter(p -> p.getTaskType().equals(BRANCH_ISSUE_SYNC))
-      .map(CeQueueDto::getUuid)
-      .toList();
-
-    LOG.info(String.format("%s pending indexation task found to be deleted...", uuids.size()));
-    for (String uuid : uuids) {
+  private void removeIndexationTasks(DbSession dbSession, Set<String> ceQueueUuids, Set<String> ceActivityUuids) {
+    LOG.info(String.format("%s pending indexation task found to be deleted...", ceQueueUuids.size()));
+    for (String uuid : ceQueueUuids) {
       dbClient.ceQueueDao().deleteByUuid(dbSession, uuid);
     }
-    dbSession.commit();
 
-    Set<String> ceUuids = dbClient.ceActivityDao().selectByTaskType(dbSession, BRANCH_ISSUE_SYNC).stream()
-      .map(CeActivityDto::getUuid)
-      .collect(Collectors.toSet());
-    LOG.info(String.format("%s completed indexation task found to be deleted...", uuids.size()));
-    dbClient.ceActivityDao().deleteByUuids(dbSession, ceUuids);
-    dbSession.commit();
+    LOG.info(String.format("%s completed indexation task found to be deleted...", ceQueueUuids.size()));
+    dbClient.ceActivityDao().deleteByUuids(dbSession, ceActivityUuids);
     LOG.info("Indexation task deletion complete.");
 
     LOG.info("Deleting tasks characteristics...");
-    Set<String> tasksUuid = Stream.concat(uuids.stream(), ceUuids.stream()).collect(Collectors.toSet());
+    Set<String> tasksUuid = Stream.concat(ceQueueUuids.stream(), ceActivityUuids.stream()).collect(Collectors.toSet());
     dbClient.ceTaskCharacteristicsDao().deleteByTaskUuids(dbSession, tasksUuid);
-    dbSession.commit();
     LOG.info("Tasks characteristics deletion complete.");
+
+    dbSession.commit();
   }
 
   private CeTaskSubmit buildTaskSubmit(BranchDto branch) {
index 09ad2bef97cf50f9769af8395f438141a03c0a1c..f1aa41c491ba4686d9c7bb5e15c8b4bc774b5d6a 100644 (file)
@@ -150,7 +150,11 @@ public class AsyncIssueIndexingImplTest {
 
     underTest.triggerOnIndexCreation();
 
-    assertCeTasks(reportTaskUuid);
+    assertThat(dbClient.ceQueueDao().selectAllInAscOrder(dbTester.getSession())).extracting("uuid").containsExactly(reportTaskUuid);
+    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), BRANCH_ISSUE_SYNC)).isEmpty();
+    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1);
+    assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(List.of("uuid_2")))).isEmpty();
+
     assertThat(logTester.logs(LoggerLevel.INFO))
       .contains(
         "1 pending indexation task found to be deleted...",
@@ -167,17 +171,36 @@ public class AsyncIssueIndexingImplTest {
     ProjectDto projectDto = dbTester.components().insertPrivateProjectDto();
     String branchUuid = "branch_uuid";
     dbTester.components().insertProjectBranch(projectDto, b -> b.setBranchType(BRANCH).setUuid(branchUuid));
+
     CeQueueDto mainBranchTask = new CeQueueDto().setUuid("uuid_2").setTaskType(BRANCH_ISSUE_SYNC)
       .setMainComponentUuid(projectDto.getUuid()).setComponentUuid(projectDto.getUuid());
+    dbClient.ceQueueDao().insert(dbTester.getSession(), mainBranchTask);
+
     CeQueueDto branchTask = new CeQueueDto().setUuid("uuid_3").setTaskType(BRANCH_ISSUE_SYNC)
       .setMainComponentUuid(projectDto.getUuid()).setComponentUuid(branchUuid);
-    dbClient.ceQueueDao().insert(dbTester.getSession(), mainBranchTask);
     dbClient.ceQueueDao().insert(dbTester.getSession(), branchTask);
+
+    ProjectDto anotherProjectDto = dbTester.components().insertPrivateProjectDto();
+    CeQueueDto taskOnAnotherProject = new CeQueueDto().setUuid("uuid_4").setTaskType(BRANCH_ISSUE_SYNC)
+      .setMainComponentUuid(anotherProjectDto.getUuid()).setComponentUuid("another-branchUuid");
+    CeActivityDto canceledTaskOnAnotherProject = new CeActivityDto(taskOnAnotherProject).setStatus(Status.CANCELED);
+    dbClient.ceActivityDao().insert(dbTester.getSession(), canceledTaskOnAnotherProject);
+
     dbTester.commit();
 
     underTest.triggerForProject(projectDto.getUuid());
 
-    assertCeTasks(reportTaskUuid);
+    assertThat(dbClient.ceQueueDao().selectAllInAscOrder(dbTester.getSession())).extracting("uuid")
+      .containsExactly(reportTaskUuid);
+    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1);
+    assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(List.of("uuid_2")))).isEmpty();
+
+    // verify that the canceled tasks on anotherProject is still here, and was not removed by the project reindexation
+    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), BRANCH_ISSUE_SYNC))
+      .hasSize(1)
+      .extracting(CeActivityDto::getMainComponentUuid)
+      .containsExactly(anotherProjectDto.getUuid());
+
     assertThat(logTester.logs(LoggerLevel.INFO))
       .contains(
         "2 pending indexation task found to be deleted...",
@@ -327,12 +350,4 @@ public class AsyncIssueIndexingImplTest {
     return reportTask.getUuid();
   }
 
-  private void assertCeTasks(String reportTaskUuid) {
-    assertThat(dbClient.ceQueueDao().selectAllInAscOrder(dbTester.getSession())).extracting("uuid")
-      .containsExactly(reportTaskUuid);
-    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), BRANCH_ISSUE_SYNC)).isEmpty();
-    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1);
-    assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(List.of("uuid_2")))).isEmpty();
-  }
-
 }