Skip to content

Commit

Permalink
Lower default mappings parsing compatibility to MINIMUM_READONLY_COMP…
Browse files Browse the repository at this point in the history
…ATIBLE (#119017)

Legacy indices check what minimum compatibility version the type parser supports,
for every field type found in the mappings. Only some of the basic fields are
supported, otherwise a placeholder mapper is created in place of the real field.

The minimum supported version is v5 for the supported field mappers. For all the
others, we can lower that from MINIMUM_COMPATIBLE to MINIMUM_READONLY_COMPATIBLE.

This commit also centralizes the creation of type parsers that declare support
for archive indices, as they all declare the same version.
  • Loading branch information
javanna authored Dec 19, 2024
1 parent 21bcc31 commit 49ebd42
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ private FieldValues<Boolean> scriptValues() {
}
}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static final TypeParser PARSER = new TypeParser(
(n, c) -> new Builder(n, c.scriptCompiler(), IGNORE_MALFORMED_SETTING.get(c.getSettings()), c.indexVersionCreated()),
MINIMUM_COMPATIBILITY_VERSION
public static final TypeParser PARSER = createTypeParserWithLegacySupport(
(n, c) -> new Builder(n, c.scriptCompiler(), IGNORE_MALFORMED_SETTING.get(c.getSettings()), c.indexVersionCreated())
);

public static final class BooleanFieldType extends TermBasedFieldType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,7 @@ public DateFieldMapper build(MapperBuilderContext context) {
}
}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static final TypeParser MILLIS_PARSER = new TypeParser((n, c) -> {
public static final TypeParser MILLIS_PARSER = createTypeParserWithLegacySupport((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(
n,
Expand All @@ -410,9 +408,9 @@ public DateFieldMapper build(MapperBuilderContext context) {
ignoreMalformedByDefault,
c.indexVersionCreated()
);
}, MINIMUM_COMPATIBILITY_VERSION);
});

public static final TypeParser NANOS_PARSER = new TypeParser((n, c) -> {
public static final TypeParser NANOS_PARSER = createTypeParserWithLegacySupport((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(
n,
Expand All @@ -422,7 +420,7 @@ public DateFieldMapper build(MapperBuilderContext context) {
ignoreMalformedByDefault,
c.indexVersionCreated()
);
}, MINIMUM_COMPATIBILITY_VERSION);
});

public static final class DateFieldType extends MappedFieldType {
final DateFormatter dateTimeFormatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,12 @@ public static BiConsumer<String, MappingParserContext> notFromDynamicTemplates(S
};
}

private static final IndexVersion MINIMUM_LEGACY_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static TypeParser createTypeParserWithLegacySupport(BiFunction<String, MappingParserContext, Builder> builderFunction) {
return new TypeParser(builderFunction, MINIMUM_LEGACY_COMPATIBILITY_VERSION);
}

/**
* TypeParser implementation that automatically handles parsing
*/
Expand All @@ -1632,29 +1638,29 @@ public static final class TypeParser implements Mapper.TypeParser {
* @param builderFunction a function that produces a Builder from a name and parsercontext
*/
public TypeParser(BiFunction<String, MappingParserContext, Builder> builderFunction) {
this(builderFunction, (n, c) -> {}, IndexVersions.MINIMUM_COMPATIBLE);
this(builderFunction, (n, c) -> {}, IndexVersions.MINIMUM_READONLY_COMPATIBLE);
}

/**
* Variant of {@link #TypeParser(BiFunction)} that allows to defining a minimumCompatibilityVersion to
* Variant of {@link #TypeParser(BiFunction)} that allows to define a minimumCompatibilityVersion to
* allow parsing mapping definitions of legacy indices (see {@link Mapper.TypeParser#supportsVersion(IndexVersion)}).
*/
public TypeParser(BiFunction<String, MappingParserContext, Builder> builderFunction, IndexVersion minimumCompatibilityVersion) {
private TypeParser(BiFunction<String, MappingParserContext, Builder> builderFunction, IndexVersion minimumCompatibilityVersion) {
this(builderFunction, (n, c) -> {}, minimumCompatibilityVersion);
}

public TypeParser(
BiFunction<String, MappingParserContext, Builder> builderFunction,
BiConsumer<String, MappingParserContext> contextValidator
) {
this(builderFunction, contextValidator, IndexVersions.MINIMUM_COMPATIBLE);
this(builderFunction, contextValidator, IndexVersions.MINIMUM_READONLY_COMPATIBLE);
}

public TypeParser(
BiFunction<String, MappingParserContext, Builder> builderFunction,
List<BiConsumer<String, MappingParserContext>> contextValidator
) {
this(builderFunction, (n, c) -> contextValidator.forEach(v -> v.accept(n, c)), IndexVersions.MINIMUM_COMPATIBLE);
this(builderFunction, (n, c) -> contextValidator.forEach(v -> v.accept(n, c)), IndexVersions.MINIMUM_READONLY_COMPATIBLE);
}

private TypeParser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,14 @@ public FieldMapper build(MapperBuilderContext context) {

}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static TypeParser PARSER = new TypeParser(
public static TypeParser PARSER = createTypeParserWithLegacySupport(
(n, c) -> new Builder(
n,
c.scriptCompiler(),
IGNORE_MALFORMED_SETTING.get(c.getSettings()),
c.indexVersionCreated(),
c.getIndexSettings().getMode()
),
MINIMUM_COMPATIBILITY_VERSION
)
);

private final Builder builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,10 @@ public IpFieldMapper build(MapperBuilderContext context) {

}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static final TypeParser PARSER = new TypeParser((n, c) -> {
public static final TypeParser PARSER = createTypeParserWithLegacySupport((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(n, c.scriptCompiler(), ignoreMalformedByDefault, c.indexVersionCreated());
}, MINIMUM_COMPATIBILITY_VERSION);
});

public static final class IpFieldType extends SimpleMappedFieldType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,7 @@ public KeywordFieldMapper build(MapperBuilderContext context) {
}
}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static final TypeParser PARSER = new TypeParser(Builder::new, MINIMUM_COMPATIBILITY_VERSION);
public static final TypeParser PARSER = createTypeParserWithLegacySupport(Builder::new);

public static final class KeywordFieldType extends StringFieldType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public interface TypeParser {
* Whether we can parse this type on indices with the given index created version.
*/
default boolean supportsVersion(IndexVersion indexCreatedVersion) {
return indexCreatedVersion.onOrAfter(IndexVersions.MINIMUM_COMPATIBLE);
return indexCreatedVersion.onOrAfter(IndexVersions.MINIMUM_READONLY_COMPATIBLE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ public MapperRegistry(
*/
public Mapper.TypeParser getMapperParser(String type, IndexVersion indexVersionCreated) {
Mapper.TypeParser parser = mapperParsers.get(type);
if (indexVersionCreated.isLegacyIndexVersion() && (parser == null || parser.supportsVersion(indexVersionCreated) == false)) {
return PlaceHolderFieldMapper.PARSER.apply(type);
if (indexVersionCreated.isLegacyIndexVersion()) {
if (parser == null || parser.supportsVersion(indexVersionCreated) == false) {
return PlaceHolderFieldMapper.PARSER.apply(type);
}
return parser;
} else {
assert parser == null || parser.supportsVersion(indexVersionCreated);
return parser;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ private static NumberFieldMapper toType(FieldMapper in) {
return (NumberFieldMapper) in;
}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static final class Builder extends FieldMapper.DimensionBuilder {

private final Parameter<Boolean> indexed;
Expand Down Expand Up @@ -1378,16 +1376,8 @@ private boolean isOutOfRange(Object value) {
NumberType(String name, NumericType numericType) {
this.name = name;
this.numericType = numericType;
this.parser = new TypeParser(
(n, c) -> new Builder(
n,
this,
c.scriptCompiler(),
c.getSettings(),
c.indexVersionCreated(),
c.getIndexSettings().getMode()
),
MINIMUM_COMPATIBILITY_VERSION
this.parser = createTypeParserWithLegacySupport(
(n, c) -> new Builder(n, this, c.scriptCompiler(), c.getSettings(), c.indexVersionCreated(), c.getIndexSettings().getMode())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,8 @@ public TextFieldMapper build(MapperBuilderContext context) {
}
}

private static final IndexVersion MINIMUM_COMPATIBILITY_VERSION = IndexVersion.fromId(5000099);

public static final TypeParser PARSER = new TypeParser(
(n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers(), SourceFieldMapper.isSynthetic(c.getIndexSettings())),
MINIMUM_COMPATIBILITY_VERSION
public static final TypeParser PARSER = createTypeParserWithLegacySupport(
(n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers(), SourceFieldMapper.isSynthetic(c.getIndexSettings()))
);

private static class PhraseWrappedAnalyzer extends AnalyzerWrapper {
Expand Down

0 comments on commit 49ebd42

Please sign in to comment.