Skip to content

Commit 547a050

Browse files
committed
code udpated after review
1 parent bfcd31e commit 547a050

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchRefcountAndFallbackTests.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import org.elasticsearch.ElasticsearchStatusException;
1010
import org.elasticsearch.ExceptionsHelper;
11-
import org.elasticsearch.action.ActionListener;
1211
import org.elasticsearch.action.DocWriteResponse;
1312
import org.elasticsearch.action.delete.DeleteResponse;
1413
import org.elasticsearch.action.search.SearchResponse;
@@ -17,7 +16,6 @@
1716
import org.elasticsearch.cluster.service.ClusterService;
1817
import org.elasticsearch.common.lucene.Lucene;
1918
import org.elasticsearch.common.util.BigArrays;
20-
import org.elasticsearch.core.SuppressForbidden;
2119
import org.elasticsearch.core.TimeValue;
2220
import org.elasticsearch.rest.RestStatus;
2321
import org.elasticsearch.search.SearchHits;
@@ -34,8 +32,6 @@
3432
import org.junit.After;
3533
import org.junit.Before;
3634

37-
import java.lang.reflect.Field;
38-
import java.lang.reflect.Method;
3935
import java.util.Map;
4036

4137
import static org.hamcrest.Matchers.instanceOf;
@@ -115,23 +111,16 @@ public void testBuildSucceedsIfAnotherThreadHoldsRef() {
115111
* When the in-memory MutableSearchResponse has been released (tryIncRef == false),
116112
* the task falls back to the async-search store and returns the persisted response (200 OK).
117113
*/
118-
@SuppressForbidden(reason = "access violation required in order to read private field for this test")
119114
public void testFallbackToStoreWhenInMemoryResponseReleased() throws Exception {
120115
// Create an AsyncSearchTask
121116
AsyncSearchTask task = createAsyncSearchTask();
122117
assertNotNull(task);
123118

124-
// Get response instance and method from task
125-
Field f = AsyncSearchTask.class.getDeclaredField("searchResponse");
126-
f.setAccessible(true);
127-
Method getResponse = AsyncSearchTask.class.getDeclaredMethod("getResponse", boolean.class, ActionListener.class);
128-
getResponse.setAccessible(true);
129-
130119
// Build a SearchResponse (ssr refCount -> 1) to be stored in the index
131120
SearchResponse storedSearchResponse = createSearchResponse(totalShards, totalShards, skippedShards);
132121

133122
// Take a ref - (msr refCount -> 1, ssr refCount -> 2)
134-
MutableSearchResponse msr = (MutableSearchResponse) f.get(task);
123+
MutableSearchResponse msr = task.getSearchResponse();
135124
msr.updateShardsAndClusters(totalShards, skippedShards, /*clusters*/ null);
136125
msr.updateFinalResponse(storedSearchResponse, /*ccsMinimizeRoundtrips*/ false);
137126

@@ -168,7 +157,7 @@ public void testFallbackToStoreWhenInMemoryResponseReleased() throws Exception {
168157
msr.decRef();
169158

170159
PlainActionFuture<AsyncSearchResponse> future = new PlainActionFuture<>();
171-
getResponse.invoke(task, true, future);
160+
task.getResponse(future);
172161

173162
AsyncSearchResponse resp = future.actionGet();
174163
assertNotNull("Expected response loaded from store", resp);
@@ -183,18 +172,11 @@ public void testFallbackToStoreWhenInMemoryResponseReleased() throws Exception {
183172
* When both the in-memory MutableSearchResponse has been released AND the stored
184173
* document has been deleted or not found, the task returns GONE (410).
185174
*/
186-
@SuppressForbidden(reason = "access violation required in order to read private field for this test")
187175
public void testGoneWhenInMemoryReleasedAndStoreMissing() throws Exception {
188176
AsyncSearchTask task = createAsyncSearchTask();
189177

190-
Field f = AsyncSearchTask.class.getDeclaredField("searchResponse");
191-
f.setAccessible(true);
192-
Method getResponse = AsyncSearchTask.class.getDeclaredMethod("getResponse", boolean.class, ActionListener.class);
193-
getResponse.setAccessible(true);
194-
195178
SearchResponse searchResponse = createSearchResponse(totalShards, totalShards, skippedShards);
196-
197-
MutableSearchResponse msr = (MutableSearchResponse) f.get(task);
179+
MutableSearchResponse msr = task.getSearchResponse();
198180
msr.updateShardsAndClusters(totalShards, skippedShards, null);
199181
msr.updateFinalResponse(searchResponse, false);
200182

@@ -223,9 +205,10 @@ public void testGoneWhenInMemoryReleasedAndStoreMissing() throws Exception {
223205
del.actionGet();
224206

225207
// Now the task must surface GONE
226-
PlainActionFuture<AsyncSearchResponse> fut = new PlainActionFuture<>();
227-
getResponse.invoke(task, /*restoreHeaders*/ true, fut);
228-
Exception ex = expectThrows(Exception.class, fut::actionGet);
208+
PlainActionFuture<AsyncSearchResponse> future = new PlainActionFuture<>();
209+
task.getResponse(future);
210+
211+
Exception ex = expectThrows(Exception.class, future::actionGet);
229212
Throwable cause = ExceptionsHelper.unwrapCause(ex);
230213
assertThat(cause, instanceOf(ElasticsearchStatusException.class));
231214
assertThat(ExceptionsHelper.status(cause), is(RestStatus.GONE));

x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearchTask.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,10 @@ private void executeCompletionListeners() {
370370
/**
371371
* Invokes the listener with the current {@link AsyncSearchResponse}
372372
* without restoring response headers into the calling thread context.
373+
*
374+
* Visible for testing
373375
*/
374-
private void getResponse(ActionListener<AsyncSearchResponse> listener) {
376+
void getResponse(ActionListener<AsyncSearchResponse> listener) {
375377
getResponse(false, listener);
376378
}
377379

@@ -426,6 +428,11 @@ private void getResponse(boolean restoreResponseHeaders, ActionListener<AsyncSea
426428
}
427429
}
428430

431+
// Visible for testing.
432+
MutableSearchResponse getSearchResponse() {
433+
return searchResponse;
434+
}
435+
429436
// checks if the search task should be cancelled
430437
private synchronized void checkCancellation() {
431438
long now = System.currentTimeMillis();

0 commit comments

Comments
 (0)