-
Notifications
You must be signed in to change notification settings - Fork 25.6k
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
Changes from 5 commits
0d8e02f
3292e00
8189993
625639c
69ce68d
66454c4
2233230
67e7578
e5b4fa1
fc2e364
33c29a7
ff9bb35
68f4023
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))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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))); | |
| Terms terms = super.terms(field); | |
| // Only org.apache.lucene.codecs.lucene90.blocktree.FieldReader keeps min and max term in jvm heap, | |
| // so only account for these cases: | |
| if (terms instanceof FieldReader fieldReader) { | |
| int fieldNum = fieldInfos.fieldInfo(field).number; | |
| int length = fieldReader.getMin().length; | |
| length += fieldReader.getMax().length; | |
| maxLengths.put(fieldNum, length); | |
| } | |
| return terms; |
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 comment
The 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 terms is FreqProxTermsWriterPerField. Invoking getMax() is potentially expensive as it causes reading ahead to figure out which is the max term, these terms get later read via terms enum.
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.
Given that we need to estimate the terms that get loaded in jvm heap would the following be more accurate?
| 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; | |
| } | |
| int prevTermLength = 0; | |
| @Override | |
| public BytesRef next() throws IOException { | |
| final BytesRef term = super.next(); | |
| if (term == null) { | |
| maxTermLength += prevTermLength; | |
| onFinish.accept(maxTermLength); | |
| return term; | |
| } | |
| if (maxTermLength == 0) { | |
| maxTermLength = term.length; | |
| } | |
| prevTermLength = term.length; | |
| return term; | |
| } |
In the org.apache.lucene.codecs.lucene90.blocktree.FieldReader class, the lowest and highest lexicographically term is kept around in jvm heap. The current code just keeps track what the longest term is and report that, which doesn't map with the minTerm and maxTerm in FieldReader?
| 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())); | ||
|
||
| boolean useCompoundFile = engineConfig.getUseCompoundFile(); | ||
| iwc.setUseCompoundFile(useCompoundFile); | ||
| if (useCompoundFile == false) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.