Skip to content

Commit 7d70c5a

Browse files
committed
Fix ArrayIndexOutOfBoundsException in ShardBulkInferenceActionFilter (elastic#122538)
(cherry picked from commit 229d392) # Conflicts: # x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/action/filter/ShardBulkInferenceActionFilterIT.java
1 parent 61aec94 commit 7d70c5a

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Set;
3838

3939
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticTextInput;
40+
import static org.hamcrest.Matchers.containsString;
4041
import static org.hamcrest.Matchers.equalTo;
4142

4243
public class ShardBulkInferenceActionFilterIT extends ESIntegTestCase {
@@ -152,4 +153,47 @@ public void testBulkOperations() throws Exception {
152153
}
153154
}
154155

156+
public void testItemFailures() {
157+
prepareCreate(INDEX_NAME).setMapping(
158+
String.format(
159+
Locale.ROOT,
160+
"""
161+
{
162+
"properties": {
163+
"sparse_field": {
164+
"type": "semantic_text",
165+
"inference_id": "%s"
166+
},
167+
"dense_field": {
168+
"type": "semantic_text",
169+
"inference_id": "%s"
170+
}
171+
}
172+
}
173+
""",
174+
TestSparseInferenceServiceExtension.TestInferenceService.NAME,
175+
TestDenseInferenceServiceExtension.TestInferenceService.NAME
176+
)
177+
).get();
178+
179+
BulkRequestBuilder bulkReqBuilder = client().prepareBulk();
180+
int totalBulkSize = randomIntBetween(100, 200); // Use a bulk request size large enough to require batching
181+
for (int bulkSize = 0; bulkSize < totalBulkSize; bulkSize++) {
182+
String id = Integer.toString(bulkSize);
183+
184+
// Set field values that will cause errors when generating inference requests
185+
Map<String, Object> source = new HashMap<>();
186+
source.put("sparse_field", List.of(Map.of("foo", "bar"), Map.of("baz", "bar")));
187+
source.put("dense_field", List.of(Map.of("foo", "bar"), Map.of("baz", "bar")));
188+
189+
bulkReqBuilder.add(new IndexRequestBuilder(client()).setIndex(INDEX_NAME).setId(id).setSource(source));
190+
}
191+
192+
BulkResponse bulkResponse = bulkReqBuilder.get();
193+
assertThat(bulkResponse.hasFailures(), equalTo(true));
194+
for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
195+
assertThat(bulkItemResponse.isFailed(), equalTo(true));
196+
assertThat(bulkItemResponse.getFailureMessage(), containsString("expected [String|Number|Boolean]"));
197+
}
198+
}
155199
}

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
@@ -427,7 +427,7 @@ private Map<String, List<FieldInferenceRequest>> createFieldInferenceRequests(Bu
427427
isUpdateRequest = true;
428428
if (updateRequest.script() != null) {
429429
addInferenceResponseFailure(
430-
item.id(),
430+
itemIndex,
431431
new ElasticsearchStatusException(
432432
"Cannot apply update with a script on indices that contain [{}] field(s)",
433433
RestStatus.BAD_REQUEST,
@@ -458,7 +458,7 @@ private Map<String, List<FieldInferenceRequest>> createFieldInferenceRequests(Bu
458458
if (valueObj == null) {
459459
if (isUpdateRequest) {
460460
addInferenceResponseFailure(
461-
item.id(),
461+
itemIndex,
462462
new ElasticsearchStatusException(
463463
"Field [{}] must be specified on an update request to calculate inference for field [{}]",
464464
RestStatus.BAD_REQUEST,
@@ -475,7 +475,7 @@ private Map<String, List<FieldInferenceRequest>> createFieldInferenceRequests(Bu
475475
try {
476476
values = nodeStringValues(field, valueObj);
477477
} catch (Exception exc) {
478-
addInferenceResponseFailure(item.id(), exc);
478+
addInferenceResponseFailure(itemIndex, exc);
479479
break;
480480
}
481481
List<FieldInferenceRequest> fieldRequests = fieldRequestsMap.computeIfAbsent(inferenceId, k -> new ArrayList<>());

0 commit comments

Comments
 (0)