|
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;
|
@@ -256,6 +258,85 @@ public void testMaybeSampleMaxSize() {
|
256 | 258 | assertThat(samplingService.getLocalSampleStats(projectId, indexName).getSamplesRejectedForSize(), equalTo((long) maxSamples - 2));
|
257 | 259 | }
|
258 | 260 |
|
| 261 | + public void testClusterChanged() { |
| 262 | + String indexName = randomIdentifier(); |
| 263 | + SamplingService samplingService = getTestSamplingService(); |
| 264 | + Map<String, Object> inputRawDocSource = randomMap(1, 100, () -> Tuple.tuple(randomAlphaOfLength(10), randomAlphaOfLength(10))); |
| 265 | + final IndexRequest indexRequest = new IndexRequest(indexName).id("_id").source(inputRawDocSource); |
| 266 | + |
| 267 | + // Test that the sample is removed if the new state does not have the project that the sample was configured in: |
| 268 | + ProjectMetadata projectMetadata = ProjectMetadata.builder(ProjectId.fromId(randomIdentifier())) |
| 269 | + .putCustom( |
| 270 | + SamplingMetadata.TYPE, |
| 271 | + new SamplingMetadata( |
| 272 | + Map.of( |
| 273 | + indexName, |
| 274 | + new SamplingConfiguration( |
| 275 | + 1.0, |
| 276 | + randomIntBetween(1, 1000), |
| 277 | + ByteSizeValue.ofBytes(randomLongBetween(100, 1000000)), |
| 278 | + TimeValue.timeValueDays(randomIntBetween(1, 10)), |
| 279 | + null |
| 280 | + ) |
| 281 | + ) |
| 282 | + ) |
| 283 | + ) |
| 284 | + .build(); |
| 285 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 286 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 287 | + ClusterState oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 288 | + ClusterState newState = ClusterState.builder(ClusterState.EMPTY_STATE) |
| 289 | + .putProjectMetadata(ProjectMetadata.builder(ProjectId.fromId(randomIdentifier()))) |
| 290 | + .build(); |
| 291 | + ClusterChangedEvent event = new ClusterChangedEvent("test", newState, oldState); |
| 292 | + samplingService.clusterChanged(event); |
| 293 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(0L)); |
| 294 | + |
| 295 | + // Test that the sample is removed if the sampling metadata is removed from the project: |
| 296 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 297 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 298 | + oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 299 | + newState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(ProjectMetadata.builder(projectMetadata.id())).build(); |
| 300 | + event = new ClusterChangedEvent("test", newState, oldState); |
| 301 | + samplingService.clusterChanged(event); |
| 302 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(0L)); |
| 303 | + |
| 304 | + // Test that the sample is removed if the sampling configuration is changed |
| 305 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 306 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 307 | + oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 308 | + projectMetadata = ProjectMetadata.builder(projectMetadata.id()) |
| 309 | + .putCustom( |
| 310 | + SamplingMetadata.TYPE, |
| 311 | + new SamplingMetadata( |
| 312 | + Map.of( |
| 313 | + indexName, |
| 314 | + new SamplingConfiguration( |
| 315 | + 1.0, |
| 316 | + 1001, |
| 317 | + ByteSizeValue.ofBytes(randomLongBetween(100, 1000000)), |
| 318 | + TimeValue.timeValueDays(randomIntBetween(1, 10)), |
| 319 | + null |
| 320 | + ) |
| 321 | + ) |
| 322 | + ) |
| 323 | + ) |
| 324 | + .build(); |
| 325 | + newState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 326 | + event = new ClusterChangedEvent("test", newState, oldState); |
| 327 | + samplingService.clusterChanged(event); |
| 328 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(0L)); |
| 329 | + |
| 330 | + // Test that the sample is _not_ removed if the sampling configuration does not change: |
| 331 | + samplingService.maybeSample(projectMetadata, indexRequest); |
| 332 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 333 | + oldState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 334 | + newState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(); |
| 335 | + event = new ClusterChangedEvent("test", newState, oldState); |
| 336 | + samplingService.clusterChanged(event); |
| 337 | + assertThat(samplingService.getLocalSampleStats(projectMetadata.id(), indexName).getSamples(), equalTo(1L)); |
| 338 | + } |
| 339 | + |
259 | 340 | private SamplingService getTestSamplingService() {
|
260 | 341 | final ScriptService scriptService = new ScriptService(
|
261 | 342 | Settings.EMPTY,
|
|
0 commit comments