aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce-task-projectanalysis
diff options
context:
space:
mode:
authorEric Hartmann <hartmann.eric@gmail.com>2018-08-28 14:27:04 +0200
committerEric Hartmann <hartmann.eric@gmail.com>2018-08-30 10:34:41 +0200
commit1d2eb3b5ac8a23c5c4aa1b1ede5ce3f24840ce8a (patch)
tree90719e437e539e69a8c9d54a791e6e61c1ccf0dd /server/sonar-ce-task-projectanalysis
parent839fff93961cd93c14fd2986825a8b778e846c4a (diff)
downloadsonarqube-1d2eb3b5ac8a23c5c4aa1b1ede5ce3f24840ce8a.tar.gz
sonarqube-1d2eb3b5ac8a23c5c4aa1b1ede5ce3f24840ce8a.zip
SONAR-11097 Order live measures to prevent deadlocks
Diffstat (limited to 'server/sonar-ce-task-projectanalysis')
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistLiveMeasuresStep.java24
1 files changed, 9 insertions, 15 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistLiveMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistLiveMeasuresStep.java
index 4b50f7343fd..299b812247d 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistLiveMeasuresStep.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistLiveMeasuresStep.java
@@ -22,7 +22,6 @@ package org.sonar.ce.task.projectanalysis.step;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
@@ -42,8 +41,8 @@ import org.sonar.ce.task.step.ComputationStep;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.measure.LiveMeasureComparator;
import org.sonar.db.measure.LiveMeasureDao;
-import org.sonar.db.measure.LiveMeasureDto;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;
@@ -110,7 +109,6 @@ public class PersistLiveMeasuresStep implements ComputationStep {
@Override
public void visitAny(Component component) {
- int count = 0;
LiveMeasureDao dao = dbClient.liveMeasureDao();
Multimap<String, Measure> measures = measureRepository.getRawMeasures(component);
for (Map.Entry<String, Collection<Measure>> measuresByMetricKey : measures.asMap().entrySet()) {
@@ -120,21 +118,16 @@ public class PersistLiveMeasuresStep implements ComputationStep {
}
Metric metric = metricRepository.getByKey(metricKey);
Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate();
- Iterator<LiveMeasureDto> liveMeasures = measuresByMetricKey.getValue().stream()
+ measuresByMetricKey.getValue().stream()
.filter(NonEmptyMeasure.INSTANCE)
.filter(notBestValueOptimized)
.map(measure -> measureToMeasureDto.toLiveMeasureDto(measure, metric, component))
- .iterator();
- while (liveMeasures.hasNext()) {
- dao.insertOrUpdate(dbSession, liveMeasures.next(), marker);
- count++;
- total++;
- if (count % 100 == 0) {
- // use short transactions to avoid potential deadlocks on MySQL
- // https://jira.sonarsource.com/browse/SONAR-10117?focusedCommentId=153555&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-153555
- dbSession.commit();
- }
- }
+ // To prevent deadlock, live measures are ordered the same way as in LiveMeasureComputerImpl#refreshComponentsOnSameProject
+ .sorted(LiveMeasureComparator.INSTANCE)
+ .forEach(lm -> {
+ dao.insertOrUpdate(dbSession, lm, marker);
+ total++;
+ });
}
}
}
@@ -148,4 +141,5 @@ public class PersistLiveMeasuresStep implements ComputationStep {
}
}
+
}