Skip to content

Commit a923dca

Browse files
authored
Fix ArrayIndexOutOfBoundsException in ShardBulkInferenceActionFilter (elastic#122538) (elastic#122849)
1 parent d73b7c2 commit a923dca

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

docs/changelog/122538.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122538
2+
summary: Fix `ArrayIndexOutOfBoundsException` in `ShardBulkInferenceActionFilter`
3+
area: Ingest
4+
type: bug
5+
issues: []

x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/action/filter/ShardBulkInferenceActionFilterIT.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Set;
4545

4646
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticTextInput;
47+
import static org.hamcrest.Matchers.containsString;
4748
import static org.hamcrest.Matchers.equalTo;
4849

4950
public class ShardBulkInferenceActionFilterIT extends ESIntegTestCase {
@@ -183,4 +184,48 @@ public void testBulkOperations() throws Exception {
183184
searchResponse.decRef();
184185
}
185186
}
187+
188+
public void testItemFailures() {
189+
prepareCreate(INDEX_NAME).setMapping(
190+
String.format(
191+
Locale.ROOT,
192+
"""
193+
{
194+
"properties": {
195+
"sparse_field": {
196+
"type": "semantic_text",
197+
"inference_id": "%s"
198+
},
199+
"dense_field": {
200+
"type": "semantic_text",
201+
"inference_id": "%s"
202+
}
203+
}
204+
}
205+
""",
206+
TestSparseInferenceServiceExtension.TestInferenceService.NAME,
207+
TestDenseInferenceServiceExtension.TestInferenceService.NAME
208+
)
209+
).get();
210+
211+
BulkRequestBuilder bulkReqBuilder = client().prepareBulk();
212+
int totalBulkSize = randomIntBetween(100, 200); // Use a bulk request size large enough to require batching
213+
for (int bulkSize = 0; bulkSize < totalBulkSize; bulkSize++) {
214+
String id = Integer.toString(bulkSize);
215+
216+
// Set field values that will cause errors when generating inference requests
217+
Map<String, Object> source = new HashMap<>();
218+
source.put("sparse_field", List.of(Map.of("foo", "bar"), Map.of("baz", "bar")));
219+
source.put("dense_field", List.of(Map.of("foo", "bar"), Map.of("baz", "bar")));
220+
221+
bulkReqBuilder.add(new IndexRequestBuilder(client()).setIndex(INDEX_NAME).setId(id).setSource(source));
222+
}
223+
224+
BulkResponse bulkResponse = bulkReqBuilder.get();
225+
assertThat(bulkResponse.hasFailures(), equalTo(true));
226+
for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
227+
assertThat(bulkItemResponse.isFailed(), equalTo(true));
228+
assertThat(bulkItemResponse.getFailureMessage(), containsString("expected [String|Number|Boolean]"));
229+
}
230+
}
186231
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/action/filter/ShardBulkInferenceActionFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ private Map<String, List<FieldInferenceRequest>> createFieldInferenceRequests(Bu
482482
isUpdateRequest = true;
483483
if (updateRequest.script() != null) {
484484
addInferenceResponseFailure(
485-
item.id(),
485+
itemIndex,
486486
new ElasticsearchStatusException(
487487
"Cannot apply update with a script on indices that contain [{}] field(s)",
488488
RestStatus.BAD_REQUEST,
@@ -540,7 +540,7 @@ private Map<String, List<FieldInferenceRequest>> createFieldInferenceRequests(Bu
540540
if (valueObj == null || valueObj == EXPLICIT_NULL) {
541541
if (isUpdateRequest && useLegacyFormat) {
542542
addInferenceResponseFailure(
543-
item.id(),
543+
itemIndex,
544544
new ElasticsearchStatusException(
545545
"Field [{}] must be specified on an update request to calculate inference for field [{}]",
546546
RestStatus.BAD_REQUEST,
@@ -557,7 +557,7 @@ private Map<String, List<FieldInferenceRequest>> createFieldInferenceRequests(Bu
557557
try {
558558
values = SemanticTextUtils.nodeStringValues(field, valueObj);
559559
} catch (Exception exc) {
560-
addInferenceResponseFailure(item.id(), exc);
560+
addInferenceResponseFailure(itemIndex, exc);
561561
break;
562562
}
563563

0 commit comments

Comments
 (0)