From 3e73b2b43c26a74d0722ba2acc9adfc62b0b33c2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 20 Oct 2016 15:08:12 +0200 Subject: [PATCH] SONAR-8306 don't fail inserting scanner context if task is restarted --- .../step/PersistScannerContextStep.java | 6 ++++++ .../step/PersistScannerContextStepTest.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java index 3d9e9823221..9be3a55f288 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java @@ -26,6 +26,8 @@ import org.sonar.db.DbSession; import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader; import org.sonar.server.computation.task.step.ComputationStep; +import static java.util.Collections.singleton; + public class PersistScannerContextStep implements ComputationStep { private final BatchReportReader reportReader; private final DbClient dbClient; @@ -47,6 +49,10 @@ public class PersistScannerContextStep implements ComputationStep { try (CloseableIterator logsIterator = reportReader.readScannerLogs()) { if (logsIterator.hasNext()) { try (DbSession dbSession = dbClient.openSession(false)) { + // in case the task was restarted, the context might have been already persisted + // for total reliability, we rather delete the existing row as we don't want to assume the content + // consistent with the report + dbClient.ceScannerContextDao().deleteByUuids(dbSession, singleton(ceTask.getUuid())); dbClient.ceScannerContextDao().insert(dbSession, ceTask.getUuid(), logsIterator); dbSession.commit(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java index 2a8f9d73592..ec75a9d4b5f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java @@ -19,11 +19,13 @@ */ package org.sonar.server.computation.task.projectanalysis.step; +import java.util.Arrays; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.ce.queue.CeTask; +import org.sonar.core.util.CloseableIterator; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; @@ -78,4 +80,19 @@ public class PersistScannerContextStepTest { .isEmpty(); } + /** + * @see SONAR-8306 + */ + @Test + public void execute_does_not_fail_if_scanner_context_has_already_been_persisted() { + dbClient.ceScannerContextDao().insert(dbTester.getSession(), ANALYSIS_UUID, CloseableIterator.from(Arrays.asList("a", "b", "c").iterator())); + dbTester.commit(); + reportReader.setScannerLogs(asList("1", "2", "3")); + when(ceTask.getUuid()).thenReturn(ANALYSIS_UUID); + + underTest.execute(); + + assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID)) + .contains("1" + '\n' + "2" + '\n' + "3"); + } } -- 2.39.5