Skip to content

Commit fc8e2fc

Browse files
Move source mode setting from SourceFieldMapper to IndexSettings (elastic#120096)
Here we move the `index.mapping.source.mode` setting to `IndexSettings` because of dependencies and because of the initialisation order of static fields for classes `IndexSettings` and `SourceFieldMapper`. Not initialising settings `index.mode`, `index.mapping.source.mode`, and `index.recovery.use_synthetic_source` in the right order results in multiple `NullPointerException`. This work is done to simplify another PR elastic#119110
1 parent c638f74 commit fc8e2fc

File tree

20 files changed

+115
-118
lines changed

20 files changed

+115
-118
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/MappingStats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.elasticsearch.common.unit.ByteSizeValue;
2222
import org.elasticsearch.core.Nullable;
2323
import org.elasticsearch.features.NodeFeature;
24-
import org.elasticsearch.index.mapper.SourceFieldMapper;
24+
import org.elasticsearch.index.IndexSettings;
2525
import org.elasticsearch.xcontent.ToXContentFragment;
2626
import org.elasticsearch.xcontent.XContentBuilder;
2727

@@ -71,7 +71,7 @@ public static MappingStats of(Metadata metadata, Runnable ensureNotCancelled) {
7171
}
7272
AnalysisStats.countMapping(mappingCounts, indexMetadata);
7373

74-
var sourceMode = SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexMetadata.getSettings());
74+
var sourceMode = IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexMetadata.getSettings());
7575
sourceModeUsageCount.merge(sourceMode.toString().toLowerCase(Locale.ENGLISH), 1, Integer::sum);
7676
}
7777
final AtomicLong totalFieldCount = new AtomicLong();

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import org.elasticsearch.index.mapper.DocumentMapper;
7070
import org.elasticsearch.index.mapper.MapperService;
7171
import org.elasticsearch.index.mapper.MapperService.MergeReason;
72-
import org.elasticsearch.index.mapper.SourceFieldMapper;
7372
import org.elasticsearch.index.query.SearchExecutionContext;
7473
import org.elasticsearch.indices.IndexCreationException;
7574
import org.elasticsearch.indices.IndicesService;
@@ -1586,7 +1585,7 @@ static void validateCloneIndex(
15861585

15871586
private static final Set<String> UNMODIFIABLE_SETTINGS_DURING_RESIZE = Set.of(
15881587
IndexSettings.MODE.getKey(),
1589-
SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(),
1588+
IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(),
15901589
IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(),
15911590
IndexSortConfig.INDEX_SORT_FIELD_SETTING.getKey(),
15921591
IndexSortConfig.INDEX_SORT_ORDER_SETTING.getKey(),

server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper;
3737
import org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper;
3838
import org.elasticsearch.index.mapper.MapperService;
39-
import org.elasticsearch.index.mapper.SourceFieldMapper;
4039
import org.elasticsearch.index.similarity.SimilarityService;
4140
import org.elasticsearch.index.store.FsDirectoryFactory;
4241
import org.elasticsearch.index.store.Store;
@@ -191,7 +190,7 @@ public final class IndexScopedSettings extends AbstractScopedSettings {
191190
FieldMapper.SYNTHETIC_SOURCE_KEEP_INDEX_SETTING,
192191
IgnoredSourceFieldMapper.SKIP_IGNORED_SOURCE_WRITE_SETTING,
193192
IgnoredSourceFieldMapper.SKIP_IGNORED_SOURCE_READ_SETTING,
194-
SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING,
193+
IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING,
195194
IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING,
196195
InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT,
197196

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

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING;
5353
import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING;
5454
import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING;
55-
import static org.elasticsearch.index.mapper.SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING;
5655

5756
/**
5857
* This class encapsulates all index level settings and handles settings updates.
@@ -655,48 +654,6 @@ public Iterator<Setting<?>> settings() {
655654
Property.Final
656655
);
657656

658-
public static final Setting<Boolean> RECOVERY_USE_SYNTHETIC_SOURCE_SETTING = Setting.boolSetting(
659-
"index.recovery.use_synthetic_source",
660-
false,
661-
new Setting.Validator<>() {
662-
@Override
663-
public void validate(Boolean value) {}
664-
665-
@Override
666-
public void validate(Boolean enabled, Map<Setting<?>, Object> settings) {
667-
if (enabled == false) {
668-
return;
669-
}
670-
671-
// Verify if synthetic source is enabled on the index; fail if it is not
672-
var indexMode = (IndexMode) settings.get(MODE);
673-
if (indexMode.defaultSourceMode() != SourceFieldMapper.Mode.SYNTHETIC) {
674-
var sourceMode = (SourceFieldMapper.Mode) settings.get(INDEX_MAPPER_SOURCE_MODE_SETTING);
675-
if (sourceMode != SourceFieldMapper.Mode.SYNTHETIC) {
676-
throw new IllegalArgumentException(
677-
String.format(
678-
Locale.ROOT,
679-
"The setting [%s] is only permitted when [%s] is set to [%s]. Current mode: [%s].",
680-
RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(),
681-
INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(),
682-
SourceFieldMapper.Mode.SYNTHETIC.name(),
683-
sourceMode.name()
684-
)
685-
);
686-
}
687-
}
688-
}
689-
690-
@Override
691-
public Iterator<Setting<?>> settings() {
692-
List<Setting<?>> res = List.of(INDEX_MAPPER_SOURCE_MODE_SETTING, MODE);
693-
return res.iterator();
694-
}
695-
},
696-
Property.IndexScope,
697-
Property.Final
698-
);
699-
700657
/**
701658
* Returns <code>true</code> if TSDB encoding is enabled. The default is <code>true</code>
702659
*/
@@ -753,6 +710,60 @@ public Iterator<Setting<?>> settings() {
753710
Property.ServerlessPublic
754711
);
755712

713+
public static final Setting<SourceFieldMapper.Mode> INDEX_MAPPER_SOURCE_MODE_SETTING = Setting.enumSetting(
714+
SourceFieldMapper.Mode.class,
715+
settings -> {
716+
final IndexMode indexMode = IndexSettings.MODE.get(settings);
717+
return indexMode.defaultSourceMode().name();
718+
},
719+
"index.mapping.source.mode",
720+
value -> {},
721+
Setting.Property.Final,
722+
Setting.Property.IndexScope
723+
);
724+
725+
public static final Setting<Boolean> RECOVERY_USE_SYNTHETIC_SOURCE_SETTING = Setting.boolSetting(
726+
"index.recovery.use_synthetic_source",
727+
false,
728+
new Setting.Validator<>() {
729+
@Override
730+
public void validate(Boolean value) {}
731+
732+
@Override
733+
public void validate(Boolean enabled, Map<Setting<?>, Object> settings) {
734+
if (enabled == false) {
735+
return;
736+
}
737+
738+
// Verify if synthetic source is enabled on the index; fail if it is not
739+
var indexMode = (IndexMode) settings.get(MODE);
740+
if (indexMode.defaultSourceMode() != SourceFieldMapper.Mode.SYNTHETIC) {
741+
var sourceMode = (SourceFieldMapper.Mode) settings.get(INDEX_MAPPER_SOURCE_MODE_SETTING);
742+
if (sourceMode != SourceFieldMapper.Mode.SYNTHETIC) {
743+
throw new IllegalArgumentException(
744+
String.format(
745+
Locale.ROOT,
746+
"The setting [%s] is only permitted when [%s] is set to [%s]. Current mode: [%s].",
747+
RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(),
748+
INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(),
749+
SourceFieldMapper.Mode.SYNTHETIC.name(),
750+
sourceMode.name()
751+
)
752+
);
753+
}
754+
}
755+
}
756+
757+
@Override
758+
public Iterator<Setting<?>> settings() {
759+
List<Setting<?>> res = List.of(INDEX_MAPPER_SOURCE_MODE_SETTING, MODE);
760+
return res.iterator();
761+
}
762+
},
763+
Property.IndexScope,
764+
Property.Final
765+
);
766+
756767
/**
757768
* Legacy index setting, kept for 7.x BWC compatibility. This setting has no effect in 8.x. Do not use.
758769
* TODO: Remove in 9.0

server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.common.Explicit;
1919
import org.elasticsearch.common.Strings;
2020
import org.elasticsearch.common.bytes.BytesReference;
21-
import org.elasticsearch.common.settings.Setting;
2221
import org.elasticsearch.common.settings.Settings;
2322
import org.elasticsearch.common.util.CollectionUtils;
2423
import org.elasticsearch.core.Nullable;
@@ -70,11 +69,6 @@ public class SourceFieldMapper extends MetadataFieldMapper {
7069

7170
public static final String LOSSY_PARAMETERS_ALLOWED_SETTING_NAME = "index.lossy.source-mapping-parameters";
7271

73-
public static final Setting<Mode> INDEX_MAPPER_SOURCE_MODE_SETTING = Setting.enumSetting(SourceFieldMapper.Mode.class, settings -> {
74-
final IndexMode indexMode = IndexSettings.MODE.get(settings);
75-
return indexMode.defaultSourceMode().name();
76-
}, "index.mapping.source.mode", value -> {}, Setting.Property.Final, Setting.Property.IndexScope);
77-
7872
public static final String DEPRECATION_WARNING = "Configuring source mode in mappings is deprecated and will be removed "
7973
+ "in future versions. Use [index.mapping.source.mode] index setting instead.";
8074

@@ -264,8 +258,8 @@ public SourceFieldMapper build() {
264258
private Mode resolveSourceMode() {
265259
// If the `index.mapping.source.mode` exists it takes precedence to determine the source mode for `_source`
266260
// otherwise the mode is determined according to `_source.mode`.
267-
if (INDEX_MAPPER_SOURCE_MODE_SETTING.exists(settings)) {
268-
return INDEX_MAPPER_SOURCE_MODE_SETTING.get(settings);
261+
if (IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.exists(settings)) {
262+
return IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.get(settings);
269263
}
270264

271265
// If `_source.mode` is not set we need to apply a default according to index mode.
@@ -297,7 +291,7 @@ private static SourceFieldMapper resolveStaticInstance(final Mode sourceMode) {
297291
return DEFAULT;
298292
}
299293

300-
final Mode settingSourceMode = INDEX_MAPPER_SOURCE_MODE_SETTING.get(c.getSettings());
294+
final Mode settingSourceMode = IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.get(c.getSettings());
301295
// Needed for bwc so that "mode" is not serialized in case of standard index with stored source.
302296
if (indexMode == IndexMode.STANDARD && settingSourceMode == Mode.STORED) {
303297
return DEFAULT;
@@ -482,11 +476,11 @@ public boolean isSynthetic() {
482476
}
483477

484478
public static boolean isSynthetic(IndexSettings indexSettings) {
485-
return INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexSettings.getSettings()) == SourceFieldMapper.Mode.SYNTHETIC;
479+
return IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexSettings.getSettings()) == SourceFieldMapper.Mode.SYNTHETIC;
486480
}
487481

488482
public static boolean isStored(IndexSettings indexSettings) {
489-
return INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexSettings.getSettings()) == Mode.STORED;
483+
return IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexSettings.getSettings()) == Mode.STORED;
490484
}
491485

492486
public boolean isDisabled() {

server/src/main/java/org/elasticsearch/snapshots/RestoreService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.elasticsearch.index.IndexVersion;
7171
import org.elasticsearch.index.mapper.MapperService;
7272
import org.elasticsearch.index.mapper.Mapping;
73-
import org.elasticsearch.index.mapper.SourceFieldMapper;
7473
import org.elasticsearch.index.shard.IndexLongFieldRange;
7574
import org.elasticsearch.index.shard.IndexShard;
7675
import org.elasticsearch.index.shard.ShardId;
@@ -158,7 +157,7 @@ public final class RestoreService implements ClusterStateApplier {
158157
SETTING_CREATION_DATE,
159158
SETTING_HISTORY_UUID,
160159
IndexSettings.MODE.getKey(),
161-
SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(),
160+
IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(),
162161
IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(),
163162
IndexSortConfig.INDEX_SORT_FIELD_SETTING.getKey(),
164163
IndexSortConfig.INDEX_SORT_ORDER_SETTING.getKey(),

server/src/test/java/org/elasticsearch/action/admin/cluster/stats/MappingStatsTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import org.elasticsearch.common.io.stream.StreamInput;
1818
import org.elasticsearch.common.io.stream.Writeable.Reader;
1919
import org.elasticsearch.common.settings.Settings;
20+
import org.elasticsearch.index.IndexSettings;
2021
import org.elasticsearch.index.IndexVersion;
21-
import org.elasticsearch.index.mapper.SourceFieldMapper;
2222
import org.elasticsearch.script.Script;
2323
import org.elasticsearch.tasks.TaskCancelledException;
2424
import org.elasticsearch.test.AbstractWireSerializingTestCase;
@@ -586,15 +586,15 @@ public void testSourceModes() {
586586
int numDisabledIndices = randomIntBetween(1, 5);
587587
for (int i = 0; i < numSyntheticIndices; i++) {
588588
IndexMetadata.Builder indexMetadata = new IndexMetadata.Builder("foo-synthetic-" + i).settings(
589-
indexSettings(IndexVersion.current(), 4, 1).put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "synthetic")
589+
indexSettings(IndexVersion.current(), 4, 1).put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "synthetic")
590590
);
591591
builder.put(indexMetadata);
592592
}
593593
for (int i = 0; i < numStoredIndices; i++) {
594594
IndexMetadata.Builder indexMetadata;
595595
if (randomBoolean()) {
596596
indexMetadata = new IndexMetadata.Builder("foo-stored-" + i).settings(
597-
indexSettings(IndexVersion.current(), 4, 1).put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "stored")
597+
indexSettings(IndexVersion.current(), 4, 1).put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "stored")
598598
);
599599
} else {
600600
indexMetadata = new IndexMetadata.Builder("foo-stored-" + i).settings(indexSettings(IndexVersion.current(), 4, 1));
@@ -603,7 +603,7 @@ public void testSourceModes() {
603603
}
604604
for (int i = 0; i < numDisabledIndices; i++) {
605605
IndexMetadata.Builder indexMetadata = new IndexMetadata.Builder("foo-disabled-" + i).settings(
606-
indexSettings(IndexVersion.current(), 4, 1).put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "disabled")
606+
indexSettings(IndexVersion.current(), 4, 1).put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "disabled")
607607
);
608608
builder.put(indexMetadata);
609609
}

server/src/test/java/org/elasticsearch/index/engine/LuceneSyntheticSourceChangesSnapshotTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919

2020
import java.io.IOException;
2121

22-
import static org.elasticsearch.index.mapper.SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING;
23-
2422
public class LuceneSyntheticSourceChangesSnapshotTests extends SearchBasedChangesSnapshotTests {
2523
@Override
2624
protected Settings indexSettings() {
2725
return Settings.builder()
2826
.put(super.indexSettings())
29-
.put(INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name())
27+
.put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name())
3028
.put(IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(), true)
3129
.build();
3230
}

server/src/test/java/org/elasticsearch/index/engine/TranslogOperationAsserterTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424

2525
import java.io.IOException;
2626

27-
import static org.elasticsearch.index.mapper.SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING;
28-
2927
public class TranslogOperationAsserterTests extends EngineTestCase {
3028

3129
@Override
3230
protected Settings indexSettings() {
3331
return Settings.builder()
3432
.put(super.indexSettings())
3533
.put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true)
36-
.put(INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name())
34+
.put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name())
3735
.put(IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(), true)
3836
.build();
3937
}
@@ -57,10 +55,10 @@ EngineConfig engineConfig(boolean useSyntheticSource) {
5755
EngineConfig config = engine.config();
5856
Settings.Builder settings = Settings.builder().put(config.getIndexSettings().getSettings());
5957
if (useSyntheticSource) {
60-
settings.put(INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name());
58+
settings.put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name());
6159
settings.put(IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(), true);
6260
} else {
63-
settings.put(INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.STORED.name());
61+
settings.put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.STORED.name());
6462
settings.put(IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(), false);
6563
}
6664
IndexMetadata imd = IndexMetadata.builder(config.getIndexSettings().getIndexMetadata()).settings(settings).build();

server/src/test/java/org/elasticsearch/index/mapper/IgnoredSourceFieldMapperConfigurationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.DirectoryReader;
1313
import org.elasticsearch.common.settings.Settings;
1414
import org.elasticsearch.core.CheckedConsumer;
15+
import org.elasticsearch.index.IndexSettings;
1516
import org.elasticsearch.xcontent.XContentBuilder;
1617

1718
import java.io.IOException;
@@ -130,7 +131,7 @@ private MapperService mapperServiceWithCustomSettings(
130131
for (var entry : customSettings.entrySet()) {
131132
settings.put(entry.getKey(), entry.getValue());
132133
}
133-
settings.put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC);
134+
settings.put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC);
134135
return createMapperService(settings.build(), mapping(mapping));
135136
}
136137

0 commit comments

Comments
 (0)