From dead565a2cfdaea686e087b0f414ae4b8c84615e Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 29 Apr 2014 11:14:45 +0200 Subject: [PATCH] SONAR-5237 - Fixed broken ES isolation (broken in previous commit) --- .../main/java/org/sonar/server/es/ESNode.java | 7 +- .../org/sonar/server/rule2/RuleIndex.java | 7 +- .../org/sonar/server/search/BaseIndex.java | 45 +++--- .../sonar/server/es/config/elasticsearch.json | 2 +- .../org/sonar/server/rule2/RuleIndexTest.java | 78 +++++----- .../sonar/server/search/BaseIndexTest.java | 135 ------------------ 6 files changed, 74 insertions(+), 200 deletions(-) delete mode 100644 sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java diff --git a/sonar-server/src/main/java/org/sonar/server/es/ESNode.java b/sonar-server/src/main/java/org/sonar/server/es/ESNode.java index 1bf7d889545..446b6ff1ca0 100644 --- a/sonar-server/src/main/java/org/sonar/server/es/ESNode.java +++ b/sonar-server/src/main/java/org/sonar/server/es/ESNode.java @@ -79,6 +79,7 @@ public class ESNode implements Startable { .loadFromUrl(getClass().getResource("config/elasticsearch.json")); initDirs(esSettings); initRestConsole(esSettings); + initNetwork(esSettings); node = NodeBuilder.nodeBuilder() .settings(esSettings) @@ -105,7 +106,7 @@ public class ESNode implements Startable { node.client().admin().indices().preparePutTemplate("default") .setSource(IOUtils.toString(getClass().getResource("config/templates/default.json"))) .execute().actionGet(); - } catch(IOException ioe) { + } catch (IOException ioe) { throw new IllegalStateException("Unable to load index templates", ioe); } } @@ -114,6 +115,10 @@ public class ESNode implements Startable { ESLoggerFactory.setDefaultFactory(new Slf4jESLoggerFactory()); } + private void initNetwork(ImmutableSettings.Builder esSettings) { + esSettings.put("network.bind_host", "127.0.0.1"); + } + private void initRestConsole(ImmutableSettings.Builder esSettings) { int httpPort = settings.getInt("sonar.es.http.port"); if (httpPort > 0) { diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java index c4cc7b457f5..9b794a6f939 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java @@ -19,7 +19,6 @@ */ package org.sonar.server.rule2; -import org.sonar.core.cluster.IndexAction; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -32,10 +31,10 @@ import org.sonar.core.cluster.WorkQueue; import org.sonar.core.profiling.Profiling; import org.sonar.core.rule.RuleConstants; import org.sonar.core.rule.RuleDao; +import org.sonar.server.es.ESNode; import org.sonar.server.search.BaseIndex; import java.io.IOException; -import java.util.Collection; import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -45,8 +44,8 @@ public class RuleIndex extends BaseIndex { private static final Logger LOG = LoggerFactory.getLogger(RuleIndex.class); - public RuleIndex(WorkQueue queue, RuleDao dao, Profiling profiling) { - super(queue, dao, profiling); + public RuleIndex(WorkQueue queue, RuleDao dao, Profiling profiling, ESNode node) { + super(queue, dao, profiling, node); } @Override 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 666677b8207..60c54eb4004 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 @@ -19,6 +19,8 @@ */ package org.sonar.server.search; +import org.sonar.server.es.ESNode; + import org.elasticsearch.action.admin.cluster.stats.ClusterStatsNodes; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; @@ -55,15 +57,17 @@ public abstract class BaseIndex implements Index { private final Profiling profiling; private Client client; + private final ESNode node; private WorkQueue workQueue; private IndexSynchronizer synchronizer; protected Dao dao; - public BaseIndex(WorkQueue workQueue, Dao dao, Profiling profiling) { + public BaseIndex(WorkQueue workQueue, Dao dao, Profiling profiling, ESNode node) { this.profiling = profiling; this.workQueue = workQueue; this.synchronizer = new IndexSynchronizer(this, dao, this.workQueue); this.dao = dao; + this.node = node; } protected Dao getDao() { @@ -101,25 +105,26 @@ public abstract class BaseIndex implements Index { } public void connect() { - /* Settings to access our local ES node */ - Settings settings = ImmutableSettings.settingsBuilder() - .put("client.transport.sniff", true) - .put("cluster.name", ES_CLUSTER_NAME) - .put("node.name", "localclient_") - .build(); - - this.client = new TransportClient(settings) - .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT)); - - /* - * Cannot do that yet, need version >= 1.0 - * ImmutableList nodes = client.connectedNodes(); - * if (nodes.isEmpty()) { - * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!"); - * } else { - * log.info("connected to nodes: " + nodes.toString()); - * } - */ + this.client = this.node.client(); +// /* Settings to access our local ES node */ +// Settings settings = ImmutableSettings.settingsBuilder() +// .put("client.transport.sniff", true) +// .put("cluster.name", ES_CLUSTER_NAME) +// .put("node.name", "localclient_") +// .build(); +// +// this.client = new TransportClient(settings) +// .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT)); +// +// /* +// * Cannot do that yet, need version >= 1.0 +// * ImmutableList nodes = client.connectedNodes(); +// * if (nodes.isEmpty()) { +// * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!"); +// * } else { +// * log.info("connected to nodes: " + nodes.toString()); +// * } +// */ } /* Cluster And ES Stats/Client methods */ diff --git a/sonar-server/src/main/resources/org/sonar/server/es/config/elasticsearch.json b/sonar-server/src/main/resources/org/sonar/server/es/config/elasticsearch.json index 0e9c2e651c0..64913338ee0 100644 --- a/sonar-server/src/main/resources/org/sonar/server/es/config/elasticsearch.json +++ b/sonar-server/src/main/resources/org/sonar/server/es/config/elasticsearch.json @@ -4,7 +4,7 @@ }, "node": { "name": "sonarqube", - "local": false, + "local": true, "data": true }, "discovery": { diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java index ab863b40ff3..15b5c3f6a22 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java @@ -38,43 +38,43 @@ import static org.fest.assertions.Assertions.assertThat; @Ignore("Same problem as with BaseIndex test") public class RuleIndexTest { - private static final String TEST_NODE_NAME = "es_node_for_tests"; - - @ElasticsearchNode(name = TEST_NODE_NAME, - clusterName = BaseIndex.ES_CLUSTER_NAME, - local = false, data = true) - private Node node; - - @Before - public void setUp() throws Exception { - - } - - private RuleIndex getRuleIndex(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - Settings settings = new Settings(); - settings.setProperty("sonar.log.profilingLevel", "BASIC"); - RuleIndex rindex = new RuleIndex(queue, null, new Profiling(settings)); - return rindex; - } - - @After - public void tearDown() { - if (node != null && !node.isClosed()) { - node.close(); - } - } - - @Test - public void test_ruleIndex_conencts_to_es() { - - RuleIndex ruleIndex = getRuleIndex(); - ruleIndex.connect(); - - assertThat(node.client().admin().cluster().prepareClusterStats().get().getNodesStats().getCounts().getTotal()) - .isEqualTo(ruleIndex.getNodesStats().getCounts().getTotal()); - - ruleIndex.stop(); - - } +// private static final String TEST_NODE_NAME = "es_node_for_tests"; +// +// @ElasticsearchNode(name = TEST_NODE_NAME, +// clusterName = BaseIndex.ES_CLUSTER_NAME, +// local = false, data = true) +// private Node node; +// +// @Before +// public void setUp() throws Exception { +// +// } +// +// private RuleIndex getRuleIndex(){ +// LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); +// Settings settings = new Settings(); +// settings.setProperty("sonar.log.profilingLevel", "BASIC"); +// RuleIndex rindex = new RuleIndex(queue, null, new Profiling(settings)); +// return rindex; +// } +// +// @After +// public void tearDown() { +// if (node != null && !node.isClosed()) { +// node.close(); +// } +// } +// +// @Test +// public void test_ruleIndex_conencts_to_es() { +// +// RuleIndex ruleIndex = getRuleIndex(); +// ruleIndex.connect(); +// +// assertThat(node.client().admin().cluster().prepareClusterStats().get().getNodesStats().getCounts().getTotal()) +// .isEqualTo(ruleIndex.getNodesStats().getCounts().getTotal()); +// +// ruleIndex.stop(); +// +// } } diff --git a/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java b/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java deleted file mode 100644 index f29e635d433..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.search; - -import org.sonar.core.cluster.IndexAction; - -import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchNode; -import com.github.tlrx.elasticsearch.test.support.junit.runners.ElasticsearchRunner; -import org.elasticsearch.client.transport.NoNodeAvailableException; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.node.Node; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.sonar.api.config.Settings; -import org.sonar.core.profiling.Profiling; -import org.sonar.server.cluster.LocalNonBlockingWorkQueue; - -import java.io.Serializable; -import java.util.Collection; -import java.util.Map; - -import static org.fest.assertions.Assertions.assertThat; - -@Ignore("Should be fixed") -@RunWith(ElasticsearchRunner.class) -public class BaseIndexTest { - - private static final String TEST_NODE_NAME = "es_node_for_tests"; - - @ElasticsearchNode(name = TEST_NODE_NAME, - clusterName = BaseIndex.ES_CLUSTER_NAME, - local = false, data = true) - private Node node; - - @Before - public void setUp() throws Exception { - - } - - private BaseIndex getBaseIndex(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - Settings settings = new Settings(); - settings.setProperty("sonar.log.profilingLevel", "BASIC"); - return new BaseIndex(queue, null, new Profiling(settings)) { - - @Override - public String getIndexName() { - // TODO Auto-generated method stub - return null; - } - - @Override - protected org.elasticsearch.common.settings.Settings getIndexSettings() { - // TODO Auto-generated method stub - return null; - } - - @Override - protected String getType() { - // TODO Auto-generated method stub - return null; - } - - @Override - protected XContentBuilder getMapping() { - // TODO Auto-generated method stub - return null; - } - - @Override - protected QueryBuilder getKeyQuery(Serializable key) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Map normalize(Serializable key) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void executeAction(IndexAction action) { - // TODO Auto-generated method stub - - } - }; - } - - @After - public void tearDown() { - if (node != null && !node.isClosed()) { - node.close(); - } - } - - @Test - public void baseIndex_connects_to_es() { - BaseIndex searchIndex = getBaseIndex(); - searchIndex.connect(); - assertThat(node.client().admin().cluster().prepareClusterStats().get().getNodesStats().getCounts().getTotal()) - .isEqualTo(searchIndex.getNodesStats().getCounts().getTotal()); - - searchIndex.stop(); - } - - @Test(expected = NoNodeAvailableException.class) - public void baseIndex_fails_when_es_gone(){ - BaseIndex searchIndex = getBaseIndex(); - searchIndex.connect(); - node.close(); - assertThat(searchIndex.getNodesStats().getCounts().getTotal()).isNotNull(); - } -} -- 2.39.5