diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-08 13:54:39 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-08 16:00:52 +0200 |
commit | 3a77f796dd8dc89698378fd51fa74c2795b7570b (patch) | |
tree | 16be3e8b1e4dd3f4abcd8e99bfacba222c9b486b /sonar-db | |
parent | 04925ce87b32704eafdc3fc193cf2f1f23f138af (diff) | |
download | sonarqube-3a77f796dd8dc89698378fd51fa74c2795b7570b.tar.gz sonarqube-3a77f796dd8dc89698378fd51fa74c2795b7570b.zip |
SONAR-6701 - Drop the db table SEMAPHORES
Diffstat (limited to 'sonar-db')
16 files changed, 21 insertions, 831 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/DaoUtils.java b/sonar-db/src/main/java/org/sonar/db/DaoUtils.java index 409d3d6b344..86d53c57caa 100644 --- a/sonar-db/src/main/java/org/sonar/db/DaoUtils.java +++ b/sonar-db/src/main/java/org/sonar/db/DaoUtils.java @@ -53,7 +53,6 @@ import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualityprofile.QualityProfileDao; -import org.sonar.db.semaphore.SemaphoreDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.AuthorizationDao; @@ -104,7 +103,6 @@ public final class DaoUtils { ResourceDao.class, ResourceKeyUpdaterDao.class, RoleDao.class, - SemaphoreDao.class, SnapshotDao.class, UserDao.class, UserGroupDao.class, diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index f71210c74ee..3017ac1a0a0 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -54,7 +54,6 @@ import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualityprofile.QualityProfileDao; -import org.sonar.db.semaphore.SemaphoreDao; import org.sonar.db.source.FileSourceDao; import org.sonar.db.user.AuthorDao; import org.sonar.db.user.AuthorizationDao; @@ -106,7 +105,6 @@ public class DbClient { private final ProjectQgateAssociationDao projectQgateAssociationDao; private final DuplicationDao duplicationDao; private final NotificationQueueDao notificationQueueDao; - private final SemaphoreDao semaphoreDao; public DbClient(Database database, MyBatis myBatis, Dao[] daos) { this.database = database; @@ -155,7 +153,6 @@ public class DbClient { projectQgateAssociationDao = getDao(map, ProjectQgateAssociationDao.class); duplicationDao = getDao(map, DuplicationDao.class); notificationQueueDao = getDao(map, NotificationQueueDao.class); - semaphoreDao = getDao(map, SemaphoreDao.class); doOnLoad(map); } @@ -332,10 +329,6 @@ public class DbClient { return notificationQueueDao; } - public SemaphoreDao semaphoreDao() { - return semaphoreDao; - } - protected <K extends Dao> K getDao(Map<Class, Dao> map, Class<K> clazz) { return (K) map.get(clazz); } diff --git a/sonar-db/src/main/java/org/sonar/db/MyBatis.java b/sonar-db/src/main/java/org/sonar/db/MyBatis.java index e4e3bce008b..8bf39f3dedf 100644 --- a/sonar-db/src/main/java/org/sonar/db/MyBatis.java +++ b/sonar-db/src/main/java/org/sonar/db/MyBatis.java @@ -121,8 +121,6 @@ import org.sonar.db.qualityprofile.QualityProfileMapper; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMapper; import org.sonar.db.rule.RuleParamDto; -import org.sonar.db.semaphore.SemaphoreDto; -import org.sonar.db.semaphore.SemaphoreMapper; import org.sonar.db.source.FileSourceMapper; import org.sonar.db.user.AuthorDto; import org.sonar.db.user.AuthorMapper; @@ -206,7 +204,6 @@ public class MyBatis { loadAlias(conf, "Rule", RuleDto.class); loadAlias(conf, "RuleParam", RuleParamDto.class); loadAlias(conf, "Snapshot", SnapshotDto.class); - loadAlias(conf, "Semaphore", SemaphoreDto.class); loadAlias(conf, "SchemaMigration", SchemaMigrationDto.class); loadAlias(conf, "User", UserDto.class); loadAlias(conf, "UserRole", UserRoleDto.class); @@ -250,7 +247,7 @@ public class MyBatis { IsAliveMapper.class, LoadedTemplateMapper.class, MeasureFilterMapper.class, Migration44Mapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, ResourceKeyUpdaterMapper.class, ResourceIndexMapper.class, RoleMapper.class, RuleMapper.class, - SchemaMigrationMapper.class, SemaphoreMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, + SchemaMigrationMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, FileSourceMapper.class, ActionPlanMapper.class, ActionPlanStatsMapper.class, NotificationQueueMapper.class, CharacteristicMapper.class, diff --git a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreDao.java b/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreDao.java deleted file mode 100644 index 59c89c738b1..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreDao.java +++ /dev/null @@ -1,148 +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.db.semaphore; - -import com.google.common.base.Strings; -import javax.annotation.CheckForNull; -import org.apache.ibatis.session.SqlSession; -import org.sonar.api.utils.Semaphores; -import org.sonar.api.utils.System2; -import org.sonar.db.Dao; -import org.sonar.db.MyBatis; - -import static com.google.common.base.Preconditions.checkArgument; -import static org.sonar.api.utils.DateUtils.longToDate; - -public class SemaphoreDao implements Dao { - - private static final String SEMAPHORE_NAME_MUST_NOT_BE_EMPTY = "Semaphore name must not be empty"; - private final MyBatis mybatis; - private final System2 system; - - public SemaphoreDao(MyBatis mybatis, System2 system) { - this.mybatis = mybatis; - this.system = system; - } - - public Semaphores.Semaphore acquire(String name, int maxAgeInSeconds) { - checkArgument(!Strings.isNullOrEmpty(name), SEMAPHORE_NAME_MUST_NOT_BE_EMPTY); - checkArgument(maxAgeInSeconds >= 0, "Semaphore max age must be positive: " + maxAgeInSeconds); - - try (SqlSession session = mybatis.openSession(false)) { - SemaphoreDto semaphore = tryToInsert(name, system.now(), session); - boolean isAcquired; - if (semaphore == null) { - semaphore = selectSemaphore(name, session); - isAcquired = acquireIfOutdated(name, maxAgeInSeconds, session); - } else { - isAcquired = true; - } - return createLock(semaphore, isAcquired); - } - } - - public Semaphores.Semaphore acquire(String name) { - checkArgument(!Strings.isNullOrEmpty(name), SEMAPHORE_NAME_MUST_NOT_BE_EMPTY); - - try (SqlSession session = mybatis.openSession(false)) { - SemaphoreDto semaphore = tryToInsert(name, system.now(), session); - if (semaphore == null) { - semaphore = selectSemaphore(name, session); - return createLock(semaphore, false); - } else { - return createLock(semaphore, true); - } - } - } - - public void update(Semaphores.Semaphore semaphore) { - checkArgument(semaphore != null, "Semaphore must not be null"); - - try (SqlSession session = mybatis.openSession(false)) { - mapper(session).update(semaphore.getName(), system.now()); - session.commit(); - } - } - - public void release(String name) { - checkArgument(!Strings.isNullOrEmpty(name), SEMAPHORE_NAME_MUST_NOT_BE_EMPTY); - try (SqlSession session = mybatis.openSession(false)) { - mapper(session).release(name); - session.commit(); - } - } - - private boolean acquireIfOutdated(String name, int maxAgeInSeconds, SqlSession session) { - long now = system.now(); - long updatedBefore = now - (long) maxAgeInSeconds * 1000; - - boolean ok = mapper(session).acquire(name, updatedBefore, now) == 1; - session.commit(); - return ok; - } - - /** - * Insert the semaphore and commit. Rollback and return null if the semaphore already exists in db (whatever - * the lock date) - */ - @CheckForNull - private SemaphoreDto tryToInsert(String name, long lockedNow, SqlSession session) { - try { - long now = system.now(); - SemaphoreDto semaphore = new SemaphoreDto() - .setName(name) - .setCreatedAt(now) - .setUpdatedAt(now) - .setLockedAt(lockedNow); - mapper(session).initialize(semaphore); - session.commit(); - return semaphore; - } catch (Exception e) { - // probably because the semaphore already exists in db - session.rollback(); - return null; - } - } - - private Semaphores.Semaphore createLock(SemaphoreDto dto, boolean acquired) { - Semaphores.Semaphore semaphore = new Semaphores.Semaphore() - .setName(dto.getName()) - .setLocked(acquired) - .setLockedAt(longToDate(dto.getLockedAt())) - .setCreatedAt(longToDate(dto.getCreatedAt())) - .setUpdatedAt(longToDate(dto.getUpdatedAt())); - if (!acquired) { - semaphore.setDurationSinceLocked(lockedSince(dto)); - } - return semaphore; - } - - private long lockedSince(SemaphoreDto semaphore) { - return system.now() - semaphore.getLockedAt(); - } - - protected SemaphoreDto selectSemaphore(String name, SqlSession session) { - return mapper(session).selectSemaphore(name); - } - - private SemaphoreMapper mapper(SqlSession session) { - return session.getMapper(SemaphoreMapper.class); - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreDto.java b/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreDto.java deleted file mode 100644 index a26e6a35037..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreDto.java +++ /dev/null @@ -1,86 +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.db.semaphore; - -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.lang.builder.ReflectionToStringBuilder; - -/** - * @since 3.4 - */ -public class SemaphoreDto { - private Long id; - private String name; - private String checksum; - private Long lockedAt; - private Long createdAt; - private Long updatedAt; - - public String getName() { - return name; - } - - public SemaphoreDto setName(String s) { - this.name = s; - this.checksum = DigestUtils.md5Hex(s); - return this; - } - - public Long getLockedAt() { - return lockedAt; - } - - public SemaphoreDto setLockedAt(Long d) { - this.lockedAt = d; - return this; - } - - public Long getId() { - return id; - } - - public SemaphoreDto setId(Long id) { - this.id = id; - return this; - } - - public Long getCreatedAt() { - return createdAt; - } - - public SemaphoreDto setCreatedAt(Long createdAt) { - this.createdAt = createdAt; - return this; - } - - public Long getUpdatedAt() { - return updatedAt; - } - - public SemaphoreDto setUpdatedAt(Long updatedAt) { - this.updatedAt = updatedAt; - return this; - } - - @Override - public String toString() { - return ReflectionToStringBuilder.toString(this); - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreMapper.java b/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreMapper.java deleted file mode 100644 index fc517e9f42d..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreMapper.java +++ /dev/null @@ -1,35 +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.db.semaphore; - -import org.apache.ibatis.annotations.Param; - -public interface SemaphoreMapper { - - int initialize(SemaphoreDto semaphore); - - int acquire(@Param("name") String name, @Param("updatedBefore") Long updatedBefore, @Param("now") Long now); - - void release(String name); - - SemaphoreDto selectSemaphore(@Param("name") String name); - - void update(@Param("name") String name, @Param("now") Long now); -} diff --git a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreUpdater.java b/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreUpdater.java deleted file mode 100644 index 767539d385c..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoreUpdater.java +++ /dev/null @@ -1,80 +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.db.semaphore; - -import com.google.common.collect.Maps; -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 - */ -@ServerSide -public class SemaphoreUpdater { - - private static final Logger LOG = LoggerFactory.getLogger(SemaphoreUpdater.class); - - private SemaphoreDao dao; - - private Map<String, ScheduledExecutorService> handlers = Maps.newHashMap(); - - public SemaphoreUpdater(SemaphoreDao dao) { - this.dao = dao; - } - - public void scheduleForUpdate(final Semaphores.Semaphore semaphore, int updatePeriodInSeconds) { - ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); - Runnable updater = new Runnable() { - @Override - public void run() { - try { - LOG.debug("Updating semaphore " + semaphore.getName()); - dao.update(semaphore); - } catch (Exception e) { - LOG.error("Unable to update semaphore", e); - } - - } - }; - scheduler.scheduleWithFixedDelay(updater, updatePeriodInSeconds, updatePeriodInSeconds, TimeUnit.SECONDS); - handlers.put(semaphore.getName(), scheduler); - } - - public void stopUpdate(final String name) { - if (handlers.containsKey(name)) { - handlers.get(name).shutdown(); - try { - if (!handlers.get(name).awaitTermination(1, TimeUnit.SECONDS)) { - LOG.error("Unable to cancel semaphore updater in 1 second"); - } - } catch (InterruptedException e) { - LOG.error("Unable to cancel semaphore updater", e); - } finally { - handlers.remove(name); - } - } - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoresImpl.java b/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoresImpl.java index 63958124965..c586836fc06 100644 --- a/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoresImpl.java +++ b/sonar-db/src/main/java/org/sonar/db/semaphore/SemaphoresImpl.java @@ -21,34 +21,24 @@ package org.sonar.db.semaphore; import org.sonar.api.utils.Semaphores; -/** - * @since 3.4 - */ public class SemaphoresImpl implements Semaphores { - private SemaphoreDao dao; - private SemaphoreUpdater updater; - - public SemaphoresImpl(SemaphoreDao dao, SemaphoreUpdater updater) { - this.dao = dao; - this.updater = updater; - } - @Override public Semaphore acquire(String name, int maxAgeInSeconds, int updatePeriodInSeconds) { - Semaphore semaphore = dao.acquire(name, maxAgeInSeconds); - updater.scheduleForUpdate(semaphore, updatePeriodInSeconds); - return semaphore; + throw fail(); } @Override public Semaphore acquire(String name) { - return dao.acquire(name); + throw fail(); } @Override public void release(String name) { - updater.stopUpdate(name); - dao.release(name); + throw fail(); + } + + private static RuntimeException fail() { + throw new UnsupportedOperationException("Semaphores are not supported since 5.2 and the drop of database connection from analyzer"); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index e8435f2b05b..7313d12d2f8 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -28,7 +28,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 923; + public static final int LAST_VERSION = 924; /** * List of all the tables. @@ -75,7 +75,6 @@ public class DatabaseVersion { "rules", "rules_parameters", "rules_profiles", - "semaphores", "schema_migrations", "snapshots", "users", diff --git a/sonar-db/src/main/resources/org/sonar/db/semaphore/SemaphoreMapper.xml b/sonar-db/src/main/resources/org/sonar/db/semaphore/SemaphoreMapper.xml deleted file mode 100644 index 5d1c1028478..00000000000 --- a/sonar-db/src/main/resources/org/sonar/db/semaphore/SemaphoreMapper.xml +++ /dev/null @@ -1,36 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - -<mapper namespace="org.sonar.db.semaphore.SemaphoreMapper"> - - <insert id="initialize" parameterType="map" useGeneratedKeys="false"> - INSERT INTO semaphores (name, checksum, created_at, updated_at, locked_at) - VALUES (#{name}, #{checksum}, #{createdAt}, #{updatedAt}, #{lockedAt}) - </insert> - - <update id="acquire" parameterType="map"> - update semaphores - set updated_at = #{now}, locked_at = #{now} - where name=#{name} - <if test="updatedBefore != null"> - AND updated_at < #{updatedBefore} - </if> - </update> - - <delete id="release" parameterType="String"> - delete from semaphores where name=#{id} - </delete> - - <select id="selectSemaphore" parameterType="String" resultType="Semaphore"> - select s.id, s.name as name, s.locked_at as lockedAt, s.created_at as createdAt, s.updated_at as updatedAt - from semaphores s where s.name=#{name} - </select> - - <update id="update" parameterType="map"> - update semaphores - set updated_at = #{now} - where name=#{name} - </update> - -</mapper> - diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 706947ab524..dd4afdca565 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -311,7 +311,6 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('781'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('782'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('783'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('784'); -INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('785'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('786'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('787'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('788'); @@ -343,6 +342,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('920'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('921'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('922'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('923'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('924'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index d0ad5236787..9aa98272ef5 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -391,15 +391,6 @@ CREATE TABLE "AUTHORS" ( "UPDATED_AT" TIMESTAMP ); -CREATE TABLE "SEMAPHORES" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "NAME" VARCHAR(4000), - "CHECKSUM" VARCHAR(200), - "CREATED_AT" BIGINT, - "UPDATED_AT" BIGINT, - "LOCKED_AT" BIGINT -); - CREATE TABLE "MEASURE_FILTERS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "NAME" VARCHAR(100) NOT NULL, @@ -617,10 +608,6 @@ CREATE INDEX "RESOURCE_INDEX_RID" ON "RESOURCE_INDEX" ("RESOURCE_ID"); CREATE INDEX "INDEX_ACTION_PLANS_ON_PROJET_ID" ON "ACTION_PLANS" ("PROJECT_ID"); -CREATE UNIQUE INDEX "UNIQ_SEMAPHORE_CHECKSUMS" ON "SEMAPHORES" ("CHECKSUM"); - -CREATE INDEX "SEMAPHORE_NAMES" ON "SEMAPHORES" ("NAME"); - CREATE UNIQUE INDEX "UNIQ_AUTHOR_LOGINS" ON "AUTHORS" ("LOGIN"); CREATE INDEX "MEASURE_FILTERS_NAME" ON "MEASURE_FILTERS" ("NAME"); diff --git a/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoreDaoTest.java b/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoreDaoTest.java deleted file mode 100644 index 8652aacd5dd..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoreDaoTest.java +++ /dev/null @@ -1,301 +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.db.semaphore; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.CyclicBarrier; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.rules.ExpectedException; -import org.sonar.api.utils.Semaphores; -import org.sonar.api.utils.System2; -import org.sonar.db.DbTester; -import org.sonar.test.DbTests; - -import static org.assertj.core.api.Assertions.assertThat; - -@Category(DbTests.class) -public class SemaphoreDaoTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - System2 system = System2.INSTANCE; - - @Rule - public DbTester dbTester = DbTester.create(system); - - SemaphoreDao dao = dbTester.getDbClient().semaphoreDao(); - - @Before - public void setUp() throws Exception { - dbTester.truncateTables(); - } - - @Test - public void should_fail_to_acquire_if_null_semaphore_name() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Semaphore name must not be empty"); - - dao.acquire(null, 5000); - } - - @Test - public void should_fail_to_acquire_if_blank_semaphore_name() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Semaphore name must not be empty"); - - dao.acquire("", 5000); - } - - @Test - public void should_fail_to_acquire_if_negative_timeout() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Semaphore max age must be positive: -5000"); - - dao.acquire("foo", -5000); - } - - @Test - public void should_fail_to_release_if_blank_semaphore_name() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Semaphore name must not be empty"); - - dao.release(null); - } - - @Test - public void create_and_acquire_semaphore() { - Semaphores.Semaphore lock = dao.acquire("foo", 60); - assertThat(lock.isLocked()).isTrue(); - assertThat(lock.getDurationSinceLocked()).isNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isTrue(); - assertThat(isRecent(semaphore.getUpdatedAt())).isTrue(); - assertThat(isRecent(semaphore.getLockedAt())).isTrue(); - - dao.release("foo"); - assertThat(selectSemaphore("foo")).isNull(); - } - - @Test - public void create_and_acquire_and_update_semaphore() throws Exception { - Semaphores.Semaphore lock = dao.acquire("foo", 60); - assertThat(lock.isLocked()).isTrue(); - assertThat(lock.getDurationSinceLocked()).isNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore.getCreatedAt()).isEqualTo(semaphore.getUpdatedAt()); - - Thread.sleep(1000); - - dao.update(lock); - - semaphore = selectSemaphore("foo"); - assertThat(semaphore.getCreatedAt()).isLessThan(semaphore.getUpdatedAt()); - - dao.release("foo"); - assertThat(selectSemaphore("foo")).isNull(); - } - - @Test - public void fail_to_update_null_semaphore() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Semaphore must not be null"); - - dao.update(null); - } - - @Test - public void create_and_acquire_semaphore_when_maxage_is_zeo() { - Semaphores.Semaphore lock = dao.acquire("foo", 0); - assertThat(lock.isLocked()).isTrue(); - assertThat(lock.getDurationSinceLocked()).isNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isTrue(); - assertThat(isRecent(semaphore.getUpdatedAt())).isTrue(); - assertThat(isRecent(semaphore.getLockedAt())).isTrue(); - - dao.release("foo"); - assertThat(selectSemaphore("foo")).isNull(); - } - - @Test - public void create_and_acquire_semaphore_when_no_timeout() { - Semaphores.Semaphore lock = dao.acquire("foo"); - assertThat(lock.isLocked()).isTrue(); - assertThat(lock.getDurationSinceLocked()).isNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isTrue(); - assertThat(isRecent(semaphore.getUpdatedAt())).isTrue(); - assertThat(isRecent(semaphore.getLockedAt())).isTrue(); - - dao.release("foo"); - assertThat(selectSemaphore("foo")).isNull(); - } - - @Test - public void fail_to_acquire_locked_semaphore() { - dbTester.prepareDbUnit(getClass(), "old_semaphore.xml"); - Semaphores.Semaphore lock = dao.acquire("foo", Integer.MAX_VALUE); - assertThat(lock.isLocked()).isFalse(); - assertThat(lock.getDurationSinceLocked()).isNotNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt())).isFalse(); - assertThat(isRecent(semaphore.getLockedAt())).isFalse(); - } - - @Test - public void acquire_long_locked_semaphore() { - dbTester.prepareDbUnit(getClass(), "old_semaphore.xml"); - Semaphores.Semaphore lock = dao.acquire("foo", 60); - assertThat(lock.isLocked()).isTrue(); - assertThat(lock.getDurationSinceLocked()).isNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt())).isTrue(); - assertThat(isRecent(semaphore.getLockedAt())).isTrue(); - } - - @Test - public void acquire_locked_semaphore_when_timeout_is_zero() { - dbTester.prepareDbUnit(getClass(), "old_semaphore.xml"); - Semaphores.Semaphore lock = dao.acquire("foo", 0); - assertThat(lock.isLocked()).isTrue(); - assertThat(lock.getDurationSinceLocked()).isNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt())).isTrue(); - assertThat(isRecent(semaphore.getLockedAt())).isTrue(); - - dao.release("foo"); - assertThat(selectSemaphore("foo")).isNull(); - } - - @Test - public void fail_to_acquire_locked_semaphore_when_no_timeout() { - dbTester.prepareDbUnit(getClass(), "old_semaphore.xml"); - Semaphores.Semaphore lock = dao.acquire("foo"); - assertThat(lock.isLocked()).isFalse(); - assertThat(lock.getDurationSinceLocked()).isNotNull(); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(isRecent(semaphore.getCreatedAt())).isFalse(); - assertThat(isRecent(semaphore.getUpdatedAt())).isFalse(); - assertThat(isRecent(semaphore.getLockedAt())).isFalse(); - } - - @Test - public void should_select_semaphore_return_current_semaphore_when_acquiring() { - dao.acquire("foo"); - - SemaphoreDto semaphore = selectSemaphore("foo"); - assertThat(semaphore).isNotNull(); - assertThat(semaphore.getName()).isEqualTo("foo"); - assertThat(semaphore.getCreatedAt()).isNotNull(); - assertThat(semaphore.getUpdatedAt()).isNotNull(); - assertThat(semaphore.getLockedAt()).isNotNull(); - } - - @Test - public void test_concurrent_locks() throws Exception { - for (int tests = 0; tests < 5; tests++) { - dao.release("my-lock"); - int size = 5; - CyclicBarrier barrier = new CyclicBarrier(size); - CountDownLatch latch = new CountDownLatch(size); - - AtomicInteger locks = new AtomicInteger(0); - for (int i = 0; i < size; i++) { - new Runner(dao, locks, barrier, latch).start(); - } - latch.await(); - - // semaphore was locked only 1 time - assertThat(locks.get()).isEqualTo(1); - } - } - - private SemaphoreDto selectSemaphore(String name) { - dbTester.getSession().commit(); - return dao.selectSemaphore(name, dbTester.getSession()); - } - - private boolean isRecent(Long date) { - int oneMinuteInMs = 60 * 1000; - long future = system.now() + oneMinuteInMs; - long past = system.now() - oneMinuteInMs; - return date > past && date < future; - } - - private static class Runner extends Thread { - SemaphoreDao dao; - AtomicInteger locks; - CountDownLatch latch; - CyclicBarrier barrier; - - Runner(SemaphoreDao dao, AtomicInteger atomicSeq, CyclicBarrier barrier, CountDownLatch latch) { - this.dao = dao; - this.locks = atomicSeq; - this.latch = latch; - this.barrier = barrier; - } - - @Override - public void run() { - try { - barrier.await(); - for (int i = 0; i < 100; i++) { - if (dao.acquire("my-lock", 60 * 60 * 24).isLocked()) { - locks.incrementAndGet(); - } - } - latch.countDown(); - - } catch (Exception e) { - e.printStackTrace(); - } - } - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoreUpdaterTest.java b/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoreUpdaterTest.java deleted file mode 100644 index 7400e143e30..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoreUpdaterTest.java +++ /dev/null @@ -1,73 +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.db.semaphore; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.utils.Semaphores; - -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -public class SemaphoreUpdaterTest { - - private SemaphoreUpdater updater; - private SemaphoreDao dao; - - @Before - public void before() { - dao = mock(SemaphoreDao.class); - updater = new SemaphoreUpdater(dao); - } - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void testScheduleUpdate() throws Exception { - Semaphores.Semaphore semaphore = new Semaphores.Semaphore().setName("foo"); - updater.scheduleForUpdate(semaphore, 1); - - Thread.sleep(2000); - - verify(dao, atLeastOnce()).update(semaphore); - } - - @Test - public void testCancelUpdate() throws Exception { - Semaphores.Semaphore semaphore = new Semaphores.Semaphore().setName("foo"); - updater.scheduleForUpdate(semaphore, 1); - updater.stopUpdate("foo"); - - Thread.sleep(2000); - - verify(dao, never()).update(semaphore); - } - - @Test - public void shouldNotFailWhenCancelNotExistingSemaphore() { - updater.stopUpdate("foo"); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoresImplTest.java b/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoresImplTest.java index a8d13641fdd..c9435d547fb 100644 --- a/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoresImplTest.java +++ b/sonar-db/src/test/java/org/sonar/db/semaphore/SemaphoresImplTest.java @@ -20,32 +20,21 @@ package org.sonar.db.semaphore; import org.junit.Test; -import org.sonar.api.utils.Semaphores; - -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; public class SemaphoresImplTest { - @Test - public void should_be_a_bridge_over_dao() { - SemaphoreDao dao = mock(SemaphoreDao.class); - SemaphoreUpdater updater = mock(SemaphoreUpdater.class); - Semaphores.Semaphore semaphore = new Semaphores.Semaphore(); - when(dao.acquire(anyString(), anyInt())).thenReturn(semaphore); - - SemaphoresImpl impl = new SemaphoresImpl(dao, updater); - - impl.acquire("do-xxx", 50000, 10); - verify(dao).acquire("do-xxx", 50000); + @Test(expected = UnsupportedOperationException.class) + public void acquire_is_unsupported() { + new SemaphoresImpl().acquire("foo"); + } - impl.acquire("do-xxx"); - verify(dao).acquire("do-xxx"); + @Test(expected = UnsupportedOperationException.class) + public void acquire_with_timeout_is_unsupported() { + new SemaphoresImpl().acquire("foo", 1, 2); + } - impl.release("do-xxx"); - verify(dao).release("do-xxx"); + @Test(expected = UnsupportedOperationException.class) + public void release_is_unsupported() { + new SemaphoresImpl().release("foo"); } } diff --git a/sonar-db/src/test/resources/org/sonar/db/semaphore/SemaphoreDaoTest/old_semaphore.xml b/sonar-db/src/test/resources/org/sonar/db/semaphore/SemaphoreDaoTest/old_semaphore.xml deleted file mode 100644 index ac6cf63ee0e..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/semaphore/SemaphoreDaoTest/old_semaphore.xml +++ /dev/null @@ -1,4 +0,0 @@ -<dataset> - <semaphores id="1" name="foo" checksum="acbd18db4cc2f85cedef654fccc4a4d8" created_at="1264374000000" - updated_at="1264374000000" locked_at="1264374000000"/> -</dataset> |