Skip to content

Commit

Permalink
SyntheticSourceIndexSettingsProvider restores stored source
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrik-es committed Oct 17, 2024
1 parent 32ddbb3 commit 66cf545
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.SourceFieldMapper;

import java.io.IOException;
import java.time.Instant;
Expand Down Expand Up @@ -62,7 +63,9 @@ public Settings getAdditionalIndexSettings(
if (newIndexHasSyntheticSourceUsage(indexName, templateIndexMode, indexTemplateAndCreateRequestSettings, combinedTemplateMappings)
&& syntheticSourceLicenseService.fallbackToStoredSource(isTemplateValidation)) {
LOGGER.debug("creation of index [{}] with synthetic source without it being allowed", indexName);
// TODO: handle falling back to stored source
return Settings.builder()
.put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.STORED.toString())
.build();
}
return Settings.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,41 @@
package org.elasticsearch.xpack.logsdb;

import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.MapperTestUtils;
import org.elasticsearch.index.mapper.SourceFieldMapper;
import org.elasticsearch.license.MockLicenseState;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;

import java.io.IOException;
import java.time.Instant;
import java.util.List;

import static org.elasticsearch.common.settings.Settings.builder;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class SyntheticSourceIndexSettingsProviderTests extends ESTestCase {

private SyntheticSourceLicenseService syntheticSourceLicenseService;
private SyntheticSourceIndexSettingsProvider provider;

@Before
public void setup() {
SyntheticSourceLicenseService syntheticSourceLicenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
MockLicenseState licenseState = mock(MockLicenseState.class);
when(licenseState.isAllowed(any())).thenReturn(true);
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
licenseService.setLicenseState(licenseState);
syntheticSourceLicenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
syntheticSourceLicenseService.setLicenseState(licenseState);

provider = new SyntheticSourceIndexSettingsProvider(
syntheticSourceLicenseService,
im -> MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), im.getSettings(), im.getIndex().getName())
Expand Down Expand Up @@ -226,4 +245,44 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
}
}

public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws IOException {
String dataStreamName = "logs-app1";
Metadata.Builder mb = Metadata.builder(
DataStreamTestHelper.getClusterStateWithDataStreams(
List.of(Tuple.tuple(dataStreamName, 1)),
List.of(),
Instant.now().toEpochMilli(),
builder().build(),
1
).getMetadata()
);
Metadata metadata = mb.build();

Settings settings = builder().put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC)
.build();

Settings result = provider.getAdditionalIndexSettings(
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
dataStreamName,
null,
metadata,
Instant.ofEpochMilli(1L),
settings,
List.of()
);
assertThat(result.size(), equalTo(0));

syntheticSourceLicenseService.setSyntheticSourceFallback(true);
result = provider.getAdditionalIndexSettings(
DataStream.getDefaultBackingIndexName(dataStreamName, 2),
dataStreamName,
null,
metadata,
Instant.ofEpochMilli(1L),
settings,
List.of()
);
assertThat(result.size(), equalTo(1));
assertEquals(SourceFieldMapper.Mode.STORED, SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.get(result));
}
}

0 comments on commit 66cf545

Please sign in to comment.