COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
+ 26 // level 1
+ 46 // content of DaoModule
- + 1 // content of EsSearchModule
+ + 2 // content of EsSearchModule
+ 55 // content of CorePropertyDefinitions
+ 1 // content of CePropertyDefinitions
);
*/
package org.sonar.server.es;
+import java.io.Closeable;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequestBuilder;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.max.Max;
-import org.picocontainer.Startable;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.server.es.request.ProxyBulkRequestBuilder;
* Facade to connect to Elasticsearch node. Handles correctly errors (logging + exceptions
* with context) and profiling of requests.
*/
-public class EsClient implements Startable {
+public class EsClient implements Closeable {
public static final Logger LOGGER = Loggers.get("es");
return (long) max.getValue();
}
- @Override
- public void start() {
- // nothing to do
+ public Client nativeClient() {
+ return nativeClient;
}
@Override
- public void stop() {
+ public void close() {
nativeClient.close();
}
-
- public Client nativeClient() {
- return nativeClient;
- }
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.es;
+
+import org.sonar.api.Startable;
+
+/**
+ * Workaround of a behaviour of picocontainer: components
+ * instantiated by {@link org.picocontainer.injectors.ProviderAdapter}
+ * can't have a lifecycle. The methods start() and stop()
+ * of {@link Startable} are not executed.
+ * The same behaviour exists for the {@link org.picocontainer.injectors.ProviderAdapter}
+ * itself.
+ *
+ * As {@link EsClientStopper} implements {@link Startable}, it can
+ * close {@link EsClient} when process shutdowns.
+ *
+ */
+public class EsClientStopper implements Startable {
+
+ private final EsClient esClient;
+
+ public EsClientStopper(EsClient esClient) {
+ this.esClient = esClient;
+ }
+
+ @Override
+ public void start() {
+ // nothing to do
+ }
+
+ @Override
+ public void stop() {
+ esClient.close();
+ }
+}
import org.sonar.core.platform.Module;
import org.sonar.server.es.EsClientProvider;
+import org.sonar.server.es.EsClientStopper;
public class EsSearchModule extends Module {
@Override
protected void configureModule() {
add(new EsClientProvider());
+ add(EsClientStopper.class);
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.es;
+
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+
+public class EsClientStopperTest {
+
+ private EsClient client = mock(EsClient.class);
+ private EsClientStopper underTest = new EsClientStopper(client);
+
+ @Test
+ public void stop_client() {
+ underTest.start();
+ verifyZeroInteractions(client);
+
+ underTest.stop();
+ verify(client).close();
+ }
+}
@Test
public void proxify_requests() {
EsClient underTest = es.client();
- underTest.start();
assertThat(underTest.nativeClient()).isNotNull();
assertThat(underTest.prepareBulk()).isInstanceOf(ProxyBulkRequestBuilder.class);
assertThat(underTest.prepareClusterStats()).isInstanceOf(ProxyClusterStatsRequestBuilder.class);
assertThat(underTest.prepareState()).isInstanceOf(ProxyClusterStateRequestBuilder.class);
assertThat(underTest.prepareStats()).isInstanceOf(ProxyIndicesStatsRequestBuilder.class);
- underTest.stop();
+ underTest.close();
}
}
@Override
protected void before() throws Throwable {
- client.start();
truncateIndices();
if (!indexDefinitions.isEmpty()) {
container.stopComponents();
}
if (client != null) {
- client.stop();
+ client.close();
}
}
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new EsSearchModule().configure(container);
- assertThat(container.size()).isEqualTo(3);
+ assertThat(container.size()).isEqualTo(4);
}
}