Skip to content

Commit 807a9d8

Browse files
Remove redundant exception object creation (elastic#81407) (elastic#88847)
Co-authored-by: GrandFisher <[email protected]>
1 parent 4c9f8b0 commit 807a9d8

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

server/src/main/java/org/elasticsearch/action/support/broadcast/node/TransportBroadcastByNodeAction.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.action.ActionListener;
1313
import org.elasticsearch.action.FailedNodeException;
1414
import org.elasticsearch.action.IndicesRequest;
15-
import org.elasticsearch.action.NoShardAvailableActionException;
1615
import org.elasticsearch.action.support.ActionFilters;
1716
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
1817
import org.elasticsearch.action.support.HandledTransportAction;
@@ -120,7 +119,7 @@ public TransportBroadcastByNodeAction(
120119
private Response newResponse(
121120
Request request,
122121
AtomicReferenceArray<?> responses,
123-
List<NoShardAvailableActionException> unavailableShardExceptions,
122+
int unavailableShardCount,
124123
Map<String, List<ShardRouting>> nodes,
125124
ClusterState clusterState
126125
) {
@@ -154,7 +153,7 @@ private Response newResponse(
154153
}
155154
}
156155
}
157-
totalShards += unavailableShardExceptions.size();
156+
totalShards += unavailableShardCount;
158157
int failedShards = exceptions.size();
159158
return newResponse(request, totalShards, successfulShards, failedShards, broadcastByNodeResponses, exceptions, clusterState);
160159
}
@@ -267,7 +266,7 @@ protected class AsyncAction {
267266
private final Map<String, List<ShardRouting>> nodeIds;
268267
private final AtomicReferenceArray<Object> responses;
269268
private final AtomicInteger counter = new AtomicInteger();
270-
private List<NoShardAvailableActionException> unavailableShardExceptions = new ArrayList<>();
269+
private final int unavailableShardCount;
271270

272271
protected AsyncAction(Task task, Request request, ActionListener<Response> listener) {
273272
this.task = task;
@@ -294,6 +293,7 @@ protected AsyncAction(Task task, Request request, ActionListener<Response> liste
294293
ShardsIterator shardIt = shards(clusterState, request, concreteIndices);
295294
nodeIds = new HashMap<>();
296295

296+
int unavailableShardCount = 0;
297297
for (ShardRouting shard : shardIt) {
298298
// send a request to the shard only if it is assigned to a node that is in the local node's cluster state
299299
// a scenario in which a shard can be assigned but to a node that is not in the local node's cluster state
@@ -308,15 +308,11 @@ protected AsyncAction(Task task, Request request, ActionListener<Response> liste
308308
}
309309
nodeIds.get(nodeId).add(shard);
310310
} else {
311-
unavailableShardExceptions.add(
312-
new NoShardAvailableActionException(
313-
shard.shardId(),
314-
" no shards available for shard " + shard.toString() + " while executing " + actionName
315-
)
316-
);
311+
unavailableShardCount++;
317312
}
318-
}
319313

314+
}
315+
this.unavailableShardCount = unavailableShardCount;
320316
responses = new AtomicReferenceArray<>(nodeIds.size());
321317
}
322318

@@ -409,7 +405,7 @@ protected void onCompletion() {
409405

410406
Response response = null;
411407
try {
412-
response = newResponse(request, responses, unavailableShardExceptions, nodeIds, clusterState);
408+
response = newResponse(request, responses, unavailableShardCount, nodeIds, clusterState);
413409
} catch (Exception e) {
414410
logger.debug("failed to combine responses from nodes", e);
415411
listener.onFailure(e);

0 commit comments

Comments
 (0)