2424import java .io .IOException ;
2525import java .time .Instant ;
2626import java .util .List ;
27+ import java .util .concurrent .atomic .AtomicInteger ;
2728
2829import static org .elasticsearch .common .settings .Settings .builder ;
2930import static org .hamcrest .Matchers .equalTo ;
@@ -35,6 +36,7 @@ public class SyntheticSourceIndexSettingsProviderTests extends ESTestCase {
3536
3637 private SyntheticSourceLicenseService syntheticSourceLicenseService ;
3738 private SyntheticSourceIndexSettingsProvider provider ;
39+ private final AtomicInteger newMapperServiceCounter = new AtomicInteger ();
3840
3941 private static LogsdbIndexModeSettingsProvider getLogsdbIndexModeSettingsProvider (boolean enabled ) {
4042 return new LogsdbIndexModeSettingsProvider (Settings .builder ().put ("cluster.logsdb.enabled" , enabled ).build ());
@@ -49,11 +51,11 @@ public void setup() {
4951 syntheticSourceLicenseService = new SyntheticSourceLicenseService (Settings .EMPTY );
5052 syntheticSourceLicenseService .setLicenseState (licenseState );
5153
52- provider = new SyntheticSourceIndexSettingsProvider (
53- syntheticSourceLicenseService ,
54- im -> MapperTestUtils .newMapperService (xContentRegistry (), createTempDir (), im .getSettings (), im .getIndex ().getName ()),
55- getLogsdbIndexModeSettingsProvider (false )
56- );
54+ provider = new SyntheticSourceIndexSettingsProvider (syntheticSourceLicenseService , im -> {
55+ newMapperServiceCounter . incrementAndGet ();
56+ return MapperTestUtils .newMapperService (xContentRegistry (), createTempDir (), im .getSettings (), im .getIndex ().getName ());
57+ }, getLogsdbIndexModeSettingsProvider (false ));
58+ newMapperServiceCounter . set ( 0 );
5759 }
5860
5961 public void testNewIndexHasSyntheticSourceUsage () throws IOException {
@@ -77,6 +79,7 @@ public void testNewIndexHasSyntheticSourceUsage() throws IOException {
7779 """ ;
7880 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , settings , List .of (new CompressedXContent (mapping )));
7981 assertTrue (result );
82+ assertThat (newMapperServiceCounter .get (), equalTo (1 ));
8083 }
8184 {
8285 String mapping ;
@@ -110,6 +113,7 @@ public void testNewIndexHasSyntheticSourceUsage() throws IOException {
110113 }
111114 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , settings , List .of (new CompressedXContent (mapping )));
112115 assertFalse (result );
116+ assertThat (newMapperServiceCounter .get (), equalTo (2 ));
113117 }
114118 }
115119
@@ -152,15 +156,18 @@ public void testNewIndexHasSyntheticSourceUsageLogsdbIndex() throws IOException
152156 Settings settings = Settings .builder ().put ("index.mode" , "logsdb" ).build ();
153157 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , settings , List .of (new CompressedXContent (mapping )));
154158 assertTrue (result );
159+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
155160 }
156161 {
157162 Settings settings = Settings .builder ().put ("index.mode" , "logsdb" ).build ();
158163 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , settings , List .of ());
159164 assertTrue (result );
165+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
160166 }
161167 {
162168 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , Settings .EMPTY , List .of ());
163169 assertFalse (result );
170+ assertThat (newMapperServiceCounter .get (), equalTo (1 ));
164171 }
165172 {
166173 boolean result = provider .newIndexHasSyntheticSourceUsage (
@@ -170,6 +177,7 @@ public void testNewIndexHasSyntheticSourceUsageLogsdbIndex() throws IOException
170177 List .of (new CompressedXContent (mapping ))
171178 );
172179 assertFalse (result );
180+ assertThat (newMapperServiceCounter .get (), equalTo (2 ));
173181 }
174182 }
175183
@@ -234,6 +242,7 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
234242 """ ;
235243 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , settings , List .of (new CompressedXContent (mapping )));
236244 assertFalse (result );
245+ assertThat (newMapperServiceCounter .get (), equalTo (1 ));
237246 }
238247 {
239248 String mapping = """
@@ -249,6 +258,7 @@ public void testNewIndexHasSyntheticSourceUsage_invalidSettings() throws IOExcep
249258 """ ;
250259 boolean result = provider .newIndexHasSyntheticSourceUsage (indexName , null , settings , List .of (new CompressedXContent (mapping )));
251260 assertFalse (result );
261+ assertThat (newMapperServiceCounter .get (), equalTo (2 ));
252262 }
253263 }
254264
@@ -278,6 +288,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
278288 List .of ()
279289 );
280290 assertThat (result .size (), equalTo (0 ));
291+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
281292
282293 syntheticSourceLicenseService .setSyntheticSourceFallback (true );
283294 result = provider .getAdditionalIndexSettings (
@@ -291,6 +302,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
291302 );
292303 assertThat (result .size (), equalTo (1 ));
293304 assertEquals (SourceFieldMapper .Mode .STORED , SourceFieldMapper .INDEX_MAPPER_SOURCE_MODE_SETTING .get (result ));
305+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
294306
295307 result = provider .getAdditionalIndexSettings (
296308 DataStream .getDefaultBackingIndexName (dataStreamName , 2 ),
@@ -303,6 +315,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
303315 );
304316 assertThat (result .size (), equalTo (1 ));
305317 assertEquals (SourceFieldMapper .Mode .STORED , SourceFieldMapper .INDEX_MAPPER_SOURCE_MODE_SETTING .get (result ));
318+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
306319
307320 result = provider .getAdditionalIndexSettings (
308321 DataStream .getDefaultBackingIndexName (dataStreamName , 2 ),
@@ -315,6 +328,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
315328 );
316329 assertThat (result .size (), equalTo (1 ));
317330 assertEquals (SourceFieldMapper .Mode .STORED , SourceFieldMapper .INDEX_MAPPER_SOURCE_MODE_SETTING .get (result ));
331+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
318332 }
319333
320334 public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch () throws IOException {
@@ -347,6 +361,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
347361 List .of ()
348362 );
349363 assertThat (result .size (), equalTo (0 ));
364+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
350365
351366 dataStreamName = "logs-app1-0" ;
352367 mb = Metadata .builder (
@@ -371,6 +386,7 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
371386 );
372387 assertThat (result .size (), equalTo (1 ));
373388 assertEquals (SourceFieldMapper .Mode .STORED , SourceFieldMapper .INDEX_MAPPER_SOURCE_MODE_SETTING .get (result ));
389+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
374390
375391 result = provider .getAdditionalIndexSettings (
376392 DataStream .getDefaultBackingIndexName (dataStreamName , 2 ),
@@ -382,5 +398,6 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch(
382398 List .of ()
383399 );
384400 assertThat (result .size (), equalTo (0 ));
401+ assertThat (newMapperServiceCounter .get (), equalTo (0 ));
385402 }
386403}
0 commit comments