-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Refine indexing pressure accounting in semantic bulk inference filter #129320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
e44d8c7
4c56277
aeda08b
a12c9f1
c85718d
474c9a7
6e62f09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,9 @@ | |
| import org.elasticsearch.cluster.metadata.InferenceFieldMetadata; | ||
| import org.elasticsearch.cluster.metadata.ProjectMetadata; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.bytes.CompositeBytesReference; | ||
| import org.elasticsearch.common.settings.Setting; | ||
| import org.elasticsearch.common.unit.ByteSizeValue; | ||
| import org.elasticsearch.common.util.concurrent.AtomicArray; | ||
|
|
@@ -52,6 +54,7 @@ | |
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.xcontent.XContent; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
@@ -469,8 +472,8 @@ private void recordRequestCountMetrics(Model model, int incrementBy, Throwable t | |
| * Adds all inference requests associated with their respective inference IDs to the given {@code requestsMap} | ||
| * for the specified {@code item}. | ||
| * | ||
| * @param item The bulk request item to process. | ||
| * @param itemIndex The position of the item within the original bulk request. | ||
| * @param item The bulk request item to process. | ||
| * @param itemIndex The position of the item within the original bulk request. | ||
| * @param requestsMap A map storing inference requests, where each key is an inference ID, | ||
| * and the value is a list of associated {@link FieldInferenceRequest} objects. | ||
| * @return The total content length of all newly added requests, or {@code 0} if no requests were added. | ||
|
|
@@ -671,27 +674,137 @@ private void applyInferenceResponses(BulkItemRequest item, FieldInferenceRespons | |
| ); | ||
| inferenceFieldsMap.put(fieldName, result); | ||
| } | ||
|
|
||
| BytesReference originalSource = indexRequest.source(); | ||
| if (useLegacyFormat) { | ||
| var newDocMap = indexRequest.sourceAsMap(); | ||
| for (var entry : inferenceFieldsMap.entrySet()) { | ||
| SemanticTextUtils.insertValue(entry.getKey(), newDocMap, entry.getValue()); | ||
| } | ||
| indexRequest.source(newDocMap, indexRequest.getContentType()); | ||
| XContentBuilder builder = XContentFactory.contentBuilder(indexRequest.getContentType()); | ||
| builder.map(newDocMap); | ||
| var newSource = BytesReference.bytes(builder); | ||
| if (incrementIndexingPressure(item, indexRequest, newSource.length())) { | ||
| indexRequest.source(newSource, indexRequest.getContentType()); | ||
| } | ||
| } else { | ||
| updateSourceWithInferenceFields(item, indexRequest, inferenceFieldsMap); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Updates the {@link IndexRequest}'s source to include additional inference fields. | ||
| * <p> | ||
| * If the original source uses an array-backed {@link BytesReference}, this method attempts an in-place update, | ||
| * reusing the existing array where possible and appending additional bytes only if needed. | ||
| * <p> | ||
| * If the original source is not array-backed, the entire source is replaced with the new source that includes | ||
| * the inference fields. In this case, the full size of the new source is accounted for in indexing pressure. | ||
| * <p> | ||
| * Note: We do not subtract the indexing pressure of the original source since its bytes may be pooled and not | ||
| * reclaimable by the garbage collector during the request lifecycle. | ||
| * | ||
| * @param item The {@link BulkItemRequest} being processed. | ||
| * @param indexRequest The {@link IndexRequest} whose source will be updated. | ||
| * @param inferenceFieldsMap A map of additional fields to append to the source. | ||
| * @throws IOException if building the new source fails. | ||
| */ | ||
| private void updateSourceWithInferenceFields( | ||
| BulkItemRequest item, | ||
| IndexRequest indexRequest, | ||
| Map<String, Object> inferenceFieldsMap | ||
| ) throws IOException { | ||
| var originalSource = indexRequest.source(); | ||
| final BytesReference newSource; | ||
|
|
||
| // Build a new source by appending the inference fields to the existing source. | ||
| try (XContentBuilder builder = XContentBuilder.builder(indexRequest.getContentType().xContent())) { | ||
| appendSourceAndInferenceMetadata(builder, originalSource, indexRequest.getContentType(), inferenceFieldsMap); | ||
| newSource = BytesReference.bytes(builder); | ||
| } | ||
|
|
||
| // Calculate the additional size to account for in indexing pressure. | ||
| final int additionalSize = originalSource.hasArray() ? newSource.length() - originalSource.length() : newSource.length(); | ||
|
|
||
| // If we exceed the indexing pressure limit, do not proceed with the update. | ||
| if (incrementIndexingPressure(item, indexRequest, additionalSize) == false) { | ||
| return; | ||
| } | ||
|
|
||
| // Apply the updated source to the index request. | ||
| if (originalSource.hasArray()) { | ||
| // If the original source is backed by an array, perform in-place update: | ||
| // - Copy as much of the new source as fits into the original array. | ||
| System.arraycopy( | ||
| newSource.array(), | ||
| newSource.arrayOffset(), | ||
| originalSource.array(), | ||
| originalSource.arrayOffset(), | ||
| originalSource.length() | ||
| ); | ||
|
|
||
| int remainingSize = newSource.length() - originalSource.length(); | ||
| if (remainingSize > 0) { | ||
| // If there are additional bytes, append them as a new BytesArray segment. | ||
| byte[] remainingBytes = new byte[remainingSize]; | ||
| System.arraycopy( | ||
| newSource.array(), | ||
| newSource.arrayOffset() + originalSource.length(), | ||
| remainingBytes, | ||
| 0, | ||
| remainingSize | ||
| ); | ||
| indexRequest.source( | ||
| CompositeBytesReference.of(originalSource, new BytesArray(remainingBytes)), | ||
| indexRequest.getContentType() | ||
| ); | ||
| } else { | ||
| // No additional bytes; just adjust the slice length. | ||
| indexRequest.source(originalSource.slice(0, newSource.length())); | ||
| } | ||
| } else { | ||
| try (XContentBuilder builder = XContentBuilder.builder(indexRequest.getContentType().xContent())) { | ||
| appendSourceAndInferenceMetadata(builder, indexRequest.source(), indexRequest.getContentType(), inferenceFieldsMap); | ||
| indexRequest.source(builder); | ||
| // If the original source is not array-backed, replace it entirely. | ||
| indexRequest.source(newSource, indexRequest.getContentType()); | ||
| } | ||
|
Comment on lines
+732
to
+766
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have to do this in this PR, but it would be good to put this logic in a common place ( |
||
| } | ||
|
|
||
| /** | ||
| * Appends the original source and the new inference metadata field directly to the provided | ||
| * {@link XContentBuilder}, avoiding the need to materialize the original source as a {@link Map}. | ||
| */ | ||
| private void appendSourceAndInferenceMetadata( | ||
| XContentBuilder builder, | ||
| BytesReference source, | ||
| XContentType xContentType, | ||
| Map<String, Object> inferenceFieldsMap | ||
| ) throws IOException { | ||
| builder.startObject(); | ||
|
|
||
| // append the original source | ||
| try ( | ||
| XContentParser parser = XContentHelper.createParserNotCompressed(XContentParserConfiguration.EMPTY, source, xContentType) | ||
| ) { | ||
| // skip start object | ||
| parser.nextToken(); | ||
| while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| builder.copyCurrentStructure(parser); | ||
| } | ||
| } | ||
| long modifiedSourceSize = indexRequest.source().ramBytesUsed(); | ||
|
|
||
| // Add the indexing pressure from the source modifications. | ||
| // add the inference metadata field | ||
| builder.field(InferenceMetadataFieldsMapper.NAME); | ||
| try (XContentParser parser = XContentHelper.mapToXContentParser(XContentParserConfiguration.EMPTY, inferenceFieldsMap)) { | ||
| builder.copyCurrentStructure(parser); | ||
| } | ||
|
|
||
| builder.endObject(); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the operation performed in this method temporarily generate a second copy of the source with |
||
|
|
||
| private boolean incrementIndexingPressure(BulkItemRequest item, IndexRequest indexRequest, int inc) { | ||
| try { | ||
| coordinatingIndexingPressure.increment(1, modifiedSourceSize - originalSource.ramBytesUsed()); | ||
| if (inc > 0) { | ||
| coordinatingIndexingPressure.increment(1, inc); | ||
| } | ||
| return true; | ||
| } catch (EsRejectedExecutionException e) { | ||
| indexRequest.source(originalSource, indexRequest.getContentType()); | ||
| inferenceStats.bulkRejection().incrementBy(1); | ||
| item.abort( | ||
| item.index(), | ||
|
|
@@ -702,40 +815,11 @@ private void applyInferenceResponses(BulkItemRequest item, FieldInferenceRespons | |
| e | ||
| ) | ||
| ); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Appends the original source and the new inference metadata field directly to the provided | ||
| * {@link XContentBuilder}, avoiding the need to materialize the original source as a {@link Map}. | ||
| */ | ||
| private static void appendSourceAndInferenceMetadata( | ||
| XContentBuilder builder, | ||
| BytesReference source, | ||
| XContentType xContentType, | ||
| Map<String, Object> inferenceFieldsMap | ||
| ) throws IOException { | ||
| builder.startObject(); | ||
|
|
||
| // append the original source | ||
| try (XContentParser parser = XContentHelper.createParserNotCompressed(XContentParserConfiguration.EMPTY, source, xContentType)) { | ||
| // skip start object | ||
| parser.nextToken(); | ||
| while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| builder.copyCurrentStructure(parser); | ||
| } | ||
| } | ||
|
|
||
| // add the inference metadata field | ||
| builder.field(InferenceMetadataFieldsMapper.NAME); | ||
| try (XContentParser parser = XContentHelper.mapToXContentParser(XContentParserConfiguration.EMPTY, inferenceFieldsMap)) { | ||
| builder.copyCurrentStructure(parser); | ||
| } | ||
|
|
||
| builder.endObject(); | ||
| } | ||
|
|
||
| static IndexRequest getIndexRequestOrNull(DocWriteRequest<?> docWriteRequest) { | ||
| if (docWriteRequest instanceof IndexRequest indexRequest) { | ||
| return indexRequest; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there was an update to this test suite in #129140 that disabled these tests on the new semantic text format. We should probably fix that... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oups, good catch thanks. I added the test back in c85718d There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should apply just this change to 9.1 and 8.19 so that we restore test coverage in those branches |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we assume that
newSourcewill always be array-backed?