-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Memory efficient xcontent filtering #77154
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d92b0ae
Filter source on read
nik9000 e416463
Rename
nik9000 277e1e6
Merge branch 'master' into faster_source_filter
nik9000 118ee83
Merge branch 'master' into faster_source_filter
nik9000 eda57ea
Test
nik9000 35bbf97
test
nik9000 89de703
Merge branch 'master' into faster_source_filter
nik9000 12a080f
Merge branch 'master' into faster_source_filter
nik9000 09b1899
Fail early is double star excludes
nik9000 aaf3a3e
Merge branch 'master' into faster_source_filter
nik9000 0fc13ed
Merge branch 'master' into faster_source_filter
nik9000 7d399f2
Merge branch 'master' into faster_source_filter
nik9000 9c7341c
Merge branch 'master' into faster_source_filter
nik9000 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
138 changes: 138 additions & 0 deletions
138
...ain/java/org/elasticsearch/benchmark/search/fetch/subphase/FetchSourcePhaseBenchmark.java
This file contains 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,138 @@ | ||
package org.elasticsearch.benchmark.search.fetch.subphase; | ||
|
||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.io.Streams; | ||
import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
import org.elasticsearch.common.xcontent.DeprecationHandler; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.common.xcontent.support.filtering.FilterPath; | ||
import org.elasticsearch.search.fetch.subphase.FetchSourceContext; | ||
import org.elasticsearch.search.fetch.subphase.FetchSourcePhase; | ||
import org.elasticsearch.search.lookup.SourceLookup; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Fork(1) | ||
@Warmup(iterations = 5) | ||
@Measurement(iterations = 5) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@State(Scope.Benchmark) | ||
public class FetchSourcePhaseBenchmark { | ||
private BytesReference sourceBytes; | ||
private FetchSourceContext fetchContext; | ||
private Set<String> includesSet; | ||
private Set<String> excludesSet; | ||
private FilterPath[] includesFilters; | ||
private FilterPath[] excludesFilters; | ||
|
||
@Param({ "tiny", "short", "one_4k_field", "one_4m_field" }) | ||
private String source; | ||
@Param({ "message" }) | ||
private String includes; | ||
@Param({ "" }) | ||
private String excludes; | ||
|
||
@Setup | ||
public void setup() throws IOException { | ||
switch (source) { | ||
case "tiny": | ||
sourceBytes = new BytesArray("{\"message\": \"short\"}"); | ||
break; | ||
case "short": | ||
sourceBytes = read300BytesExample(); | ||
break; | ||
case "one_4k_field": | ||
sourceBytes = buildBigExample("huge".repeat(1024)); | ||
break; | ||
case "one_4m_field": | ||
sourceBytes = buildBigExample("huge".repeat(1024 * 1024)); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unknown source [" + source + "]"); | ||
} | ||
fetchContext = new FetchSourceContext( | ||
true, | ||
Strings.splitStringByCommaToArray(includes), | ||
Strings.splitStringByCommaToArray(excludes) | ||
); | ||
includesSet = Set.of(fetchContext.includes()); | ||
excludesSet = Set.of(fetchContext.excludes()); | ||
includesFilters = FilterPath.compile(Set.of(fetchContext.includes())); | ||
excludesFilters = FilterPath.compile(Set.of(fetchContext.excludes())); | ||
} | ||
|
||
private BytesReference read300BytesExample() throws IOException { | ||
return Streams.readFully(FetchSourcePhaseBenchmark.class.getResourceAsStream("300b_example.json")); | ||
} | ||
|
||
private BytesReference buildBigExample(String extraText) throws IOException { | ||
String bigger = read300BytesExample().utf8ToString(); | ||
bigger = "{\"huge\": \"" + extraText + "\"," + bigger.substring(1); | ||
return new BytesArray(bigger); | ||
} | ||
|
||
@Benchmark | ||
public BytesReference filterObjects() throws IOException { | ||
SourceLookup lookup = new SourceLookup(); | ||
lookup.setSource(sourceBytes); | ||
Object value = lookup.filter(fetchContext); | ||
return FetchSourcePhase.objectToBytes(value, XContentType.JSON, Math.min(1024, lookup.internalSourceRef().length())); | ||
} | ||
|
||
@Benchmark | ||
public BytesReference filterXContentOnParser() throws IOException { | ||
BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, sourceBytes.length())); | ||
XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), streamOutput); | ||
try ( | ||
XContentParser parser = XContentType.JSON.xContent() | ||
.createParser( | ||
NamedXContentRegistry.EMPTY, | ||
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, | ||
sourceBytes.streamInput(), | ||
includesFilters, | ||
excludesFilters | ||
) | ||
) { | ||
builder.copyCurrentStructure(parser); | ||
return BytesReference.bytes(builder); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public BytesReference filterXContentOnBuilder() throws IOException { | ||
BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, sourceBytes.length())); | ||
XContentBuilder builder = new XContentBuilder( | ||
XContentType.JSON.xContent(), | ||
streamOutput, | ||
includesSet, | ||
excludesSet, | ||
XContentType.JSON.toParsedMediaType() | ||
); | ||
try ( | ||
XContentParser parser = XContentType.JSON.xContent() | ||
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, sourceBytes.streamInput()) | ||
) { | ||
builder.copyCurrentStructure(parser); | ||
return BytesReference.bytes(builder); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ks/src/main/resources/org/elasticsearch/benchmark/search/fetch/subphase/300b_example.json
This file contains 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,20 @@ | ||
{ | ||
"@timestamp": "2099-11-15T14:12:12", | ||
"http": { | ||
"request": { | ||
"method": "get" | ||
}, | ||
"response": { | ||
"bytes": 1070000, | ||
"status_code": 200 | ||
}, | ||
"version": "1.1" | ||
}, | ||
"message": "GET /search HTTP/1.1 200 1070000", | ||
"source": { | ||
"ip": "192.168.0.1" | ||
}, | ||
"user": { | ||
"id": "user" | ||
} | ||
} |
This file contains 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 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 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 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 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 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.
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.
This should probably fail in
hasDoubleWildcard
. Unless I can figure out a way to make it work!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.
I added this test.