Skip to content
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

Lower default mappings parsing compatibility to MINIMUM_READONLY_COMPATIBLE #119017

Merged
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);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the code a little easier to follow: there is one lower bound legacy compatibility constant, rather than one per mapper. After all the lower bound is always v5. It's also easier to follow what mappers provide legacy support, as they must now all call this method.


/**
* 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);
Copy link
Member Author

@javanna javanna Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effectively, lowering the minimum compatible doesn't take any effect, because we were checking the lower bound only for legacy indices which provide their specific lower bound. The default minimum compatible version could have been null for what it mattered.

}

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this assertion, we are now checking the min compatible version for all indices, and making sure that things are consistent. That said, only legacy indices will ever check the version compatibility in prod code.

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