-
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
Merged
felixbarny
merged 14 commits into
elastic:main
from
felixbarny:refactor-extract-from-source
Sep 25, 2025
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
205466f
Refactor IndexRouting.ExtractFromSource to be an abstract class
felixbarny aa15c57
Merge branch 'main' into refactor-extract-from-source
felixbarny d22c530
Merge remote-tracking branch 'origin/main' into refactor-extract-from…
felixbarny 45b206a
Streamline constructors
felixbarny cdfe72a
Improve IndexMode.buildRoutingFields
felixbarny 9551184
Create tsid during translog replay
felixbarny 9c680f0
Create tsid in RootDocumentParserContext if missing
felixbarny 591709d
Add test cases where tsid is created in DocumentParser
felixbarny 18fadf3
Merge remote-tracking branch 'origin/main' into refactor-extract-from…
felixbarny 8b28540
Address review comments
felixbarny cf8ab97
Merge remote-tracking branch 'origin/main' into refactor-extract-from…
felixbarny 58f8621
Address review comments
felixbarny 2b5d93b
Merge remote-tracking branch 'origin/main' into refactor-extract-from…
felixbarny 2117dbe
Merge branch 'main' into refactor-extract-from-source
felixbarny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
322 changes: 114 additions & 208 deletions
322
server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java
Large diffs are not rendered by default.
Oops, something went wrong.
135 changes: 135 additions & 0 deletions
135
server/src/main/java/org/elasticsearch/cluster/routing/RoutingHashBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.