|
17 | 17 | import org.elasticsearch.action.admin.indices.sampling.GetSampleAction; |
18 | 18 | import org.elasticsearch.action.admin.indices.sampling.PutSampleConfigurationAction; |
19 | 19 | import org.elasticsearch.action.admin.indices.sampling.SamplingConfiguration; |
| 20 | +import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; |
20 | 21 | import org.elasticsearch.action.bulk.BulkRequest; |
21 | 22 | import org.elasticsearch.action.bulk.BulkResponse; |
22 | 23 | import org.elasticsearch.action.bulk.TransportBulkAction; |
| 24 | +import org.elasticsearch.action.datastreams.CreateDataStreamAction; |
| 25 | +import org.elasticsearch.action.datastreams.DeleteDataStreamAction; |
23 | 26 | import org.elasticsearch.action.index.IndexRequest; |
| 27 | +import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; |
24 | 28 | import org.elasticsearch.cluster.metadata.ProjectId; |
| 29 | +import org.elasticsearch.cluster.metadata.Template; |
| 30 | +import org.elasticsearch.common.compress.CompressedXContent; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
25 | 32 | import org.elasticsearch.core.TimeValue; |
| 33 | +import org.elasticsearch.datastreams.DataStreamsPlugin; |
| 34 | +import org.elasticsearch.plugins.Plugin; |
26 | 35 | import org.elasticsearch.test.ESIntegTestCase; |
27 | 36 | import org.junit.After; |
28 | 37 |
|
| 38 | +import java.util.Collection; |
29 | 39 | import java.util.HashMap; |
30 | 40 | import java.util.List; |
31 | 41 | import java.util.Map; |
|
36 | 46 | import static org.hamcrest.Matchers.equalTo; |
37 | 47 |
|
38 | 48 | public class SamplingServiceIT extends ESIntegTestCase { |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 52 | + return List.of(DataStreamsPlugin.class); |
| 53 | + } |
| 54 | + |
39 | 55 | public void testTTL() throws Exception { |
40 | 56 | assumeTrue("Requires sampling feature flag", RANDOM_SAMPLING_FEATURE_FLAG); |
41 | 57 | assertAcked( |
@@ -102,6 +118,66 @@ public void testDeleteIndex() throws Exception { |
102 | 118 | }); |
103 | 119 | } |
104 | 120 |
|
| 121 | + public void testDeleteDataStream() throws Exception { |
| 122 | + assumeTrue("Requires sampling feature flag", RANDOM_SAMPLING_FEATURE_FLAG); |
| 123 | + String indexName = randomIdentifier(); |
| 124 | + var template = ComposableIndexTemplate.builder() |
| 125 | + .indexPatterns(List.of(indexName)) |
| 126 | + |
| 127 | + .template(new Template(Settings.EMPTY, CompressedXContent.fromJSON(""" |
| 128 | + { |
| 129 | + "_doc":{ |
| 130 | + "dynamic":true, |
| 131 | + "properties":{ |
| 132 | + "foo":{ |
| 133 | + "type":"text" |
| 134 | + }, |
| 135 | + "bar":{ |
| 136 | + "type":"text" |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + """), null)) |
| 142 | + .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) |
| 143 | + .build(); |
| 144 | + var request = new TransportPutComposableIndexTemplateAction.Request("logs-template"); |
| 145 | + request.indexTemplate(template); |
| 146 | + safeGet(client().execute(TransportPutComposableIndexTemplateAction.TYPE, request)); |
| 147 | + client().execute( |
| 148 | + CreateDataStreamAction.INSTANCE, |
| 149 | + new CreateDataStreamAction.Request(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS, indexName) |
| 150 | + ).actionGet(); |
| 151 | + ensureYellow(indexName); |
| 152 | + PutSampleConfigurationAction.Request putSampleConfigRequest = new PutSampleConfigurationAction.Request( |
| 153 | + new SamplingConfiguration(1.0d, 10, null, null, null), |
| 154 | + TimeValue.THIRTY_SECONDS, |
| 155 | + TimeValue.THIRTY_SECONDS |
| 156 | + ).indices(indexName); |
| 157 | + client().execute(PutSampleConfigurationAction.INSTANCE, putSampleConfigRequest).actionGet(); |
| 158 | + for (int i = 0; i < 5; i++) { |
| 159 | + BulkRequest bulkRequest = new BulkRequest(); |
| 160 | + for (int j = 0; j < 20; j++) { |
| 161 | + IndexRequest indexRequest = new IndexRequest(indexName); |
| 162 | + indexRequest.create(true); |
| 163 | + indexRequest.source(Map.of("@timestamp", 12345, "foo", randomBoolean() ? 3L : randomLong(), "bar", randomBoolean())); |
| 164 | + bulkRequest.add(indexRequest); |
| 165 | + } |
| 166 | + BulkResponse bulkResponse = client().execute(TransportBulkAction.TYPE, bulkRequest).actionGet(); |
| 167 | + assertThat(bulkResponse.hasFailures(), equalTo(false)); |
| 168 | + } |
| 169 | + GetSampleAction.Response getSampleResponse = client().execute(GetSampleAction.INSTANCE, new GetSampleAction.Request(indexName)) |
| 170 | + .actionGet(); |
| 171 | + assertThat(getSampleResponse.getSample().size(), equalTo(10)); |
| 172 | + client().execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(TimeValue.THIRTY_SECONDS, indexName)) |
| 173 | + .actionGet(); |
| 174 | + assertBusy(() -> { |
| 175 | + for (SamplingService samplingService : internalCluster().getInstances(SamplingService.class)) { |
| 176 | + assertThat(samplingService.getLocalSample(ProjectId.DEFAULT, indexName), equalTo(List.of())); |
| 177 | + } |
| 178 | + }); |
| 179 | + } |
| 180 | + |
105 | 181 | @After |
106 | 182 | public void cleanup() { |
107 | 183 | Map<String, Object> clearedSettings = new HashMap<>(); |
|
0 commit comments