Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"indices.get_sample_configuration": {
"documentation": {
"url": "https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get-sample-configuration",
"description": "Get sampling configuration for an index or data stream"
},
"stability": "experimental",
"visibility": "public",
"headers": {
"accept": [
"application/json"
]
},
"url": {
"paths": [
{
"path": "/{index}/_sample/config",
"methods": [
"GET"
],
"parts": {
"index": {
"type": "string",
"description": "The name of a data stream or index"
}
}
}
]
},
"params": {
"master_timeout": {
"type": "time",
"description": "Timeout for connection to master node"
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
---
setup:
- requires:
cluster_features: [ "random_sampling" ]
reason: requires feature 'random_sampling' to get random samples

---
teardown:
- do:
indices.delete:
index: "*test*"
ignore_unavailable: true
allow_no_indices: true

---
"Get sampling configuration after setting it":
- do:
indices.create:
index: test-get-config-index
body:
settings:
number_of_shards: 1

- do:
indices.put_sample_configuration:
index: test-get-config-index
body:
rate: 0.5
max_samples: 100
max_size: "10mb"
time_to_live: "1h"

- match: { acknowledged: true }

- do:
indices.get_sample_configuration:
index: test-get-config-index
human: true

- match: { configuration.rate: 0.5 }
- match: { configuration.max_samples: 100 }
- match: { configuration.max_size: "10mb" }
- match: { configuration.time_to_live: "1h" }

---
"Get sampling configuration with minimal parameters":
- do:
indices.create:
index: test-minimal-config-index

- do:
indices.put_sample_configuration:
index: test-minimal-config-index
body:
rate: 0.1

- match: { acknowledged: true }

- do:
indices.get_sample_configuration:
index: test-minimal-config-index
human: true

- match: { configuration.rate: 0.1 }

---
"Get sampling configuration with condition":
- do:
indices.create:
index: test-condition-config-index

- do:
indices.put_sample_configuration:
index: test-condition-config-index
body:
rate: 1.0
max_samples: 50
if: "ctx?.field == 'sample_me'"

- match: { acknowledged: true }

- do:
indices.get_sample_configuration:
index: test-condition-config-index
human: true

- match: { configuration.rate: 1.0 }
- match: { configuration.max_samples: 50 }
- match: { configuration.if: "ctx?.field == 'sample_me'" }

---
"Get sampling configuration for non-existent index":
- do:
catch: missing
indices.get_sample_configuration:
index: non-existent-index

---
"Get sampling configuration for index without configuration":
- do:
indices.create:
index: test-no-config-index

- do:
indices.get_sample_configuration:
index: test-no-config-index
human: true

# Should return empty or default configuration
- is_false: test-no-config-index.sampling

---
"Get sampling configuration after update":
- do:
indices.create:
index: test-update-config-index

# Set initial configuration
- do:
indices.put_sample_configuration:
index: test-update-config-index
body:
rate: 0.3
max_samples: 25

- match: { acknowledged: true }

- do:
indices.get_sample_configuration:
index: test-update-config-index
human: true

- match: { configuration.rate: 0.3 }
- match: { configuration.max_samples: 25 }

# Update configuration
- do:
indices.put_sample_configuration:
index: test-update-config-index
body:
rate: 0.8
max_samples: 75
max_size: "5mb"

- match: { acknowledged: true }

# Verify updated configuration
- do:
indices.get_sample_configuration:
index: test-update-config-index
human: true

- match: { configuration.rate: 0.8 }
- match: { configuration.max_samples: 75 }
- match: { configuration.max_size: "5mb" }

---
"Get sampling configuration with master timeout":
- do:
indices.create:
index: test-timeout-config-index

- do:
indices.put_sample_configuration:
index: test-timeout-config-index
body:
rate: 0.7
max_samples: 200

- match: { acknowledged: true }

- do:
indices.get_sample_configuration:
index: test-timeout-config-index
master_timeout: "30s"
human: true

- match: { configuration.rate: 0.7 }
- match: { configuration.max_samples: 200 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.action.admin.indices.sampling;

import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.ingest.SamplingService;
import org.elasticsearch.test.ESIntegTestCase;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class GetSampleConfigurationActionIT extends ESIntegTestCase {

public void testGetSampleConfiguration() {
assumeTrue("Requires the sampling feature flag to be enabled", SamplingService.RANDOM_SAMPLING_FEATURE_FLAG);

String indexName = randomIdentifier();
createIndex(indexName);

// Create a random sampling configuration
SamplingConfiguration config = randomSamplingConfiguration();
putSamplingConfiguration(indexName, config);

// Get and verify the sampling configuration
assertGetConfigurationMatches(indexName, config);
}

public void testGetSampleConfigurationForNonExistentIndex() {
assumeTrue("Requires the sampling feature flag to be enabled", SamplingService.RANDOM_SAMPLING_FEATURE_FLAG);

String nonExistentIndex = randomIdentifier();

// Try to get configuration for non-existent index
GetSampleConfigurationAction.Request getRequest = new GetSampleConfigurationAction.Request(randomValidTimeValue());
getRequest.indices(nonExistentIndex);

// This should fail - cannot get config for non-existent index
expectThrows(Exception.class, () -> client().execute(GetSampleConfigurationAction.INSTANCE, getRequest).actionGet());
}

public void testGetSampleConfigurationForIndexWithoutConfiguration() {
assumeTrue("Requires the sampling feature flag to be enabled", SamplingService.RANDOM_SAMPLING_FEATURE_FLAG);

String indexName = randomIdentifier();
createIndex(indexName);

// Get configuration for index without any sampling configuration
GetSampleConfigurationAction.Request getRequest = new GetSampleConfigurationAction.Request(randomValidTimeValue());
getRequest.indices(indexName);

GetSampleConfigurationAction.Response response = client().execute(GetSampleConfigurationAction.INSTANCE, getRequest).actionGet();

// Verify response returns null configuration
assertThat("Response should not be null", response, notNullValue());
assertThat("Index name should match", response.getIndex(), equalTo(indexName));
assertThat("Configuration should be null for index without config", response.getConfiguration(), nullValue());
}

public void testGetSampleConfigurationAfterUpdate() {
assumeTrue("Requires the sampling feature flag to be enabled", SamplingService.RANDOM_SAMPLING_FEATURE_FLAG);

String indexName = randomIdentifier();
createIndex(indexName);

// Create initial random configuration
SamplingConfiguration initialConfig = randomSamplingConfiguration();
putSamplingConfiguration(indexName, initialConfig);

// Get initial configuration
assertGetConfigurationMatches(indexName, initialConfig);

// Update with new random configuration
SamplingConfiguration updatedConfig = randomSamplingConfiguration();
putSamplingConfiguration(indexName, updatedConfig);

// Get and verify updated configuration
assertGetConfigurationMatches(indexName, updatedConfig);
}

public void testGetSampleConfigurationPersistsAcrossClusterStateUpdates() {
assumeTrue("Requires the sampling feature flag to be enabled", SamplingService.RANDOM_SAMPLING_FEATURE_FLAG);

String indexName = randomIdentifier();
createIndex(indexName);

// Store random sampling configuration
SamplingConfiguration config = randomSamplingConfiguration();
putSamplingConfiguration(indexName, config);

// Get initial configuration
assertGetConfigurationMatches(indexName, config);

// Trigger cluster state updates by creating additional indices with random names
int numDummyIndices = randomIntBetween(2, 5);
for (int i = 0; i < numDummyIndices; i++) {
createIndex(randomIdentifier());
}

// Get configuration again after cluster state changes and verify it persists
assertGetConfigurationMatches(indexName, config);
}

private SamplingConfiguration randomSamplingConfiguration() {
return new SamplingConfiguration(
randomDoubleBetween(0.1, 1.0, true),
randomBoolean() ? randomIntBetween(1, SamplingConfiguration.MAX_SAMPLES_LIMIT) : null,
randomBoolean() ? ByteSizeValue.ofMb(randomIntBetween(1, 100)) : null,
randomBoolean() ? randomValidTimeValue() : null,
randomBoolean() ? randomAlphaOfLengthBetween(5, 30) : null
);
}

private void putSamplingConfiguration(String indexName, SamplingConfiguration config) {
PutSampleConfigurationAction.Request putRequest = new PutSampleConfigurationAction.Request(
config,
randomValidTimeValue(),
randomValidTimeValue()
);
putRequest.indices(indexName);
client().execute(PutSampleConfigurationAction.INSTANCE, putRequest).actionGet();
ensureGreen();
}

private void assertGetConfigurationMatches(String indexName, SamplingConfiguration expectedConfig) {
GetSampleConfigurationAction.Request getRequest = new GetSampleConfigurationAction.Request(randomValidTimeValue());
getRequest.indices(indexName);

GetSampleConfigurationAction.Response response = client().execute(GetSampleConfigurationAction.INSTANCE, getRequest).actionGet();

assertThat("Response should not be null", response, notNullValue());
assertThat("Index name should match", response.getIndex(), equalTo(indexName));
assertThat("Configuration should match", response.getConfiguration(), equalTo(expectedConfig));
}

private TimeValue randomValidTimeValue() {
return TimeValue.timeValueDays(randomIntBetween(10, 20));
}
}
Loading