-
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
base: main
Are you sure you want to change the base?
Changes from all commits
0d8e02f
3292e00
8189993
625639c
69ce68d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,139 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* 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 java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.function.IntConsumer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
public class TrackingPostingsInMemoryBytesCodec extends FilterCodec { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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 maxLengths; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
TrackingLengthFieldsConsumer(SegmentWriteState state, FieldsConsumer in) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
this.state = state; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
this.in = in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
this.maxLengths = new IntIntHashMap(state.fieldInfos.size()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public void write(Fields fields, NormsProducer norms) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
in.write(new TrackingLengthFields(fields, maxLengths, state.fieldInfos), norms); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
long totalLength = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int len : maxLengths.values) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
totalLength += len; // minTerm | ||||||||||||||||||||||||||||||||||||||||||||||||||||
totalLength += len; // maxTerm | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
state.segmentInfo.putAttribute(IN_MEMORY_POSTINGS_BYTES_KEY, Long.toString(totalLength)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public void close() throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
in.close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
static final class TrackingLengthFields extends FilterLeafReader.FilterFields { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
final IntIntHashMap maxLengths; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
final FieldInfos fieldInfos; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
TrackingLengthFields(Fields in, IntIntHashMap maxLengths, FieldInfos fieldInfos) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
super(in); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
this.maxLengths = maxLengths; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
this.fieldInfos = fieldInfos; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
public Terms terms(String field) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Terms terms = super.terms(field); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if (terms == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return terms; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
int fieldNum = fieldInfos.fieldInfo(field).number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return new TrackingLengthTerms(terms, len -> maxLengths.put(fieldNum, Math.max(maxLengths.getOrDefault(fieldNum, 0), len))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+96
to
+101
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. I wonder whether we can do this instead:
Suggested change
This way there is way less wrapping. We only care about min and max term, given that this is loaded in jvm heap. 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. Scratch that idea. The implementation provided here different. This gets invoked during indexing / merging. During indexing this implementation of |
||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
maxTermLength = Math.max(maxTermLength, term.length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
onFinish.accept(maxTermLength); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return term; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+129
to
+137
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. Given that we need to estimate the terms that get loaded in jvm heap would the following be more accurate?
Suggested change
In the |
||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ | |
import org.elasticsearch.index.IndexVersions; | ||
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.index.cache.query.TrivialQueryCachingPolicy; | ||
import org.elasticsearch.index.codec.TrackingPostingsInMemoryBytesCodec; | ||
import org.elasticsearch.index.mapper.DocumentParser; | ||
import org.elasticsearch.index.mapper.IdFieldMapper; | ||
import org.elasticsearch.index.mapper.LuceneDocument; | ||
|
@@ -2778,7 +2779,7 @@ private IndexWriterConfig getIndexWriterConfig() { | |
iwc.setMaxFullFlushMergeWaitMillis(-1); | ||
iwc.setSimilarity(engineConfig.getSimilarity()); | ||
iwc.setRAMBufferSizeMB(engineConfig.getIndexingBufferSize().getMbFrac()); | ||
iwc.setCodec(engineConfig.getCodec()); | ||
iwc.setCodec(new TrackingPostingsInMemoryBytesCodec(engineConfig.getCodec())); | ||
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. I wonder what the overhead is of always wrapping the codec in Additionally I wonder whether this should only be done for stateless only. |
||
boolean useCompoundFile = engineConfig.getUseCompoundFile(); | ||
iwc.setUseCompoundFile(useCompoundFile); | ||
if (useCompoundFile == false) { | ||
|
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?