From 0def12a0a76202cf026a4c33f8c2b5c82986bb7c Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 9 Feb 2023 17:35:43 +0100 Subject: [PATCH] SONAR-18466 reindexing a project should not delete activities of other projects --- .../issue/index/AsyncIssueIndexingImpl.java | 41 ++++++++++--------- .../index/AsyncIssueIndexingImplTest.java | 37 ++++++++++++----- 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 ceQueueUuids = dbClient.ceQueueDao().selectAllInAscOrder(dbSession) + .stream().filter(p -> p.getTaskType().equals(BRANCH_ISSUE_SYNC)) + .map(CeQueueDto::getUuid).collect(Collectors.toSet()); + Set 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 ceQueueUuidsForProject = dbClient.ceQueueDao().selectByMainComponentUuid(dbSession, projectUuid) + .stream().filter(p -> p.getTaskType().equals(BRANCH_ISSUE_SYNC)) + .map(CeQueueDto::getUuid).collect(Collectors.toSet()); + Set 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 ceQueueDtos) { - List 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 ceQueueUuids, Set 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 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 tasksUuid = Stream.concat(uuids.stream(), ceUuids.stream()).collect(Collectors.toSet()); + Set 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(); - } - } -- 2.39.5