Skip to content

Commit

Permalink
implementing codec aliases
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
  • Loading branch information
sarthakaggarwal97 committed Sep 2, 2023
1 parent f42a285 commit a87c166
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ public abstract class Lucene95CustomCodec extends FilterCodec {

/** Each mode represents a compression algorithm. */
public enum Mode {
/** ZStandard mode with dictionary*/
ZSTD,
/** ZStandard mode without dictionary*/
ZSTD_NO_DICT
/**
* ZStandard mode with dictionary
*/
ZSTD("zstd"),
/**
* ZStandard mode without dictionary
*/
ZSTD_NO_DICT("zstdnodict");

private final String codec;

Mode(String codec) {
this.codec = codec;
}

/**
* Returns the Codec that is registered with Lucene
*/
public String getCodec() {
return codec;
}
}

private final StoredFieldsFormat storedFieldsFormat;
Expand All @@ -55,7 +72,7 @@ public Lucene95CustomCodec(Mode mode) {
* @param compressionLevel The compression level.
*/
public Lucene95CustomCodec(Mode mode, int compressionLevel) {
super("Lucene95CustomCodec", new Lucene95Codec());
super(mode.getCodec(), new Lucene95Codec());
this.storedFieldsFormat = new Lucene95CustomStoredFieldsFormat(mode, compressionLevel);
}

Expand All @@ -70,7 +87,7 @@ public Lucene95CustomCodec(Mode mode, int compressionLevel) {
* @param logger The logger.
*/
public Lucene95CustomCodec(Mode mode, int compressionLevel, MapperService mapperService, Logger logger) {
super("Lucene95CustomCodec", new PerFieldMappingPostingFormatCodec(Lucene95Codec.Mode.BEST_SPEED, mapperService, logger));
super(mode.getCodec(), new PerFieldMappingPostingFormatCodec(Lucene95Codec.Mode.BEST_SPEED, mapperService, logger));
this.storedFieldsFormat = new Lucene95CustomStoredFieldsFormat(mode, compressionLevel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

import org.apache.logging.log4j.Logger;
import org.opensearch.common.settings.Setting;
import org.opensearch.index.codec.CodecAliases;
import org.opensearch.index.codec.CodecSettings;
import org.opensearch.index.engine.EngineConfig;
import org.opensearch.index.mapper.MapperService;

import java.util.Set;

/**
* ZstdCodec provides ZSTD compressor using the <a href="https://github.com/luben/zstd-jni">zstd-jni</a> library.
*/
public class ZstdCodec extends Lucene95CustomCodec implements CodecSettings {
public class ZstdCodec extends Lucene95CustomCodec implements CodecSettings, CodecAliases {

/**
* Creates a new ZstdCodec instance with the default compression level.
Expand Down Expand Up @@ -56,4 +59,9 @@ public String toString() {
public boolean supports(Setting<?> setting) {
return setting.equals(EngineConfig.INDEX_CODEC_COMPRESSION_LEVEL_SETTING);
}

@Override
public Set<String> aliases() {
return Set.of("zstd");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

import org.apache.logging.log4j.Logger;
import org.opensearch.common.settings.Setting;
import org.opensearch.index.codec.CodecAliases;
import org.opensearch.index.codec.CodecSettings;
import org.opensearch.index.engine.EngineConfig;
import org.opensearch.index.mapper.MapperService;

import java.util.Set;

/**
* ZstdNoDictCodec provides ZSTD compressor without a dictionary support.
*/
public class ZstdNoDictCodec extends Lucene95CustomCodec implements CodecSettings {
public class ZstdNoDictCodec extends Lucene95CustomCodec implements CodecSettings, CodecAliases {

/**
* Creates a new ZstdNoDictCodec instance with the default compression level.
Expand Down Expand Up @@ -56,4 +59,9 @@ public String toString() {
public boolean supports(Setting<?> setting) {
return setting.equals(EngineConfig.INDEX_CODEC_COMPRESSION_LEVEL_SETTING);
}

@Override
public Set<String> aliases() {
return Set.of("zstd_no_dict");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

import static org.opensearch.index.codec.customcodecs.CustomCodecService.ZSTD_CODEC;
Expand Down Expand Up @@ -118,7 +119,7 @@ public void testLuceneCodecsWithCompressionLevel() {

final Settings customCodecSettings = Settings.builder()
.put(INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), "Lucene95CustomCodec")
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), randomFrom("zstd", "zstd_no_dict"))
.build();

final IndexScopedSettings customCodecIndexScopedSettings = new IndexScopedSettings(
Expand Down
22 changes: 22 additions & 0 deletions server/src/main/java/org/opensearch/index/codec/CodecAliases.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.codec;

import org.apache.lucene.codecs.Codec;

import java.util.Set;

/**
* This {@link CodecAliases} allows us to manage the settings with {@link Codec}.
*
* @opensearch.internal
*/
public interface CodecAliases {
Set<String> aliases();
}
29 changes: 18 additions & 11 deletions server/src/main/java/org/opensearch/index/engine/EngineConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.indices.breaker.CircuitBreakerService;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.codec.CodecAliases;
import org.opensearch.index.codec.CodecService;
import org.opensearch.index.codec.CodecSettings;
import org.opensearch.index.mapper.ParsedDocument;
Expand Down Expand Up @@ -136,12 +137,14 @@ public Supplier<RetentionLeases> retentionLeasesSupplier() {
case "lucene_default":
return s;
default:
// Though the external visible codec name is zstd or zstd_no_dict, internally it is registered as Lucene95CustomCodec
// Hence this check is required, Lucene95CustomCodec will not be part of availableCodecs if the custom-codecs plugin
// is not installed
if (("zstd".equals(s) || "zstd_no_dict".equals(s)) && Codec.availableCodecs().contains("Lucene95CustomCodec")) {
return s;

for (String codecName : Codec.availableCodecs()) {
Codec codec = Codec.forName(codecName);
if (codec instanceof CodecAliases && ((CodecAliases) codec).aliases().contains(s)) {
return s;
}
}

if (Codec.availableCodecs().contains(s) == false) { // we don't error message the not officially supported ones
throw new IllegalArgumentException(
"unknown value for [index.codec] must be one of [default, lz4, best_compression, zlib] but was: " + s
Expand Down Expand Up @@ -191,19 +194,23 @@ private static void doValidateCodecSettings(final String codec) {
case "lz4":
break;
default:
// Though the external visible codec name is zstd or zstd_no_dict, internally it is registered as Lucene95CustomCodec
// Hence this check is required, Lucene95CustomCodec will not be part of availableCodecs if the custom-codecs plugin
// is not installed
if (("zstd".equals(codec) || "zstd_no_dict".equals(codec)) && Codec.availableCodecs().contains("Lucene95CustomCodec")) {
return;
}
if (Codec.availableCodecs().contains(codec)) {
Codec luceneCodec = Codec.forName(codec);
if (luceneCodec instanceof CodecSettings
&& ((CodecSettings) luceneCodec).supports(INDEX_CODEC_COMPRESSION_LEVEL_SETTING)) {
return;
}
}

for (String codecName : Codec.availableCodecs()) {
Codec availableCodec = Codec.forName(codecName);
if (availableCodec instanceof CodecAliases && ((CodecAliases) availableCodec).aliases().contains(codecName)) {
if (availableCodec instanceof CodecSettings
&& ((CodecSettings) availableCodec).supports(INDEX_CODEC_COMPRESSION_LEVEL_SETTING)) {
return;
}
}
}
}
throw new IllegalArgumentException("Compression level cannot be set for the " + codec + " codec.");
}
Expand Down

0 comments on commit a87c166

Please sign in to comment.