]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Fix date-synch for ES Synchronizer
authorStephane Gamard <stephane.gamard@searchbox.com>
Thu, 3 Jul 2014 16:19:28 +0000 (18:19 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 4 Jul 2014 07:33:47 +0000 (09:33 +0200)
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java

index 83d457c1d5990cc8b5619c521bdd502c3cbb2bae..6dd84f084dfe852b4f853c1979ee3271c7cf2d90 100644 (file)
@@ -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<DOMAIN, DTO extends Dto<KEY>, 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);
     }
index ce38f3781b42a94057b31eb323ba7bd2e568f5e2..66662c8203fa611f192f5b601424f72bb3799e0c 100644 (file)
@@ -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