From eb87d2d89414c2d621515c8b44a91e94ee653660 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 1 Mar 2018 10:08:31 +0100 Subject: [PATCH] GOV-322 support not creating task in CeQueue if pending task exists --- .../main/java/org/sonar/db/ce/CeQueueDao.java | 27 +- .../java/org/sonar/db/ce/CeQueueMapper.java | 4 +- .../main/java/org/sonar/db/ce/QueueCount.java | 34 +++ .../org/sonar/db/ce/CeQueueMapper.xml | 15 ++ .../java/org/sonar/db/ce/CeQueueDaoTest.java | 32 ++- .../main/java/org/sonar/ce/queue/CeQueue.java | 28 +- .../java/org/sonar/ce/queue/CeQueueImpl.java | 164 +++++++----- .../org/sonar/ce/queue/CeQueueImplTest.java | 244 +++++++++++++++++- 8 files changed, 468 insertions(+), 80 deletions(-) create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/ce/QueueCount.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDao.java index 30823b1253c..42260c55ae9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDao.java @@ -19,7 +19,9 @@ */ package org.sonar.db.ce; +import com.google.common.collect.ImmutableMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; @@ -30,6 +32,7 @@ import org.sonar.db.DbSession; import org.sonar.db.Pagination; import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; import static org.sonar.db.DatabaseUtils.executeLargeUpdates; import static org.sonar.db.ce.CeQueueDto.Status.IN_PROGRESS; import static org.sonar.db.ce.CeQueueDto.Status.PENDING; @@ -92,8 +95,7 @@ public class CeQueueDao implements Dao { } else { // executeLargeUpdates won't call the SQL command if knownWorkerUUIDs is empty executeLargeUpdates(knownWorkerUUIDs, - uuids -> mapper(dbSession).resetTasksWithUnknownWorkerUUIDs(uuids, system2.now()) - ); + uuids -> mapper(dbSession).resetTasksWithUnknownWorkerUUIDs(uuids, system2.now())); } } @@ -135,6 +137,27 @@ public class CeQueueDao implements Dao { return mapper(dbSession).countByStatusAndComponentUuid(status, componentUuid); } + /** + * Counts entries in the queue with the specified status for each specified component uuid. + * + * The returned map doesn't contain any entry for component uuid for which there is no entry in the queue (ie. + * all entries have a value >= 0). + */ + public Map countByStatusAndComponentUuids(DbSession dbSession, CeQueueDto.Status status, Set componentUuids) { + if (componentUuids.isEmpty()) { + return emptyMap(); + } + + ImmutableMap.Builder builder = ImmutableMap.builder(); + executeLargeUpdates( + componentUuids, + uuids -> { + List i = mapper(dbSession).countByStatusAndComponentUuids(status, componentUuids); + i.forEach(o -> builder.put(o.getComponentUuid(), o.getTotal())); + }); + return builder.build(); + } + public Optional peek(DbSession session, String workerUuid, int maxExecutionCount) { List eligibles = mapper(session).selectEligibleForPeek(maxExecutionCount, ONE_RESULT_PAGINATION); if (eligibles.isEmpty()) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueMapper.java index 9858fcb6e29..0d36f3e31a6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueMapper.java @@ -20,6 +20,7 @@ package org.sonar.db.ce; import java.util.List; +import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; @@ -56,9 +57,10 @@ public interface CeQueueMapper { */ void resetAllInProgressTasks(@Param("updatedAt") long updatedAt); - int countByStatusAndComponentUuid(@Param("status") CeQueueDto.Status status, @Nullable @Param("componentUuid") String componentUuid); + List countByStatusAndComponentUuids(@Param("status") CeQueueDto.Status status, @Param("componentUuids") Set componentUuids); + void insert(CeQueueDto dto); void resetAllToPendingStatus(@Param("updatedAt") long updatedAt); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/QueueCount.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/QueueCount.java new file mode 100644 index 00000000000..5b948c38df9 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/QueueCount.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.ce; + +public class QueueCount { + // set by reflection by MyBatis + private String componentUuid; + private int total; + + public String getComponentUuid() { + return componentUuid; + } + + public int getTotal() { + return total; + } +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeQueueMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeQueueMapper.xml index 8259043c4e0..f10b38becf1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeQueueMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeQueueMapper.xml @@ -49,6 +49,21 @@ + +