]> source.dussan.org Git - sonarqube.git/commitdiff
do not create ES indexes for every ServerTester instances 353/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 4 Jun 2015 10:16:06 +0000 (12:16 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 4 Jun 2015 12:59:00 +0000 (14:59 +0200)
15 files changed:
server/sonar-server/src/main/java/org/sonar/server/es/BaseIndexer.java
server/sonar-server/src/main/java/org/sonar/server/es/IndexDefinitions.java
server/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java
server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryActionMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java
server/sonar-server/src/test/java/org/sonar/server/es/IndexCreatorTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java

index b6c994f2f12eb51aacdb500809423e8de74b6412..7317c20fabe5605d1f8cd8794247333abde5fed9 100644 (file)
@@ -36,7 +36,7 @@ public abstract class BaseIndexer implements Startable {
   private final ThreadPoolExecutor executor;
   private final String indexName, typeName, dateFieldName;
   protected final EsClient esClient;
-  private volatile long lastUpdatedAt = 0L;
+  private volatile long lastUpdatedAt = -1L;
 
   /**
    * Indexers are disabled during server startup, to avoid too many consecutive refreshes of the same index
@@ -65,6 +65,9 @@ public abstract class BaseIndexer implements Startable {
       Future submit = executor.submit(new Runnable() {
         @Override
         public void run() {
+          if (lastUpdatedAt == -1L) {
+            lastUpdatedAt = esClient.getMaxFieldValue(indexName, typeName, dateFieldName);
+          }
           if (requestedAt > lastUpdatedAt) {
             long l = task.index(lastUpdatedAt);
             // l can be 0 if no documents were indexed
@@ -98,7 +101,7 @@ public abstract class BaseIndexer implements Startable {
 
   @Override
   public void start() {
-    lastUpdatedAt = esClient.getMaxFieldValue(indexName, typeName, dateFieldName);
+    // nothing to do at startup
   }
 
   @Override
index 393efeab4c4ccbc5843f27fb01b7d25a59ea99bc..6f0186cd54ffd653dbe41637a6ab0eca4ae2b407 100644 (file)
@@ -21,12 +21,11 @@ package org.sonar.server.es;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
+import java.util.Map;
 import org.elasticsearch.common.settings.Settings;
 import org.picocontainer.Startable;
 import org.sonar.api.server.ServerSide;
 
-import java.util.Map;
-
 /**
  * This class collects definitions of all Elasticsearch indices during server startup
  */
@@ -88,9 +87,11 @@ public class IndexDefinitions implements Startable {
 
   private final Map<String, Index> byKey = Maps.newHashMap();
   private final IndexDefinition[] defs;
+  private final org.sonar.api.config.Settings settings;
 
-  public IndexDefinitions(IndexDefinition[] defs) {
+  public IndexDefinitions(IndexDefinition[] defs, org.sonar.api.config.Settings settings) {
     this.defs = defs;
+    this.settings = settings;
   }
 
   public Map<String, Index> getIndices() {
@@ -101,12 +102,15 @@ public class IndexDefinitions implements Startable {
   public void start() {
     // collect definitions
     IndexDefinition.IndexDefinitionContext context = new IndexDefinition.IndexDefinitionContext();
-    for (IndexDefinition definition : defs) {
-      definition.define(context);
-    }
 
-    for (Map.Entry<String, NewIndex> entry : context.getIndices().entrySet()) {
-      byKey.put(entry.getKey(), new Index(entry.getValue()));
+    if (!settings.getBoolean("sonar.internal.es.disableIndexes")) {
+      for (IndexDefinition definition : defs) {
+        definition.define(context);
+      }
+
+      for (Map.Entry<String, NewIndex> entry : context.getIndices().entrySet()) {
+        byKey.put(entry.getKey(), new Index(entry.getValue()));
+      }
     }
   }
 
index 8d24946a043703d2bd64ca79385ad499fb17c42a..26758b3994e810365dcfc6f28bebbd78f6528169 100644 (file)
@@ -19,7 +19,8 @@
  */
 package org.sonar.server.search;
 
-
+import java.util.Date;
+import org.sonar.api.config.Settings;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.core.persistence.DbSession;
@@ -35,8 +36,6 @@ import org.sonar.server.test.index.TestIndexer;
 import org.sonar.server.user.index.UserIndexer;
 import org.sonar.server.view.index.ViewIndexer;
 
-import java.util.Date;
-
 public class IndexSynchronizer {
 
   private static final Logger LOG = Loggers.get(IndexSynchronizer.class);
@@ -50,6 +49,7 @@ public class IndexSynchronizer {
   private final UserIndexer userIndexer;
   private final ViewIndexer viewIndexer;
   private final ActivityIndexer activityIndexer;
+  private final Settings settings;
 
   /**
    * Limitation - {@link org.sonar.server.es.BaseIndexer} are not injected through an array or a collection
@@ -57,8 +57,8 @@ public class IndexSynchronizer {
    * {@link org.sonar.server.issue.index.IssueIndexer}
    */
   public IndexSynchronizer(DbClient db, IndexClient index, SourceLineIndexer sourceLineIndexer,
-                           TestIndexer testIndexer, IssueAuthorizationIndexer issueAuthorizationIndexer, IssueIndexer issueIndexer,
-                           UserIndexer userIndexer, ViewIndexer viewIndexer, ActivityIndexer activityIndexer) {
+    TestIndexer testIndexer, IssueAuthorizationIndexer issueAuthorizationIndexer, IssueIndexer issueIndexer,
+    UserIndexer userIndexer, ViewIndexer viewIndexer, ActivityIndexer activityIndexer, Settings settings) {
     this.db = db;
     this.index = index;
     this.sourceLineIndexer = sourceLineIndexer;
@@ -68,6 +68,7 @@ public class IndexSynchronizer {
     this.userIndexer = userIndexer;
     this.viewIndexer = viewIndexer;
     this.activityIndexer = activityIndexer;
+    this.settings = settings;
   }
 
   public void executeDeprecated() {
@@ -82,24 +83,26 @@ public class IndexSynchronizer {
   }
 
   public void execute() {
-    LOG.info("Index activities");
-    activityIndexer.setEnabled(true).index();
+    if (!settings.getBoolean("sonar.internal.es.disableIndexes")) {
+      LOG.info("Index activities");
+      activityIndexer.setEnabled(true).index();
 
-    LOG.info("Index issues");
-    issueAuthorizationIndexer.setEnabled(true).index();
-    issueIndexer.setEnabled(true).index();
+      LOG.info("Index issues");
+      issueAuthorizationIndexer.setEnabled(true).index();
+      issueIndexer.setEnabled(true).index();
 
-    LOG.info("Index source lines");
-    sourceLineIndexer.setEnabled(true).index();
+      LOG.info("Index source lines");
+      sourceLineIndexer.setEnabled(true).index();
 
-    LOG.info("Index tests");
-    testIndexer.setEnabled(true).index();
+      LOG.info("Index tests");
+      testIndexer.setEnabled(true).index();
 
-    LOG.info("Index users");
-    userIndexer.setEnabled(true).index();
+      LOG.info("Index users");
+      userIndexer.setEnabled(true).index();
 
-    LOG.info("Index views");
-    viewIndexer.setEnabled(true).index();
+      LOG.info("Index views");
+      viewIndexer.setEnabled(true).index();
+    }
   }
 
   void synchronize(DbSession session, Dao dao, Index index) {
index 25a4e53be94dfdc5e2123524236d28d904abb319..91cf4c730ae4dab45aacac6e027dedea149e6588 100644 (file)
@@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 public class ActivitiesWsMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 9c146fbba9a1774a3e489e38f19fa5a80f0fb97b..42a93b83300bbf61ad4d4a288fd8f6d74ec25a2c 100644 (file)
@@ -41,7 +41,7 @@ import java.util.Date;
 public class HistoryActionMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index e8df89bb00381476cdb66ed671a3234e98a4168d..2e1345d370a8831d415184d5b3252fb4b8f9e77f 100644 (file)
@@ -41,6 +41,7 @@ import org.elasticsearch.node.Node;
 import org.elasticsearch.node.NodeBuilder;
 import org.elasticsearch.search.SearchHit;
 import org.junit.rules.ExternalResource;
+import org.sonar.api.config.Settings;
 import org.sonar.core.platform.ComponentContainer;
 import org.sonar.server.search.BaseDoc;
 import org.sonar.test.TestUtils;
@@ -101,6 +102,7 @@ public class EsTester extends ExternalResource {
 
     if (!definitions.isEmpty()) {
       ComponentContainer container = new ComponentContainer();
+      container.addSingleton(new Settings());
       container.addSingletons(definitions);
       container.addSingleton(client);
       container.addSingleton(IndexDefinitions.class);
index ac55e4e4c53d7e9450cab8658ed4f638fc79ba74..a3a1920fecdcc5770ba95b06f6d11862955b660a 100644 (file)
@@ -29,6 +29,7 @@ import javax.annotation.CheckForNull;
 
 import java.io.IOException;
 import java.util.Map;
+import org.sonar.api.config.Settings;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
@@ -41,7 +42,7 @@ public class IndexCreatorTest {
   public void create_index() throws Exception {
     assertThat(mappings()).isEmpty();
 
-    IndexDefinitions registry = new IndexDefinitions(new IndexDefinition[] {new FakeIndexDefinition()});
+    IndexDefinitions registry = new IndexDefinitions(new IndexDefinition[] {new FakeIndexDefinition()}, new Settings());
     registry.start();
     IndexCreator creator = new IndexCreator(es.client(), registry);
     creator.start();
@@ -66,7 +67,7 @@ public class IndexCreatorTest {
     assertThat(mappings()).isEmpty();
 
     // v1
-    IndexDefinitions registry = new IndexDefinitions(new IndexDefinition[] {new FakeIndexDefinition()});
+    IndexDefinitions registry = new IndexDefinitions(new IndexDefinition[] {new FakeIndexDefinition()}, new Settings());
     registry.start();
     IndexCreator creator = new IndexCreator(es.client(), registry);
     creator.start();
@@ -75,7 +76,7 @@ public class IndexCreatorTest {
     assertThat(hashV1).isNotEmpty();
 
     // v2
-    registry = new IndexDefinitions(new IndexDefinition[] {new FakeIndexDefinitionV2()});
+    registry = new IndexDefinitions(new IndexDefinition[] {new FakeIndexDefinitionV2()}, new Settings());
     registry.start();
     creator = new IndexCreator(es.client(), registry);
     creator.start();
index 6e01a4b3c9ebc64c2593cf59137df8b41dbe84aa..992df6249f9b1fb92b7145295eb48d7bc2d71301 100644 (file)
@@ -62,7 +62,7 @@ import static org.junit.Assert.fail;
 public class IssueBulkChangeServiceMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 7942227af2d1c169562e712595694b5ccab0339e..a343a0394126a1e34cc344b647817d55f7c90cc2 100644 (file)
@@ -58,7 +58,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 public class IssueCommentServiceMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 24ae581f6fb93192909df69df3d3c3e9a8e90567..60092ddb04f98dca0436f637899d56b99a784d65 100644 (file)
@@ -79,7 +79,7 @@ import static org.junit.Assert.fail;
 public class IssueServiceMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 18ac9cd2a4a14ed4dac0dd2d031b3fbf508aa0c8..4c90e46023c7e193c2d1d14e6a32de70f715de83 100644 (file)
@@ -59,7 +59,7 @@ import static com.google.common.collect.Lists.newArrayList;
 public class SearchActionComponentsMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 4cf591bc18762e92b60d9e61e4e049c88b9afba3..791433a9e332601ef18052ba15f2b420e8631ed7 100644 (file)
@@ -63,7 +63,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 public class SearchActionMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 6c369d534a992ed0c47aca772d5307c454a38586..2ba6021d607e48f3c57be6ce2c933bea691ca310 100644 (file)
@@ -52,7 +52,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 public class InternalPermissionServiceMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks();
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 98e82dafb36f8b547aa15ee96902c4122e5243c0..ff6118371a1ca800a469c32fd5cf4e53d54579f5 100644 (file)
@@ -64,7 +64,7 @@ import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
 public class QProfileServiceMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks().addComponents(XooProfileImporter.class, XooExporter.class);
+  public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes().addComponents(XooProfileImporter.class, XooExporter.class);
   @org.junit.Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
index 6c5506ca867a40cd5d8d055a0077fc59fb9d0f0c..a465a1b0151b89efb1125444cc4a0e5165b679db 100644 (file)
@@ -70,12 +70,18 @@ public class ServerTester extends ExternalResource {
   private final ServletContext servletContext = new AttributeHolderServletContext();
   private URL updateCenterUrl;
   private boolean startupTasks = false;
+  private boolean esIndexes = false;
 
   public ServerTester withStartupTasks() {
     this.startupTasks = true;
     return this;
   }
 
+  public ServerTester withEsIndexes() {
+    this.esIndexes = true;
+    return this;
+  }
+
   /**
    * Called only when JUnit @Rule or @ClassRule is used.
    */
@@ -111,6 +117,9 @@ public class ServerTester extends ExternalResource {
           properties.put(StringUtils.substringAfter(key, PROP_PREFIX), entry.getValue());
         }
       }
+      if (!esIndexes) {
+        properties.put("sonar.internal.es.disableIndexes", true);
+      }
       platform = new ServerTesterPlatform();
       platform.init(properties, servletContext);
       platform.addComponents(components);