Skip to content

Commit 2403413

Browse files
authored
Only create MapperService in SyntheticSourceIndexSettingsProvider when required (#116075) (#116233)
In case the index.mapping.source.mode is specified, there is no need to create a mapper service to determine whether synthetic source is used. In case of logsdb/tsdb there is also no reason to create a mapper service. If _source.mode attribute is specified, then it doesn't really matter whether what its value is for the SyntheticSourceIndexSettingsProvider. If it is synthetic, then that is the same as the index mode's default source mode. If it is stored, we just will add set index.mapping.source.mode to stored, which has no effect. And disabled source mode isn't allowed in the case of logsdb and tsdb. Closes #116070
1 parent 9533a47 commit 2403413

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

x-pack/plugin/logsdb/src/main/java/org/elasticsearch/xpack/logsdb/SyntheticSourceIndexSettingsProvider.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ boolean newIndexHasSyntheticSourceUsage(
101101

102102
try {
103103
var tmpIndexMetadata = buildIndexMetadataForMapperService(indexName, templateIndexMode, indexTemplateAndCreateRequestSettings);
104+
var indexMode = tmpIndexMetadata.getIndexMode();
105+
if (SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.exists(tmpIndexMetadata.getSettings())
106+
|| indexMode == IndexMode.LOGSDB
107+
|| indexMode == IndexMode.TIME_SERIES) {
108+
// In case when index mode is tsdb or logsdb and only _source.mode mapping attribute is specified, then the default
109+
// could be wrong. However, it doesn't really matter, because if the _source.mode mapping attribute is set to stored,
110+
// then configuring the index.mapping.source.mode setting to stored has no effect. Additionally _source.mode can't be set
111+
// to disabled, because that isn't allowed with logsdb/tsdb. In other words setting index.mapping.source.mode setting to
112+
// stored when _source.mode mapping attribute is stored is fine as it has no effect, but avoids creating MapperService.
113+
var sourceMode = SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(tmpIndexMetadata.getSettings());
114+
return sourceMode == SourceFieldMapper.Mode.SYNTHETIC;
115+
}
116+
117+
// TODO: remove this when _source.mode attribute has been removed:
104118
try (var mapperService = mapperServiceFactory.apply(tmpIndexMetadata)) {
105119
// combinedTemplateMappings can be null when creating system indices
106120
// combinedTemplateMappings can be empty when creating a normal index that doesn't match any template and without mapping.
@@ -112,7 +126,8 @@ boolean newIndexHasSyntheticSourceUsage(
112126
}
113127
} catch (AssertionError | Exception e) {
114128
// In case invalid mappings or setting are provided, then mapper service creation can fail.
115-
// In that case it is ok to return false here. The index creation will fail anyway later, so need to fallback to stored source.
129+
// In that case it is ok to return false here. The index creation will fail anyway later, so no need to fallback to stored
130+
// source.
116131
LOGGER.info(() -> Strings.format("unable to create mapper service for index [%s]", indexName), e);
117132
return false;
118133
}

x-pack/plugin/logsdb/src/test/java/org/elasticsearch/xpack/logsdb/SyntheticSourceIndexSettingsProviderTests.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.time.Instant;
2626
import java.util.List;
27+
import java.util.concurrent.atomic.AtomicInteger;
2728

2829
import static org.elasticsearch.common.settings.Settings.builder;
2930
import static org.hamcrest.Matchers.equalTo;
@@ -35,6 +36,7 @@ public class SyntheticSourceIndexSettingsProviderTests extends ESTestCase {
3536

3637
private SyntheticSourceLicenseService syntheticSourceLicenseService;
3738
private SyntheticSourceIndexSettingsProvider provider;
39+
private final AtomicInteger newMapperServiceCounter = new AtomicInteger();
3840

3941
private static LogsdbIndexModeSettingsProvider getLogsdbIndexModeSettingsProvider(boolean enabled) {
4042
return new LogsdbIndexModeSettingsProvider(Settings.builder().put("cluster.logsdb.enabled", enabled).build());
@@ -49,11 +51,11 @@ public void setup() {
4951
syntheticSourceLicenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
5052
syntheticSourceLicenseService.setLicenseState(licenseState);
5153

52-
provider = new SyntheticSourceIndexSettingsProvider(
53-
syntheticSourceLicenseService,
54-
im -> MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName()),
55-
getLogsdbIndexModeSettingsProvider(false)
56-
);
54+
provider = new SyntheticSourceIndexSettingsProvider(syntheticSourceLicenseService, im -> {
55+
newMapperServiceCounter.incrementAndGet();
56+
return MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName());
57+
}, getLogsdbIndexModeSettingsProvider(false));
58+
newMapperServiceCounter.set(0);
5759
}
5860

5961
public void testNewIndexHasSyntheticSourceUsage() throws IOException {
@@ -77,6 +79,7 @@ public void testNewIndexHasSyntheticSourceUsage() throws IOException {
7779
""";
7880
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
7981
assertTrue(result);
82+
assertThat(newMapperServiceCounter.get(), equalTo(1));
8083
}
8184
{
8285
String mapping;
@@ -110,6 +113,7 @@ public void testNewIndexHasSyntheticSourceUsage() throws IOException {
110113
}
111114
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
112115
assertFalse(result);
116+
assertThat(newMapperServiceCounter.get(), equalTo(2));
113117
}
114118
}
115119

@@ -152,15 +156,18 @@ public void testNewIndexHasSyntheticSourceUsageLogsdbIndex() throws IOException
152156
Settings settings = Settings.builder().put("index.mode", "logsdb").build();
153157
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
154158
assertTrue(result);
159+
assertThat(newMapperServiceCounter.get(), equalTo(0));
155160
}
156161
{
157162
Settings settings = Settings.builder().put("index.mode", "logsdb").build();
158163
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of());
159164
assertTrue(result);
165+
assertThat(newMapperServiceCounter.get(), equalTo(0));
160166
}
161167
{
162168
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, Settings.EMPTY, List.of());
163169
assertFalse(result);
170+
assertThat(newMapperServiceCounter.get(), equalTo(1));
164171
}
165172
{
166173
boolean result = provider.newIndexHasSyntheticSourceUsage(
@@ -170,6 +177,7 @@ public void testNewIndexHasSyntheticSourceUsageLogsdbIndex() throws IOException
170177
List.of(new CompressedXContent(mapping))
171178
);
172179
assertFalse(result);
180+
assertThat(newMapperServiceCounter.get(), equalTo(2));
173181
}
174182
}
175183

@@ -234,6 +242,7 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
234242
""";
235243
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
236244
assertFalse(result);
245+
assertThat(newMapperServiceCounter.get(), equalTo(1));
237246
}
238247
{
239248
String mapping = """
@@ -249,6 +258,7 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
249258
""";
250259
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
251260
assertFalse(result);
261+
assertThat(newMapperServiceCounter.get(), equalTo(2));
252262
}
253263
}
254264

@@ -278,6 +288,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
278288
List.of()
279289
);
280290
assertThat(result.size(), equalTo(0));
291+
assertThat(newMapperServiceCounter.get(), equalTo(0));
281292

282293
syntheticSourceLicenseService.setSyntheticSourceFallback(true);
283294
result = provider.getAdditionalIndexSettings(
@@ -291,6 +302,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
291302
);
292303
assertThat(result.size(), equalTo(1));
293304
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
305+
assertThat(newMapperServiceCounter.get(), equalTo(0));
294306

295307
result = provider.getAdditionalIndexSettings(
296308
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
@@ -303,6 +315,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
303315
);
304316
assertThat(result.size(), equalTo(1));
305317
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
318+
assertThat(newMapperServiceCounter.get(), equalTo(0));
306319

307320
result = provider.getAdditionalIndexSettings(
308321
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
@@ -315,6 +328,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
315328
);
316329
assertThat(result.size(), equalTo(1));
317330
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
331+
assertThat(newMapperServiceCounter.get(), equalTo(0));
318332
}
319333

320334
public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch() throws IOException {
@@ -347,6 +361,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
347361
List.of()
348362
);
349363
assertThat(result.size(), equalTo(0));
364+
assertThat(newMapperServiceCounter.get(), equalTo(0));
350365

351366
dataStreamName = "logs-app1-0";
352367
mb = Metadata.builder(
@@ -371,6 +386,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
371386
);
372387
assertThat(result.size(), equalTo(1));
373388
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
389+
assertThat(newMapperServiceCounter.get(), equalTo(0));
374390

375391
result = provider.getAdditionalIndexSettings(
376392
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
@@ -382,5 +398,6 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
382398
List.of()
383399
);
384400
assertThat(result.size(), equalTo(0));
401+
assertThat(newMapperServiceCounter.get(), equalTo(0));
385402
}
386403
}

0 commit comments

Comments
 (0)