From 65fc8f900285d67c59f2c94dfc8428949874b39e Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 14 Jul 2017 17:12:23 +0200 Subject: Select finished analyses in DB by component uuids and from dates --- .../org/sonar/ce/queue/InternalCeQueueImpl.java | 9 ++-- .../java/org/sonar/db/component/SnapshotDao.java | 63 ++++++++++++++++++++++ .../org/sonar/db/component/SnapshotMapper.java | 5 ++ .../org/sonar/db/component/SnapshotMapper.xml | 16 ++++++ .../org/sonar/db/component/SnapshotDaoTest.java | 52 ++++++++++++++++++ .../org/sonar/server/ce/ws/ActivityActionTest.java | 2 +- 6 files changed, 141 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java index 72c98073dce..ee30acd613a 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java @@ -43,7 +43,6 @@ import org.sonar.db.ce.CeQueueDto; import org.sonar.server.organization.DefaultOrganizationProvider; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; @ComputeEngineSide @@ -99,14 +98,14 @@ public class InternalCeQueueImpl extends CeQueueImpl implements InternalCeQueue public void remove(CeTask task, CeActivityDto.Status status, @Nullable CeTaskResult taskResult, @Nullable Throwable error) { checkArgument(error == null || status == CeActivityDto.Status.FAILED, "Error can be provided only when status is FAILED"); try (DbSession dbSession = dbClient.openSession(false)) { - Optional queueDto = dbClient.ceQueueDao().selectByUuid(dbSession, task.getUuid()); - checkState(queueDto.isPresent(), "Task does not exist anymore: %s", task); - CeActivityDto activityDto = new CeActivityDto(queueDto.get()); + CeQueueDto queueDto = dbClient.ceQueueDao().selectByUuid(dbSession, task.getUuid()) + .orElseThrow(() -> new IllegalStateException("Task does not exist anymore: " + task)); + CeActivityDto activityDto = new CeActivityDto(queueDto); activityDto.setStatus(status); updateQueueStatus(status, activityDto); updateTaskResult(activityDto, taskResult); updateError(activityDto, error); - remove(dbSession, queueDto.get(), activityDto); + remove(dbSession, queueDto, activityDto); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java index 15409db9997..2c759cafb42 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -23,15 +23,21 @@ import com.google.common.collect.Lists; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.stream.IntStream; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.session.RowBounds; +import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.ce.CeActivityDto.Status; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.FluentIterable.from; +import static java.util.Objects.requireNonNull; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class SnapshotDao implements Dao { @@ -91,6 +97,20 @@ public class SnapshotDao implements Dao { return snapshotDtos.isEmpty() ? null : snapshotDtos.get(0); } + /** + * + */ + public List selectFinishedByComponentUuidsAndFromDates(DbSession dbSession, List componentUuids, List fromDates) { + checkArgument(componentUuids.size() == fromDates.size(), "The number of components (%s) and from dates (%s) must be the same.", + String.valueOf(componentUuids.size()), + String.valueOf(fromDates.size())); + List componentUuidFromDatePairs = IntStream.range(0, componentUuids.size()) + .mapToObj(i -> new ComponentUuidFromDatePair(componentUuids.get(i), fromDates.get(i))) + .collect(MoreCollectors.toList(componentUuids.size())); + + return executeLargeInputs(componentUuidFromDatePairs, partition -> mapper(dbSession).selectFinishedByComponentUuidsAndFromDates(partition, Status.SUCCESS), i -> i / 2); + } + public void switchIsLastFlagAndSetProcessedStatus(DbSession dbSession, String componentUuid, String analysisUuid) { SnapshotMapper mapper = mapper(dbSession); mapper.unsetIsLastFlagForComponentUuid(componentUuid); @@ -129,4 +149,47 @@ public class SnapshotDao implements Dao { private static SnapshotMapper mapper(DbSession session) { return session.getMapper(SnapshotMapper.class); } + + static class ComponentUuidFromDatePair implements Comparable { + private final String componentUuid; + private final long from; + + ComponentUuidFromDatePair(String componentUuid, long from) { + this.componentUuid = requireNonNull(componentUuid); + this.from = from; + } + + @Override + public int compareTo(ComponentUuidFromDatePair other) { + if (this == other) { + return 0; + } + + int c = componentUuid.compareTo(other.componentUuid); + if (c == 0) { + c = Long.compare(from, other.from); + } + + return c; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + ComponentUuidFromDatePair other = (ComponentUuidFromDatePair) o; + return componentUuid.equals(other.componentUuid) + && from == other.from; + } + + @Override + public int hashCode() { + return Objects.hash(componentUuid, from); + } + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java index 60f979748d5..b8a6226d44f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java @@ -24,6 +24,8 @@ import java.util.List; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.RowBounds; +import org.sonar.db.ce.CeActivityDto; +import org.sonar.db.component.SnapshotDao.ComponentUuidFromDatePair; public interface SnapshotMapper { @@ -52,4 +54,7 @@ public interface SnapshotMapper { void setIsLastFlagForAnalysisUuid(@Param("analysisUuid") String analysisUuid); void update(SnapshotDto analysis); + + List selectFinishedByComponentUuidsAndFromDates(@Param("componentUuidFromDatePairs") List pairs, + @Param("ceStatus") CeActivityDto.Status ceStatus); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml index 5d2a0a478d6..3ba07ab067c 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml @@ -98,6 +98,22 @@ + +