1212import org .elasticsearch .client .Request ;
1313import org .elasticsearch .client .Response ;
1414import org .elasticsearch .client .ResponseException ;
15+ import org .elasticsearch .cluster .metadata .DataStreamFailureStoreSettings ;
16+ import org .elasticsearch .common .settings .Settings ;
17+ import org .elasticsearch .rest .RestStatus ;
1518import org .junit .Before ;
1619
1720import java .io .IOException ;
@@ -122,13 +125,25 @@ public void testExplicitlyResetDataStreamOptions() throws IOException {
122125 assertOK (client ().performRequest (otherRequest ));
123126 }
124127
125- public void testEnableDisableFailureStore () throws IOException {
128+ public void testBehaviorWithEachFailureStoreOptionAndClusterSetting () throws IOException {
126129 {
130+ // Default data stream options
127131 assertAcknowledged (client ().performRequest (new Request ("DELETE" , "/_data_stream/" + DATA_STREAM_NAME + "/_options" )));
128- assertFailureStore ( false , 1 );
132+ setDataStreamFailureStoreClusterSetting ( DATA_STREAM_NAME );
129133 assertDataStreamOptions (null );
134+ assertFailureStoreValuesInGetDataStreamResponse (true , 1 );
135+ assertRedirectsDocWithBadMappingToFailureStore ();
136+ setDataStreamFailureStoreClusterSetting ("does-not-match-failure-data-stream" );
137+ assertDataStreamOptions (null );
138+ assertFailureStoreValuesInGetDataStreamResponse (false , 1 );
139+ assertFailsDocWithBadMapping ();
140+ setDataStreamFailureStoreClusterSetting (null ); // should get same behaviour as when we set it to something non-matching
141+ assertDataStreamOptions (null );
142+ assertFailureStoreValuesInGetDataStreamResponse (false , 1 );
143+ assertFailsDocWithBadMapping ();
130144 }
131145 {
146+ // Data stream options with failure store enabled
132147 Request enableRequest = new Request ("PUT" , "/_data_stream/" + DATA_STREAM_NAME + "/_options" );
133148 enableRequest .setJsonEntity ("""
134149 {
@@ -137,11 +152,21 @@ public void testEnableDisableFailureStore() throws IOException {
137152 }
138153 }""" );
139154 assertAcknowledged (client ().performRequest (enableRequest ));
140- assertFailureStore (true , 1 );
155+ setDataStreamFailureStoreClusterSetting (DATA_STREAM_NAME );
156+ assertDataStreamOptions (true );
157+ assertFailureStoreValuesInGetDataStreamResponse (true , 1 );
158+ assertRedirectsDocWithBadMappingToFailureStore ();
159+ setDataStreamFailureStoreClusterSetting ("does-not-match-failure-data-stream" ); // should have no effect as enabled in options
141160 assertDataStreamOptions (true );
161+ assertFailureStoreValuesInGetDataStreamResponse (true , 1 );
162+ assertRedirectsDocWithBadMappingToFailureStore ();
163+ setDataStreamFailureStoreClusterSetting (null ); // same as previous
164+ assertDataStreamOptions (true );
165+ assertFailureStoreValuesInGetDataStreamResponse (true , 1 );
166+ assertRedirectsDocWithBadMappingToFailureStore ();
142167 }
143-
144168 {
169+ // Data stream options with failure store disabled
145170 Request disableRequest = new Request ("PUT" , "/_data_stream/" + DATA_STREAM_NAME + "/_options" );
146171 disableRequest .setJsonEntity ("""
147172 {
@@ -150,13 +175,23 @@ public void testEnableDisableFailureStore() throws IOException {
150175 }
151176 }""" );
152177 assertAcknowledged (client ().performRequest (disableRequest ));
153- assertFailureStore ( false , 1 );
178+ setDataStreamFailureStoreClusterSetting ( DATA_STREAM_NAME ); // should have no effect as disabled in options
154179 assertDataStreamOptions (false );
180+ assertFailureStoreValuesInGetDataStreamResponse (false , 1 );
181+ assertFailsDocWithBadMapping ();
182+ setDataStreamFailureStoreClusterSetting ("does-not-match-failure-data-stream" );
183+ assertDataStreamOptions (false );
184+ assertFailureStoreValuesInGetDataStreamResponse (false , 1 );
185+ assertFailsDocWithBadMapping ();
186+ setDataStreamFailureStoreClusterSetting (null );
187+ assertDataStreamOptions (false );
188+ assertFailureStoreValuesInGetDataStreamResponse (false , 1 );
189+ assertFailsDocWithBadMapping ();
155190 }
156191 }
157192
158193 @ SuppressWarnings ("unchecked" )
159- private void assertFailureStore (boolean failureStoreEnabled , int failureStoreSize ) throws IOException {
194+ private void assertFailureStoreValuesInGetDataStreamResponse (boolean failureStoreEnabled , int failureStoreSize ) throws IOException {
160195 final Response dataStreamResponse = client ().performRequest (new Request ("GET" , "/_data_stream/" + DATA_STREAM_NAME ));
161196 List <Object > dataStreams = (List <Object >) entityAsMap (dataStreamResponse ).get ("data_streams" );
162197 assertThat (dataStreams .size (), is (1 ));
@@ -198,4 +233,32 @@ private List<String> getIndices(Map<String, Object> response) {
198233 List <Map <String , String >> indices = (List <Map <String , String >>) response .get ("indices" );
199234 return indices .stream ().map (index -> index .get ("index_name" )).toList ();
200235 }
236+
237+ private static void setDataStreamFailureStoreClusterSetting (String value ) throws IOException {
238+ updateClusterSettings (
239+ Settings .builder ().put (DataStreamFailureStoreSettings .DATA_STREAM_FAILURE_STORED_ENABLED_SETTING .getKey (), value ).build ()
240+ );
241+ }
242+
243+ private Response putDocumentWithBadMapping () throws IOException {
244+ Request request = new Request ("POST" , DATA_STREAM_NAME + "/_doc" );
245+ request .setJsonEntity ("""
246+ {
247+ "@timestamp": "not a timestamp",
248+ "foo": "bar"
249+ }
250+ """ );
251+ return client ().performRequest (request );
252+ }
253+
254+ private void assertRedirectsDocWithBadMappingToFailureStore () throws IOException {
255+ Response response = putDocumentWithBadMapping ();
256+ String failureStoreResponse = (String ) entityAsMap (response ).get ("failure_store" );
257+ assertThat (failureStoreResponse , is ("used" ));
258+ }
259+
260+ private void assertFailsDocWithBadMapping () {
261+ ResponseException e = assertThrows (ResponseException .class , this ::putDocumentWithBadMapping );
262+ assertThat (e .getResponse ().getStatusLine ().getStatusCode (), is (RestStatus .BAD_REQUEST .getStatus ()));
263+ }
201264}
0 commit comments