Skip to content

Commit 075a40a

Browse files
authored
SyntheticSourceIndexSettingsProvider restores stored source (elastic#114978) (elastic#115187)
* SyntheticSourceIndexSettingsProvider restores stored source * remove asserts * add and fix tests * fix test * more tests * fix assert * remove assert (cherry picked from commit 5645240)
1 parent b769c5e commit 075a40a

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/DisabledSecurityDataStreamTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract class DisabledSecurityDataStreamTestCase extends ESRestTestCase
2828
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
2929
.distribution(DistributionType.DEFAULT)
3030
.feature(FeatureFlag.FAILURE_STORE_ENABLED)
31+
.setting("xpack.license.self_generated.type", "trial")
3132
.setting("xpack.security.enabled", "false")
3233
.setting("xpack.watcher.enabled", "false")
3334
.build();

x-pack/plugin/downsample/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/downsample/DownsampleRestIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class DownsampleRestIT extends ESClientYamlSuiteTestCase {
2020
@ClassRule
2121
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
2222
.distribution(DistributionType.DEFAULT)
23-
.setting("xpack.license.self_generated.type", "basic")
23+
.setting("xpack.license.self_generated.type", "trial")
2424
.setting("xpack.security.enabled", "false")
2525
.build();
2626

x-pack/plugin/logsdb/qa/with-basic/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/LogsdbRestIT.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.logsdb;
99

1010
import org.elasticsearch.common.settings.Settings;
11+
import org.elasticsearch.index.mapper.SourceFieldMapper;
1112
import org.elasticsearch.test.cluster.ElasticsearchCluster;
1213
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
1314
import org.elasticsearch.test.rest.ESRestTestCase;
@@ -72,4 +73,37 @@ public void testFeatureUsageWithLogsdbIndex() throws IOException {
7273
}
7374
}
7475

76+
public void testLogsdbIndexGetsStoredSource() throws IOException {
77+
final String index = "test-index";
78+
createIndex(index, Settings.builder().put("index.mode", "logsdb").build());
79+
var settings = (Map<?, ?>) ((Map<?, ?>) getIndexSettings(index).get(index)).get("settings");
80+
assertEquals("logsdb", settings.get("index.mode"));
81+
assertEquals(SourceFieldMapper.Mode.STORED.toString(), settings.get("index.mapping.source.mode"));
82+
}
83+
84+
public void testLogsdbOverrideSyntheticSourceModeInMapping() throws IOException {
85+
final String index = "test-index";
86+
String mapping = """
87+
{
88+
"_source": {
89+
"mode": "synthetic"
90+
}
91+
}
92+
""";
93+
createIndex(index, Settings.builder().put("index.mode", "logsdb").build(), mapping);
94+
var settings = (Map<?, ?>) ((Map<?, ?>) getIndexSettings(index).get(index)).get("settings");
95+
assertEquals("logsdb", settings.get("index.mode"));
96+
assertEquals(SourceFieldMapper.Mode.STORED.toString(), settings.get("index.mapping.source.mode"));
97+
}
98+
99+
public void testLogsdbNoOverrideSyntheticSourceSetting() throws IOException {
100+
final String index = "test-index";
101+
createIndex(
102+
index,
103+
Settings.builder().put("index.mode", "logsdb").put("index.mapping.source.mode", SourceFieldMapper.Mode.SYNTHETIC).build()
104+
);
105+
var settings = (Map<?, ?>) ((Map<?, ?>) getIndexSettings(index).get(index)).get("settings");
106+
assertEquals("logsdb", settings.get("index.mode"));
107+
assertEquals(SourceFieldMapper.Mode.SYNTHETIC.toString(), settings.get("index.mapping.source.mode"));
108+
}
75109
}

x-pack/plugin/logsdb/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/LogsdbRestIT.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public void testFeatureUsageWithLogsdbIndex() throws IOException {
7575
Map<?, ?> feature = features.stream().filter(map -> "mappings".equals(map.get("family"))).findFirst().get();
7676
assertThat(feature.get("name"), equalTo("synthetic-source"));
7777
assertThat(feature.get("license_level"), equalTo("enterprise"));
78+
79+
var settings = (Map<?, ?>) ((Map<?, ?>) getIndexSettings("test-index").get("test-index")).get("settings");
80+
assertNull(settings.get("index.mapping.source.mode")); // Default, no downgrading.
7881
}
7982
}
8083

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.index.IndexSettings;
2222
import org.elasticsearch.index.IndexVersion;
2323
import org.elasticsearch.index.mapper.MapperService;
24+
import org.elasticsearch.index.mapper.SourceFieldMapper;
2425

2526
import java.io.IOException;
2627
import java.time.Instant;
@@ -62,7 +63,9 @@ public Settings getAdditionalIndexSettings(
6263
if (newIndexHasSyntheticSourceUsage(indexName, templateIndexMode, indexTemplateAndCreateRequestSettings, combinedTemplateMappings)
6364
&& syntheticSourceLicenseService.fallbackToStoredSource(isTemplateValidation)) {
6465
LOGGER.debug("creation of index [{}] with synthetic source without it being allowed", indexName);
65-
// TODO: handle falling back to stored source
66+
return Settings.builder()
67+
.put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.STORED.toString())
68+
.build();
6669
}
6770
return Settings.EMPTY;
6871
}

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,42 @@
88
package org.elasticsearch.xpack.logsdb;
99

1010
import org.elasticsearch.cluster.metadata.DataStream;
11+
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
12+
import org.elasticsearch.cluster.metadata.Metadata;
1113
import org.elasticsearch.common.compress.CompressedXContent;
1214
import org.elasticsearch.common.settings.Settings;
15+
import org.elasticsearch.core.Tuple;
16+
import org.elasticsearch.index.IndexMode;
1317
import org.elasticsearch.index.MapperTestUtils;
18+
import org.elasticsearch.index.mapper.SourceFieldMapper;
19+
import org.elasticsearch.license.MockLicenseState;
1420
import org.elasticsearch.test.ESTestCase;
1521
import org.junit.Before;
1622

1723
import java.io.IOException;
24+
import java.time.Instant;
1825
import java.util.List;
1926

27+
import static org.elasticsearch.common.settings.Settings.builder;
28+
import static org.hamcrest.Matchers.equalTo;
29+
import static org.mockito.ArgumentMatchers.any;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.when;
32+
2033
public class SyntheticSourceIndexSettingsProviderTests extends ESTestCase {
2134

35+
private SyntheticSourceLicenseService syntheticSourceLicenseService;
2236
private SyntheticSourceIndexSettingsProvider provider;
2337

2438
@Before
2539
public void setup() {
26-
SyntheticSourceLicenseService syntheticSourceLicenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
40+
MockLicenseState licenseState = mock(MockLicenseState.class);
41+
when(licenseState.isAllowed(any())).thenReturn(true);
42+
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
43+
licenseService.setLicenseState(licenseState);
44+
syntheticSourceLicenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
45+
syntheticSourceLicenseService.setLicenseState(licenseState);
46+
2747
provider = new SyntheticSourceIndexSettingsProvider(
2848
syntheticSourceLicenseService,
2949
im -> MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName())
@@ -226,4 +246,68 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
226246
}
227247
}
228248

249+
public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws IOException {
250+
String dataStreamName = "logs-app1";
251+
Metadata.Builder mb = Metadata.builder(
252+
DataStreamTestHelper.getClusterStateWithDataStreams(
253+
List.of(Tuple.tuple(dataStreamName, 1)),
254+
List.of(),
255+
Instant.now().toEpochMilli(),
256+
builder().build(),
257+
1
258+
).getMetadata()
259+
);
260+
Metadata metadata = mb.build();
261+
262+
Settings settings = builder().put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC)
263+
.build();
264+
265+
Settings result = provider.getAdditionalIndexSettings(
266+
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
267+
dataStreamName,
268+
null,
269+
metadata,
270+
Instant.ofEpochMilli(1L),
271+
settings,
272+
List.of()
273+
);
274+
assertThat(result.size(), equalTo(0));
275+
276+
syntheticSourceLicenseService.setSyntheticSourceFallback(true);
277+
result = provider.getAdditionalIndexSettings(
278+
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
279+
dataStreamName,
280+
null,
281+
metadata,
282+
Instant.ofEpochMilli(1L),
283+
settings,
284+
List.of()
285+
);
286+
assertThat(result.size(), equalTo(1));
287+
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
288+
289+
result = provider.getAdditionalIndexSettings(
290+
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
291+
dataStreamName,
292+
IndexMode.TIME_SERIES,
293+
metadata,
294+
Instant.ofEpochMilli(1L),
295+
settings,
296+
List.of()
297+
);
298+
assertThat(result.size(), equalTo(1));
299+
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
300+
301+
result = provider.getAdditionalIndexSettings(
302+
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
303+
dataStreamName,
304+
IndexMode.LOGSDB,
305+
metadata,
306+
Instant.ofEpochMilli(1L),
307+
settings,
308+
List.of()
309+
);
310+
assertThat(result.size(), equalTo(1));
311+
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
312+
}
229313
}

0 commit comments

Comments
 (0)