Skip to content

Commit

Permalink
Only track feature usage when creating an index. (#113789)
Browse files Browse the repository at this point in the history
The SyntheticSourceLicenseService should only track if usage is allowed and an index will actually be created.

Relates to #113468
  • Loading branch information
martijnvg committed Sep 30, 2024
1 parent 6a134ac commit 176f070
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public Settings getAdditionalIndexSettings(
Settings indexTemplateAndCreateRequestSettings,
List<CompressedXContent> combinedTemplateMappings
) {
// This index name is used when validating component and index templates, we should skip this check in that case.
// (See MetadataIndexTemplateService#validateIndexTemplateV2(...) method)
boolean isTemplateValidation = "validate-index-name".equals(indexName);
if (newIndexHasSyntheticSourceUsage(indexTemplateAndCreateRequestSettings)
&& syntheticSourceLicenseService.fallbackToStoredSource()) {
&& syntheticSourceLicenseService.fallbackToStoredSource(isTemplateValidation)) {
LOGGER.debug("creation of index [{}] with synthetic source without it being allowed", indexName);
// TODO: handle falling back to stored source
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public SyntheticSourceLicenseService(Settings settings) {
/**
* @return whether synthetic source mode should fallback to stored source.
*/
public boolean fallbackToStoredSource() {
public boolean fallbackToStoredSource(boolean isTemplateValidation) {
if (syntheticSourceFallback) {
return true;
}

return SYNTHETIC_SOURCE_FEATURE.check(licenseState) == false;
if (isTemplateValidation) {
return SYNTHETIC_SOURCE_FEATURE.checkWithoutTracking(licenseState) == false;
} else {
return SYNTHETIC_SOURCE_FEATURE.check(licenseState) == false;
}
}

void setSyntheticSourceFallback(boolean syntheticSourceFallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.MockLicenseState;
import org.elasticsearch.test.ESTestCase;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand All @@ -22,15 +23,26 @@ public void testLicenseAllowsSyntheticSource() {
when(licenseState.isAllowed(any())).thenReturn(true);
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
licenseService.setLicenseState(licenseState);
assertFalse("synthetic source is allowed, so not fallback to stored source", licenseService.fallbackToStoredSource());
assertFalse("synthetic source is allowed, so not fallback to stored source", licenseService.fallbackToStoredSource(false));
Mockito.verify(licenseState, Mockito.times(1)).featureUsed(any());
}

public void testLicenseAllowsSyntheticSourceTemplateValidation() {
MockLicenseState licenseState = mock(MockLicenseState.class);
when(licenseState.isAllowed(any())).thenReturn(true);
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
licenseService.setLicenseState(licenseState);
assertFalse("synthetic source is allowed, so not fallback to stored source", licenseService.fallbackToStoredSource(true));
Mockito.verify(licenseState, Mockito.never()).featureUsed(any());
}

public void testDefaultDisallow() {
MockLicenseState licenseState = mock(MockLicenseState.class);
when(licenseState.isAllowed(any())).thenReturn(false);
var licenseService = new SyntheticSourceLicenseService(Settings.EMPTY);
licenseService.setLicenseState(licenseState);
assertTrue("synthetic source is not allowed, so fallback to stored source", licenseService.fallbackToStoredSource());
assertTrue("synthetic source is not allowed, so fallback to stored source", licenseService.fallbackToStoredSource(false));
Mockito.verify(licenseState, Mockito.never()).featureUsed(any());
}

public void testFallback() {
Expand All @@ -41,8 +53,9 @@ public void testFallback() {
licenseService.setSyntheticSourceFallback(true);
assertTrue(
"synthetic source is allowed, but fallback has been enabled, so fallback to stored source",
licenseService.fallbackToStoredSource()
licenseService.fallbackToStoredSource(false)
);
Mockito.verifyNoInteractions(licenseState);
}

}

0 comments on commit 176f070

Please sign in to comment.