Skip to content

Commit 5c12978

Browse files
authored
Use min node version to guard injecting settings in logs provider (#123005)
* Use min node version to guard injecting settings in logs provider * Update docs/changelog/123005.yaml * no random in cluster init
1 parent 3da1bb8 commit 5c12978

File tree

5 files changed

+120
-41
lines changed

5 files changed

+120
-41
lines changed

docs/changelog/123005.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 123005
2+
summary: Use min node version to guard injecting settings in logs provider
3+
area: Logs
4+
type: bug
5+
issues:
6+
- 122950

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public Collection<IndexSettingProvider> getAdditionalIndexSettingProviders(Index
8080
IndexVersion.current(),
8181
parameters.clusterService().state().nodes().getMaxDataNodeCompatibleIndexVersion()
8282
),
83+
() -> parameters.clusterService().state().nodes().getMinNodeVersion(),
8384
DiscoveryNode.isStateless(settings) == false,
8485
DiscoveryNode.isStateless(settings) == false
8586
);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.logging.log4j.LogManager;
1111
import org.apache.logging.log4j.Logger;
1212
import org.apache.lucene.util.SetOnce;
13+
import org.elasticsearch.Version;
1314
import org.elasticsearch.cluster.metadata.IndexMetadata;
1415
import org.elasticsearch.cluster.metadata.Metadata;
1516
import org.elasticsearch.common.UUIDs;
@@ -52,6 +53,7 @@ final class LogsdbIndexModeSettingsProvider implements IndexSettingProvider {
5253
private final LogsdbLicenseService licenseService;
5354
private final SetOnce<CheckedFunction<IndexMetadata, MapperService, IOException>> mapperServiceFactory = new SetOnce<>();
5455
private final SetOnce<Supplier<IndexVersion>> createdIndexVersion = new SetOnce<>();
56+
private final SetOnce<Supplier<Version>> minNodeVersion = new SetOnce<>();
5557
private final SetOnce<Boolean> supportFallbackToStoredSource = new SetOnce<>();
5658
private final SetOnce<Boolean> supportFallbackLogsdbRouting = new SetOnce<>();
5759

@@ -69,11 +71,13 @@ void updateClusterIndexModeLogsdbEnabled(boolean isLogsdbEnabled) {
6971
void init(
7072
CheckedFunction<IndexMetadata, MapperService, IOException> factory,
7173
Supplier<IndexVersion> indexVersion,
74+
Supplier<Version> minNodeVersion,
7275
boolean supportFallbackToStoredSource,
7376
boolean supportFallbackLogsdbRouting
7477
) {
7578
this.mapperServiceFactory.set(factory);
7679
this.createdIndexVersion.set(indexVersion);
80+
this.minNodeVersion.set(minNodeVersion);
7781
this.supportFallbackToStoredSource.set(supportFallbackToStoredSource);
7882
this.supportFallbackLogsdbRouting.set(supportFallbackLogsdbRouting);
7983
}
@@ -111,7 +115,9 @@ && matchesLogsPattern(dataStreamName)) {
111115
MappingHints mappingHints = getMappingHints(indexName, templateIndexMode, settings, combinedTemplateMappings);
112116

113117
// Inject stored source mode if synthetic source if not available per licence.
114-
if (mappingHints.hasSyntheticSourceUsage && supportFallbackToStoredSource.get()) {
118+
if (mappingHints.hasSyntheticSourceUsage
119+
&& supportFallbackToStoredSource.get()
120+
&& minNodeVersion.get().get().onOrAfter(Version.V_8_17_0)) {
115121
// This index name is used when validating component and index templates, we should skip this check in that case.
116122
// (See MetadataIndexTemplateService#validateIndexTemplateV2(...) method)
117123
boolean legacyLicensedUsageOfSyntheticSourceAllowed = isLegacyLicensedUsageOfSyntheticSourceAllowed(
@@ -128,7 +134,7 @@ && matchesLogsPattern(dataStreamName)) {
128134
}
129135
}
130136

131-
if (isLogsDB) {
137+
if (isLogsDB && minNodeVersion.get().get().onOrAfter(Version.V_8_18_0)) {
132138
// Inject sorting on [host.name], in addition to [@timestamp].
133139
if (mappingHints.sortOnHostName) {
134140
if (mappingHints.addHostNameField) {

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

Lines changed: 102 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.logsdb;
99

10+
import org.elasticsearch.Version;
1011
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
1112
import org.elasticsearch.cluster.metadata.ComposableIndexTemplateMetadata;
1213
import org.elasticsearch.cluster.metadata.DataStream;
@@ -83,6 +84,10 @@ public void setup() throws Exception {
8384
}
8485

8586
private LogsdbIndexModeSettingsProvider withSyntheticSourceDemotionSupport(boolean enabled) {
87+
return withSyntheticSourceDemotionSupport(enabled, Version.CURRENT);
88+
}
89+
90+
private LogsdbIndexModeSettingsProvider withSyntheticSourceDemotionSupport(boolean enabled, Version version) {
8691
newMapperServiceCounter.set(0);
8792
var provider = new LogsdbIndexModeSettingsProvider(
8893
logsdbLicenseService,
@@ -91,15 +96,28 @@ private LogsdbIndexModeSettingsProvider withSyntheticSourceDemotionSupport(boole
9196
provider.init(im -> {
9297
newMapperServiceCounter.incrementAndGet();
9398
return MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName());
94-
}, IndexVersion::current, true, true);
99+
}, IndexVersion::current, () -> version, true, true);
100+
return provider;
101+
}
102+
103+
private LogsdbIndexModeSettingsProvider withoutMapperService(boolean enabled) {
104+
var provider = new LogsdbIndexModeSettingsProvider(
105+
logsdbLicenseService,
106+
Settings.builder().put("cluster.logsdb.enabled", enabled).build()
107+
);
108+
provider.init(im -> null, IndexVersion::current, () -> Version.CURRENT, true, true);
95109
return provider;
96110
}
97111

98112
private Settings generateLogsdbSettings(Settings settings) throws IOException {
99-
return generateLogsdbSettings(settings, null);
113+
return generateLogsdbSettings(settings, null, Version.CURRENT);
100114
}
101115

102116
private Settings generateLogsdbSettings(Settings settings, String mapping) throws IOException {
117+
return generateLogsdbSettings(settings, mapping, Version.CURRENT);
118+
}
119+
120+
private Settings generateLogsdbSettings(Settings settings, String mapping, Version version) throws IOException {
103121
Metadata metadata = Metadata.EMPTY_METADATA;
104122
var provider = new LogsdbIndexModeSettingsProvider(
105123
logsdbLicenseService,
@@ -108,7 +126,7 @@ private Settings generateLogsdbSettings(Settings settings, String mapping) throw
108126
provider.init(im -> {
109127
newMapperServiceCounter.incrementAndGet();
110128
return MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName());
111-
}, IndexVersion::current, true, true);
129+
}, IndexVersion::current, () -> version, true, true);
112130
var result = provider.getAdditionalIndexSettings(
113131
DataStream.getDefaultBackingIndexName(DATA_STREAM_NAME, 0),
114132
DATA_STREAM_NAME,
@@ -217,11 +235,7 @@ public void testNonLogsDataStream() throws IOException {
217235
}
218236

219237
public void testWithoutLogsComponentTemplate() throws IOException {
220-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
221-
logsdbLicenseService,
222-
Settings.builder().put("cluster.logsdb.enabled", true).build()
223-
);
224-
238+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(true);
225239
final Settings additionalIndexSettings = provider.getAdditionalIndexSettings(
226240
null,
227241
"logs-apache-production",
@@ -236,11 +250,7 @@ public void testWithoutLogsComponentTemplate() throws IOException {
236250
}
237251

238252
public void testWithLogsComponentTemplate() throws IOException {
239-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
240-
logsdbLicenseService,
241-
Settings.builder().put("cluster.logsdb.enabled", true).build()
242-
);
243-
253+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(true);
244254
final Settings additionalIndexSettings = provider.getAdditionalIndexSettings(
245255
null,
246256
"logs-apache-production",
@@ -255,11 +265,7 @@ public void testWithLogsComponentTemplate() throws IOException {
255265
}
256266

257267
public void testWithMultipleComponentTemplates() throws IOException {
258-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
259-
logsdbLicenseService,
260-
Settings.builder().put("cluster.logsdb.enabled", true).build()
261-
);
262-
268+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(true);
263269
final Settings additionalIndexSettings = provider.getAdditionalIndexSettings(
264270
null,
265271
"logs-apache-production",
@@ -274,11 +280,7 @@ public void testWithMultipleComponentTemplates() throws IOException {
274280
}
275281

276282
public void testWithCustomComponentTemplatesOnly() throws IOException {
277-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
278-
logsdbLicenseService,
279-
Settings.builder().put("cluster.logsdb.enabled", true).build()
280-
);
281-
283+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(true);
282284
final Settings additionalIndexSettings = provider.getAdditionalIndexSettings(
283285
null,
284286
"logs-apache-production",
@@ -293,11 +295,7 @@ public void testWithCustomComponentTemplatesOnly() throws IOException {
293295
}
294296

295297
public void testNonMatchingTemplateIndexPattern() throws IOException {
296-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
297-
logsdbLicenseService,
298-
Settings.builder().put("cluster.logsdb.enabled", true).build()
299-
);
300-
298+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(true);
301299
final Settings additionalIndexSettings = provider.getAdditionalIndexSettings(
302300
null,
303301
"logs-apache-production",
@@ -331,10 +329,7 @@ public void testCaseSensitivity() throws IOException {
331329
}
332330

333331
public void testMultipleHyphensInDataStreamName() throws IOException {
334-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
335-
logsdbLicenseService,
336-
Settings.builder().put("cluster.logsdb.enabled", true).build()
337-
);
332+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(true);
338333

339334
final Settings additionalIndexSettings = provider.getAdditionalIndexSettings(
340335
null,
@@ -349,12 +344,8 @@ public void testMultipleHyphensInDataStreamName() throws IOException {
349344
assertIndexMode(additionalIndexSettings, IndexMode.LOGSDB.getName());
350345
}
351346

352-
public void testBeforeAndAFterSettingUpdate() throws IOException {
353-
final LogsdbIndexModeSettingsProvider provider = new LogsdbIndexModeSettingsProvider(
354-
logsdbLicenseService,
355-
Settings.builder().put("cluster.logsdb.enabled", false).build()
356-
);
357-
347+
public void testBeforeAndAfterSettingUpdate() throws IOException {
348+
final LogsdbIndexModeSettingsProvider provider = withoutMapperService(false);
358349
final Settings beforeSettings = provider.getAdditionalIndexSettings(
359350
null,
360351
"logs-apache-production",
@@ -627,7 +618,7 @@ public void testNewIndexHasSyntheticSourceUsageInvalidSettings() throws IOExcept
627618
}
628619
}
629620

630-
public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws IOException {
621+
public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() {
631622
String dataStreamName = DATA_STREAM_NAME;
632623
Metadata.Builder mb = Metadata.builder(
633624
DataStreamTestHelper.getClusterStateWithDataStreams(
@@ -698,6 +689,33 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
698689
assertThat(newMapperServiceCounter.get(), equalTo(4));
699690
}
700691

692+
public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceOldNode() {
693+
logsdbLicenseService.setSyntheticSourceFallback(true);
694+
LogsdbIndexModeSettingsProvider provider = withSyntheticSourceDemotionSupport(true, Version.V_8_16_0);
695+
Metadata.Builder mb = Metadata.builder(
696+
DataStreamTestHelper.getClusterStateWithDataStreams(
697+
List.of(Tuple.tuple(DATA_STREAM_NAME, 1)),
698+
List.of(),
699+
Instant.now().toEpochMilli(),
700+
builder().build(),
701+
1
702+
).getMetadata()
703+
);
704+
Metadata metadata = mb.build();
705+
Settings settings = builder().put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC)
706+
.build();
707+
var result = provider.getAdditionalIndexSettings(
708+
DataStream.getDefaultBackingIndexName(DATA_STREAM_NAME, 2),
709+
DATA_STREAM_NAME,
710+
null,
711+
metadata,
712+
Instant.ofEpochMilli(1L),
713+
settings,
714+
List.of()
715+
);
716+
assertTrue(result.isEmpty());
717+
}
718+
701719
public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch() throws IOException {
702720
logsdbLicenseService.setSyntheticSourceFallback(true);
703721
LogsdbIndexModeSettingsProvider provider = withSyntheticSourceDemotionSupport(true);
@@ -773,6 +791,15 @@ public void testRoutingPathOnSortFields() throws Exception {
773791
assertThat(IndexMetadata.INDEX_ROUTING_PATH.get(result), contains("host", "message"));
774792
}
775793

794+
public void testRoutingPathOnSortFieldsDisabledInOldNode() throws Exception {
795+
var settings = Settings.builder()
796+
.put(IndexSortConfig.INDEX_SORT_FIELD_SETTING.getKey(), "host,message")
797+
.put(IndexSettings.LOGSDB_ROUTE_ON_SORT_FIELDS.getKey(), true)
798+
.build();
799+
Settings result = generateLogsdbSettings(settings, null, Version.V_8_17_0);
800+
assertTrue(result.isEmpty());
801+
}
802+
776803
public void testRoutingPathOnSortFieldsFilterTimestamp() throws Exception {
777804
var settings = Settings.builder()
778805
.put(IndexSortConfig.INDEX_SORT_FIELD_SETTING.getKey(), "host,message,@timestamp")
@@ -868,6 +895,42 @@ public void testSortAndHostNameWithCustomSortConfig() throws Exception {
868895
assertEquals(0, newMapperServiceCounter.get());
869896
}
870897

898+
public void testSortAndHostNoHost() throws Exception {
899+
var settings = Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB).build();
900+
var mappings = """
901+
{
902+
"_doc": {
903+
"properties": {
904+
"@timestamp": {
905+
"type": "date"
906+
}
907+
}
908+
}
909+
}
910+
""";
911+
Settings result = generateLogsdbSettings(settings, mappings);
912+
assertTrue(IndexSettings.LOGSDB_SORT_ON_HOST_NAME.get(result));
913+
assertTrue(IndexSettings.LOGSDB_ADD_HOST_NAME_FIELD.get(result));
914+
assertEquals(1, newMapperServiceCounter.get());
915+
}
916+
917+
public void testSortAndHostNoHostOldNode() throws Exception {
918+
var settings = Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB).build();
919+
var mappings = """
920+
{
921+
"_doc": {
922+
"properties": {
923+
"@timestamp": {
924+
"type": "date"
925+
}
926+
}
927+
}
928+
}
929+
""";
930+
Settings result = generateLogsdbSettings(settings, mappings, Version.V_8_17_0);
931+
assertTrue(result.isEmpty());
932+
}
933+
871934
public void testSortAndHostNameKeyword() throws Exception {
872935
var settings = Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB).build();
873936
var mappings = """

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.logsdb;
99

10+
import org.elasticsearch.Version;
1011
import org.elasticsearch.cluster.metadata.DataStream;
1112
import org.elasticsearch.common.settings.Settings;
1213
import org.elasticsearch.index.IndexMode;
@@ -53,6 +54,7 @@ public void setup() throws Exception {
5354
provider.init(
5455
im -> MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName()),
5556
IndexVersion::current,
57+
() -> Version.CURRENT,
5658
true,
5759
true
5860
);
@@ -116,6 +118,7 @@ public void testGetAdditionalIndexSettingsTsdbAfterCutoffDate() throws Exception
116118
provider.init(
117119
im -> MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName()),
118120
IndexVersion::current,
121+
() -> Version.CURRENT,
119122
true,
120123
true
121124
);

0 commit comments

Comments
 (0)