diff options
author | Stephane Gamard <stephane.gamard@sonarsource.com> | 2014-09-29 17:51:38 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@sonarsource.com> | 2014-09-29 17:52:05 +0200 |
commit | 41fdce05769fc9cfffaf4051c7e600627398cf89 (patch) | |
tree | 74b54e3159d62e7ffb4e0183b5470533781215b7 | |
parent | 7de252ea61bdfc2cb33f74f04aa781d0ad773183 (diff) | |
download | sonarqube-41fdce05769fc9cfffaf4051c7e600627398cf89.tar.gz sonarqube-41fdce05769fc9cfffaf4051c7e600627398cf89.zip |
SONAR-5614 - Fixed BatchSession increment when enqueue ES Action
4 files changed, 31 insertions, 11 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index f584ab9efd1..d3125b117e6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -370,6 +370,7 @@ public abstract class BaseDao<MAPPER, DTO extends Dto<KEY>, KEY extends Serializ session.select(getSynchronizeStatementFQN(), getSynchronizationParams(date, params), handler); handler.enqueueCollected(); session.enqueue(new RefreshIndex(this.getIndexType())); + session.commit(); } catch (Exception e) { throw new IllegalStateException(e); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java index 46db6810b71..215ff555bb6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java @@ -624,6 +624,8 @@ public class IssueIndexMediumTest { @Test public void synchronize_issues() throws Exception { + Integer numberOfIssues = 1000; + ComponentDto project = new ComponentDto() .setKey("MyProject"); db.componentDao().insert(session, project); @@ -639,7 +641,7 @@ public class IssueIndexMediumTest { db.snapshotDao().insert(session, SnapshotTesting.createForComponent(resource)); List<String> issueKeys = newArrayList(); - for (int i = 0; i < 11; i++) { + for (int i = 0; i < numberOfIssues; i++) { IssueDto issue = IssueTesting.newDto(rule, resource, project); tester.get(IssueDao.class).insert(session, issue); issueKeys.add(issue.getKey()); @@ -647,12 +649,13 @@ public class IssueIndexMediumTest { session.commit(); // 0 Assert that all issues are both in ES and DB - assertThat(db.issueDao().findAfterDate(session, new Date(0))).hasSize(11); - assertThat(index.countAll()).isEqualTo(11); + assertThat(db.issueDao().findAfterDate(session, new Date(0))).hasSize(numberOfIssues); + assertThat(index.countAll()).isEqualTo(numberOfIssues); // Clear issue index in order to simulate these issues have been inserted without being indexed in E/S (from a previous version of SQ or from batch) tester.get(BackendCleanup.class).clearIndex(IndexDefinition.ISSUES); tester.clearIndexes(); + assertThat(index.countAll()).isEqualTo(0); DbSession newSession = db.openSession(true); try { @@ -663,7 +666,7 @@ public class IssueIndexMediumTest { newSession.close(); } - assertThat(index.countAll()).isEqualTo(11); + assertThat(index.countAll()).isEqualTo(numberOfIssues); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerMediumTest.java index 1e313236343..9ca6b1ad461 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerMediumTest.java @@ -25,6 +25,7 @@ import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.api.rule.RuleKey; +import org.sonar.core.persistence.BatchSession; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.DbClient; import org.sonar.server.platform.Platform; @@ -65,19 +66,27 @@ public class IndexSynchronizerMediumTest { @Test public void can_synchronize() throws Exception { - int numberOfRules = 100; + int numberOfRules = 1000; + int batchSize = BatchSession.MAX_BATCH_SIZE; - for (int i = 0; i < numberOfRules; i++) { - dbClient.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("test", "x" + i))); + int count = 0; + for (int step = 0; (step * batchSize) < numberOfRules; step++) { + for (int i = 0; i < batchSize; i++) { + dbClient.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("test", "x" + (count++)))); + } + dbSession.commit(); } - dbSession.commit(); assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(numberOfRules); tester.clearIndexes(); assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(0); - synchronizer.synchronize(dbSession, dbClient.ruleDao(), indexClient.get(RuleIndex.class)); - dbSession.commit(); - assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(numberOfRules); + DbSession syncSession = dbClient.openSession(true); + try { + synchronizer.synchronize(syncSession, dbClient.ruleDao(), indexClient.get(RuleIndex.class)); + assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(numberOfRules); + } finally { + syncSession.close(); + } } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java b/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java index d7f3d8255bd..68145a8b478 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java @@ -27,6 +27,7 @@ import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.SqlSession; +import org.sonar.core.cluster.ClusterAction; import org.sonar.core.cluster.WorkQueue; import java.util.List; @@ -49,6 +50,12 @@ public class BatchSession extends DbSession { } @Override + public void enqueue(ClusterAction action) { + increment(); + super.enqueue(action); + } + + @Override public void select(String statement, Object parameter, ResultHandler handler) { reset(); super.select(statement, parameter, handler); |