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

[8.x] Only create MapperService in SyntheticSourceIndexSettingsProvider when required (#116075) #116233

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ boolean newIndexHasSyntheticSourceUsage(

try {
var tmpIndexMetadata = buildIndexMetadataForMapperService(indexName, templateIndexMode, indexTemplateAndCreateRequestSettings);
var indexMode = tmpIndexMetadata.getIndexMode();
if (SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.exists(tmpIndexMetadata.getSettings())
|| indexMode == IndexMode.LOGSDB
|| indexMode == IndexMode.TIME_SERIES) {
// In case when index mode is tsdb or logsdb and only _source.mode mapping attribute is specified, then the default
// could be wrong. However, it doesn't really matter, because if the _source.mode mapping attribute is set to stored,
// then configuring the index.mapping.source.mode setting to stored has no effect. Additionally _source.mode can't be set
// to disabled, because that isn't allowed with logsdb/tsdb. In other words setting index.mapping.source.mode setting to
// stored when _source.mode mapping attribute is stored is fine as it has no effect, but avoids creating MapperService.
var sourceMode = SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(tmpIndexMetadata.getSettings());
return sourceMode == SourceFieldMapper.Mode.SYNTHETIC;
}

// TODO: remove this when _source.mode attribute has been removed:
try (var mapperService = mapperServiceFactory.apply(tmpIndexMetadata)) {
// combinedTemplateMappings can be null when creating system indices
// combinedTemplateMappings can be empty when creating a normal index that doesn't match any template and without mapping.
Expand All @@ -112,7 +126,8 @@ boolean newIndexHasSyntheticSourceUsage(
}
} catch (AssertionError | Exception e) {
// In case invalid mappings or setting are provided, then mapper service creation can fail.
// In that case it is ok to return false here. The index creation will fail anyway later, so need to fallback to stored source.
// In that case it is ok to return false here. The index creation will fail anyway later, so no need to fallback to stored
// source.
LOGGER.info(() -> Strings.format("unable to create mapper service for index [%s]", indexName), e);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

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

private SyntheticSourceLicenseService syntheticSourceLicenseService;
private SyntheticSourceIndexSettingsProvider provider;
private final AtomicInteger newMapperServiceCounter = new AtomicInteger();

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

provider = new SyntheticSourceIndexSettingsProvider(
syntheticSourceLicenseService,
im -> MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName()),
getLogsdbIndexModeSettingsProvider(false)
);
provider = new SyntheticSourceIndexSettingsProvider(syntheticSourceLicenseService, im -> {
newMapperServiceCounter.incrementAndGet();
return MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName());
}, getLogsdbIndexModeSettingsProvider(false));
newMapperServiceCounter.set(0);
}

public void testNewIndexHasSyntheticSourceUsage() throws IOException {
Expand All @@ -77,6 +79,7 @@ public void testNewIndexHasSyntheticSourceUsage() throws IOException {
""";
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
assertTrue(result);
assertThat(newMapperServiceCounter.get(), equalTo(1));
}
{
String mapping;
Expand Down Expand Up @@ -110,6 +113,7 @@ public void testNewIndexHasSyntheticSourceUsage() throws IOException {
}
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
assertFalse(result);
assertThat(newMapperServiceCounter.get(), equalTo(2));
}
}

Expand Down Expand Up @@ -152,15 +156,18 @@ public void testNewIndexHasSyntheticSourceUsageLogsdbIndex() throws IOException
Settings settings = Settings.builder().put("index.mode", "logsdb").build();
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
assertTrue(result);
assertThat(newMapperServiceCounter.get(), equalTo(0));
}
{
Settings settings = Settings.builder().put("index.mode", "logsdb").build();
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of());
assertTrue(result);
assertThat(newMapperServiceCounter.get(), equalTo(0));
}
{
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, Settings.EMPTY, List.of());
assertFalse(result);
assertThat(newMapperServiceCounter.get(), equalTo(1));
}
{
boolean result = provider.newIndexHasSyntheticSourceUsage(
Expand All @@ -170,6 +177,7 @@ public void testNewIndexHasSyntheticSourceUsageLogsdbIndex() throws IOException
List.of(new CompressedXContent(mapping))
);
assertFalse(result);
assertThat(newMapperServiceCounter.get(), equalTo(2));
}
}

Expand Down Expand Up @@ -234,6 +242,7 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
""";
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
assertFalse(result);
assertThat(newMapperServiceCounter.get(), equalTo(1));
}
{
String mapping = """
Expand All @@ -249,6 +258,7 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
""";
boolean result = provider.newIndexHasSyntheticSourceUsage(indexName, null, settings, List.of(new CompressedXContent(mapping)));
assertFalse(result);
assertThat(newMapperServiceCounter.get(), equalTo(2));
}
}

Expand Down Expand Up @@ -278,6 +288,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
List.of()
);
assertThat(result.size(), equalTo(0));
assertThat(newMapperServiceCounter.get(), equalTo(0));

syntheticSourceLicenseService.setSyntheticSourceFallback(true);
result = provider.getAdditionalIndexSettings(
Expand All @@ -291,6 +302,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
);
assertThat(result.size(), equalTo(1));
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
assertThat(newMapperServiceCounter.get(), equalTo(0));

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

result = provider.getAdditionalIndexSettings(
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
Expand All @@ -315,6 +328,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
);
assertThat(result.size(), equalTo(1));
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
assertThat(newMapperServiceCounter.get(), equalTo(0));
}

public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch() throws IOException {
Expand Down Expand Up @@ -347,6 +361,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
List.of()
);
assertThat(result.size(), equalTo(0));
assertThat(newMapperServiceCounter.get(), equalTo(0));

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

result = provider.getAdditionalIndexSettings(
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
Expand All @@ -382,5 +398,6 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
List.of()
);
assertThat(result.size(), equalTo(0));
assertThat(newMapperServiceCounter.get(), equalTo(0));
}
}