forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncSearchIndexServiceTests.java
352 lines (314 loc) · 17.7 KB
/
AsyncSearchIndexServiceTests.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.core.async;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.breaker.CircuitBreakingException;
import org.elasticsearch.common.breaker.NoopCircuitBreaker;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.indices.breaker.AllCircuitBreakerStats;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.indices.breaker.CircuitBreakerStats;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.transport.TransportService;
import org.junit.Before;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.search.SearchService.MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN;
import static org.hamcrest.Matchers.equalTo;
// TODO: test CRUD operations
public class AsyncSearchIndexServiceTests extends ESSingleNodeTestCase {
private AsyncTaskIndexService<TestAsyncResponse> indexService;
public static class TestAsyncResponse implements AsyncResponse<TestAsyncResponse> {
public final String test;
public final long expirationTimeMillis;
public String failure;
public TestAsyncResponse(String test, long expirationTimeMillis) {
this.test = test;
this.expirationTimeMillis = expirationTimeMillis;
}
public TestAsyncResponse(String test, long expirationTimeMillis, String failure) {
this.test = test;
this.expirationTimeMillis = expirationTimeMillis;
this.failure = failure;
}
public TestAsyncResponse(StreamInput input) throws IOException {
test = input.readOptionalString();
this.expirationTimeMillis = input.readLong();
failure = input.readOptionalString();
}
@Override
public long getExpirationTime() {
return expirationTimeMillis;
}
@Override
public TestAsyncResponse withExpirationTime(long expirationTime) {
return new TestAsyncResponse(test, expirationTime, failure);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(test);
out.writeLong(expirationTimeMillis);
out.writeOptionalString(failure);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestAsyncResponse that = (TestAsyncResponse) o;
return expirationTimeMillis == that.expirationTimeMillis &&
Objects.equals(test, that.test) && Objects.equals(failure, that.failure);
}
@Override
public int hashCode() {
return Objects.hash(test, expirationTimeMillis, failure);
}
@Override
public String toString() {
return "TestAsyncResponse{" +
"test='" + test + '\'' +
"failure='" + failure + '\'' +
", expirationTimeMillis=" + expirationTimeMillis +
'}';
}
@Override
public TestAsyncResponse convertToFailure(Exception exc) {
return new TestAsyncResponse(test, expirationTimeMillis, exc.getMessage());
}
}
@Before
public void setup() {
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
BigArrays bigArrays = getInstanceFromNode(BigArrays.class);
TransportService transportService = getInstanceFromNode(TransportService.class);
indexService = new AsyncTaskIndexService<>("test", clusterService, transportService.getThreadPool().getThreadContext(),
client(), ASYNC_SEARCH_ORIGIN, TestAsyncResponse::new, writableRegistry(), bigArrays);
}
public void testEncodeSearchResponse() throws IOException {
final int iterations = iterations(1, 20);
for (int i = 0; i < iterations; i++) {
long expirationTime = randomLong();
String testMessage = randomAlphaOfLength(10);
TestAsyncResponse initialResponse = new TestAsyncResponse(testMessage, expirationTime);
AsyncExecutionId executionId = new AsyncExecutionId(
Long.toString(randomNonNegativeLong()),
new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()));
PlainActionFuture<IndexResponse> createFuture = new PlainActionFuture<>();
indexService.createResponse(executionId.getDocId(), Map.of(), initialResponse, createFuture);
assertThat(createFuture.actionGet().getResult(), equalTo(DocWriteResponse.Result.CREATED));
if (randomBoolean()) {
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId, randomBoolean(), getFuture);
assertThat(getFuture.actionGet(), equalTo(initialResponse));
}
int updates = randomIntBetween(1, 5);
for (int u = 0; u < updates; u++) {
if (randomBoolean()) {
testMessage = randomAlphaOfLength(10);
TestAsyncResponse updateResponse = new TestAsyncResponse(testMessage, randomLong());
PlainActionFuture<UpdateResponse> updateFuture = new PlainActionFuture<>();
indexService.updateResponse(executionId.getDocId(), Map.of(), updateResponse, updateFuture);
updateFuture.actionGet();
} else {
expirationTime = randomLong();
PlainActionFuture<UpdateResponse> updateFuture = new PlainActionFuture<>();
indexService.updateExpirationTime(executionId.getDocId(), expirationTime, updateFuture);
updateFuture.actionGet();
}
if (randomBoolean()) {
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId, randomBoolean(), getFuture);
assertThat(getFuture.actionGet().test, equalTo(testMessage));
assertThat(getFuture.actionGet().expirationTimeMillis, equalTo(expirationTime));
}
}
}
}
static class AdjustableLimitCircuitBreaker extends NoopCircuitBreaker {
private long used = 0;
private long limit = 0;
AdjustableLimitCircuitBreaker(String name) {
super(name);
}
@Override
public void addEstimateBytesAndMaybeBreak(long bytes, String label) throws CircuitBreakingException {
if (bytes <= 0) {
addWithoutBreaking(bytes);
} else {
if (used + bytes > limit) {
throw new CircuitBreakingException("Current used [" + used + "] and requesting bytes [" + bytes + "] " +
"is greater than the limit [" + limit + "]", Durability.TRANSIENT);
}
used += bytes;
}
}
@Override
public void addWithoutBreaking(long bytes) {
used += bytes;
}
@Override
public long getUsed() {
return used;
}
@Override
public long getLimit() {
return limit;
}
void adjustLimit(long limit) {
if (limit < used) {
throw new IllegalArgumentException("Limit must not be smaller than used; used=" + used + "; limit=" + limit);
}
this.limit = limit;
}
}
public void testCircuitBreaker() throws Exception {
AdjustableLimitCircuitBreaker circuitBreaker = new AdjustableLimitCircuitBreaker("test");
CircuitBreakerService circuitBreakerService = new CircuitBreakerService() {
@Override
public CircuitBreaker getBreaker(String name) {
assertThat(name, equalTo(CircuitBreaker.REQUEST));
return circuitBreaker;
}
@Override
public AllCircuitBreakerStats stats() {
return null;
}
@Override
public CircuitBreakerStats stats(String name) {
return null;
}
};
BigArrays bigArrays = new BigArrays(null, circuitBreakerService, CircuitBreaker.REQUEST);
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
TransportService transportService = getInstanceFromNode(TransportService.class);
indexService = new AsyncTaskIndexService<>("test", clusterService, transportService.getThreadPool().getThreadContext(),
client(), ASYNC_SEARCH_ORIGIN, TestAsyncResponse::new, writableRegistry(), bigArrays);
AsyncExecutionId executionId = new AsyncExecutionId(
Long.toString(randomNonNegativeLong()),
new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()));
long expirationTime = randomLong();
String testMessage = randomAlphaOfLength(10);
{
circuitBreaker.adjustLimit(randomIntBetween(1, 64)); // small limit
TestAsyncResponse initialResponse = new TestAsyncResponse(testMessage, expirationTime);
PlainActionFuture<IndexResponse> createFuture = new PlainActionFuture<>();
indexService.createResponse(executionId.getDocId(), Map.of(), initialResponse, createFuture);
CircuitBreakingException e = expectThrows(CircuitBreakingException.class, createFuture::actionGet);
assertEquals(0, e.getSuppressed().length); // no other suppressed exceptions
assertThat(circuitBreaker.getUsed(), equalTo(0L));
}
{
circuitBreaker.adjustLimit(randomIntBetween(16 * 1024, 1024 * 1024)); // large enough
TestAsyncResponse initialResponse = new TestAsyncResponse(testMessage, expirationTime);
PlainActionFuture<IndexResponse> createFuture = new PlainActionFuture<>();
indexService.createResponse(executionId.getDocId(), Map.of(), initialResponse, createFuture);
assertThat(createFuture.actionGet().getResult(), equalTo(DocWriteResponse.Result.CREATED));
assertThat(circuitBreaker.getUsed(), equalTo(0L));
if (randomBoolean()) {
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId, randomBoolean(), getFuture);
assertThat(getFuture.actionGet(), equalTo(initialResponse));
assertBusy(() -> assertThat(circuitBreaker.getUsed(), equalTo(0L)));
}
if (randomBoolean()) {
circuitBreaker.adjustLimit(between(1, 16));
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId, randomBoolean(), getFuture);
expectThrows(CircuitBreakingException.class, getFuture::actionGet);
assertBusy(() -> assertThat(circuitBreaker.getUsed(), equalTo(0L)));
}
}
int updates = randomIntBetween(1, 5);
for (int u = 0; u < updates; u++) {
if (randomBoolean()) {
circuitBreaker.adjustLimit(randomIntBetween(16 * 1024, 1024 * 1024));
testMessage = randomAlphaOfLength(10);
TestAsyncResponse updateResponse = new TestAsyncResponse(testMessage, randomLong());
PlainActionFuture<UpdateResponse> updateFuture = new PlainActionFuture<>();
indexService.updateResponse(executionId.getDocId(), Map.of(), updateResponse, updateFuture);
updateFuture.actionGet();
assertThat(circuitBreaker.getUsed(), equalTo(0L));
} else {
circuitBreaker.adjustLimit(randomIntBetween(1, 64)); // small limit
PlainActionFuture<UpdateResponse> updateFuture = new PlainActionFuture<>();
TestAsyncResponse updateResponse = new TestAsyncResponse(randomAlphaOfLength(100), randomLong());
indexService.updateResponse(executionId.getDocId(), Map.of(), updateResponse, updateFuture);
CircuitBreakingException e = expectThrows(CircuitBreakingException.class, updateFuture::actionGet);
assertEquals(0, e.getSuppressed().length); // no other suppressed exceptions
assertThat(circuitBreaker.getUsed(), equalTo(0L));
}
if (randomBoolean()) {
circuitBreaker.adjustLimit(randomIntBetween(16 * 1024, 1024 * 1024)); // small limit
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId, randomBoolean(), getFuture);
assertThat(getFuture.actionGet().test, equalTo(testMessage));
assertThat(getFuture.actionGet().expirationTimeMillis, equalTo(expirationTime));
assertBusy(() -> assertThat(circuitBreaker.getUsed(), equalTo(0L)));
}
if (randomBoolean()) {
circuitBreaker.adjustLimit(randomIntBetween(1, 16)); // small limit
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId, randomBoolean(), getFuture);
expectThrows(CircuitBreakingException.class, getFuture::actionGet);
assertBusy(() -> assertThat(circuitBreaker.getUsed(), equalTo(0L)));
}
}
}
public void testMaxAsyncSearchResponseSize() throws Exception {
try {
// successfully create an initial response
AsyncExecutionId executionId1 = new AsyncExecutionId(Long.toString(randomNonNegativeLong()),
new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()));
TestAsyncResponse initialResponse = new TestAsyncResponse(randomAlphaOfLength(130), randomLong());
PlainActionFuture<IndexResponse> createFuture1 = new PlainActionFuture<>();
indexService.createResponse(executionId1.getDocId(), Map.of(), initialResponse, createFuture1);
createFuture1.actionGet();
// setting very small limit for the max size of async search response
int limit = randomIntBetween(1, 125);
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.transientSettings(Settings.builder().put("search.max_async_search_response_size", limit + "b"));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
String expectedErrMsg = "Can't store an async search response larger than [" + limit + "] bytes. " +
"This limit can be set by changing the [" + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + "] setting.";
// test that an update operation of the initial response fails
PlainActionFuture<UpdateResponse> updateFuture = new PlainActionFuture<>();
TestAsyncResponse updateResponse = new TestAsyncResponse(randomAlphaOfLength(130), randomLong());
indexService.updateResponse(executionId1.getDocId(), Map.of(), updateResponse, updateFuture);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, updateFuture::actionGet);
assertEquals(expectedErrMsg, e.getMessage());
// test that the inital response is overwritten with a failure
PlainActionFuture<TestAsyncResponse> getFuture = new PlainActionFuture<>();
indexService.getResponse(executionId1, randomBoolean(), getFuture);
assertEquals(expectedErrMsg, getFuture.actionGet().failure);
// test that a create operation fails
AsyncExecutionId executionId2 = new AsyncExecutionId(Long.toString(randomNonNegativeLong()),
new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()));
PlainActionFuture<IndexResponse> createFuture = new PlainActionFuture<>();
TestAsyncResponse initialResponse2 = new TestAsyncResponse(randomAlphaOfLength(130), randomLong());
indexService.createResponse(executionId2.getDocId(), Map.of(), initialResponse2, createFuture);
IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, createFuture::actionGet);
assertEquals(expectedErrMsg, e2.getMessage());
} finally {
// restoring limit
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.transientSettings(Settings.builder().put("search.max_async_search_response_size", (String) null));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
}
}
}