aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-06-10 15:00:55 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-06-10 15:00:55 +0200
commit2d44a387b9aed4ae15b2f0d5d54a408297053fee (patch)
tree23d0254ee772821aa5e69645ec9ee6533c3f3b52
parentc08a0d6b059a46f5807e22ba43afa3eb7cc356b4 (diff)
downloadsonarqube-2d44a387b9aed4ae15b2f0d5d54a408297053fee.tar.gz
sonarqube-2d44a387b9aed4ae15b2f0d5d54a408297053fee.zip
SONAR-6596 Remove database semaphores from batch
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchDatabase.java10
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java9
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java90
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java4
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/ProjectLockTest.java102
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreUpdater.java11
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/Semaphores.java9
7 files changed, 13 insertions, 222 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchDatabase.java
index a2ed089ee18..1907c948578 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchDatabase.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchDatabase.java
@@ -19,11 +19,10 @@
*/
package org.sonar.batch.bootstrap;
+import java.util.Properties;
import org.sonar.api.config.Settings;
import org.sonar.core.persistence.DefaultDatabase;
-import java.util.Properties;
-
/**
* @since 2.12
*/
@@ -38,11 +37,10 @@ public class BatchDatabase extends DefaultDatabase {
@Override
protected void doCompleteProperties(Properties properties) {
- // three connections are required : one for Hibernate, one for MyBatis for regular operations,
- // and one for the SemaphoreUpdater
+ // two connections are required : one for Hibernate, and one for MyBatis for regular operations
// Note that Hibernate will be removed soon
- properties.setProperty("sonar.jdbc.initialSize", "3");
- properties.setProperty("sonar.jdbc.maxActive", "3");
+ properties.setProperty("sonar.jdbc.initialSize", "2");
+ properties.setProperty("sonar.jdbc.maxActive", "2");
// SONAR-2965
properties.setProperty("sonar.jdbc.defaultAutoCommit", "false");
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java
index 2e1542ada81..b2280f7bcb8 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java
@@ -19,6 +19,8 @@
*/
package org.sonar.batch.bootstrap;
+import java.util.List;
+import java.util.Map;
import org.sonar.api.CoreProperties;
import org.sonar.api.Plugin;
import org.sonar.api.config.EmailSettings;
@@ -51,8 +53,6 @@ import org.sonar.core.i18n.RuleI18nManager;
import org.sonar.core.persistence.DaoUtils;
import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.persistence.SemaphoreUpdater;
-import org.sonar.core.persistence.SemaphoresImpl;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.core.platform.PluginClassloaderFactory;
import org.sonar.core.platform.PluginInfo;
@@ -66,9 +66,6 @@ import org.sonar.jpa.dao.MeasuresDao;
import org.sonar.jpa.session.DefaultDatabaseConnector;
import org.sonar.jpa.session.JpaDatabaseSession;
-import java.util.List;
-import java.util.Map;
-
public class GlobalContainer extends ComponentContainer {
private final Map<String, String> bootstrapProperties;
@@ -152,8 +149,6 @@ public class GlobalContainer extends ComponentContainer {
RuleI18nManager.class,
MeasuresDao.class,
HibernateUserFinder.class,
- SemaphoreUpdater.class,
- SemaphoresImpl.class,
PastSnapshotFinderByDate.class,
PastSnapshotFinderByDays.class,
PastSnapshotFinderByPreviousAnalysis.class,
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java
deleted file mode 100644
index bdd70c14d60..00000000000
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch.scan;
-
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.i18n.I18n;
-import org.sonar.api.resources.Project;
-import org.sonar.api.utils.Semaphores;
-import org.sonar.api.utils.SonarException;
-import org.sonar.batch.DefaultProjectTree;
-import org.sonar.batch.bootstrap.DefaultAnalysisMode;
-
-import java.util.Locale;
-
-public class ProjectLock {
-
- private static final Logger LOG = LoggerFactory.getLogger(ProjectLock.class);
-
- private final Semaphores semaphores;
- private final DefaultProjectTree projectTree;
- private final DefaultAnalysisMode analysisMode;
- private final I18n i18n;
-
- public ProjectLock(Semaphores semaphores, DefaultProjectTree projectTree, DefaultAnalysisMode analysisMode, I18n i18n) {
- this.semaphores = semaphores;
- this.projectTree = projectTree;
- this.analysisMode = analysisMode;
- this.i18n = i18n;
- }
-
- public void start() {
- if (!analysisMode.isPreview() && StringUtils.isNotBlank(getProject().getKey())) {
- Semaphores.Semaphore semaphore = acquire();
- if (!semaphore.isLocked()) {
- LOG.error(getErrorMessage(semaphore));
- throw new SonarException("The project is already being analysed.");
- }
- }
- }
-
- private String getErrorMessage(Semaphores.Semaphore semaphore) {
- long duration = semaphore.getDurationSinceLocked();
- String durationDisplay = i18n.age(Locale.ENGLISH, duration);
-
- return "It looks like an analysis of '" + getProject().getName() + "' is already running (started " + durationDisplay + " ago).";
- }
-
- public void stop() {
- if (!analysisMode.isPreview()) {
- release();
- }
- }
-
- private Semaphores.Semaphore acquire() {
- LOG.debug("Acquire semaphore on project : {}, with key {}", getProject(), getSemaphoreKey());
- return semaphores.acquire(getSemaphoreKey(), 15, 10);
- }
-
- private void release() {
- LOG.debug("Release semaphore on project : {}, with key {}", getProject(), getSemaphoreKey());
- semaphores.release(getSemaphoreKey());
- }
-
- private String getSemaphoreKey() {
- return "batch-" + getProject().getKey();
- }
-
- private Project getProject() {
- return projectTree.getRootProject();
- }
-}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
index b3295cb2974..0cb92aa0994 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
@@ -219,9 +219,7 @@ public class ProjectScanContainer extends ComponentContainer {
DefaultTechnicalDebtModel.class,
// Issue tracking
- InitialOpenIssuesStack.class,
-
- ProjectLock.class);
+ InitialOpenIssuesStack.class);
}
private void addBatchExtensions() {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectLockTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectLockTest.java
deleted file mode 100644
index 6df0b99025c..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectLockTest.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch.scan;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.i18n.I18n;
-import org.sonar.api.resources.Project;
-import org.sonar.api.utils.Semaphores;
-import org.sonar.api.utils.SonarException;
-import org.sonar.batch.DefaultProjectTree;
-import org.sonar.batch.bootstrap.DefaultAnalysisMode;
-
-import java.util.Locale;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
-
-public class ProjectLockTest {
-
- ProjectLock projectLock;
- Semaphores semaphores = mock(Semaphores.class);
- DefaultProjectTree projectTree = mock(DefaultProjectTree.class);
- I18n i18n = mock(I18n.class);
- Project project;
- private DefaultAnalysisMode mode;
-
- @Before
- public void setUp() {
- mode = mock(DefaultAnalysisMode.class);
-
- project = new Project("my-project-key");
- when(projectTree.getRootProject()).thenReturn(project);
-
- projectLock = new ProjectLock(semaphores, projectTree, mode, i18n);
- }
-
- @Test
- public void shouldAcquireSemaphore() {
- when(semaphores.acquire(anyString(), anyInt(), anyInt())).thenReturn(new Semaphores.Semaphore().setLocked(true));
- projectLock.start();
-
- verify(semaphores).acquire("batch-my-project-key", 15, 10);
- }
-
- @Test
- public void shouldNotAcquireSemaphoreIfTheProjectIsAlreadyBeenAnalysing() {
- when(semaphores.acquire(anyString(), anyInt(), anyInt())).thenReturn(new Semaphores.Semaphore().setLocked(false).setDurationSinceLocked(1234L));
- try {
- projectLock.start();
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(SonarException.class);
- }
- verify(i18n).age(eq(Locale.ENGLISH), eq(1234L));
- }
-
- @Test
- public void shouldNotAcquireSemaphoreInDryRunMode() {
- when(mode.isPreview()).thenReturn(true);
- projectLock.start();
- verifyZeroInteractions(semaphores);
- }
-
- @Test
- public void shouldReleaseSemaphore() {
- projectLock.stop();
- verify(semaphores).release("batch-my-project-key");
- }
-
- @Test
- public void shouldNotReleaseSemaphoreInPreviewMode() {
- when(mode.isPreview()).thenReturn(true);
- projectLock.stop();
- verifyZeroInteractions(semaphores);
- }
-
-}
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreUpdater.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreUpdater.java
index e300c965e56..77ff8402d9f 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreUpdater.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreUpdater.java
@@ -20,21 +20,18 @@
package org.sonar.core.persistence;
import com.google.common.collect.Maps;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.BatchSide;
-import org.sonar.api.server.ServerSide;
-import org.sonar.api.utils.Semaphores;
-
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.Semaphores;
/**
* @since 3.5
*/
-@BatchSide
@ServerSide
public class SemaphoreUpdater {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Semaphores.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Semaphores.java
index ec47356e75d..a5281c42cab 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Semaphores.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Semaphores.java
@@ -19,20 +19,15 @@
*/
package org.sonar.api.utils;
-import org.sonar.api.batch.BatchSide;
-import org.sonar.api.server.ServerSide;
-
-import javax.annotation.Nullable;
-
import java.util.Date;
+import javax.annotation.Nullable;
+import org.sonar.api.server.ServerSide;
/**
* A semaphore shared among all the processes that can connect to the central database.
- * It's ignored when enabling the dry run mode.
*
* @since 3.4
*/
-@BatchSide
@ServerSide
public interface Semaphores {