]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5237 - Fixed broken ES isolation (broken in previous commit)
authorStephane Gamard <stephane.gamard@searchbox.com>
Tue, 29 Apr 2014 09:14:45 +0000 (11:14 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Tue, 29 Apr 2014 09:16:27 +0000 (11:16 +0200)
sonar-server/src/main/java/org/sonar/server/es/ESNode.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/main/resources/org/sonar/server/es/config/elasticsearch.json
sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java
sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java [deleted file]

index 1bf7d8895456cc82aa2dd03b507944906022af06..446b6ff1ca0877f2aacb318540d980e8d425cad1 100644 (file)
@@ -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) {
index c4cc7b457f510c82a203fe066aa40f76ce78b1aa..9b794a6f939142d5c72520e86115b931fccc025f 100644 (file)
@@ -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<RuleKey> {
 
   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
index 666677b8207c4ec0c2440750881aa41517174da7..60c54eb4004d62a99148a6ce404d13b6d73a7659 100644 (file)
@@ -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<K extends Serializable> implements Index<K> {
 
   private final Profiling profiling;
   private Client client;
+  private final ESNode node;
   private WorkQueue workQueue;
   private IndexSynchronizer<K> synchronizer;
   protected Dao<?, K> dao;
 
-  public BaseIndex(WorkQueue workQueue, Dao<?, K> dao, Profiling profiling) {
+  public BaseIndex(WorkQueue workQueue, Dao<?, K> dao, Profiling profiling, ESNode node) {
     this.profiling = profiling;
     this.workQueue = workQueue;
     this.synchronizer = new IndexSynchronizer<K>(this, dao, this.workQueue);
     this.dao = dao;
+    this.node = node;
   }
 
   protected Dao<?, K> getDao() {
@@ -101,25 +105,26 @@ public abstract class BaseIndex<K extends Serializable> implements Index<K> {
   }
 
   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<DiscoveryNode> 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<DiscoveryNode> 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 */
index 0e9c2e651c0b12cb4675d1868e9a139bd8fbdff0..64913338ee0fce9d880d217f9f790e2be454d9e5 100644 (file)
@@ -4,7 +4,7 @@
   },
   "node": {
     "name": "sonarqube",
-    "local": false,
+    "local": true,
     "data": true
   },
   "discovery": {
index ab863b40ff3f02f0ddcc0c1ebd421b47e6956703..15b5c3f6a2232ea6a63a784c3fa335b134cd0e21 100644 (file)
@@ -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 (file)
index f29e635..0000000
+++ /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<Serializable>(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<String, Object> 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();
-  }
-}