Skip to content

Commit 84c3c39

Browse files
authored
Only track feature usage when creating an index. (#113789) (#113801)
The SyntheticSourceLicenseService should only track if usage is allowed and an index will actually be created. Relates to #113468
1 parent c58dbb2 commit 84c3c39

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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
@@ -43,8 +43,11 @@ public Settings getAdditionalIndexSettings(
4343
Settings indexTemplateAndCreateRequestSettings,
4444
List<CompressedXContent> combinedTemplateMappings
4545
) {
46+
// This index name is used when validating component and index templates, we should skip this check in that case.
47+
// (See MetadataIndexTemplateService#validateIndexTemplateV2(...) method)
48+
boolean isTemplateValidation = "validate-index-name".equals(indexName);
4649
if (newIndexHasSyntheticSourceUsage(indexTemplateAndCreateRequestSettings)
47-
&& syntheticSourceLicenseService.fallbackToStoredSource()) {
50+
&& syntheticSourceLicenseService.fallbackToStoredSource(isTemplateValidation)) {
4851
LOGGER.debug("creation of index [{}] with synthetic source without it being allowed", indexName);
4952
// TODO: handle falling back to stored source
5053
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ public SyntheticSourceLicenseService(Settings settings) {
4646
/**
4747
* @return whether synthetic source mode should fallback to stored source.
4848
*/
49-
public boolean fallbackToStoredSource() {
49+
public boolean fallbackToStoredSource(boolean isTemplateValidation) {
5050
if (syntheticSourceFallback) {
5151
return true;
5252
}
5353

54-
return SYNTHETIC_SOURCE_FEATURE.check(licenseState) == false;
54+
if (isTemplateValidation) {
55+
return SYNTHETIC_SOURCE_FEATURE.checkWithoutTracking(licenseState) == false;
56+
} else {
57+
return SYNTHETIC_SOURCE_FEATURE.check(licenseState) == false;
58+
}
5559
}
5660

5761
void setSyntheticSourceFallback(boolean syntheticSourceFallback) {

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.common.settings.Settings;
1111
import org.elasticsearch.license.MockLicenseState;
1212
import org.elasticsearch.test.ESTestCase;
13+
import org.mockito.Mockito;
1314

1415
import static org.mockito.ArgumentMatchers.any;
1516
import static org.mockito.Mockito.mock;
@@ -22,15 +23,26 @@ public void testLicenseAllowsSyntheticSource() {
2223
when(licenseState.isAllowed(any())).thenReturn(true);
2324
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
2425
licenseService.setLicenseState(licenseState);
25-
assertFalse("synthetic source is allowed, so not fallback to stored source", licenseService.fallbackToStoredSource());
26+
assertFalse("synthetic source is allowed, so not fallback to stored source", licenseService.fallbackToStoredSource(false));
27+
Mockito.verify(licenseState, Mockito.times(1)).featureUsed(any());
28+
}
29+
30+
public void testLicenseAllowsSyntheticSourceTemplateValidation() {
31+
MockLicenseState licenseState = mock(MockLicenseState.class);
32+
when(licenseState.isAllowed(any())).thenReturn(true);
33+
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
34+
licenseService.setLicenseState(licenseState);
35+
assertFalse("synthetic source is allowed, so not fallback to stored source", licenseService.fallbackToStoredSource(true));
36+
Mockito.verify(licenseState, Mockito.never()).featureUsed(any());
2637
}
2738

2839
public void testDefaultDisallow() {
2940
MockLicenseState licenseState = mock(MockLicenseState.class);
3041
when(licenseState.isAllowed(any())).thenReturn(false);
3142
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
3243
licenseService.setLicenseState(licenseState);
33-
assertTrue("synthetic source is not allowed, so fallback to stored source", licenseService.fallbackToStoredSource());
44+
assertTrue("synthetic source is not allowed, so fallback to stored source", licenseService.fallbackToStoredSource(false));
45+
Mockito.verify(licenseState, Mockito.never()).featureUsed(any());
3446
}
3547

3648
public void testFallback() {
@@ -41,8 +53,9 @@ public void testFallback() {
4153
licenseService.setSyntheticSourceFallback(true);
4254
assertTrue(
4355
"synthetic source is allowed, but fallback has been enabled, so fallback to stored source",
44-
licenseService.fallbackToStoredSource()
56+
licenseService.fallbackToStoredSource(false)
4557
);
58+
Mockito.verifyNoInteractions(licenseState);
4659
}
4760

4861
}

0 commit comments

Comments
 (0)