-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Track bytes used by in-memory postings #129969
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
Open
jordan-powers
wants to merge
10
commits into
elastic:main
Choose a base branch
from
jordan-powers:track-field-term-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0d8e02f
Add TrackingPostingsInMemoryBytesCodec
jordan-powers 3292e00
Add totalPostingBytes to ShardFieldStats
jordan-powers 8189993
Add postingsInMemoryBytes to testShardFieldStats
jordan-powers 625639c
Merge remote-tracking branch 'upstream/main' into track-field-term-le…
jordan-powers 69ce68d
Update min numDocs to 2 in testShardFieldStats
jordan-powers 66454c4
Gate postings memory tracking behind feature flag
jordan-powers 2233230
Add javadoc to TrackingPostingsInMemoryBytesCodec
jordan-powers 67e7578
Track bytes of min and max term instead of longest term
jordan-powers e5b4fa1
Update testShardFieldStats for new calculation
jordan-powers fc2e364
Merge remote-tracking branch 'upstream/main' into track-field-term-le…
jordan-powers 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
160 changes: 160 additions & 0 deletions
160
server/src/main/java/org/elasticsearch/index/codec/TrackingPostingsInMemoryBytesCodec.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,160 @@ | ||
/* | ||
* 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.index.codec; | ||
|
||
import org.apache.lucene.codecs.Codec; | ||
import org.apache.lucene.codecs.FieldsConsumer; | ||
import org.apache.lucene.codecs.FieldsProducer; | ||
import org.apache.lucene.codecs.FilterCodec; | ||
import org.apache.lucene.codecs.NormsProducer; | ||
import org.apache.lucene.codecs.PostingsFormat; | ||
import org.apache.lucene.index.FieldInfos; | ||
import org.apache.lucene.index.Fields; | ||
import org.apache.lucene.index.FilterLeafReader; | ||
import org.apache.lucene.index.SegmentReadState; | ||
import org.apache.lucene.index.SegmentWriteState; | ||
import org.apache.lucene.index.Terms; | ||
import org.apache.lucene.index.TermsEnum; | ||
import org.apache.lucene.internal.hppc.IntIntHashMap; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.util.FeatureFlag; | ||
|
||
import java.io.IOException; | ||
import java.util.function.IntConsumer; | ||
|
||
/** | ||
* A codec that tracks the length of the min and max written terms. Used to improve memory usage estimates in serverless, since | ||
* {@link org.apache.lucene.codecs.lucene90.blocktree.FieldReader} keeps an in-memory reference to the min and max term. | ||
*/ | ||
public class TrackingPostingsInMemoryBytesCodec extends FilterCodec { | ||
public static final FeatureFlag TRACK_POSTINGS_IN_MEMORY_BYTES = new FeatureFlag("track_postings_in_memory_bytes"); | ||
|
||
public static final String IN_MEMORY_POSTINGS_BYTES_KEY = "es.postings.in_memory_bytes"; | ||
|
||
public TrackingPostingsInMemoryBytesCodec(Codec delegate) { | ||
super(delegate.getName(), delegate); | ||
} | ||
|
||
@Override | ||
public PostingsFormat postingsFormat() { | ||
PostingsFormat format = super.postingsFormat(); | ||
|
||
return new PostingsFormat(format.getName()) { | ||
@Override | ||
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException { | ||
FieldsConsumer consumer = format.fieldsConsumer(state); | ||
return new TrackingLengthFieldsConsumer(state, consumer); | ||
} | ||
|
||
@Override | ||
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException { | ||
return format.fieldsProducer(state); | ||
} | ||
}; | ||
} | ||
|
||
static final class TrackingLengthFieldsConsumer extends FieldsConsumer { | ||
final SegmentWriteState state; | ||
final FieldsConsumer in; | ||
final IntIntHashMap termsBytesPerField; | ||
|
||
TrackingLengthFieldsConsumer(SegmentWriteState state, FieldsConsumer in) { | ||
this.state = state; | ||
this.in = in; | ||
this.termsBytesPerField = new IntIntHashMap(state.fieldInfos.size()); | ||
} | ||
|
||
@Override | ||
public void write(Fields fields, NormsProducer norms) throws IOException { | ||
in.write(new TrackingLengthFields(fields, termsBytesPerField, state.fieldInfos), norms); | ||
long totalBytes = 0; | ||
for (int bytes : termsBytesPerField.values) { | ||
totalBytes += bytes; | ||
} | ||
state.segmentInfo.putAttribute(IN_MEMORY_POSTINGS_BYTES_KEY, Long.toString(totalBytes)); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
in.close(); | ||
} | ||
} | ||
|
||
static final class TrackingLengthFields extends FilterLeafReader.FilterFields { | ||
final IntIntHashMap termsBytesPerField; | ||
final FieldInfos fieldInfos; | ||
|
||
TrackingLengthFields(Fields in, IntIntHashMap termsBytesPerField, FieldInfos fieldInfos) { | ||
super(in); | ||
this.termsBytesPerField = termsBytesPerField; | ||
this.fieldInfos = fieldInfos; | ||
} | ||
|
||
@Override | ||
public Terms terms(String field) throws IOException { | ||
Terms terms = super.terms(field); | ||
if (terms == null) { | ||
return null; | ||
} | ||
int fieldNum = fieldInfos.fieldInfo(field).number; | ||
return new TrackingLengthTerms( | ||
terms, | ||
bytes -> termsBytesPerField.put(fieldNum, Math.max(termsBytesPerField.getOrDefault(fieldNum, 0), bytes)) | ||
); | ||
} | ||
} | ||
|
||
static final class TrackingLengthTerms extends FilterLeafReader.FilterTerms { | ||
final IntConsumer onFinish; | ||
|
||
TrackingLengthTerms(Terms in, IntConsumer onFinish) { | ||
super(in); | ||
this.onFinish = onFinish; | ||
} | ||
|
||
@Override | ||
public TermsEnum iterator() throws IOException { | ||
return new TrackingLengthTermsEnum(super.iterator(), onFinish); | ||
} | ||
} | ||
|
||
static final class TrackingLengthTermsEnum extends FilterLeafReader.FilterTermsEnum { | ||
int maxTermLength = 0; | ||
int minTermLength = 0; | ||
int termCount = 0; | ||
final IntConsumer onFinish; | ||
|
||
TrackingLengthTermsEnum(TermsEnum in, IntConsumer onFinish) { | ||
super(in); | ||
this.onFinish = onFinish; | ||
} | ||
|
||
@Override | ||
public BytesRef next() throws IOException { | ||
final BytesRef term = super.next(); | ||
if (term != null) { | ||
if (termCount == 0) { | ||
minTermLength = term.length; | ||
} | ||
maxTermLength = term.length; | ||
termCount++; | ||
} else { | ||
if (termCount == 1) { | ||
// If the minTerm and maxTerm are the same, only one instance is kept on the heap. | ||
assert minTermLength == maxTermLength; | ||
onFinish.accept(maxTermLength); | ||
} else { | ||
onFinish.accept(maxTermLength + minTermLength); | ||
} | ||
} | ||
return term; | ||
} | ||
} | ||
} |
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
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.
Maybe add class level javadocs explain the purpose of this class?