aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-10-19 11:02:02 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-10-19 11:02:02 +0200
commit03fd91021257124849677eb12e6b3330c5755c6d (patch)
treeb8f719e430203ecf48415e17bb559a4cac4408c3 /sonar-core
parentc0a0e72e7f05afb9a46e9fe3734ae1173009ae88 (diff)
downloadsonarqube-03fd91021257124849677eb12e6b3330c5755c6d.tar.gz
sonarqube-03fd91021257124849677eb12e6b3330c5755c6d.zip
SONAR-3887 add org.sonar.api.utils.DatabaseSemaphore
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java14
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreMapper.java4
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/persistence/SemaphoreMapper.xml4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java16
4 files changed, 19 insertions, 19 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
index 2519823fadc..fd92ccc3a0f 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreDao.java
@@ -37,34 +37,34 @@ public class SemaphoreDao {
this.mybatis = mybatis;
}
- public boolean lock(String name, int durationInSeconds) {
+ public boolean acquire(String name, int maxDurationInSeconds) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Semaphore name must not be empty");
- Preconditions.checkArgument(durationInSeconds > 0, "Semaphore duration must be positive");
+ Preconditions.checkArgument(maxDurationInSeconds > 0, "Semaphore max duration must be positive");
SqlSession session = mybatis.openSession();
try {
SemaphoreMapper mapper = session.getMapper(SemaphoreMapper.class);
initialize(name, session, mapper);
- return doLock(name, durationInSeconds, session, mapper);
+ return doAcquire(name, maxDurationInSeconds, session, mapper);
} finally {
MyBatis.closeQuietly(session);
}
}
- public void unlock(String name) {
+ public void release(String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Semaphore name must not be empty");
SqlSession session = mybatis.openSession();
try {
- session.getMapper(SemaphoreMapper.class).unlock(name);
+ session.getMapper(SemaphoreMapper.class).release(name);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}
- private boolean doLock(String name, int durationInSeconds, SqlSession session, SemaphoreMapper mapper) {
+ private boolean doAcquire(String name, int durationInSeconds, SqlSession session, SemaphoreMapper mapper) {
Date lockedBefore = DateUtils.addSeconds(mapper.now(), -durationInSeconds);
- boolean ok = mapper.lock(name, lockedBefore) == 1;
+ boolean ok = mapper.acquire(name, lockedBefore) == 1;
session.commit();
return ok;
}
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreMapper.java b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreMapper.java
index a2be689e1c6..d77e8cd3c3f 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SemaphoreMapper.java
@@ -27,9 +27,9 @@ public interface SemaphoreMapper {
int initialize(@Param("name") String name, @Param("lockedAt") Date lockedAt);
- int lock(@Param("name") String name, @Param("lockedBefore") Date lockedBefore);
+ int acquire(@Param("name") String name, @Param("lockedBefore") Date lockedBefore);
Date now();
- void unlock(String name);
+ void release(String name);
}
diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/SemaphoreMapper.xml b/sonar-core/src/main/resources/org/sonar/core/persistence/SemaphoreMapper.xml
index df8dac1d124..ea5f9f1102b 100644
--- a/sonar-core/src/main/resources/org/sonar/core/persistence/SemaphoreMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/persistence/SemaphoreMapper.xml
@@ -12,14 +12,14 @@
select current_timestamp
</select>
- <update id="lock" parameterType="map">
+ <update id="acquire" parameterType="map">
update semaphores
set updated_at = current_timestamp, locked_at = current_timestamp
where name=#{name}
AND locked_at &lt; #{lockedBefore}
</update>
- <delete id="unlock" parameterType="String">
+ <delete id="release" parameterType="String">
delete from semaphores where name=#{id}
</delete>
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java
index 46617b2830b..6abc912b896 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java
@@ -36,9 +36,9 @@ import static org.fest.assertions.Assertions.assertThat;
public class SemaphoreDaoTest extends AbstractDaoTestCase {
@Test
- public void create_and_lock_semaphore() throws Exception {
+ public void create_and_acquire_semaphore() throws Exception {
SemaphoreDao dao = new SemaphoreDao(getMyBatis());
- assertThat(dao.lock("foo", 60)).isTrue();
+ assertThat(dao.acquire("foo", 60)).isTrue();
Semaphore semaphore = selectSemaphore("foo");
assertThat(semaphore).isNotNull();
@@ -47,7 +47,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
assertThat(isRecent(semaphore.updatedAt, 60)).isTrue();
assertThat(isRecent(semaphore.lockedAt, 60)).isTrue();
- dao.unlock("foo");
+ dao.release("foo");
assertThat(selectSemaphore("foo")).isNull();
}
@@ -55,7 +55,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
public void fail_to_acquire_locked_semaphore() throws Exception {
setupData("old_semaphore");
SemaphoreDao dao = new SemaphoreDao(getMyBatis());
- assertThat(dao.lock("foo", Integer.MAX_VALUE)).isFalse();
+ assertThat(dao.acquire("foo", Integer.MAX_VALUE)).isFalse();
Semaphore semaphore = selectSemaphore("foo");
assertThat(semaphore).isNotNull();
@@ -69,7 +69,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
public void acquire_long_locked_semaphore() throws Exception {
setupData("old_semaphore");
SemaphoreDao dao = new SemaphoreDao(getMyBatis());
- assertThat(dao.lock("foo", 60)).isTrue();
+ assertThat(dao.acquire("foo", 60)).isTrue();
Semaphore semaphore = selectSemaphore("foo");
assertThat(semaphore).isNotNull();
@@ -83,8 +83,8 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
public void test_concurrent_locks() throws Exception {
SemaphoreDao dao = new SemaphoreDao(getMyBatis());
- for (int tests = 0; tests < 5000; tests++) {
- dao.unlock("my-lock");
+ for (int tests = 0; tests < 5; tests++) {
+ dao.release("my-lock");
int size = 5;
CyclicBarrier barrier = new CyclicBarrier(size);
CountDownLatch latch = new CountDownLatch(size);
@@ -152,7 +152,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase {
try {
barrier.await();
for (int i = 0; i < 100; i++) {
- if (dao.lock("my-lock", 60 * 5)) {
+ if (dao.acquire("my-lock", 60 * 5)) {
locks.incrementAndGet();
}
}