Skip to content

Commit 4f1da52

Browse files
committed
setting validation
1 parent 80e32c2 commit 4f1da52

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

server/src/main/java/org/elasticsearch/index/IndexSettings.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,12 +1028,26 @@ public void validate(Boolean enabled, Map<Setting<?>, Object> settings) {
10281028
)
10291029
);
10301030
}
1031+
// Sequence numbers cannot be trimmed for points, so we enforce doc values only usage
1032+
var seqNoIndexOptions = (SeqNoFieldMapper.SeqNoIndexOptions) settings.get(SEQ_NO_INDEX_OPTIONS_SETTING);
1033+
if (seqNoIndexOptions != SeqNoFieldMapper.SeqNoIndexOptions.DOC_VALUES_ONLY) {
1034+
throw new IllegalArgumentException(
1035+
String.format(
1036+
Locale.ROOT,
1037+
"The setting [%s] is only permitted when [%s] is set to [%s]. Current value: [%s].",
1038+
DISABLE_SEQUENCE_NUMBERS.getKey(),
1039+
SEQ_NO_INDEX_OPTIONS_SETTING.getKey(),
1040+
SeqNoFieldMapper.SeqNoIndexOptions.DOC_VALUES_ONLY,
1041+
seqNoIndexOptions
1042+
)
1043+
);
1044+
}
10311045
}
10321046
}
10331047

10341048
@Override
10351049
public Iterator<Setting<?>> settings() {
1036-
List<Setting<?>> list = List.of(SETTING_INDEX_VERSION_CREATED);
1050+
List<Setting<?>> list = List.of(SETTING_INDEX_VERSION_CREATED, SEQ_NO_INDEX_OPTIONS_SETTING);
10371051
return list.iterator();
10381052
}
10391053
},

server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.index.engine.EngineConfig;
2525
import org.elasticsearch.index.mapper.MapperMetrics;
2626
import org.elasticsearch.index.mapper.MapperRegistry;
27+
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
2728
import org.elasticsearch.index.translog.Translog;
2829
import org.elasticsearch.plugins.MapperPlugin;
2930
import org.elasticsearch.test.ESTestCase;
@@ -1081,12 +1082,75 @@ public void testDisableSequenceNumbersSetting() {
10811082
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(IndexVersions.DISABLE_SEQUENCE_NUMBERS, IndexVersion.current());
10821083

10831084
var disabled = randomBoolean();
1084-
Settings settings = Settings.builder().put(IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(), disabled).build();
1085+
Settings settings = Settings.builder()
1086+
.put(IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(), disabled)
1087+
.put(IndexSettings.SEQ_NO_INDEX_OPTIONS_SETTING.getKey(), SeqNoFieldMapper.SeqNoIndexOptions.DOC_VALUES_ONLY)
1088+
.build();
10851089
IndexMetadata indexMetadata = newIndexMeta("some-index", settings, indexVersion);
10861090
IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
10871091
assertThat(indexSettings.sequenceNumbersDisabled(), is(equalTo(disabled)));
10881092
}
10891093

1094+
public void testDisableSequenceNumbersRequiresDocValuesOnly() {
1095+
assumeTrue("Test should only run with feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
1096+
final var indexVersion = IndexVersionUtils.randomVersionBetween(IndexVersions.DISABLE_SEQUENCE_NUMBERS, IndexVersion.current());
1097+
1098+
var builder = Settings.builder().put(IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(), true);
1099+
if (randomBoolean()) {
1100+
builder.put(
1101+
IndexSettings.SEQ_NO_INDEX_OPTIONS_SETTING.getKey(),
1102+
SeqNoFieldMapper.SeqNoIndexOptions.POINTS_AND_DOC_VALUES
1103+
);
1104+
}
1105+
var indexMetadata = newIndexMeta("some-index", builder.build(), indexVersion);
1106+
var e = assertThrows(
1107+
IllegalArgumentException.class,
1108+
() -> new IndexSettings(indexMetadata, Settings.EMPTY)
1109+
);
1110+
assertThat(
1111+
e.getMessage(),
1112+
Matchers.containsString(
1113+
String.format(
1114+
Locale.ROOT,
1115+
"The setting [%s] is only permitted when [%s] is set to [%s]. Current value: [%s].",
1116+
IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(),
1117+
IndexSettings.SEQ_NO_INDEX_OPTIONS_SETTING.getKey(),
1118+
SeqNoFieldMapper.SeqNoIndexOptions.DOC_VALUES_ONLY,
1119+
SeqNoFieldMapper.SeqNoIndexOptions.POINTS_AND_DOC_VALUES
1120+
)
1121+
)
1122+
);
1123+
}
1124+
1125+
public void testDisableSequenceNumbersRequiresDocValuesOnlyForNonStandardModes() {
1126+
assumeTrue("Test should only run with feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
1127+
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(IndexVersions.DISABLE_SEQUENCE_NUMBERS, IndexVersion.current());
1128+
1129+
IndexMode mode = randomFrom(IndexMode.TIME_SERIES, IndexMode.LOGSDB);
1130+
Settings.Builder builder = Settings.builder()
1131+
.put(IndexSettings.MODE.getKey(), mode.getName())
1132+
.put(IndexSettings.SEQ_NO_INDEX_OPTIONS_SETTING.getKey(), SeqNoFieldMapper.SeqNoIndexOptions.POINTS_AND_DOC_VALUES)
1133+
.put(IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(), true);
1134+
if (mode == IndexMode.TIME_SERIES) {
1135+
builder.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "foo");
1136+
}
1137+
IndexMetadata indexMetadata = newIndexMeta("some-index", builder.build(), indexVersion);
1138+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> new IndexSettings(indexMetadata, Settings.EMPTY));
1139+
assertThat(
1140+
e.getMessage(),
1141+
Matchers.containsString(
1142+
String.format(
1143+
Locale.ROOT,
1144+
"The setting [%s] is only permitted when [%s] is set to [%s]. Current value: [%s].",
1145+
IndexSettings.DISABLE_SEQUENCE_NUMBERS.getKey(),
1146+
IndexSettings.SEQ_NO_INDEX_OPTIONS_SETTING.getKey(),
1147+
SeqNoFieldMapper.SeqNoIndexOptions.DOC_VALUES_ONLY,
1148+
SeqNoFieldMapper.SeqNoIndexOptions.POINTS_AND_DOC_VALUES
1149+
)
1150+
)
1151+
);
1152+
}
1153+
10901154
public void testDisableSequenceNumbersValidationWithInvalidVersion() {
10911155
assumeTrue("Test should only run with feature flag", IndexSettings.DISABLE_SEQUENCE_NUMBERS_FEATURE_FLAG);
10921156
IndexVersion badVersion = IndexVersionUtils.getPreviousVersion(IndexVersions.DISABLE_SEQUENCE_NUMBERS);

0 commit comments

Comments
 (0)