diff options
author | Pierre <pierre.guillot@sonarsource.com> | 2023-02-09 17:35:43 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-02-10 20:02:45 +0000 |
commit | 0def12a0a76202cf026a4c33f8c2b5c82986bb7c (patch) | |
tree | d2f68272d4a0ea1b17fa3500db25e896c38b4dd1 /server/sonar-webserver-core | |
parent | 99d377c6d55a96d9d3cbea55c48df63c906781d4 (diff) | |
download | sonarqube-0def12a0a76202cf026a4c33f8c2b5c82986bb7c.tar.gz sonarqube-0def12a0a76202cf026a4c33f8c2b5c82986bb7c.zip |
SONAR-18466 reindexing a project should not delete activities of other projects
Diffstat (limited to 'server/sonar-webserver-core')
2 files changed, 48 insertions, 30 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java index 8918d48bd2a..fd0c1f63ccd 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java @@ -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) { diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java index 09ad2bef97c..f1aa41c491b 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java @@ -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(); - } - } |