-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Refactor IndexRouting.ExtractFromSource to be an abstract class #135206
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
Changes from 9 commits
205466f
aa15c57
d22c530
45b206a
cdfe72a
9551184
9c680f0
591709d
18fadf3
8b28540
cf8ab97
58f8621
2b5d93b
2117dbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.cluster.routing; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.ParsingException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.util.ByteUtils; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.index.IndexVersions; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
import org.elasticsearch.xcontent.XContentString; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.IntSupplier; | ||
import java.util.function.Predicate; | ||
|
||
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
import static org.elasticsearch.common.xcontent.XContentParserUtils.expectValueToken; | ||
|
||
/** | ||
* A builder for computing a hash from fields in the document source that are part of the | ||
* {@link org.elasticsearch.cluster.metadata.IndexMetadata#INDEX_ROUTING_PATH}. | ||
* It is used in the context of {@link IndexRouting.ExtractFromSource.ForRoutingPath} to determine the shard a document should be routed to. | ||
*/ | ||
public class RoutingHashBuilder { | ||
private final List<NameAndHash> hashes = new ArrayList<>(); | ||
private final Predicate<String> isRoutingPath; | ||
|
||
public RoutingHashBuilder(Predicate<String> isRoutingPath) { | ||
this.isRoutingPath = isRoutingPath; | ||
} | ||
|
||
public void addMatching(String fieldName, BytesRef string) { | ||
if (isRoutingPath.test(fieldName)) { | ||
addHash(fieldName, string); | ||
} | ||
} | ||
|
||
/** | ||
* Only expected to be called for old indices created before | ||
* {@link IndexVersions#TIME_SERIES_ROUTING_HASH_IN_ID} while creating (during ingestion) | ||
* or synthesizing (at query time) the _id field. | ||
*/ | ||
public String createId(byte[] suffix, IntSupplier onEmpty) { | ||
byte[] idBytes = new byte[4 + suffix.length]; | ||
ByteUtils.writeIntLE(buildHash(onEmpty), idBytes, 0); | ||
System.arraycopy(suffix, 0, idBytes, 4, suffix.length); | ||
return Strings.BASE_64_NO_PADDING_URL_ENCODER.encodeToString(idBytes); | ||
} | ||
|
||
void extractObject(@Nullable String path, XContentParser source) throws IOException { | ||
while (source.currentToken() != XContentParser.Token.END_OBJECT) { | ||
ensureExpectedToken(XContentParser.Token.FIELD_NAME, source.currentToken(), source); | ||
String fieldName = source.currentName(); | ||
String subPath = path == null ? fieldName : path + "." + fieldName; | ||
source.nextToken(); | ||
extractItem(subPath, source); | ||
} | ||
} | ||
|
||
private void extractArray(@Nullable String path, XContentParser source) throws IOException { | ||
while (source.currentToken() != XContentParser.Token.END_ARRAY) { | ||
expectValueToken(source.currentToken(), source); | ||
extractItem(path, source); | ||
} | ||
} | ||
|
||
private void extractItem(String path, XContentParser source) throws IOException { | ||
switch (source.currentToken()) { | ||
case START_OBJECT: | ||
source.nextToken(); | ||
extractObject(path, source); | ||
source.nextToken(); | ||
break; | ||
case VALUE_STRING: | ||
case VALUE_NUMBER: | ||
case VALUE_BOOLEAN: | ||
XContentString.UTF8Bytes utf8Bytes = source.optimizedText().bytes(); | ||
addHash(path, new BytesRef(utf8Bytes.bytes(), utf8Bytes.offset(), utf8Bytes.length())); | ||
source.nextToken(); | ||
break; | ||
case START_ARRAY: | ||
source.nextToken(); | ||
extractArray(path, source); | ||
source.nextToken(); | ||
break; | ||
case VALUE_NULL: | ||
source.nextToken(); | ||
break; | ||
default: | ||
throw new ParsingException( | ||
source.getTokenLocation(), | ||
"Cannot extract routing path due to unexpected token [{}]", | ||
source.currentToken() | ||
); | ||
} | ||
} | ||
|
||
private void addHash(String path, BytesRef value) { | ||
hashes.add(new NameAndHash(new BytesRef(path), IndexRouting.ExtractFromSource.hash(value), hashes.size())); | ||
} | ||
|
||
int buildHash(IntSupplier onEmpty) { | ||
if (hashes.isEmpty()) { | ||
return onEmpty.getAsInt(); | ||
} | ||
Collections.sort(hashes); | ||
int hash = 0; | ||
for (NameAndHash nah : hashes) { | ||
hash = 31 * hash + (IndexRouting.ExtractFromSource.hash(nah.name) ^ nah.hash); | ||
} | ||
return hash; | ||
} | ||
|
||
private record NameAndHash(BytesRef name, int hash, int order) implements Comparable<NameAndHash> { | ||
@Override | ||
public int compareTo(NameAndHash o) { | ||
int i = name.compareTo(o.name); | ||
if (i != 0) return i; | ||
// ensures array values are in the order as they appear in the source | ||
return Integer.compare(order, o.order); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,6 +13,7 @@ | |||||||||||||||||||||||||
import org.apache.lucene.index.LeafReaderContext; | ||||||||||||||||||||||||||
import org.apache.lucene.search.Query; | ||||||||||||||||||||||||||
import org.apache.lucene.util.BytesRef; | ||||||||||||||||||||||||||
import org.elasticsearch.cluster.routing.IndexRouting; | ||||||||||||||||||||||||||
import org.elasticsearch.common.Explicit; | ||||||||||||||||||||||||||
import org.elasticsearch.common.regex.Regex; | ||||||||||||||||||||||||||
import org.elasticsearch.common.xcontent.XContentHelper; | ||||||||||||||||||||||||||
|
@@ -96,7 +97,7 @@ public ParsedDocument parseDocument(SourceToParse source, MappingLookup mappingL | |||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
context = new RootDocumentParserContext(mappingLookup, mappingParserContext, source, parser, source.tsid()); | ||||||||||||||||||||||||||
context = new RootDocumentParserContext(mappingLookup, mappingParserContext, source, parser); | ||||||||||||||||||||||||||
validateStart(context.parser()); | ||||||||||||||||||||||||||
MetadataFieldMapper[] metadataFieldsMappers = mappingLookup.getMapping().getSortedMetadataMappers(); | ||||||||||||||||||||||||||
internalParseDocument(metadataFieldsMappers, context); | ||||||||||||||||||||||||||
|
@@ -1077,8 +1078,7 @@ private static class RootDocumentParserContext extends DocumentParserContext { | |||||||||||||||||||||||||
MappingLookup mappingLookup, | ||||||||||||||||||||||||||
MappingParserContext mappingParserContext, | ||||||||||||||||||||||||||
SourceToParse source, | ||||||||||||||||||||||||||
XContentParser parser, | ||||||||||||||||||||||||||
BytesRef tsid | ||||||||||||||||||||||||||
XContentParser parser | ||||||||||||||||||||||||||
) throws IOException { | ||||||||||||||||||||||||||
super( | ||||||||||||||||||||||||||
mappingLookup, | ||||||||||||||||||||||||||
|
@@ -1087,8 +1087,17 @@ private static class RootDocumentParserContext extends DocumentParserContext { | |||||||||||||||||||||||||
mappingLookup.getMapping().getRoot(), | ||||||||||||||||||||||||||
ObjectMapper.Dynamic.getRootDynamic(mappingLookup) | ||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||
IndexSettings indexSettings = mappingParserContext.getIndexSettings(); | ||||||||||||||||||||||||||
BytesRef tsid = source.tsid(); | ||||||||||||||||||||||||||
if (tsid == null | ||||||||||||||||||||||||||
&& indexSettings.getMode() == IndexMode.TIME_SERIES | ||||||||||||||||||||||||||
&& indexSettings.getIndexRouting() instanceof IndexRouting.ExtractFromSource.ForIndexDimensions forIndexDimensions) { | ||||||||||||||||||||||||||
// the tsid is normally set on the coordinating node during shard routing and passed to the data node via the index request | ||||||||||||||||||||||||||
// but when applying a translog operation, shard routing is not happening, and we have to create the tsid from source | ||||||||||||||||||||||||||
tsid = forIndexDimensions.buildTsid(source.getXContentType(), source.source()); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
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. Ideally we would have
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. When it comes to replaying translog operations (which include the id but not the tsid), the effect of this check is similar because the id is based on the tsid, and it also checks in the elasticsearch/server/src/main/java/org/elasticsearch/index/mapper/TsidExtractingIdFieldMapper.java Lines 86 to 97 in 40f3a1c
|
||||||||||||||||||||||||||
this.tsid = tsid; | ||||||||||||||||||||||||||
assert tsid == null || mappingParserContext.getIndexSettings().getMode() == IndexMode.TIME_SERIES | ||||||||||||||||||||||||||
assert this.tsid == null || indexSettings.getMode() == IndexMode.TIME_SERIES | ||||||||||||||||||||||||||
: "tsid should only be set for time series indices"; | ||||||||||||||||||||||||||
if (mappingLookup.getMapping().getRoot().subobjects() == ObjectMapper.Subobjects.ENABLED) { | ||||||||||||||||||||||||||
this.parser = DotExpandingXContentParser.expandDots(parser, this.path); | ||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.