Skip to content

Commit 33b96cd

Browse files
authored
Reduce the number of SFM singletons. (#114969) (#115036)
This remove all recovery source specific SFM singletons. Whether recovery source is enabled can be checked via `DocumentParserContext`. This reduces the number of SFM instances by half.
1 parent 068f51d commit 33b96cd

File tree

4 files changed

+37
-133
lines changed

4 files changed

+37
-133
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.index.mapper.Mapper;
3131
import org.elasticsearch.index.mapper.SourceFieldMapper;
3232
import org.elasticsearch.index.translog.Translog;
33+
import org.elasticsearch.indices.recovery.RecoverySettings;
3334
import org.elasticsearch.ingest.IngestService;
3435
import org.elasticsearch.node.Node;
3536

@@ -817,6 +818,7 @@ private void setRetentionLeaseMillis(final TimeValue retentionLease) {
817818
private volatile boolean skipIgnoredSourceRead;
818819
private volatile boolean syntheticSourceSecondDocParsingPassEnabled;
819820
private final SourceFieldMapper.Mode indexMappingSourceMode;
821+
private final boolean recoverySourceEnabled;
820822

821823
/**
822824
* The maximum number of refresh listeners allows on this shard.
@@ -979,6 +981,7 @@ public IndexSettings(final IndexMetadata indexMetadata, final Settings nodeSetti
979981
skipIgnoredSourceRead = scopedSettings.get(IgnoredSourceFieldMapper.SKIP_IGNORED_SOURCE_READ_SETTING);
980982
syntheticSourceSecondDocParsingPassEnabled = scopedSettings.get(SYNTHETIC_SOURCE_SECOND_DOC_PARSING_PASS_SETTING);
981983
indexMappingSourceMode = scopedSettings.get(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING);
984+
recoverySourceEnabled = RecoverySettings.INDICES_RECOVERY_SOURCE_ENABLED_SETTING.get(nodeSettings);
982985

983986
scopedSettings.addSettingsUpdateConsumer(
984987
MergePolicyConfig.INDEX_COMPOUND_FORMAT_SETTING,
@@ -1674,6 +1677,14 @@ public SourceFieldMapper.Mode getIndexMappingSourceMode() {
16741677
return indexMappingSourceMode;
16751678
}
16761679

1680+
/**
1681+
* @return Whether recovery source should be enabled if needed.
1682+
* Note that this is a node setting, and this setting is not sourced from index settings.
1683+
*/
1684+
public boolean isRecoverySourceEnabled() {
1685+
return recoverySourceEnabled;
1686+
}
1687+
16771688
/**
16781689
* The bounds for {@code @timestamp} on this index or
16791690
* {@code null} if there are no bounds.

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

Lines changed: 24 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import java.util.List;
4040
import java.util.Locale;
4141

42-
import static org.elasticsearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_SOURCE_ENABLED_SETTING;
43-
4442
public class SourceFieldMapper extends MetadataFieldMapper {
4543
public static final NodeFeature SYNTHETIC_SOURCE_FALLBACK = new NodeFeature("mapper.source.synthetic_source_fallback");
4644
public static final NodeFeature SYNTHETIC_SOURCE_STORED_FIELDS_ADVANCE_FIX = new NodeFeature(
@@ -88,125 +86,55 @@ public enum Mode {
8886
Explicit.IMPLICIT_TRUE,
8987
Strings.EMPTY_ARRAY,
9088
Strings.EMPTY_ARRAY,
91-
null,
92-
true
89+
null
9390
);
9491

9592
private static final SourceFieldMapper DEFAULT_DISABLED = new SourceFieldMapper(
9693
Mode.DISABLED,
9794
Explicit.IMPLICIT_TRUE,
9895
Strings.EMPTY_ARRAY,
9996
Strings.EMPTY_ARRAY,
100-
null,
101-
true
102-
);
103-
104-
private static final SourceFieldMapper DEFAULT_DISABLED_NO_RECOVERY_SOURCE = new SourceFieldMapper(
105-
Mode.DISABLED,
106-
Explicit.IMPLICIT_TRUE,
107-
Strings.EMPTY_ARRAY,
108-
Strings.EMPTY_ARRAY,
109-
null,
110-
false
97+
null
11198
);
11299

113100
private static final SourceFieldMapper DEFAULT_SYNTHETIC = new SourceFieldMapper(
114101
Mode.SYNTHETIC,
115102
Explicit.IMPLICIT_TRUE,
116103
Strings.EMPTY_ARRAY,
117104
Strings.EMPTY_ARRAY,
118-
null,
119-
true
120-
);
121-
122-
private static final SourceFieldMapper DEFAULT_SYNTHETIC_NO_RECOVERY_SOURCE = new SourceFieldMapper(
123-
Mode.SYNTHETIC,
124-
Explicit.IMPLICIT_TRUE,
125-
Strings.EMPTY_ARRAY,
126-
Strings.EMPTY_ARRAY,
127-
null,
128-
false
129-
);
130-
131-
private static final SourceFieldMapper DEFAULT_NO_RECOVERY_SOURCE = new SourceFieldMapper(
132-
null,
133-
Explicit.IMPLICIT_TRUE,
134-
Strings.EMPTY_ARRAY,
135-
Strings.EMPTY_ARRAY,
136-
null,
137-
false
105+
null
138106
);
139107

140108
private static final SourceFieldMapper TSDB_DEFAULT = new SourceFieldMapper(
141109
Mode.SYNTHETIC,
142110
Explicit.IMPLICIT_TRUE,
143111
Strings.EMPTY_ARRAY,
144112
Strings.EMPTY_ARRAY,
145-
IndexMode.TIME_SERIES,
146-
true
113+
IndexMode.TIME_SERIES
147114
);
148115

149116
private static final SourceFieldMapper TSDB_DEFAULT_STORED = new SourceFieldMapper(
150117
Mode.STORED,
151118
Explicit.IMPLICIT_TRUE,
152119
Strings.EMPTY_ARRAY,
153120
Strings.EMPTY_ARRAY,
154-
IndexMode.TIME_SERIES,
155-
true
156-
);
157-
158-
private static final SourceFieldMapper TSDB_DEFAULT_NO_RECOVERY_SOURCE = new SourceFieldMapper(
159-
Mode.SYNTHETIC,
160-
Explicit.IMPLICIT_TRUE,
161-
Strings.EMPTY_ARRAY,
162-
Strings.EMPTY_ARRAY,
163-
IndexMode.TIME_SERIES,
164-
false
165-
);
166-
167-
private static final SourceFieldMapper TSDB_DEFAULT_NO_RECOVERY_SOURCE_STORED = new SourceFieldMapper(
168-
Mode.STORED,
169-
Explicit.IMPLICIT_TRUE,
170-
Strings.EMPTY_ARRAY,
171-
Strings.EMPTY_ARRAY,
172-
IndexMode.TIME_SERIES,
173-
false
121+
IndexMode.TIME_SERIES
174122
);
175123

176124
private static final SourceFieldMapper LOGSDB_DEFAULT = new SourceFieldMapper(
177125
Mode.SYNTHETIC,
178126
Explicit.IMPLICIT_TRUE,
179127
Strings.EMPTY_ARRAY,
180128
Strings.EMPTY_ARRAY,
181-
IndexMode.LOGSDB,
182-
true
129+
IndexMode.LOGSDB
183130
);
184131

185132
private static final SourceFieldMapper LOGSDB_DEFAULT_STORED = new SourceFieldMapper(
186133
Mode.STORED,
187134
Explicit.IMPLICIT_TRUE,
188135
Strings.EMPTY_ARRAY,
189136
Strings.EMPTY_ARRAY,
190-
IndexMode.LOGSDB,
191-
true
192-
);
193-
194-
private static final SourceFieldMapper LOGSDB_DEFAULT_NO_RECOVERY_SOURCE = new SourceFieldMapper(
195-
Mode.SYNTHETIC,
196-
Explicit.IMPLICIT_TRUE,
197-
Strings.EMPTY_ARRAY,
198-
Strings.EMPTY_ARRAY,
199-
IndexMode.LOGSDB,
200-
false
201-
);
202-
203-
private static final SourceFieldMapper LOGSDB_DEFAULT_NO_RECOVERY_SOURCE_STORED = new SourceFieldMapper(
204-
Mode.STORED,
205-
Explicit.IMPLICIT_TRUE,
206-
Strings.EMPTY_ARRAY,
207-
Strings.EMPTY_ARRAY,
208-
IndexMode.LOGSDB,
209-
false
137+
IndexMode.LOGSDB
210138
);
211139

212140
/*
@@ -218,17 +146,7 @@ public enum Mode {
218146
Explicit.IMPLICIT_TRUE,
219147
Strings.EMPTY_ARRAY,
220148
Strings.EMPTY_ARRAY,
221-
IndexMode.TIME_SERIES,
222-
true
223-
);
224-
225-
private static final SourceFieldMapper TSDB_LEGACY_DEFAULT_NO_RECOVERY_SOURCE = new SourceFieldMapper(
226-
null,
227-
Explicit.IMPLICIT_TRUE,
228-
Strings.EMPTY_ARRAY,
229-
Strings.EMPTY_ARRAY,
230-
IndexMode.TIME_SERIES,
231-
false
149+
IndexMode.TIME_SERIES
232150
);
233151

234152
public static class Defaults {
@@ -289,20 +207,12 @@ public static class Builder extends MetadataFieldMapper.Builder {
289207

290208
private final boolean supportsNonDefaultParameterValues;
291209

292-
private final boolean enableRecoverySource;
293-
294-
public Builder(
295-
IndexMode indexMode,
296-
final Settings settings,
297-
boolean supportsCheckForNonDefaultParams,
298-
boolean enableRecoverySource
299-
) {
210+
public Builder(IndexMode indexMode, final Settings settings, boolean supportsCheckForNonDefaultParams) {
300211
super(Defaults.NAME);
301212
this.settings = settings;
302213
this.indexMode = indexMode;
303214
this.supportsNonDefaultParameterValues = supportsCheckForNonDefaultParams == false
304215
|| settings.getAsBoolean(LOSSY_PARAMETERS_ALLOWED_SETTING_NAME, true);
305-
this.enableRecoverySource = enableRecoverySource;
306216
}
307217

308218
public Builder setSynthetic() {
@@ -337,7 +247,7 @@ public SourceFieldMapper build() {
337247
? INDEX_MAPPER_SOURCE_MODE_SETTING.get(settings)
338248
: mode.get();
339249
if (isDefault(sourceMode)) {
340-
return resolveSourceMode(indexMode, sourceMode == null ? Mode.STORED : sourceMode, enableRecoverySource);
250+
return resolveSourceMode(indexMode, sourceMode == null ? Mode.STORED : sourceMode);
341251

342252
}
343253
if (supportsNonDefaultParameterValues == false) {
@@ -368,8 +278,7 @@ public SourceFieldMapper build() {
368278
enabled.get(),
369279
includes.getValue().toArray(Strings.EMPTY_ARRAY),
370280
excludes.getValue().toArray(Strings.EMPTY_ARRAY),
371-
indexMode,
372-
enableRecoverySource
281+
indexMode
373282
);
374283
if (indexMode != null) {
375284
indexMode.validateSourceFieldMapper(sourceFieldMapper);
@@ -379,32 +288,26 @@ public SourceFieldMapper build() {
379288

380289
}
381290

382-
private static SourceFieldMapper resolveSourceMode(final IndexMode indexMode, final Mode sourceMode, boolean enableRecoverySource) {
291+
private static SourceFieldMapper resolveSourceMode(final IndexMode indexMode, final Mode sourceMode) {
383292
switch (indexMode) {
384293
case STANDARD:
385294
switch (sourceMode) {
386295
case SYNTHETIC:
387-
return enableRecoverySource ? DEFAULT_SYNTHETIC : DEFAULT_SYNTHETIC_NO_RECOVERY_SOURCE;
296+
return DEFAULT_SYNTHETIC;
388297
case STORED:
389-
return enableRecoverySource ? DEFAULT : DEFAULT_NO_RECOVERY_SOURCE;
298+
return DEFAULT;
390299
case DISABLED:
391-
return enableRecoverySource ? DEFAULT_DISABLED : DEFAULT_DISABLED_NO_RECOVERY_SOURCE;
300+
return DEFAULT_DISABLED;
392301
default:
393302
throw new IllegalArgumentException("Unsupported source mode: " + sourceMode);
394303
}
395304
case TIME_SERIES:
396305
case LOGSDB:
397306
switch (sourceMode) {
398307
case SYNTHETIC:
399-
return enableRecoverySource
400-
? (indexMode == IndexMode.TIME_SERIES ? TSDB_DEFAULT : LOGSDB_DEFAULT)
401-
: (indexMode == IndexMode.TIME_SERIES ? TSDB_DEFAULT_NO_RECOVERY_SOURCE : LOGSDB_DEFAULT_NO_RECOVERY_SOURCE);
308+
return indexMode == IndexMode.TIME_SERIES ? TSDB_DEFAULT : LOGSDB_DEFAULT;
402309
case STORED:
403-
return enableRecoverySource
404-
? (indexMode == IndexMode.TIME_SERIES ? TSDB_DEFAULT_STORED : LOGSDB_DEFAULT_STORED)
405-
: (indexMode == IndexMode.TIME_SERIES
406-
? TSDB_DEFAULT_NO_RECOVERY_SOURCE_STORED
407-
: LOGSDB_DEFAULT_NO_RECOVERY_SOURCE_STORED);
310+
return indexMode == IndexMode.TIME_SERIES ? TSDB_DEFAULT_STORED : LOGSDB_DEFAULT_STORED;
408311
case DISABLED:
409312
throw new IllegalArgumentException("_source can not be disabled in index using [" + indexMode + "] index mode");
410313
default:
@@ -417,21 +320,19 @@ private static SourceFieldMapper resolveSourceMode(final IndexMode indexMode, fi
417320

418321
public static final TypeParser PARSER = new ConfigurableTypeParser(c -> {
419322
final IndexMode indexMode = c.getIndexSettings().getMode();
420-
boolean enableRecoverySource = INDICES_RECOVERY_SOURCE_ENABLED_SETTING.get(c.getSettings());
421323
final Mode settingSourceMode = INDEX_MAPPER_SOURCE_MODE_SETTING.get(c.getSettings());
422324

423325
if (indexMode.isSyntheticSourceEnabled()) {
424326
if (indexMode == IndexMode.TIME_SERIES && c.getIndexSettings().getIndexVersionCreated().before(IndexVersions.V_8_7_0)) {
425-
return enableRecoverySource ? TSDB_LEGACY_DEFAULT : TSDB_LEGACY_DEFAULT_NO_RECOVERY_SOURCE;
327+
return TSDB_LEGACY_DEFAULT;
426328
}
427329
}
428-
return resolveSourceMode(indexMode, settingSourceMode == null ? Mode.STORED : settingSourceMode, enableRecoverySource);
330+
return resolveSourceMode(indexMode, settingSourceMode == null ? Mode.STORED : settingSourceMode);
429331
},
430332
c -> new Builder(
431333
c.getIndexSettings().getMode(),
432334
c.getSettings(),
433-
c.indexVersionCreated().onOrAfter(IndexVersions.SOURCE_MAPPER_LOSSY_PARAMS_CHECK),
434-
INDICES_RECOVERY_SOURCE_ENABLED_SETTING.get(c.getSettings())
335+
c.indexVersionCreated().onOrAfter(IndexVersions.SOURCE_MAPPER_LOSSY_PARAMS_CHECK)
435336
)
436337
);
437338

@@ -484,16 +385,8 @@ public BlockLoader blockLoader(BlockLoaderContext blContext) {
484385
private final SourceFilter sourceFilter;
485386

486387
private final IndexMode indexMode;
487-
private final boolean enableRecoverySource;
488-
489-
private SourceFieldMapper(
490-
Mode mode,
491-
Explicit<Boolean> enabled,
492-
String[] includes,
493-
String[] excludes,
494-
IndexMode indexMode,
495-
boolean enableRecoverySource
496-
) {
388+
389+
private SourceFieldMapper(Mode mode, Explicit<Boolean> enabled, String[] includes, String[] excludes, IndexMode indexMode) {
497390
super(new SourceFieldType((enabled.explicit() && enabled.value()) || (enabled.explicit() == false && mode != Mode.DISABLED)));
498391
assert enabled.explicit() == false || mode == null;
499392
this.mode = mode;
@@ -506,7 +399,6 @@ private SourceFieldMapper(
506399
}
507400
this.complete = stored() && sourceFilter == null;
508401
this.indexMode = indexMode;
509-
this.enableRecoverySource = enableRecoverySource;
510402
}
511403

512404
private static SourceFilter buildSourceFilter(String[] includes, String[] excludes) {
@@ -551,6 +443,7 @@ public void preParse(DocumentParserContext context) throws IOException {
551443
context.doc().add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length));
552444
}
553445

446+
boolean enableRecoverySource = context.indexSettings().isRecoverySourceEnabled();
554447
if (enableRecoverySource && originalSource != null && adaptedSource != originalSource) {
555448
// if we omitted source or modified it we add the _recovery_source to ensure we have it for ops based recovery
556449
BytesRef ref = originalSource.toBytesRef();
@@ -579,7 +472,7 @@ protected String contentType() {
579472

580473
@Override
581474
public FieldMapper.Builder getMergeBuilder() {
582-
return new Builder(indexMode, Settings.EMPTY, false, enableRecoverySource).init(this);
475+
return new Builder(indexMode, Settings.EMPTY, false).init(this);
583476
}
584477

585478
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void testCreateDynamicStringFieldAsKeywordForDimension() throws IOExcepti
6969
XContentParser parser = createParser(JsonXContent.jsonXContent, source);
7070
SourceToParse sourceToParse = new SourceToParse("test", new BytesArray(source), XContentType.JSON);
7171

72-
SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(null, Settings.EMPTY, false, true).setSynthetic().build();
72+
SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(null, Settings.EMPTY, false).setSynthetic().build();
7373
RootObjectMapper root = new RootObjectMapper.Builder("_doc", Optional.empty()).add(
7474
new PassThroughObjectMapper.Builder("labels").setPriority(0).setContainsDimensions().dynamic(ObjectMapper.Dynamic.TRUE)
7575
).build(MapperBuilderContext.root(false, false));

server/src/test/java/org/elasticsearch/index/query/SearchExecutionContextTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public void testSearchRequestRuntimeFieldsAndMultifieldDetection() {
384384

385385
public void testSyntheticSourceSearchLookup() throws IOException {
386386
// Build a mapping using synthetic source
387-
SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(null, Settings.EMPTY, false, true).setSynthetic().build();
387+
SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(null, Settings.EMPTY, false).setSynthetic().build();
388388
RootObjectMapper root = new RootObjectMapper.Builder("_doc", Optional.empty()).add(
389389
new KeywordFieldMapper.Builder("cat", IndexVersion.current()).ignoreAbove(100)
390390
).build(MapperBuilderContext.root(true, false));

0 commit comments

Comments
 (0)