Skip to content

Commit 2e4f455

Browse files
committed
respond to PR comments
1 parent 2cd6b62 commit 2e4f455

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/sampling/RestPutSampleConfigurationAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
7777
// Set the target index
7878
putRequest.indices(indexNames);
7979

80+
// TODO: Make this cancellable e.g. RestGetSampleStatsAction
8081
return channel -> client.execute(PutSampleConfigurationAction.INSTANCE, putRequest, new RestToXContentListener<>(channel));
8182
}
8283

server/src/main/java/org/elasticsearch/ingest/SamplingService.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public SamplingService(
100100
this.updateSamplingConfigurationTaskQueue = clusterService.createTaskQueue(
101101
"update-sampling-configuration",
102102
Priority.NORMAL,
103-
new UpdateSamplingConfigurationExecutor(projectResolver, this)
103+
new UpdateSamplingConfigurationExecutor()
104104
);
105105
}
106106

@@ -293,9 +293,10 @@ public void updateSampleConfiguration(
293293
ActionListener<AcknowledgedResponse> listener
294294
) {
295295
// Early validation: check if adding a new configuration would exceed the limit
296-
boolean maxConfigLimitBreached = checkMaxConfigLimitBreached(projectId, index);
296+
ClusterState clusterState = clusterService.state();
297+
boolean maxConfigLimitBreached = checkMaxConfigLimitBreached(projectId, index, clusterState);
297298
if (maxConfigLimitBreached) {
298-
Integer maxConfigurations = MAX_CONFIGURATIONS_SETTING.get(clusterService.state().getMetadata().settings());
299+
Integer maxConfigurations = MAX_CONFIGURATIONS_SETTING.get(clusterState.getMetadata().settings());
299300
listener.onFailure(
300301
new IllegalStateException(
301302
"Cannot add sampling configuration for index ["
@@ -349,8 +350,7 @@ private static Script getScript(String conditional) throws IOException {
349350

350351
// Checks whether the maximum number of sampling configurations has been reached for the given project.
351352
// If the limit is breached, it notifies the listener with an IllegalStateException and returns true.
352-
private boolean checkMaxConfigLimitBreached(ProjectId projectId, String index) {
353-
ClusterState currentState = clusterService.state();
353+
private static boolean checkMaxConfigLimitBreached(ProjectId projectId, String index, ClusterState currentState) {
354354
Metadata currentMetadata = currentState.metadata();
355355
ProjectMetadata projectMetadata = currentMetadata.getProject(projectId);
356356

@@ -789,13 +789,8 @@ static class UpdateSamplingConfigurationTask extends AckedBatchedClusterStateUpd
789789

790790
static class UpdateSamplingConfigurationExecutor extends SimpleBatchedAckListenerTaskExecutor<UpdateSamplingConfigurationTask> {
791791
private static final Logger logger = LogManager.getLogger(UpdateSamplingConfigurationExecutor.class);
792-
private final ProjectResolver projectResolver;
793-
private final SamplingService samplingService;
794792

795-
UpdateSamplingConfigurationExecutor(ProjectResolver projectResolver, SamplingService samplingService) {
796-
this.projectResolver = projectResolver;
797-
this.samplingService = samplingService;
798-
}
793+
UpdateSamplingConfigurationExecutor() {}
799794

800795
@Override
801796
public Tuple<ClusterState, ClusterStateAckListener> executeTask(
@@ -836,9 +831,10 @@ public Tuple<ClusterState, ClusterStateAckListener> executeTask(
836831

837832
Integer maxConfigurations = MAX_CONFIGURATIONS_SETTING.get(metadata.settings());
838833
// check if adding a new configuration would exceed the limit
839-
boolean maxConfigLimitBreached = samplingService.checkMaxConfigLimitBreached(
834+
boolean maxConfigLimitBreached = checkMaxConfigLimitBreached(
840835
updateSamplingConfigurationTask.projectId,
841-
updateSamplingConfigurationTask.indexName
836+
updateSamplingConfigurationTask.indexName,
837+
clusterState
842838
);
843839
if (maxConfigLimitBreached) {
844840
throw new IllegalStateException(

server/src/test/java/org/elasticsearch/ingest/UpdateSamplingConfigurationExecutorTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class UpdateSamplingConfigurationExecutorTests extends ESTestCase {
5252
@Mock
5353
private Metadata metadata;
5454

55-
private SamplingService samplingService;
5655
private SamplingService.UpdateSamplingConfigurationExecutor executor;
5756

5857
@Override
@@ -61,8 +60,7 @@ public void setUp() throws Exception {
6160
MockitoAnnotations.openMocks(this);
6261

6362
// Create a real SamplingService with mocked dependencies
64-
samplingService = new SamplingService(scriptService, clusterService, projectResolver, timeSupplier);
65-
executor = new SamplingService.UpdateSamplingConfigurationExecutor(projectResolver, samplingService);
63+
executor = new SamplingService.UpdateSamplingConfigurationExecutor();
6664

6765
// Set up settings with MAX_CONFIGURATIONS_SETTING = 100 (the default value)
6866
Settings settings = Settings.builder().put("sampling.max_configurations", 100).build();

0 commit comments

Comments
 (0)