|
8 | 8 | package org.elasticsearch.xpack.esql.inference.bulk; |
9 | 9 |
|
10 | 10 | import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.support.ThreadedActionListener; |
11 | 12 | import org.elasticsearch.client.internal.Client; |
12 | 13 | import org.elasticsearch.common.util.concurrent.ConcurrentCollections; |
13 | 14 | import org.elasticsearch.threadpool.ThreadPool; |
|
25 | 26 |
|
26 | 27 | import static org.elasticsearch.xpack.core.ClientHelper.INFERENCE_ORIGIN; |
27 | 28 | import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; |
28 | | -import static org.elasticsearch.xpack.esql.plugin.EsqlPlugin.ESQL_WORKER_THREAD_POOL_NAME; |
29 | 29 |
|
30 | 30 | /** |
31 | 31 | * Implementation of bulk inference execution with throttling and concurrency control. |
@@ -88,7 +88,7 @@ public BulkInferenceRequest poll() { |
88 | 88 | public BulkInferenceRunner(Client client, int maxRunningTasks) { |
89 | 89 | this.permits = new Semaphore(maxRunningTasks); |
90 | 90 | this.client = client; |
91 | | - this.executor = client.threadPool().executor(ESQL_WORKER_THREAD_POOL_NAME); |
| 91 | + this.executor = client.threadPool().executor(ThreadPool.Names.SEARCH); |
92 | 92 | } |
93 | 93 |
|
94 | 94 | /** |
@@ -253,48 +253,51 @@ private void executePendingRequests(int recursionDepth) { |
253 | 253 | executionState.finish(); |
254 | 254 | } |
255 | 255 |
|
256 | | - final ActionListener<InferenceAction.Response> inferenceResponseListener = ActionListener.runAfter( |
257 | | - ActionListener.wrap( |
258 | | - r -> executionState.onInferenceResponse(bulkRequestItem.seqNo(), r), |
259 | | - e -> executionState.onInferenceException(bulkRequestItem.seqNo(), e) |
260 | | - ), |
261 | | - () -> { |
262 | | - // Release the permit we used |
263 | | - permits.release(); |
264 | | - |
265 | | - try { |
266 | | - synchronized (executionState) { |
267 | | - persistPendingResponses(); |
268 | | - } |
| 256 | + final ActionListener<InferenceAction.Response> inferenceResponseListener = new ThreadedActionListener<>( |
| 257 | + executor, |
| 258 | + ActionListener.runAfter( |
| 259 | + ActionListener.wrap( |
| 260 | + r -> executionState.onInferenceResponse(bulkRequestItem.seqNo(), r), |
| 261 | + e -> executionState.onInferenceException(bulkRequestItem.seqNo(), e) |
| 262 | + ), |
| 263 | + () -> { |
| 264 | + // Release the permit we used |
| 265 | + permits.release(); |
| 266 | + |
| 267 | + try { |
| 268 | + synchronized (executionState) { |
| 269 | + persistPendingResponses(); |
| 270 | + } |
269 | 271 |
|
270 | | - if (executionState.finished() && responseSent.compareAndSet(false, true)) { |
271 | | - onBulkCompletion(); |
272 | | - } |
| 272 | + if (executionState.finished() && responseSent.compareAndSet(false, true)) { |
| 273 | + onBulkCompletion(); |
| 274 | + } |
273 | 275 |
|
274 | | - if (responseSent.get()) { |
275 | | - // Response has already been sent |
276 | | - // No need to continue processing this bulk. |
277 | | - // Check if another bulk request is pending for execution. |
278 | | - BulkInferenceRequest nexBulkRequest = pendingBulkRequests.poll(); |
279 | | - if (nexBulkRequest != null) { |
280 | | - executor.execute(nexBulkRequest::executePendingRequests); |
| 276 | + if (responseSent.get()) { |
| 277 | + // Response has already been sent |
| 278 | + // No need to continue processing this bulk. |
| 279 | + // Check if another bulk request is pending for execution. |
| 280 | + BulkInferenceRequest nexBulkRequest = pendingBulkRequests.poll(); |
| 281 | + if (nexBulkRequest != null) { |
| 282 | + executor.execute(nexBulkRequest::executePendingRequests); |
| 283 | + } |
| 284 | + return; |
281 | 285 | } |
282 | | - return; |
283 | | - } |
284 | | - if (executionState.finished() == false) { |
285 | | - // Execute any pending requests if any |
286 | | - if (recursionDepth > 100) { |
287 | | - executor.execute(this::executePendingRequests); |
288 | | - } else { |
289 | | - this.executePendingRequests(recursionDepth + 1); |
| 286 | + if (executionState.finished() == false) { |
| 287 | + // Execute any pending requests if any |
| 288 | + if (recursionDepth > 100) { |
| 289 | + executor.execute(this::executePendingRequests); |
| 290 | + } else { |
| 291 | + this.executePendingRequests(recursionDepth + 1); |
| 292 | + } |
| 293 | + } |
| 294 | + } catch (Exception e) { |
| 295 | + if (responseSent.compareAndSet(false, true)) { |
| 296 | + completionListener.onFailure(e); |
290 | 297 | } |
291 | | - } |
292 | | - } catch (Exception e) { |
293 | | - if (responseSent.compareAndSet(false, true)) { |
294 | | - completionListener.onFailure(e); |
295 | 298 | } |
296 | 299 | } |
297 | | - } |
| 300 | + ) |
298 | 301 | ); |
299 | 302 |
|
300 | 303 | // Handle null requests (edge case in some iterators) |
|
0 commit comments