]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9057 make CeQueueCleaner startable and run as startup task
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 30 Mar 2017 16:50:44 +0000 (18:50 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Thu, 27 Apr 2017 07:23:18 +0000 (09:23 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/CeQueueModule.java
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
server/sonar-ce/src/main/java/org/sonar/ce/queue/package-info.java [new file with mode: 0644]
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
server/sonar-server/src/main/java/org/sonar/server/computation/queue/CeQueueCleaner.java
server/sonar-server/src/main/java/org/sonar/server/computation/queue/CeQueueInitializer.java
server/sonar-server/src/test/java/org/sonar/server/computation/queue/CeQueueCleanerTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/queue/CeQueueInitializerTest.java

index db65f99042a781700b01f8f7ecff98f48113f682..a11e826cd1208e1e411fbce008efe6bd191e310a 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.ce;
 import org.sonar.core.platform.Module;
 import org.sonar.server.computation.monitoring.CEQueueStatusImpl;
 import org.sonar.server.computation.monitoring.CeTasksMBeanImpl;
-import org.sonar.server.computation.queue.CeQueueCleaner;
 import org.sonar.server.computation.queue.CeQueueInitializer;
 import org.sonar.server.computation.queue.InternalCeQueueImpl;
 
@@ -36,9 +35,6 @@ public class CeQueueModule extends Module {
       // queue monitoring
       CEQueueStatusImpl.class,
       CeTasksMBeanImpl.class,
-
-      // queue cleaning
-      CeQueueCleaner.class,
       
       // init queue state and queue processing
       CeQueueInitializer.class);
index 90fe0838bb24d935b526897b7e98fae5ffeb895a..ed306dd165a5e45256685df196b32169a1bbfaa3 100644 (file)
@@ -68,6 +68,7 @@ import org.sonar.process.logging.LogbackHelper;
 import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.component.index.ComponentIndexer;
+import org.sonar.server.computation.queue.CeQueueCleaner;
 import org.sonar.server.computation.queue.PurgeCeActivities;
 import org.sonar.server.computation.task.projectanalysis.ProjectAnalysisTaskModule;
 import org.sonar.server.computation.taskprocessor.CeTaskProcessorModule;
@@ -400,6 +401,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
       LogServerId.class,
       ServerLifecycleNotifier.class,
       PurgeCeActivities.class,
+      CeQueueCleaner.class
     };
   }
 
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/queue/package-info.java b/server/sonar-ce/src/main/java/org/sonar/ce/queue/package-info.java
new file mode 100644 (file)
index 0000000..93eae70
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.ce.queue;
+
+import javax.annotation.ParametersAreNonnullByDefault;
index edf0062e3945f7e8a5df41e79f7435efbe277ce8..33fd4cdbdb06e47838a4b6376f44ecc38354a049 100644 (file)
@@ -90,7 +90,7 @@ public class ComputeEngineContainerImplTest {
         CONTAINER_ITSELF
           + 74 // level 4
           + 4 // content of CeConfigurationModule
-          + 5 // content of CeQueueModule
+          + 4 // content of CeQueueModule
           + 3 // content of CeHttpModule
           + 3 // content of CeTaskCommonsModule
           + 4 // content of ProjectAnalysisTaskModule
index df886517af7468a9fb4194b98ee5b56476c79f40..003b66360ead1f4034bb236be4d067e0c07dae77 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.computation.queue;
 
 import java.util.List;
+import org.picocontainer.Startable;
 import org.sonar.api.ce.ComputeEngineSide;
 import org.sonar.api.platform.ServerUpgradeStatus;
 import org.sonar.api.utils.log.Logger;
@@ -32,7 +33,7 @@ import org.sonar.db.DbSession;
  * CE workers must not be started before execution of this class.
  */
 @ComputeEngineSide
-public class CeQueueCleaner {
+public class CeQueueCleaner implements Startable {
 
   private static final Logger LOGGER = Loggers.get(CeQueueCleaner.class);
 
@@ -46,11 +47,14 @@ public class CeQueueCleaner {
     this.queue = queue;
   }
 
-  public void clean(DbSession dbSession) {
+  @Override
+  public void start() {
     if (serverUpgradeStatus.isUpgraded()) {
       cleanOnUpgrade();
     } else {
-      verifyConsistency(dbSession);
+      try (DbSession dbSession = dbClient.openSession(false)) {
+        verifyConsistency(dbSession);
+      }
     }
   }
 
@@ -73,4 +77,9 @@ public class CeQueueCleaner {
     dbClient.ceTaskInputDao().deleteByUuids(dbSession, uuids);
     dbSession.commit();
   }
+
+  @Override
+  public void stop() {
+    // nothing to do
+  }
 }
index 93a260ced5a28d1b79345d3823be0b1d7ba378b4..94ea0858d0a68a175faaa26d4a19b2618f8616dc 100644 (file)
@@ -22,8 +22,6 @@ package org.sonar.server.computation.queue;
 import org.sonar.api.ce.ComputeEngineSide;
 import org.sonar.api.platform.Server;
 import org.sonar.api.platform.ServerStartHandler;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
 import org.sonar.server.computation.taskprocessor.CeProcessingScheduler;
 
 /**
@@ -34,14 +32,10 @@ import org.sonar.server.computation.taskprocessor.CeProcessingScheduler;
 @ComputeEngineSide
 public class CeQueueInitializer implements ServerStartHandler {
 
-  private final DbClient dbClient;
-  private final CeQueueCleaner cleaner;
   private final CeProcessingScheduler scheduler;
   private boolean done = false;
 
-  public CeQueueInitializer(DbClient dbClient, CeQueueCleaner cleaner, CeProcessingScheduler scheduler) {
-    this.dbClient = dbClient;
-    this.cleaner = cleaner;
+  public CeQueueInitializer(CeProcessingScheduler scheduler) {
     this.scheduler = scheduler;
   }
 
@@ -54,9 +48,6 @@ public class CeQueueInitializer implements ServerStartHandler {
   }
 
   private void initCe() {
-    try (DbSession dbSession = dbClient.openSession(false)) {
-      cleaner.clean(dbSession);
-      scheduler.startScheduling();
-    }
+    scheduler.startScheduling();
   }
 }
index 861ede73ba7e80289547b8cb42abbe230783be02..8528ee329dffbcf01bf5c9ca26fb0f1bd68a8f9c 100644 (file)
@@ -46,34 +46,34 @@ public class CeQueueCleanerTest {
   private CeQueueCleaner underTest = new CeQueueCleaner(dbTester.getDbClient(), serverUpgradeStatus, queue);
 
   @Test
-  public void reset_in_progress_tasks_to_pending() throws IOException {
+  public void start_resets_in_progress_tasks_to_pending() throws IOException {
     insertInQueue("TASK_1", CeQueueDto.Status.PENDING);
     insertInQueue("TASK_2", CeQueueDto.Status.IN_PROGRESS);
 
-    underTest.clean(dbTester.getSession());
+    underTest.start();
 
     assertThat(dbTester.getDbClient().ceQueueDao().countByStatus(dbTester.getSession(), CeQueueDto.Status.PENDING)).isEqualTo(2);
     assertThat(dbTester.getDbClient().ceQueueDao().countByStatus(dbTester.getSession(), CeQueueDto.Status.IN_PROGRESS)).isEqualTo(0);
   }
 
   @Test
-  public void clear_queue_if_version_upgrade() {
+  public void start_clears_queue_if_version_upgrade() {
     when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
 
-    underTest.clean(dbTester.getSession());
+    underTest.start();
 
     verify(queue).clear();
   }
 
   @Test
-  public void delete_orphan_report_files() throws Exception {
+  public void start_deletes_orphan_report_files() throws Exception {
     // analysis reports are persisted but the associated
     // task is not in the queue
     insertInQueue("TASK_1", CeQueueDto.Status.PENDING);
     insertTaskData("TASK_1");
     insertTaskData("TASK_2");
 
-    underTest.clean(dbTester.getSession());
+    underTest.start();
 
     CeTaskInputDao dataDao = dbTester.getDbClient().ceTaskInputDao();
     Optional<CeTaskInputDao.DataStream> task1Data = dataDao.selectData(dbTester.getSession(), "TASK_1");
index cd62b82b2e10e4612aa23be56c3a9403aa96f0dc..b458508a31e9e0445985ae09d5ff10b89ddd6075 100644 (file)
 package org.sonar.server.computation.queue;
 
 import java.io.IOException;
-import org.junit.Rule;
 import org.junit.Test;
-import org.mockito.InOrder;
-import org.mockito.Mockito;
 import org.sonar.api.platform.Server;
-import org.sonar.api.utils.System2;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
 import org.sonar.server.computation.taskprocessor.CeProcessingScheduler;
 
-import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
 
 public class CeQueueInitializerTest {
 
-  @Rule
-  public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
-  Server server = mock(Server.class);
-  CeQueueCleaner cleaner = mock(CeQueueCleaner.class);
-  CeProcessingScheduler scheduler = mock(CeProcessingScheduler.class);
-  CeQueueInitializer underTest = new CeQueueInitializer(dbTester.getDbClient(), cleaner, scheduler);
+  private Server server = mock(Server.class);
+  private CeProcessingScheduler scheduler = mock(CeProcessingScheduler.class);
+  private CeQueueInitializer underTest = new CeQueueInitializer(scheduler);
 
   @Test
   public void clean_queue_then_start_scheduler_of_workers() throws IOException {
-    InOrder inOrder = Mockito.inOrder(cleaner, scheduler);
-
     underTest.onServerStart(server);
 
-    inOrder.verify(cleaner).clean(any(DbSession.class));
-    inOrder.verify(scheduler).startScheduling();
+    verify(scheduler).startScheduling();
   }
 
   @Test
@@ -60,11 +47,11 @@ public class CeQueueInitializerTest {
 
     underTest.onServerStart(server);
 
-    reset(cleaner, scheduler);
+    reset(scheduler);
 
     underTest.onServerStart(server);
 
-    verifyZeroInteractions(cleaner, scheduler);
+    verifyZeroInteractions(scheduler);
 
   }
 }