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
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
@Override
public void start() {
- lastUpdatedAt = esClient.getMaxFieldValue(indexName, typeName, dateFieldName);
+ // nothing to do at startup
}
@Override
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
*/
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() {
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()));
+ }
}
}
*/
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;
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);
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
* {@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;
this.userIndexer = userIndexer;
this.viewIndexer = viewIndexer;
this.activityIndexer = activityIndexer;
+ this.settings = settings;
}
public void executeDeprecated() {
}
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) {
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);
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);
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;
if (!definitions.isEmpty()) {
ComponentContainer container = new ComponentContainer();
+ container.addSingleton(new Settings());
container.addSingletons(definitions);
container.addSingleton(client);
container.addSingleton(IndexDefinitions.class);
import java.io.IOException;
import java.util.Map;
+import org.sonar.api.config.Settings;
import static org.assertj.core.api.Assertions.assertThat;
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();
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();
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();
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);
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);
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);
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);
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);
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);
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);
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.
*/
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);