Skip to content

Commit 051d7f0

Browse files
committed
pulling out UpdateSingleIndexSettingsListener into its own class
1 parent 67c4177 commit 051d7f0

File tree

2 files changed

+52
-59
lines changed

2 files changed

+52
-59
lines changed

modules/data-streams/src/main/java/org/elasticsearch/datastreams/action/TransportUpdateDataStreamSettingsAction.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private void updateSettingsOnSingleIndex(
324324
if (dryRun) {
325325
/*
326326
* This is as far as we go with dry run mode. We get the benefit of having checked that all the indices that will be touced
327-
* are not blocked, but there is no value in going behond this. So just respond to the listener and move on.
327+
* are not blocked, but there is no value in going beyond this. So just respond to the listener and move on.
328328
*/
329329
listener.onResponse(null);
330330
} else {
@@ -338,35 +338,37 @@ private void updateSettingsOnSingleIndex(
338338
UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REOPEN_INDICES,
339339
index
340340
),
341-
new ActionListener<>() {
342-
@Override
343-
public void onResponse(AcknowledgedResponse response) {
344-
UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError error;
345-
if (response.isAcknowledged() == false) {
346-
error = new UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError(
347-
index.getName(),
348-
"Updating settings not acknowledged for unknown reason"
349-
);
350-
} else {
351-
error = null;
352-
}
353-
listener.onResponse(error);
354-
}
341+
new UpdateSingleIndexSettingsListener(index.getName(), listener)
342+
);
343+
}
344+
}
355345

356-
@Override
357-
public void onFailure(Exception e) {
358-
listener.onResponse(
359-
new UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError(
360-
index.getName(),
361-
e.getMessage()
362-
)
363-
);
364-
}
365-
}
346+
}
347+
348+
private record UpdateSingleIndexSettingsListener(
349+
String indexName,
350+
ActionListener<UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError> delegateListener
351+
) implements ActionListener<AcknowledgedResponse> {
352+
@Override
353+
public void onResponse(AcknowledgedResponse response) {
354+
UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError error;
355+
if (response.isAcknowledged() == false) {
356+
error = new UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError(
357+
indexName,
358+
"Updating settings not acknowledged for unknown reason"
366359
);
360+
} else {
361+
error = null;
367362
}
363+
delegateListener.onResponse(error);
368364
}
369365

366+
@Override
367+
public void onFailure(Exception e) {
368+
delegateListener.onResponse(
369+
new UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndexSettingError(indexName, e.getMessage())
370+
);
371+
}
370372
}
371373

372374
@Override

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataDataStreamsService.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,16 @@ public Tuple<ClusterState, ClusterStateAckListener> executeTask(
151151
updateSettingsTask.settingsOverrides,
152152
clusterState
153153
);
154-
ProjectMetadata projectMetadata = clusterState.metadata().getProject(updateSettingsTask.projectId);
155-
ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata);
156-
projectMetadataBuilder.removeDataStream(updateSettingsTask.dataStreamName);
157-
projectMetadataBuilder.put(dataStream);
158-
ClusterState updatedClusterState = ClusterState.builder(clusterState).putProjectMetadata(projectMetadataBuilder).build();
154+
final ClusterState updatedClusterState;
155+
if (updateSettingsTask.dryRun) {
156+
updatedClusterState = clusterState;
157+
} else {
158+
ProjectMetadata projectMetadata = clusterState.metadata().getProject(updateSettingsTask.projectId);
159+
ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata);
160+
projectMetadataBuilder.removeDataStream(updateSettingsTask.dataStreamName);
161+
projectMetadataBuilder.put(dataStream);
162+
updatedClusterState = ClusterState.builder(clusterState).putProjectMetadata(projectMetadataBuilder).build();
163+
}
159164
return new Tuple<>(updatedClusterState, updateSettingsTask);
160165
}
161166
};
@@ -410,33 +415,16 @@ public void updateSettings(
410415
boolean dryRun,
411416
ActionListener<DataStream> listener
412417
) {
413-
if (dryRun) {
414-
/*
415-
* If this is a dry run, we'll do the settings validation and apply the changes to the data stream locally, but we won't run
416-
* the task that actually updates the cluster state.
417-
*/
418-
try {
419-
DataStream updatedDataStream = createDataStreamForUpdatedDataStreamSettings(
420-
projectId,
421-
dataStreamName,
422-
settingsOverrides,
423-
clusterService.state()
424-
);
425-
listener.onResponse(updatedDataStream);
426-
} catch (Exception e) {
427-
listener.onFailure(e);
428-
}
429-
} else {
430-
UpdateSettingsTask updateSettingsTask = new UpdateSettingsTask(
431-
projectId,
432-
dataStreamName,
433-
settingsOverrides,
434-
clusterService,
435-
ackTimeout,
436-
listener
437-
);
438-
updateSettingsTaskQueue.submitTask("updating settings on data stream", updateSettingsTask, masterNodeTimeout);
439-
}
418+
UpdateSettingsTask updateSettingsTask = new UpdateSettingsTask(
419+
projectId,
420+
dataStreamName,
421+
settingsOverrides,
422+
dryRun,
423+
clusterService,
424+
ackTimeout,
425+
listener
426+
);
427+
updateSettingsTaskQueue.submitTask("updating settings on data stream", updateSettingsTask, masterNodeTimeout);
440428
}
441429

442430
/*
@@ -562,8 +550,8 @@ private static IndexAbstraction validateIndex(ProjectMetadata project, String in
562550
* Removes the given data stream and their backing indices from the Project State.
563551
*
564552
* @param projectState The project state
565-
* @param dataStreams The data streams to remove
566-
* @param settings The settings
553+
* @param dataStreams The data streams to remove
554+
* @param settings The settings
567555
* @return The updated Project State
568556
*/
569557
public static ClusterState deleteDataStreams(ProjectState projectState, Set<DataStream> dataStreams, Settings settings) {
@@ -719,11 +707,13 @@ static class UpdateSettingsTask extends AckedBatchedClusterStateUpdateTask {
719707
final ProjectId projectId;
720708
private final String dataStreamName;
721709
private final Settings settingsOverrides;
710+
private final boolean dryRun;
722711

723712
UpdateSettingsTask(
724713
ProjectId projectId,
725714
String dataStreamName,
726715
Settings settingsOverrides,
716+
boolean dryRun,
727717
ClusterService clusterService,
728718
TimeValue ackTimeout,
729719
ActionListener<DataStream> listener
@@ -738,6 +728,7 @@ static class UpdateSettingsTask extends AckedBatchedClusterStateUpdateTask {
738728
this.projectId = projectId;
739729
this.dataStreamName = dataStreamName;
740730
this.settingsOverrides = settingsOverrides;
731+
this.dryRun = dryRun;
741732
}
742733
}
743734
}

0 commit comments

Comments
 (0)