/
EsTester.java
246 lines (220 loc) · 8.87 KB
/
EsTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* 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 com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.reflect.ConstructorUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
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.server.search.SearchClient;
import org.sonar.test.TestUtils;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class EsTester extends ExternalResource {
private static final int INSTANCE_ID = RandomUtils.nextInt();
private Node node;
private EsClient client;
private final List<IndexDefinition> definitions = newArrayList();
public EsTester addDefinitions(IndexDefinition... defs) {
Collections.addAll(definitions, defs);
return this;
}
protected void before() throws Throwable {
String nodeName = "tmp-es-" + INSTANCE_ID;
node = NodeBuilder.nodeBuilder().local(true).data(true).settings(ImmutableSettings.builder()
.put("cluster.name", nodeName)
.put("node.name", nodeName)
// the two following properties are probably not used because they are
// declared on indices too
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
// limit the number of threads created (see org.elasticsearch.common.util.concurrent.EsExecutors)
.put("processors", 1)
.put("http.enabled", false)
.put("index.store.type", "mmapfs")
.put("config.ignore_system_properties", true)
// reuse the same directory than other tests for faster initialization
.put("path.home", "target/" + nodeName)
.put("gateway.type", "none"))
.build();
node.start();
assertThat(DiscoveryNode.localNode(node.settings())).isTrue();
// wait for node to be ready
node.client().admin().cluster().prepareHealth()
.setWaitForGreenStatus()
.get();
// delete the indices created by previous tests
DeleteIndexResponse response = node.client().admin().indices().prepareDelete("_all").get();
assertThat(response.isAcknowledged()).isTrue();
client = new EsClient(new SearchClient(new Settings(), node.client()));
client.start();
if (!definitions.isEmpty()) {
ComponentContainer container = new ComponentContainer();
container.addSingleton(new Settings());
container.addSingletons(definitions);
container.addSingleton(client);
container.addSingleton(IndexDefinitions.class);
container.addSingleton(IndexCreator.class);
container.startComponents();
}
}
@Override
protected void after() {
if (client != null) {
client.stop();
}
if (node != null) {
node.stop();
node.close();
}
}
public void truncateIndices() {
client.prepareDeleteByQuery(client.prepareState().get()
.getState().getMetaData().concreteAllIndices())
.setQuery(QueryBuilders.matchAllQuery())
.get();
client.prepareRefresh(client.prepareState().get()
.getState().getMetaData().concreteAllIndices())
.setForce(true)
.get();
client.prepareFlush(client.prepareState().get()
.getState().getMetaData().concreteAllIndices())
.get();
}
public void putDocuments(String index, String type, Class<?> testClass, String... jsonPaths) throws Exception {
BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
for (String path : jsonPaths) {
File file = TestUtils.getResource(testClass, path);
if (file == null) {
throw new IllegalArgumentException(String.format("File '%s' hasn't been found in folder '%s'", path, testClass.getSimpleName()));
}
bulk.add(new IndexRequest(index, type).source(IOUtils.toString(new FileInputStream(file))));
}
bulk.get();
}
public void putDocuments(String index, String type, BaseDoc... docs) throws Exception {
BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
for (BaseDoc doc : docs) {
bulk.add(new IndexRequest(index, type).source(doc.getFields()));
}
bulk.get();
}
public void putDocuments(String index, String type, Map<String, Object>... docs) throws Exception {
BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
for (Map<String, Object> doc : docs) {
bulk.add(new IndexRequest(index, type).source(doc));
}
bulk.get();
}
public long countDocuments(String indexName, String typeName) {
return client().prepareCount(indexName).setTypes(typeName).get().getCount();
}
/**
* Get all the indexed documents (no paginated results). Results are converted to BaseDoc objects.
* Results are not sorted.
*/
public <E extends BaseDoc> List<E> getDocuments(String indexName, String typeName, final Class<E> docClass) {
List<SearchHit> hits = getDocuments(indexName, typeName);
return newArrayList(Collections2.transform(hits, new Function<SearchHit, E>() {
@Override
public E apply(SearchHit input) {
try {
return (E) ConstructorUtils.invokeConstructor(docClass, input.getSource());
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}));
}
/**
* Get all the indexed documents (no paginated results). Results are not sorted.
*/
public List<SearchHit> getDocuments(String indexName, String typeName) {
SearchRequestBuilder req = client.nativeClient().prepareSearch(indexName).setTypes(typeName).setQuery(QueryBuilders.matchAllQuery());
req.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
.setSize(100);
SearchResponse response = req.get();
List<SearchHit> result = newArrayList();
while (true) {
Iterables.addAll(result, response.getHits());
response = client.nativeClient().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
// Break condition: No hits are returned
if (response.getHits().getHits().length == 0) {
break;
}
}
return result;
}
/**
* Get a list of a specific field from all indexed documents.
*/
public <T> List<T> getDocumentFieldValues(String indexName, String typeName, final String fieldNameToReturn) {
return newArrayList(Iterables.transform(getDocuments(indexName, typeName), new Function<SearchHit, T>() {
@Override
public T apply(SearchHit input) {
return (T) input.sourceAsMap().get(fieldNameToReturn);
}
}));
}
public List<String> getIds(String indexName, String typeName){
return FluentIterable.from(getDocuments(indexName, typeName)).transform(SearchHitToId.INSTANCE).toList();
}
public Node node() {
return node;
}
public EsClient client() {
return client;
}
private enum SearchHitToId implements Function<SearchHit, String>{
INSTANCE;
@Override
public String apply(@Nonnull org.elasticsearch.search.SearchHit input) {
return input.id();
}
}
}