77
88package  org .elasticsearch .xpack .logsdb ;
99
10+ import  org .elasticsearch .Version ;
1011import  org .elasticsearch .cluster .metadata .ComposableIndexTemplate ;
1112import  org .elasticsearch .cluster .metadata .ComposableIndexTemplateMetadata ;
1213import  org .elasticsearch .cluster .metadata .DataStream ;
@@ -83,6 +84,10 @@ public void setup() throws Exception {
8384    }
8485
8586    private  LogsdbIndexModeSettingsProvider  withSyntheticSourceDemotionSupport (boolean  enabled ) {
87+         return  withSyntheticSourceDemotionSupport (enabled , Version .CURRENT );
88+     }
89+ 
90+     private  LogsdbIndexModeSettingsProvider  withSyntheticSourceDemotionSupport (boolean  enabled , Version  version ) {
8691        newMapperServiceCounter .set (0 );
8792        var  provider  = new  LogsdbIndexModeSettingsProvider (
8893            logsdbLicenseService ,
@@ -91,15 +96,28 @@ private LogsdbIndexModeSettingsProvider withSyntheticSourceDemotionSupport(boole
9196        provider .init (im  -> {
9297            newMapperServiceCounter .incrementAndGet ();
9398            return  MapperTestUtils .newMapperService (xContentRegistry (), createTempDir (), im .getSettings (), im .getIndex ().getName ());
94-         }, IndexVersion ::current , true , true );
99+         }, IndexVersion ::current , () -> version , true , true );
100+         return  provider ;
101+     }
102+ 
103+     private  LogsdbIndexModeSettingsProvider  withoutMapperService (boolean  enabled ) {
104+         var  provider  = new  LogsdbIndexModeSettingsProvider (
105+             logsdbLicenseService ,
106+             Settings .builder ().put ("cluster.logsdb.enabled" , enabled ).build ()
107+         );
108+         provider .init (im  -> null , IndexVersion ::current , () -> Version .CURRENT , true , true );
95109        return  provider ;
96110    }
97111
98112    private  Settings  generateLogsdbSettings (Settings  settings ) throws  IOException  {
99-         return  generateLogsdbSettings (settings , null );
113+         return  generateLogsdbSettings (settings , null ,  Version . CURRENT );
100114    }
101115
102116    private  Settings  generateLogsdbSettings (Settings  settings , String  mapping ) throws  IOException  {
117+         return  generateLogsdbSettings (settings , mapping , Version .CURRENT );
118+     }
119+ 
120+     private  Settings  generateLogsdbSettings (Settings  settings , String  mapping , Version  version ) throws  IOException  {
103121        Metadata  metadata  = Metadata .EMPTY_METADATA ;
104122        var  provider  = new  LogsdbIndexModeSettingsProvider (
105123            logsdbLicenseService ,
@@ -108,7 +126,7 @@ private Settings generateLogsdbSettings(Settings settings, String mapping) throw
108126        provider .init (im  -> {
109127            newMapperServiceCounter .incrementAndGet ();
110128            return  MapperTestUtils .newMapperService (xContentRegistry (), createTempDir (), im .getSettings (), im .getIndex ().getName ());
111-         }, IndexVersion ::current , true , true );
129+         }, IndexVersion ::current , () ->  version ,  true , true );
112130        var  result  = provider .getAdditionalIndexSettings (
113131            DataStream .getDefaultBackingIndexName (DATA_STREAM_NAME , 0 ),
114132            DATA_STREAM_NAME ,
@@ -217,11 +235,7 @@ public void testNonLogsDataStream() throws IOException {
217235    }
218236
219237    public  void  testWithoutLogsComponentTemplate () throws  IOException  {
220-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
221-             logsdbLicenseService ,
222-             Settings .builder ().put ("cluster.logsdb.enabled" , true ).build ()
223-         );
224- 
238+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (true );
225239        final  Settings  additionalIndexSettings  = provider .getAdditionalIndexSettings (
226240            null ,
227241            "logs-apache-production" ,
@@ -236,11 +250,7 @@ public void testWithoutLogsComponentTemplate() throws IOException {
236250    }
237251
238252    public  void  testWithLogsComponentTemplate () throws  IOException  {
239-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
240-             logsdbLicenseService ,
241-             Settings .builder ().put ("cluster.logsdb.enabled" , true ).build ()
242-         );
243- 
253+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (true );
244254        final  Settings  additionalIndexSettings  = provider .getAdditionalIndexSettings (
245255            null ,
246256            "logs-apache-production" ,
@@ -255,11 +265,7 @@ public void testWithLogsComponentTemplate() throws IOException {
255265    }
256266
257267    public  void  testWithMultipleComponentTemplates () throws  IOException  {
258-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
259-             logsdbLicenseService ,
260-             Settings .builder ().put ("cluster.logsdb.enabled" , true ).build ()
261-         );
262- 
268+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (true );
263269        final  Settings  additionalIndexSettings  = provider .getAdditionalIndexSettings (
264270            null ,
265271            "logs-apache-production" ,
@@ -274,11 +280,7 @@ public void testWithMultipleComponentTemplates() throws IOException {
274280    }
275281
276282    public  void  testWithCustomComponentTemplatesOnly () throws  IOException  {
277-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
278-             logsdbLicenseService ,
279-             Settings .builder ().put ("cluster.logsdb.enabled" , true ).build ()
280-         );
281- 
283+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (true );
282284        final  Settings  additionalIndexSettings  = provider .getAdditionalIndexSettings (
283285            null ,
284286            "logs-apache-production" ,
@@ -293,11 +295,7 @@ public void testWithCustomComponentTemplatesOnly() throws IOException {
293295    }
294296
295297    public  void  testNonMatchingTemplateIndexPattern () throws  IOException  {
296-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
297-             logsdbLicenseService ,
298-             Settings .builder ().put ("cluster.logsdb.enabled" , true ).build ()
299-         );
300- 
298+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (true );
301299        final  Settings  additionalIndexSettings  = provider .getAdditionalIndexSettings (
302300            null ,
303301            "logs-apache-production" ,
@@ -331,10 +329,7 @@ public void testCaseSensitivity() throws IOException {
331329    }
332330
333331    public  void  testMultipleHyphensInDataStreamName () throws  IOException  {
334-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
335-             logsdbLicenseService ,
336-             Settings .builder ().put ("cluster.logsdb.enabled" , true ).build ()
337-         );
332+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (true );
338333
339334        final  Settings  additionalIndexSettings  = provider .getAdditionalIndexSettings (
340335            null ,
@@ -349,12 +344,8 @@ public void testMultipleHyphensInDataStreamName() throws IOException {
349344        assertIndexMode (additionalIndexSettings , IndexMode .LOGSDB .getName ());
350345    }
351346
352-     public  void  testBeforeAndAFterSettingUpdate () throws  IOException  {
353-         final  LogsdbIndexModeSettingsProvider  provider  = new  LogsdbIndexModeSettingsProvider (
354-             logsdbLicenseService ,
355-             Settings .builder ().put ("cluster.logsdb.enabled" , false ).build ()
356-         );
357- 
347+     public  void  testBeforeAndAfterSettingUpdate () throws  IOException  {
348+         final  LogsdbIndexModeSettingsProvider  provider  = withoutMapperService (false );
358349        final  Settings  beforeSettings  = provider .getAdditionalIndexSettings (
359350            null ,
360351            "logs-apache-production" ,
@@ -627,7 +618,7 @@ public void testNewIndexHasSyntheticSourceUsageInvalidSettings() throws IOExcept
627618        }
628619    }
629620
630-     public  void  testGetAdditionalIndexSettingsDowngradeFromSyntheticSource () throws   IOException   {
621+     public  void  testGetAdditionalIndexSettingsDowngradeFromSyntheticSource () {
631622        String  dataStreamName  = DATA_STREAM_NAME ;
632623        Metadata .Builder  mb  = Metadata .builder (
633624            DataStreamTestHelper .getClusterStateWithDataStreams (
@@ -698,6 +689,33 @@ public void testGetAdditionalIndexSettingsDowngradeFromSyntheticSource() throws
698689        assertThat (newMapperServiceCounter .get (), equalTo (4 ));
699690    }
700691
692+     public  void  testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceOldNode () {
693+         logsdbLicenseService .setSyntheticSourceFallback (true );
694+         LogsdbIndexModeSettingsProvider  provider  = withSyntheticSourceDemotionSupport (true , Version .V_8_16_0 );
695+         Metadata .Builder  mb  = Metadata .builder (
696+             DataStreamTestHelper .getClusterStateWithDataStreams (
697+                 List .of (Tuple .tuple (DATA_STREAM_NAME , 1 )),
698+                 List .of (),
699+                 Instant .now ().toEpochMilli (),
700+                 builder ().build (),
701+                 1 
702+             ).getMetadata ()
703+         );
704+         Metadata  metadata  = mb .build ();
705+         Settings  settings  = builder ().put (IndexSettings .INDEX_MAPPER_SOURCE_MODE_SETTING .getKey (), SourceFieldMapper .Mode .SYNTHETIC )
706+             .build ();
707+         var  result  = provider .getAdditionalIndexSettings (
708+             DataStream .getDefaultBackingIndexName (DATA_STREAM_NAME , 2 ),
709+             DATA_STREAM_NAME ,
710+             null ,
711+             metadata ,
712+             Instant .ofEpochMilli (1L ),
713+             settings ,
714+             List .of ()
715+         );
716+         assertTrue (result .isEmpty ());
717+     }
718+ 
701719    public  void  testGetAdditionalIndexSettingsDowngradeFromSyntheticSourceFileMatch () throws  IOException  {
702720        logsdbLicenseService .setSyntheticSourceFallback (true );
703721        LogsdbIndexModeSettingsProvider  provider  = withSyntheticSourceDemotionSupport (true );
@@ -773,6 +791,15 @@ public void testRoutingPathOnSortFields() throws Exception {
773791        assertThat (IndexMetadata .INDEX_ROUTING_PATH .get (result ), contains ("host" , "message" ));
774792    }
775793
794+     public  void  testRoutingPathOnSortFieldsDisabledInOldNode () throws  Exception  {
795+         var  settings  = Settings .builder ()
796+             .put (IndexSortConfig .INDEX_SORT_FIELD_SETTING .getKey (), "host,message" )
797+             .put (IndexSettings .LOGSDB_ROUTE_ON_SORT_FIELDS .getKey (), true )
798+             .build ();
799+         Settings  result  = generateLogsdbSettings (settings , null , Version .V_8_17_0 );
800+         assertTrue (result .isEmpty ());
801+     }
802+ 
776803    public  void  testRoutingPathOnSortFieldsFilterTimestamp () throws  Exception  {
777804        var  settings  = Settings .builder ()
778805            .put (IndexSortConfig .INDEX_SORT_FIELD_SETTING .getKey (), "host,message,@timestamp" )
@@ -868,6 +895,42 @@ public void testSortAndHostNameWithCustomSortConfig() throws Exception {
868895        assertEquals (0 , newMapperServiceCounter .get ());
869896    }
870897
898+     public  void  testSortAndHostNoHost () throws  Exception  {
899+         var  settings  = Settings .builder ().put (IndexSettings .MODE .getKey (), IndexMode .LOGSDB ).build ();
900+         var  mappings  = """ 
901+             { 
902+                 "_doc": { 
903+                     "properties": { 
904+                         "@timestamp": { 
905+                             "type": "date" 
906+                         } 
907+                     } 
908+                 } 
909+             } 
910+             """ ;
911+         Settings  result  = generateLogsdbSettings (settings , mappings );
912+         assertTrue (IndexSettings .LOGSDB_SORT_ON_HOST_NAME .get (result ));
913+         assertTrue (IndexSettings .LOGSDB_ADD_HOST_NAME_FIELD .get (result ));
914+         assertEquals (1 , newMapperServiceCounter .get ());
915+     }
916+ 
917+     public  void  testSortAndHostNoHostOldNode () throws  Exception  {
918+         var  settings  = Settings .builder ().put (IndexSettings .MODE .getKey (), IndexMode .LOGSDB ).build ();
919+         var  mappings  = """ 
920+             { 
921+                 "_doc": { 
922+                     "properties": { 
923+                         "@timestamp": { 
924+                             "type": "date" 
925+                         } 
926+                     } 
927+                 } 
928+             } 
929+             """ ;
930+         Settings  result  = generateLogsdbSettings (settings , mappings , Version .V_8_17_0 );
931+         assertTrue (result .isEmpty ());
932+     }
933+ 
871934    public  void  testSortAndHostNameKeyword () throws  Exception  {
872935        var  settings  = Settings .builder ().put (IndexSettings .MODE .getKey (), IndexMode .LOGSDB ).build ();
873936        var  mappings  = """ 
0 commit comments