|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.datastreams; |
| 11 | + |
| 12 | +import org.elasticsearch.client.Request; |
| 13 | +import org.elasticsearch.client.Response; |
| 14 | +import org.junit.Before; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static org.hamcrest.Matchers.equalTo; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | +import static org.hamcrest.Matchers.nullValue; |
| 23 | + |
| 24 | +/** |
| 25 | + * This should be a yaml test, but in order to write one we would need to expose the new APIs in the rest-api-spec. |
| 26 | + * We do not want to do that until the feature flag is removed. For this reason, we temporarily, test the new APIs here. |
| 27 | + * Please convert this to a yaml test when the feature flag is removed. |
| 28 | + */ |
| 29 | +public class DataStreamOptionsIT extends DisabledSecurityDataStreamTestCase { |
| 30 | + |
| 31 | + private static final String DATA_STREAM_NAME = "failure-data-stream"; |
| 32 | + |
| 33 | + @SuppressWarnings("unchecked") |
| 34 | + @Before |
| 35 | + public void setup() throws IOException { |
| 36 | + Request putComposableIndexTemplateRequest = new Request("POST", "/_index_template/ds-template"); |
| 37 | + putComposableIndexTemplateRequest.setJsonEntity(""" |
| 38 | + { |
| 39 | + "index_patterns": ["failure-data-stream"], |
| 40 | + "template": { |
| 41 | + "settings": { |
| 42 | + "number_of_replicas": 0 |
| 43 | + } |
| 44 | + }, |
| 45 | + "data_stream": { |
| 46 | + "failure_store": true |
| 47 | + } |
| 48 | + } |
| 49 | + """); |
| 50 | + assertOK(client().performRequest(putComposableIndexTemplateRequest)); |
| 51 | + |
| 52 | + assertOK(client().performRequest(new Request("PUT", "/_data_stream/" + DATA_STREAM_NAME))); |
| 53 | + // Initialize the failure store. |
| 54 | + assertOK(client().performRequest(new Request("POST", DATA_STREAM_NAME + "/_rollover?target_failure_store"))); |
| 55 | + ensureGreen(DATA_STREAM_NAME); |
| 56 | + |
| 57 | + final Response dataStreamResponse = client().performRequest(new Request("GET", "/_data_stream/" + DATA_STREAM_NAME)); |
| 58 | + List<Object> dataStreams = (List<Object>) entityAsMap(dataStreamResponse).get("data_streams"); |
| 59 | + assertThat(dataStreams.size(), is(1)); |
| 60 | + Map<String, Object> dataStream = (Map<String, Object>) dataStreams.get(0); |
| 61 | + assertThat(dataStream.get("name"), equalTo(DATA_STREAM_NAME)); |
| 62 | + List<String> backingIndices = getIndices(dataStream); |
| 63 | + assertThat(backingIndices.size(), is(1)); |
| 64 | + List<String> failureStore = getFailureStore(dataStream); |
| 65 | + assertThat(failureStore.size(), is(1)); |
| 66 | + } |
| 67 | + |
| 68 | + public void testEnableDisableFailureStore() throws IOException { |
| 69 | + { |
| 70 | + assertAcknowledged(client().performRequest(new Request("DELETE", "/_data_stream/" + DATA_STREAM_NAME + "/_options"))); |
| 71 | + assertFailureStore(false, 1); |
| 72 | + assertDataStreamOptions(null); |
| 73 | + } |
| 74 | + { |
| 75 | + Request enableRequest = new Request("PUT", "/_data_stream/" + DATA_STREAM_NAME + "/_options"); |
| 76 | + enableRequest.setJsonEntity(""" |
| 77 | + { |
| 78 | + "failure_store": { |
| 79 | + "enabled": true |
| 80 | + } |
| 81 | + }"""); |
| 82 | + assertAcknowledged(client().performRequest(enableRequest)); |
| 83 | + assertFailureStore(true, 1); |
| 84 | + assertDataStreamOptions(true); |
| 85 | + } |
| 86 | + |
| 87 | + { |
| 88 | + Request disableRequest = new Request("PUT", "/_data_stream/" + DATA_STREAM_NAME + "/_options"); |
| 89 | + disableRequest.setJsonEntity(""" |
| 90 | + { |
| 91 | + "failure_store": { |
| 92 | + "enabled": false |
| 93 | + } |
| 94 | + }"""); |
| 95 | + assertAcknowledged(client().performRequest(disableRequest)); |
| 96 | + assertFailureStore(false, 1); |
| 97 | + assertDataStreamOptions(false); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + private void assertFailureStore(boolean failureStoreEnabled, int failureStoreSize) throws IOException { |
| 103 | + final Response dataStreamResponse = client().performRequest(new Request("GET", "/_data_stream/" + DATA_STREAM_NAME)); |
| 104 | + List<Object> dataStreams = (List<Object>) entityAsMap(dataStreamResponse).get("data_streams"); |
| 105 | + assertThat(dataStreams.size(), is(1)); |
| 106 | + Map<String, Object> dataStream = (Map<String, Object>) dataStreams.get(0); |
| 107 | + assertThat(dataStream.get("name"), equalTo(DATA_STREAM_NAME)); |
| 108 | + assertThat(dataStream.containsKey("failure_store"), is(true)); |
| 109 | + // Ensure the failure store is set to the provided value |
| 110 | + assertThat(((Map<String, Object>) dataStream.get("failure_store")).get("enabled"), equalTo(failureStoreEnabled)); |
| 111 | + // And the failure indices preserved |
| 112 | + List<String> failureStore = getFailureStore(dataStream); |
| 113 | + assertThat(failureStore.size(), is(failureStoreSize)); |
| 114 | + } |
| 115 | + |
| 116 | + @SuppressWarnings("unchecked") |
| 117 | + private void assertDataStreamOptions(Boolean failureStoreEnabled) throws IOException { |
| 118 | + final Response dataStreamResponse = client().performRequest(new Request("GET", "/_data_stream/" + DATA_STREAM_NAME + "/_options")); |
| 119 | + List<Object> dataStreams = (List<Object>) entityAsMap(dataStreamResponse).get("data_streams"); |
| 120 | + assertThat(dataStreams.size(), is(1)); |
| 121 | + Map<String, Object> dataStream = (Map<String, Object>) dataStreams.get(0); |
| 122 | + assertThat(dataStream.get("name"), equalTo(DATA_STREAM_NAME)); |
| 123 | + Map<String, Map<String, Object>> options = (Map<String, Map<String, Object>>) dataStream.get("options"); |
| 124 | + if (failureStoreEnabled == null) { |
| 125 | + assertThat(options, nullValue()); |
| 126 | + } else { |
| 127 | + assertThat(options.containsKey("failure_store"), is(true)); |
| 128 | + assertThat(options.get("failure_store").get("enabled"), equalTo(failureStoreEnabled)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @SuppressWarnings("unchecked") |
| 133 | + private List<String> getFailureStore(Map<String, Object> response) { |
| 134 | + var failureStore = (Map<String, Object>) response.get("failure_store"); |
| 135 | + return getIndices(failureStore); |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressWarnings("unchecked") |
| 140 | + private List<String> getIndices(Map<String, Object> response) { |
| 141 | + List<Map<String, String>> indices = (List<Map<String, String>>) response.get("indices"); |
| 142 | + return indices.stream().map(index -> index.get("index_name")).toList(); |
| 143 | + } |
| 144 | +} |
0 commit comments