From c561396fb47457704a2ebc12f6fd153ab7706ccb Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Thu, 3 Jul 2014 18:19:28 +0200 Subject: [PATCH] SONAR-5007 - Fix date-synch for ES Synchronizer --- .../org/sonar/server/search/BaseIndex.java | 19 +++++++++++++------ .../ActiveRuleBackendMediumTest.java | 3 ++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index 83d457c1d59..6dd84f084df 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -26,6 +26,8 @@ import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRespon import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchScrollRequestBuilder; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.Client; @@ -42,8 +44,9 @@ import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount; -import org.elasticsearch.search.sort.SortOrder; +import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.core.cluster.WorkQueue; @@ -199,13 +202,17 @@ public abstract class BaseIndex, KEY extends Serial public Date getLastSynchronization() { Date date; try { - date = getClient().prepareSearch(this.getIndexName()) + SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName()) .setTypes(this.getIndexType()) .setQuery(QueryBuilders.matchAllQuery()) - .setSize(1) - .addField(BaseNormalizer.UPDATED_AT_FIELD) - .addSort(BaseNormalizer.UPDATED_AT_FIELD, SortOrder.DESC) - .get().getHits().getAt(0).field(BaseNormalizer.UPDATED_AT_FIELD).getValue(); + .setSize(0) + .addAggregation(AggregationBuilders.max("latest") + .field(BaseNormalizer.UPDATED_AT_FIELD)); + + SearchResponse response = request.get(); + + Max max = (Max) response.getAggregations().get("latest"); + return new DateTime(Double.valueOf(max.getValue()).longValue()).toDate(); } catch (Exception e) { date = new Date(0L); } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java index ce38f3781b4..66662c8203f 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java @@ -97,9 +97,10 @@ public class ActiveRuleBackendMediumTest { // 3. Assert startup picks it up tester.clearIndexes(); + Date before = index.getLastSynchronization(); tester.get(Platform.class).executeStartupTasks(); assertThat(index.getByKey(activeRule.getKey())).isNotNull(); - + assertThat(before.before(index.getLastSynchronization())).isTrue(); } @Test -- 2.39.5