Skip to content

Commit 43c437c

Browse files
Cleanup more ActionListener Delegation Spots (#69662) (#70438)
Cleaning up the remaining spots where the short-wrapper methods could be used that I could find.
1 parent 57eaa54 commit 43c437c

File tree

37 files changed

+346
-832
lines changed

37 files changed

+346
-832
lines changed

server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ public RestResponse buildResponse(final Table table) throws Exception {
105105
}
106106
});
107107

108-
sendGetSettingsRequest(indices, indicesOptions, local, masterNodeTimeout, client, new ActionListener<GetSettingsResponse>() {
108+
sendGetSettingsRequest(indices, indicesOptions, local, masterNodeTimeout, client,
109+
new ActionListener.Delegating<GetSettingsResponse, Table>(listener) {
109110
@Override
110111
public void onResponse(final GetSettingsResponse getSettingsResponse) {
111-
final GroupedActionListener<ActionResponse> groupedListener = createGroupedListener(request, 4, listener);
112+
final GroupedActionListener<ActionResponse> groupedListener = createGroupedListener(request, 4, delegate);
112113
groupedListener.onResponse(getSettingsResponse);
113114

114115
// The list of indices that will be returned is determined by the indices returned from the Get Settings call.
@@ -132,11 +133,6 @@ public void onResponse(final GetSettingsResponse getSettingsResponse) {
132133
sendClusterHealthRequest(indices, subRequestIndicesOptions, local, masterNodeTimeout, client,
133134
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure));
134135
}
135-
136-
@Override
137-
public void onFailure(final Exception e) {
138-
listener.onFailure(e);
139-
}
140136
});
141137
};
142138
}
@@ -214,7 +210,7 @@ private void sendIndicesStatsRequest(final String[] indices,
214210

215211
private GroupedActionListener<ActionResponse> createGroupedListener(final RestRequest request, final int size,
216212
final ActionListener<Table> listener) {
217-
return new GroupedActionListener<>(new ActionListener<Collection<ActionResponse>>() {
213+
return new GroupedActionListener<>(new ActionListener.Delegating<Collection<ActionResponse>, Table>(listener) {
218214
@Override
219215
public void onResponse(final Collection<ActionResponse> responses) {
220216
try {
@@ -234,16 +230,11 @@ public void onResponse(final Collection<ActionResponse> responses) {
234230
Map<String, IndexStats> indicesStats = statsResponse.getIndices();
235231

236232
Table responseTable = buildTable(request, indicesSettings, indicesHealths, indicesStats, indicesStates);
237-
listener.onResponse(responseTable);
233+
delegate.onResponse(responseTable);
238234
} catch (Exception e) {
239235
onFailure(e);
240236
}
241237
}
242-
243-
@Override
244-
public void onFailure(final Exception e) {
245-
listener.onFailure(e);
246-
}
247238
}, size);
248239
}
249240

server/src/main/java/org/elasticsearch/search/SearchService.java

+22-38
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,10 @@ protected void doClose() {
338338
public void executeDfsPhase(ShardSearchRequest request, boolean keepStatesInContext,
339339
SearchShardTask task, ActionListener<SearchPhaseResult> listener) {
340340
final IndexShard shard = getShard(request);
341-
rewriteAndFetchShardRequest(shard, request, new ActionListener<ShardSearchRequest>() {
342-
@Override
343-
public void onResponse(ShardSearchRequest rewritten) {
344-
// fork the execution in the search thread pool
345-
runAsync(getExecutor(shard), () -> executeDfsPhase(request, task, keepStatesInContext), listener);
346-
}
347-
348-
@Override
349-
public void onFailure(Exception exc) {
350-
listener.onFailure(exc);
351-
}
352-
});
341+
rewriteAndFetchShardRequest(shard, request, listener.delegateFailure((l, rewritten) -> {
342+
// fork the execution in the search thread pool
343+
runAsync(getExecutor(shard), () -> executeDfsPhase(request, task, keepStatesInContext), l);
344+
}));
353345
}
354346

355347
private DfsSearchResult executeDfsPhase(ShardSearchRequest request,
@@ -385,34 +377,26 @@ public void executeQueryPhase(ShardSearchRequest request, boolean keepStatesInCo
385377
assert request.canReturnNullResponseIfMatchNoDocs() == false || request.numberOfShards() > 1
386378
: "empty responses require more than one shard";
387379
final IndexShard shard = getShard(request);
388-
rewriteAndFetchShardRequest(shard, request, new ActionListener<ShardSearchRequest>() {
389-
@Override
390-
public void onResponse(ShardSearchRequest orig) {
391-
// check if we can shortcut the query phase entirely.
392-
if (orig.canReturnNullResponseIfMatchNoDocs()) {
393-
assert orig.scroll() == null;
394-
final CanMatchResponse canMatchResp;
395-
try {
396-
ShardSearchRequest clone = new ShardSearchRequest(orig);
397-
canMatchResp = canMatch(clone, false);
398-
} catch (Exception exc) {
399-
listener.onFailure(exc);
400-
return;
401-
}
402-
if (canMatchResp.canMatch == false) {
403-
listener.onResponse(QuerySearchResult.nullInstance());
404-
return;
405-
}
380+
rewriteAndFetchShardRequest(shard, request, listener.delegateFailure((l, orig) -> {
381+
// check if we can shortcut the query phase entirely.
382+
if (orig.canReturnNullResponseIfMatchNoDocs()) {
383+
assert orig.scroll() == null;
384+
final CanMatchResponse canMatchResp;
385+
try {
386+
ShardSearchRequest clone = new ShardSearchRequest(orig);
387+
canMatchResp = canMatch(clone, false);
388+
} catch (Exception exc) {
389+
l.onFailure(exc);
390+
return;
391+
}
392+
if (canMatchResp.canMatch == false) {
393+
l.onResponse(QuerySearchResult.nullInstance());
394+
return;
406395
}
407-
// fork the execution in the search thread pool
408-
runAsync(getExecutor(shard), () -> executeQueryPhase(orig, task, keepStatesInContext), listener);
409-
}
410-
411-
@Override
412-
public void onFailure(Exception exc) {
413-
listener.onFailure(exc);
414396
}
415-
});
397+
// fork the execution in the search thread pool
398+
runAsync(getExecutor(shard), () -> executeQueryPhase(orig, task, keepStatesInContext), l);
399+
}));
416400
}
417401

418402
private IndexShard getShard(ShardSearchRequest request) {

server/src/main/java/org/elasticsearch/transport/ProxyConnectionStrategy.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,11 @@ public void onFailure(Exception e) {
209209
DiscoveryNode node = new DiscoveryNode(id, resolved, attributes, DiscoveryNodeRole.BUILT_IN_ROLES,
210210
Version.CURRENT.minimumCompatibilityVersion());
211211

212-
connectionManager.connectToNode(node, null, clusterNameValidator, new ActionListener<Void>() {
213-
@Override
214-
public void onResponse(Void v) {
215-
compositeListener.onResponse(v);
216-
}
217-
218-
@Override
219-
public void onFailure(Exception e) {
220-
logger.debug(new ParameterizedMessage("failed to open remote connection [remote cluster: {}, address: {}]",
212+
connectionManager.connectToNode(node, null, clusterNameValidator, compositeListener.delegateResponse((l, e) -> {
213+
logger.debug(new ParameterizedMessage("failed to open remote connection [remote cluster: {}, address: {}]",
221214
clusterAlias, resolved), e);
222-
compositeListener.onFailure(e);
223-
}
224-
});
215+
l.onFailure(e);
216+
}));
225217
}
226218
} else {
227219
int openConnections = connectionManager.size();

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportForgetFollowerAction.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,28 @@ protected void shardOperation(final ForgetFollowerAction.Request request, final
102102

103103
final IndexShard indexShard = indicesService.indexServiceSafe(leaderIndex).getShard(shardRouting.shardId().id());
104104

105-
indexShard.acquirePrimaryOperationPermit(new ActionListener<Releasable>() {
105+
indexShard.acquirePrimaryOperationPermit(new ActionListener.Delegating<Releasable, EmptyResult>(listener) {
106106
@Override
107107
public void onResponse(Releasable releasable) {
108108
try {
109109
indexShard.removeRetentionLease(id, new ActionListener<ReplicationResponse>() {
110110
@Override
111111
public void onResponse(ReplicationResponse replicationResponse) {
112112
releasable.close();
113-
listener.onResponse(EmptyResult.INSTANCE);
113+
delegate.onResponse(EmptyResult.INSTANCE);
114114
}
115115

116116
@Override
117117
public void onFailure(Exception e) {
118118
releasable.close();
119-
listener.onFailure(e);
119+
delegate.onFailure(e);
120120
}
121121
});
122122
} catch (Exception e) {
123123
releasable.close();
124-
listener.onFailure(e);
124+
onFailure(e);
125125
}
126126
}
127-
128-
@Override
129-
public void onFailure(Exception e) {
130-
listener.onFailure(e);
131-
}
132127
}, ThreadPool.Names.SAME, request);
133128
}
134129

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackUsageAction.java

+6-25
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,16 @@ public TransportXPackUsageAction(ThreadPool threadPool, TransportService transpo
4646

4747
@Override
4848
protected void masterOperation(XPackUsageRequest request, ClusterState state, ActionListener<XPackUsageResponse> listener) {
49-
final ActionListener<List<XPackFeatureSet.Usage>> usageActionListener = new ActionListener<List<Usage>>() {
50-
@Override
51-
public void onResponse(List<Usage> usages) {
52-
listener.onResponse(new XPackUsageResponse(usages));
53-
}
54-
55-
@Override
56-
public void onFailure(Exception e) {
57-
listener.onFailure(e);
58-
}
59-
};
49+
final ActionListener<List<XPackFeatureSet.Usage>> usageActionListener =
50+
listener.delegateFailure((l, usages) -> l.onResponse(new XPackUsageResponse(usages)));
6051
final AtomicReferenceArray<Usage> featureSetUsages = new AtomicReferenceArray<>(featureSets.size());
6152
final AtomicInteger position = new AtomicInteger(0);
6253
final BiConsumer<XPackFeatureSet, ActionListener<List<Usage>>> consumer = (featureSet, iteratingListener) -> {
6354
assert Transports.assertNotTransportThread("calculating usage can be more expensive than we allow on transport threads");
64-
featureSet.usage(new ActionListener<Usage>() {
65-
@Override
66-
public void onResponse(Usage usage) {
67-
featureSetUsages.set(position.getAndIncrement(), usage);
68-
// the value sent back doesn't matter since our predicate keeps iterating
69-
ActionRunnable<?> invokeListener = ActionRunnable.supply(iteratingListener, Collections::emptyList);
70-
threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(invokeListener);
71-
}
72-
73-
@Override
74-
public void onFailure(Exception e) {
75-
iteratingListener.onFailure(e);
76-
}
77-
});
55+
featureSet.usage(iteratingListener.delegateFailure((l, usage) -> {
56+
featureSetUsages.set(position.getAndIncrement(), usage);
57+
threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(ActionRunnable.supply(iteratingListener, Collections::emptyList));
58+
}));
7859
};
7960
IteratingActionListener<List<XPackFeatureSet.Usage>, XPackFeatureSet> iteratingActionListener =
8061
new IteratingActionListener<>(usageActionListener, consumer, featureSets,

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncResultsService.java

+3-23
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,8 @@ private void getSearchResponseFromTask(AsyncExecutionId searchId,
126126
if (expirationTimeMillis != -1) {
127127
task.setExpirationTime(expirationTimeMillis);
128128
}
129-
addCompletionListener.apply(task, new ActionListener<Response>() {
130-
@Override
131-
public void onResponse(Response response) {
132-
sendFinalResponse(request, response, nowInMillis, listener);
133-
}
134-
135-
@Override
136-
public void onFailure(Exception exc) {
137-
listener.onFailure(exc);
138-
}
139-
}, request.getWaitForCompletionTimeout());
129+
addCompletionListener.apply(task, listener.delegateFailure((l, response) ->
130+
sendFinalResponse(request, response, nowInMillis, l)), request.getWaitForCompletionTimeout());
140131
} catch (Exception exc) {
141132
listener.onFailure(exc);
142133
}
@@ -146,18 +137,7 @@ private void getSearchResponseFromIndex(AsyncExecutionId searchId,
146137
GetAsyncResultRequest request,
147138
long nowInMillis,
148139
ActionListener<Response> listener) {
149-
store.getResponse(searchId, true,
150-
new ActionListener<Response>() {
151-
@Override
152-
public void onResponse(Response response) {
153-
sendFinalResponse(request, response, nowInMillis, listener);
154-
}
155-
156-
@Override
157-
public void onFailure(Exception e) {
158-
listener.onFailure(e);
159-
}
160-
});
140+
store.getResponse(searchId, true, listener.delegateFailure((l, response) -> sendFinalResponse(request, response, nowInMillis, l)));
161141
}
162142

163143
private void sendFinalResponse(GetAsyncResultRequest request,

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskIndexService.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,8 @@ public <T extends AsyncTask, SR extends SearchStatusResponse> void retrieveStatu
371371
SR response = statusProducerFromTask.apply(asyncTask);
372372
sendFinalStatusResponse(request, response, listener);
373373
} else { // get status response from index
374-
getStatusResponseFromIndex(asyncExecutionId, statusProducerFromIndex,
375-
new ActionListener<SR>() {
376-
@Override
377-
public void onResponse(SR searchStatusResponse) {
378-
sendFinalStatusResponse(request, searchStatusResponse, listener);
379-
}
380-
@Override
381-
public void onFailure(Exception e) {
382-
listener.onFailure(e);
383-
}
384-
}
385-
);
374+
getStatusResponseFromIndex(asyncExecutionId, statusProducerFromIndex, listener.delegateFailure(
375+
(l, searchStatusResponse) -> sendFinalStatusResponse(request, searchStatusResponse, l)));
386376
}
387377
} catch (Exception exc) {
388378
listener.onFailure(exc);

0 commit comments

Comments
 (0)