Skip to content
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

Eagerly check keyword field length #83738

Merged
merged 8 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/83738.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 83738
summary: Check the utf8 length of keyword field is not bigger than 32766 in ES, rather than in Lucene.
area: Mapping
type: enhancement
issues:
- 80865
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -69,6 +70,8 @@
import java.util.Objects;
import java.util.function.Supplier;

import static org.apache.lucene.util.ByteBlockPool.BYTE_BLOCK_SIZE;

/**
* A field mapper for keywords. This mapper accepts strings and indexes them as-is.
*/
Expand Down Expand Up @@ -905,6 +908,27 @@ private void indexValue(DocumentParserContext context, String value) {

// convert to utf8 only once before feeding postings/dv/stored fields
final BytesRef binaryValue = new BytesRef(value);

// If the UTF8 encoding of the field value is bigger than the max length 32766, Lucene fill fail the indexing request and, to roll
// back the changes, will mark the (possibly partially indexed) document as deleted. This results in deletes, even in an append-only
// workload, which in turn leads to slower merges, as these will potentially have to fall back to MergeStrategy.DOC instead of
// MergeStrategy.BULK. To avoid this, we do a preflight check here before indexing the document into Lucene.
if (binaryValue.length > BYTE_BLOCK_SIZE - 2) {
kkewwei marked this conversation as resolved.
Show resolved Hide resolved
byte[] prefix = new byte[30];
System.arraycopy(binaryValue.bytes, binaryValue.offset, prefix, 0, 30);
String msg = "Document contains at least one immense term in field=\""
+ fieldType().name()
+ "\" (whose "
+ "UTF8 encoding is longer than the max length "
+ (BYTE_BLOCK_SIZE - 2)
+ "), all of which were "
+ "skipped. Please correct the analyzer to not produce such terms. The prefix of the first immense "
+ "term is: '"
+ Arrays.toString(prefix)
+ "...'";
throw new IllegalArgumentException(msg);
}

if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) {
Field field = new KeywordField(fieldType().name(), binaryValue, fieldType);
context.doc().add(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,17 @@ public void testDimensionInRoutingPath() throws IOException {
);
mapper.documentMapper().validate(settings, false); // Doesn't throw
}

public void testKeywordFieldUtf8LongerThan32766() throws Exception {
DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "keyword")));
StringBuilder stringBuilder = new StringBuilder(32768);
for (int i = 0; i < 32768; i++) {
stringBuilder.append("a");
}
MapperParsingException e = expectThrows(
MapperParsingException.class,
() -> mapper.parse(source(b -> b.field("field", stringBuilder.toString())))
);
assertThat(e.getCause().getMessage(), containsString("UTF8 encoding is longer than the max length"));
}
}