|
12 | 12 | import org.elasticsearch.action.admin.indices.sampling.SamplingConfiguration; |
13 | 13 | import org.elasticsearch.action.admin.indices.sampling.SamplingMetadata; |
14 | 14 | import org.elasticsearch.action.index.IndexRequest; |
| 15 | +import org.elasticsearch.cluster.ClusterChangedEvent; |
| 16 | +import org.elasticsearch.cluster.ClusterState; |
15 | 17 | import org.elasticsearch.cluster.metadata.ProjectId; |
16 | 18 | import org.elasticsearch.cluster.metadata.ProjectMetadata; |
17 | 19 | import org.elasticsearch.cluster.project.ProjectResolver; |
@@ -224,6 +226,85 @@ public void testMaybeSampleMaxSamples() { |
224 | 226 | assertThat(stats.getTimeEvaluatingCondition(), equalTo(TimeValue.ZERO)); |
225 | 227 | } |
226 | 228 |
|
| 229 | + public void testClusterChanged() { |
| 230 | + String indexName = randomIdentifier(); |
| 231 | + SamplingService samplingService = getTestSamplingService(); |
| 232 | + Map<String, Object> inputRawDocSource = randomMap(1, 100, () -> Tuple.tuple(randomAlphaOfLength(10), randomAlphaOfLength(10))); |
| 233 | + final IndexRequest indexRequest = new IndexRequest(indexName).id("_id").source(inputRawDocSource); |
| 234 | + |
| 235 | + // Test that the sample is removed if the new state does not have the project that the sample was configured in: |
| 236 | + ProjectMetadata projectMetadata = ProjectMetadata.builder(ProjectId.fromId(randomIdentifier())) |
| 237 | + .putCustom( |
| 238 | + SamplingMetadata.TYPE, |
| 239 | + new SamplingMetadata( |
| 240 | + Map.of( |
| 241 | + indexName, |
| 242 | + new SamplingConfiguration( |
| 243 | + 1.0, |
| 244 | + randomIntBetween(1, 1000), |
| 245 | + ByteSizeValue.ofBytes(randomLongBetween(100, 1000000)), |
| 246 | + TimeValue.timeValueDays(randomIntBetween(1, 10)), |
| 247 | + null |
| 248 | + ) |
| 249 | + ) |
| 250 | + ) |
| 251 | + ) |
| 252 | + .build(); |
| 253 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 254 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 255 | + ClusterState oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 256 | + ClusterState newState = ClusterState.builder(ClusterState.EMPTY_STATE) |
| 257 | + .putProjectMetadata(ProjectMetadata.builder(ProjectId.fromId(randomIdentifier()))) |
| 258 | + .build(); |
| 259 | + ClusterChangedEvent event = new ClusterChangedEvent("test", newState, oldState); |
| 260 | + samplingService.clusterChanged(event); |
| 261 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(0L)); |
| 262 | + |
| 263 | + // Test that the sample is removed if the sampling metadata is removed from the project: |
| 264 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 265 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 266 | + oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 267 | + newState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(ProjectMetadata.builder(projectMetadata.id())).build(); |
| 268 | + event = new ClusterChangedEvent("test", newState, oldState); |
| 269 | + samplingService.clusterChanged(event); |
| 270 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(0L)); |
| 271 | + |
| 272 | + // Test that the sample is removed if the sampling configuration is changed |
| 273 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 274 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 275 | + oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 276 | + projectMetadata = ProjectMetadata.builder(projectMetadata.id()) |
| 277 | + .putCustom( |
| 278 | + SamplingMetadata.TYPE, |
| 279 | + new SamplingMetadata( |
| 280 | + Map.of( |
| 281 | + indexName, |
| 282 | + new SamplingConfiguration( |
| 283 | + 1.0, |
| 284 | + 1001, |
| 285 | + ByteSizeValue.ofBytes(randomLongBetween(100, 1000000)), |
| 286 | + TimeValue.timeValueDays(randomIntBetween(1, 10)), |
| 287 | + null |
| 288 | + ) |
| 289 | + ) |
| 290 | + ) |
| 291 | + ) |
| 292 | + .build(); |
| 293 | + newState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 294 | + event = new ClusterChangedEvent("test", newState, oldState); |
| 295 | + samplingService.clusterChanged(event); |
| 296 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(0L)); |
| 297 | + |
| 298 | + // Test that the sample is _not_ removed if the sampling configuration does not change: |
| 299 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 300 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 301 | + oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 302 | + newState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 303 | + event = new ClusterChangedEvent("test", newState, oldState); |
| 304 | + samplingService.clusterChanged(event); |
| 305 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 306 | + } |
| 307 | + |
227 | 308 | private SamplingService getTestSamplingService() { |
228 | 309 | final ScriptService scriptService = new ScriptService( |
229 | 310 | Settings.EMPTY, |
|
0 commit comments