]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Updated synchronization of BaseIndexer to rely on latest updatedAt
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 18 Jun 2014 12:41:47 +0000 (14:41 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 18 Jun 2014 13:25:39 +0000 (15:25 +0200)
sonar-server/src/main/java/org/sonar/server/activity/index/ActivityDoc.java
sonar-server/src/main/java/org/sonar/server/activity/index/ActivityNormalizer.java
sonar-server/src/main/java/org/sonar/server/activity/ws/ActivityMapping.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java

index 0ba3834b58acd272033ac27e5f68cecb2b9cf81a..3a6ccaf4163145e8161edc4e0a6b3adb4413354f 100644 (file)
@@ -37,7 +37,7 @@ public class ActivityDoc extends BaseDoc implements Activity {
 
   @Override
   public Date time() {
-    return this.getField(ActivityNormalizer.LogFields.DATE.field());
+    return this.getField(ActivityNormalizer.LogFields.CREATED_AT.field());
   }
 
   @Override
index c750f0c251a702738db55eb83c524100d3a6e7bf..3f50acac4facc4f843485db17fb4653c2d4a8281 100644 (file)
@@ -50,7 +50,8 @@ public class ActivityNormalizer extends BaseNormalizer<ActivityDto, ActivityKey>
   public static final class LogFields extends Indexable {
 
     public final static IndexField TYPE = addSortable(IndexField.Type.STRING, "type");
-    public final static IndexField DATE = addSortable(IndexField.Type.DATE, "date");
+    public final static IndexField CREATED_AT = addSortable(IndexField.Type.DATE, "createdAt");
+    public final static IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, BaseNormalizer.UPDATED_AT_FIELD);
     public final static IndexField AUTHOR = addSearchable(IndexField.Type.STRING, "author");
     public final static IndexField DETAILS = addSearchable(IndexField.Type.OBJECT, "details");
     public final static IndexField MESSAGE = addSearchable(IndexField.Type.STRING, "message");
@@ -95,7 +96,8 @@ public class ActivityNormalizer extends BaseNormalizer<ActivityDto, ActivityKey>
     logDoc.put(LogFields.TYPE.field(), dto.getType());
     logDoc.put(LogFields.AUTHOR.field(), dto.getAuthor());
     logDoc.put(LogFields.MESSAGE.field(), dto.getMessage());
-    logDoc.put(LogFields.DATE.field(), dto.getCreatedAt());
+    logDoc.put(LogFields.CREATED_AT.field(), dto.getCreatedAt());
+    logDoc.put(LogFields.UPDATED_AT.field(), dto.getUpdatedAt());
 
     logDoc.put(LogFields.DETAILS.field(), KeyValueFormat.parse(dto.getData()));
 
index 9e2cb2fd24325b6eff01edd3284b27458a440111..f52e3836adf4bd4858a99ba80706ce420cd66fb3 100644 (file)
@@ -37,7 +37,7 @@ public class ActivityMapping extends BaseMapping {
   public ActivityMapping(Languages languages, MacroInterpreter macroInterpreter) {
     super();
     addIndexStringField("type", ActivityNormalizer.LogFields.TYPE.field());
-    addIndexDatetimeField("createdAt", ActivityNormalizer.LogFields.DATE.field());
+    addIndexDatetimeField("createdAt", ActivityNormalizer.LogFields.CREATED_AT.field());
     addIndexStringField("userLogin", ActivityNormalizer.LogFields.AUTHOR.field());
     addIndexStringField("message", ActivityNormalizer.LogFields.MESSAGE.field());
     addField("details", new DetailField());
index 37bc8f978766cb276c412070852bd0489cb992c6..07d3a37c6699e0650364951f51b6beb7c2741590 100644 (file)
@@ -38,7 +38,12 @@ import org.sonar.server.search.Indexable;
 import org.sonar.server.search.es.ListUpdate;
 
 import java.lang.reflect.Field;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
 
@@ -80,7 +85,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
 
     public static final IndexField NAME = addSortableAndSearchable(IndexField.Type.STRING, "name");
     public static final IndexField CREATED_AT = addSortable(IndexField.Type.DATE, "createdAt");
-    public static final IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, "updatedAt");
+    public static final IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, UPDATED_AT_FIELD);
     public static final IndexField HTML_DESCRIPTION = addSearchable(IndexField.Type.TEXT, "htmlDesc");
     public static final IndexField SEVERITY = add(IndexField.Type.STRING, "severity");
     public static final IndexField STATUS = add(IndexField.Type.STRING, "status");
index 63bd03fbac20e6af04a92d32635d1638c8cf193e..a73631a85e3303e16d52c31a9b66cef485828a84 100644 (file)
@@ -43,6 +43,7 @@ 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.valuecount.InternalValueCount;
+import org.elasticsearch.search.sort.SortOrder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.core.cluster.WorkQueue;
@@ -220,26 +221,22 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
 
   /* Synchronization methods */
 
-  private void setLastSynchronization() {
-    Date time = new Date();
-    if (time.after(getLastSynchronization())) {
-      LOG.debug("Updating synchTime updating");
-      getClient().prepareUpdate()
-        .setId(indexDefinition.getIndexName())
-        .setType(indexDefinition.getManagementType())
-        .setIndex(indexDefinition.getManagementIndex())
-        .setDoc("updatedAt", time)
-        .get();
-    }
-  }
-
   @Override
   public Date getLastSynchronization() {
-    return (java.util.Date) getClient().prepareGet()
-      .setIndex(indexDefinition.getManagementIndex())
-      .setId(this.getIndexName())
-      .setType(indexDefinition.getManagementType())
-      .get().getField("updatedAt").getValue();
+    Date date;
+    try {
+      date = 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();
+    } catch (Exception e) {
+      date = new Date(0L);
+    }
+    LOG.info("Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date);
+    return date;
   }
 
   /* Index management methods */
index e6fda79cd043edaf909d867cdbd2034f2a354bab..9532e464b003c896c48968a01e4422f651487830 100644 (file)
@@ -28,6 +28,8 @@ import java.util.List;
 
 public abstract class BaseNormalizer<DTO extends Dto<KEY>, KEY extends Serializable> {
 
+  public static final String UPDATED_AT_FIELD = "updatedAt";
+
   protected final DbClient db;
   protected final IndexDefinition definition;