Skip to content

Commit

Permalink
Enable BloomFilter for _id of non-datastream indices
Browse files Browse the repository at this point in the history
  • Loading branch information
dnhatn committed Jul 12, 2022
1 parent cbb0800 commit bc8c0d3
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.IndexSettings;
Expand Down Expand Up @@ -62,7 +63,7 @@ EngineConfig engineConfigWithLargerIndexingMemory(EngineConfig config) {
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
new CodecService(null, BigArrays.NON_RECYCLING_INSTANCE),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.lucene93.Lucene93Codec;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.mapper.MapperService;

Expand All @@ -31,14 +32,14 @@ public class CodecService {
/** the raw unfiltered lucene default. useful for testing */
public static final String LUCENE_DEFAULT_CODEC = "lucene_default";

public CodecService(@Nullable MapperService mapperService) {
public CodecService(@Nullable MapperService mapperService, BigArrays bigArrays) {
final var codecs = new HashMap<String, Codec>();
if (mapperService == null) {
codecs.put(DEFAULT_CODEC, new Lucene93Codec());
codecs.put(BEST_COMPRESSION_CODEC, new Lucene93Codec(Lucene93Codec.Mode.BEST_COMPRESSION));
} else {
codecs.put(DEFAULT_CODEC, new PerFieldMapperCodec(Lucene93Codec.Mode.BEST_SPEED, mapperService));
codecs.put(BEST_COMPRESSION_CODEC, new PerFieldMapperCodec(Lucene93Codec.Mode.BEST_COMPRESSION, mapperService));
codecs.put(DEFAULT_CODEC, new PerFieldMapperCodec(Lucene93Codec.Mode.BEST_SPEED, mapperService, bigArrays));
codecs.put(BEST_COMPRESSION_CODEC, new PerFieldMapperCodec(Lucene93Codec.Mode.BEST_COMPRESSION, mapperService, bigArrays));
}
codecs.put(LUCENE_DEFAULT_CODEC, Codec.getDefault());
for (String codec : Codec.availableCodecs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.apache.lucene.codecs.lucene90.Lucene90DocValuesFormat;
import org.apache.lucene.codecs.lucene93.Lucene93Codec;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.index.codec.bloomfilter.BloomFilterPostingsFormat;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
Expand All @@ -29,6 +32,7 @@
*/
public class PerFieldMapperCodec extends Lucene93Codec {
private final MapperService mapperService;
private final BigArrays bigArrays;

private final DocValuesFormat docValuesFormat = new Lucene90DocValuesFormat();

Expand All @@ -37,18 +41,22 @@ public class PerFieldMapperCodec extends Lucene93Codec {
: "PerFieldMapperCodec must subclass the latest " + "lucene codec: " + Lucene.LATEST_CODEC;
}

public PerFieldMapperCodec(Mode compressionMode, MapperService mapperService) {
public PerFieldMapperCodec(Mode compressionMode, MapperService mapperService, BigArrays bigArrays) {
super(compressionMode);
this.mapperService = mapperService;
this.bigArrays = bigArrays;
}

@Override
public PostingsFormat getPostingsFormatForField(String field) {
PostingsFormat format = mapperService.mappingLookup().getPostingsFormat(field);
if (format == null) {
return super.getPostingsFormatForField(field);
if (IdFieldMapper.NAME.equals(field) && mapperService.mappingLookup().isDataStreamTimestampFieldEnabled() == false) {
return new BloomFilterPostingsFormat(super.getPostingsFormatForField(field), bigArrays);
}
return format;
final PostingsFormat format = mapperService.mappingLookup().getPostingsFormat(field);
if (format != null) {
return format;
}
return super.getPostingsFormatForField(field);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public IndexShard(
assert shardRouting.initializing();
this.shardRouting = shardRouting;
final Settings settings = indexSettings.getSettings();
this.codecService = new CodecService(mapperService);
this.codecService = new CodecService(mapperService, bigArrays);
this.warmer = warmer;
this.similarityService = similarityService;
Objects.requireNonNull(store, "Store must be provided to the index shard");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.util.LuceneTestCase.SuppressCodecs;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.IndexAnalyzers;
Expand Down Expand Up @@ -93,7 +94,7 @@ private CodecService createCodecService() throws IOException {
settings.getMode().buildNoFieldDataIdFieldMapper(),
ScriptCompiler.NONE
);
return new CodecService(service);
return new CodecService(service, BigArrays.NON_RECYCLING_INSTANCE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ public void testFailStart() throws IOException {
}

public void testSettings() {
CodecService codecService = new CodecService(null);
CodecService codecService = newCodecService();
LiveIndexWriterConfig currentIndexWriterConfig = engine.getCurrentIndexWriterConfig();

assertEquals(engine.config().getCodec().getName(), codecService.codec(codecName).getName());
Expand Down Expand Up @@ -3560,7 +3560,7 @@ public void testRecoverFromForeignTranslog() throws IOException {
newMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
newCodecService(),
config.getEventListener(),
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
Expand Down Expand Up @@ -7228,7 +7228,7 @@ public void testNotWarmUpSearcherInEngineCtor() throws Exception {
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
newCodecService(),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AnalyzerScope;
Expand Down Expand Up @@ -136,7 +137,7 @@ protected IndexAnalyzers createIndexAnalyzers(IndexSettings indexSettings) {

public void testPostingsFormat() throws IOException {
MapperService mapperService = createMapperService(fieldMapping(this::minimalMapping));
CodecService codecService = new CodecService(mapperService);
CodecService codecService = new CodecService(mapperService, BigArrays.NON_RECYCLING_INSTANCE);
Codec codec = codecService.codec("default");
assertThat(codec, instanceOf(PerFieldMapperCodec.class));
PerFieldMapperCodec perFieldCodec = (PerFieldMapperCodec) codec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.codec.PerFieldMapperCodec;
import org.elasticsearch.index.mapper.DocumentMapper;
Expand Down Expand Up @@ -450,7 +451,7 @@ public void testKnnVectorsFormat() throws IOException {
b.field("ef_construction", efConstruction);
b.endObject();
}));
CodecService codecService = new CodecService(mapperService);
CodecService codecService = new CodecService(mapperService, BigArrays.NON_RECYCLING_INSTANCE);
Codec codec = codecService.codec("default");
assertThat(codec, instanceOf(PerFieldMapperCodec.class));
KnnVectorsFormat knnVectorsFormat = ((PerFieldMapperCodec) codec).getKnnVectorsFormatForField("field");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.AtomicArray;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
Expand Down Expand Up @@ -4403,7 +4404,7 @@ public void testCloseShardWhileEngineIsWarming() throws Exception {
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
new CodecService(null, BigArrays.NON_RECYCLING_INSTANCE),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void onFailedEngine(String reason, @Nullable Exception e) {
newMergePolicy(),
iwc.getAnalyzer(),
iwc.getSimilarity(),
new CodecService(null),
new CodecService(null, BigArrays.NON_RECYCLING_INSTANCE),
eventListener,
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.engine.EngineConfig;
import org.elasticsearch.index.engine.InternalEngine;
Expand Down Expand Up @@ -385,7 +386,7 @@ EngineConfig configWithRefreshListener(EngineConfig config, ReferenceManager.Ref
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
new CodecService(null, BigArrays.NON_RECYCLING_INSTANCE),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ protected Settings indexSettings() {
public void setUp() throws Exception {
super.setUp();
primaryTerm.set(randomLongBetween(1, Long.MAX_VALUE));
CodecService codecService = new CodecService(null);
CodecService codecService = newCodecService();
String name = Codec.getDefault().getName();
if (Arrays.asList(codecService.availableCodecs()).contains(name)) {
// some codecs are read only so we only take the ones that we have in the service and randomly
Expand Down Expand Up @@ -259,7 +259,7 @@ public EngineConfig copy(EngineConfig config, LongSupplier globalCheckpointSuppl
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
newCodecService(),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down Expand Up @@ -287,7 +287,7 @@ public EngineConfig copy(EngineConfig config, Analyzer analyzer) {
config.getMergePolicy(),
analyzer,
config.getSimilarity(),
new CodecService(null),
newCodecService(),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down Expand Up @@ -315,7 +315,7 @@ public EngineConfig copy(EngineConfig config, MergePolicy mergePolicy) {
mergePolicy,
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
newCodecService(),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down Expand Up @@ -833,7 +833,7 @@ public EngineConfig config(
mergePolicy,
iwc.getAnalyzer(),
iwc.getSimilarity(),
new CodecService(null),
newCodecService(),
eventListener,
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
Expand Down Expand Up @@ -869,7 +869,7 @@ protected EngineConfig config(EngineConfig config, Store store, Path translogPat
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null),
newCodecService(),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
Expand Down Expand Up @@ -1614,4 +1614,8 @@ private static LazySoftDeletesDirectoryReaderWrapper.LazyBits lazyBits(LeafReade
// hard fail - we can't get the lazybits
throw new IllegalStateException("Can not extract lazy bits from given index reader [" + reader + "]");
}

static CodecService newCodecService() {
return new CodecService(null, BigArrays.NON_RECYCLING_INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private EngineConfig engineConfig(
newMergePolicy(),
indexWriterConfig.getAnalyzer(),
indexWriterConfig.getSimilarity(),
new CodecService(null),
new CodecService(null, BigArrays.NON_RECYCLING_INSTANCE),
new Engine.EventListener() {
@Override
public void onFailedEngine(String reason, Exception e) {
Expand Down

0 comments on commit bc8c0d3

Please sign in to comment.