Skip to content

Commit ca4bf1a

Browse files
authored
Make UpdateSettingsClusterStateUpdateRequest a record (#113450)
No need to extend `IndicesClusterStateUpdateRequest`, this thing can be completely immutable.
1 parent 806a72a commit ca4bf1a

File tree

7 files changed

+141
-161
lines changed

7 files changed

+141
-161
lines changed

server/src/internalClusterTest/java/org/elasticsearch/cluster/metadata/MetadataUpdateSettingsServiceIT.java

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.concurrent.atomic.AtomicBoolean;
3030
import java.util.concurrent.atomic.AtomicReference;
31+
import java.util.function.Function;
3132

3233
import static org.hamcrest.Matchers.equalTo;
3334

@@ -42,45 +43,58 @@ public void testThatNonDynamicSettingChangesTakeEffect() throws Exception {
4243
MetadataUpdateSettingsService metadataUpdateSettingsService = internalCluster().getCurrentMasterNodeInstance(
4344
MetadataUpdateSettingsService.class
4445
);
45-
UpdateSettingsClusterStateUpdateRequest request = new UpdateSettingsClusterStateUpdateRequest().ackTimeout(TimeValue.ZERO);
46-
List<Index> indices = new ArrayList<>();
46+
List<Index> indicesList = new ArrayList<>();
4747
for (IndicesService indicesService : internalCluster().getInstances(IndicesService.class)) {
4848
for (IndexService indexService : indicesService) {
49-
indices.add(indexService.index());
49+
indicesList.add(indexService.index());
5050
}
5151
}
52-
request.indices(indices.toArray(Index.EMPTY_ARRAY));
53-
request.settings(Settings.builder().put("index.codec", "FastDecompressionCompressingStoredFieldsData").build());
52+
final var indices = indicesList.toArray(Index.EMPTY_ARRAY);
53+
54+
final Function<UpdateSettingsClusterStateUpdateRequest.OnStaticSetting, UpdateSettingsClusterStateUpdateRequest> requestFactory =
55+
onStaticSetting -> new UpdateSettingsClusterStateUpdateRequest(
56+
TEST_REQUEST_TIMEOUT,
57+
TimeValue.ZERO,
58+
Settings.builder().put("index.codec", "FastDecompressionCompressingStoredFieldsData").build(),
59+
UpdateSettingsClusterStateUpdateRequest.OnExisting.OVERWRITE,
60+
onStaticSetting,
61+
indices
62+
);
5463

5564
// First make sure it fails if reopenShards is not set on the request:
5665
AtomicBoolean expectedFailureOccurred = new AtomicBoolean(false);
57-
metadataUpdateSettingsService.updateSettings(request, new ActionListener<>() {
58-
@Override
59-
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
60-
fail("Should have failed updating a non-dynamic setting without reopenShards set to true");
61-
}
66+
metadataUpdateSettingsService.updateSettings(
67+
requestFactory.apply(UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REJECT),
68+
new ActionListener<>() {
69+
@Override
70+
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
71+
fail("Should have failed updating a non-dynamic setting without reopenShards set to true");
72+
}
6273

63-
@Override
64-
public void onFailure(Exception e) {
65-
expectedFailureOccurred.set(true);
74+
@Override
75+
public void onFailure(Exception e) {
76+
expectedFailureOccurred.set(true);
77+
}
6678
}
67-
});
79+
);
6880
assertBusy(() -> assertThat(expectedFailureOccurred.get(), equalTo(true)));
6981

7082
// Now we set reopenShards and expect it to work:
71-
request.reopenShards(true);
7283
AtomicBoolean success = new AtomicBoolean(false);
73-
metadataUpdateSettingsService.updateSettings(request, new ActionListener<>() {
74-
@Override
75-
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
76-
success.set(true);
77-
}
84+
metadataUpdateSettingsService.updateSettings(
85+
requestFactory.apply(UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REOPEN_INDICES),
86+
new ActionListener<>() {
87+
@Override
88+
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
89+
success.set(true);
90+
}
7891

79-
@Override
80-
public void onFailure(Exception e) {
81-
fail(e);
92+
@Override
93+
public void onFailure(Exception e) {
94+
fail(e);
95+
}
8296
}
83-
});
97+
);
8498
assertBusy(() -> assertThat(success.get(), equalTo(true)));
8599

86100
// Now we look into the IndexShard objects to make sure that the code was actually updated (vs just the setting):
@@ -110,16 +124,23 @@ public void testThatNonDynamicSettingChangesDoNotUnncessesarilyCauseReopens() th
110124
MetadataUpdateSettingsService metadataUpdateSettingsService = internalCluster().getCurrentMasterNodeInstance(
111125
MetadataUpdateSettingsService.class
112126
);
113-
UpdateSettingsClusterStateUpdateRequest request = new UpdateSettingsClusterStateUpdateRequest().ackTimeout(TimeValue.ZERO);
114-
List<Index> indices = new ArrayList<>();
127+
List<Index> indicesList = new ArrayList<>();
115128
for (IndicesService indicesService : internalCluster().getInstances(IndicesService.class)) {
116129
for (IndexService indexService : indicesService) {
117-
indices.add(indexService.index());
130+
indicesList.add(indexService.index());
118131
}
119132
}
120-
request.indices(indices.toArray(Index.EMPTY_ARRAY));
121-
request.settings(Settings.builder().put("index.codec", "FastDecompressionCompressingStoredFieldsData").build());
122-
request.reopenShards(true);
133+
final var indices = indicesList.toArray(Index.EMPTY_ARRAY);
134+
135+
final Function<Settings.Builder, UpdateSettingsClusterStateUpdateRequest> requestFactory =
136+
settings -> new UpdateSettingsClusterStateUpdateRequest(
137+
TEST_REQUEST_TIMEOUT,
138+
TimeValue.ZERO,
139+
settings.build(),
140+
UpdateSettingsClusterStateUpdateRequest.OnExisting.OVERWRITE,
141+
UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REOPEN_INDICES,
142+
indices
143+
);
123144

124145
ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
125146
AtomicBoolean shardsUnassigned = new AtomicBoolean(false);
@@ -142,47 +163,49 @@ public void testThatNonDynamicSettingChangesDoNotUnncessesarilyCauseReopens() th
142163

143164
AtomicBoolean success = new AtomicBoolean(false);
144165
// Make the first request, just to set things up:
145-
metadataUpdateSettingsService.updateSettings(request, new ActionListener<>() {
146-
@Override
147-
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
148-
success.set(true);
149-
}
166+
metadataUpdateSettingsService.updateSettings(
167+
requestFactory.apply(Settings.builder().put("index.codec", "FastDecompressionCompressingStoredFieldsData")),
168+
new ActionListener<>() {
169+
@Override
170+
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
171+
success.set(true);
172+
}
150173

151-
@Override
152-
public void onFailure(Exception e) {
153-
fail(e);
174+
@Override
175+
public void onFailure(Exception e) {
176+
fail(e);
177+
}
154178
}
155-
});
179+
);
156180
assertBusy(() -> assertThat(success.get(), equalTo(true)));
157181
assertBusy(() -> assertThat(expectedSettingsChangeInClusterState.get(), equalTo(true)));
158182
assertThat(shardsUnassigned.get(), equalTo(true));
159183

160184
assertBusy(() -> assertThat(hasUnassignedShards(clusterService.state(), indexName), equalTo(false)));
161185

162-
// Same request, except now we'll also set the dynamic "index.max_result_window" setting:
163-
request.settings(
164-
Settings.builder()
165-
.put("index.codec", "FastDecompressionCompressingStoredFieldsData")
166-
.put("index.max_result_window", "1500")
167-
.build()
168-
);
169186
success.set(false);
170187
expectedSettingsChangeInClusterState.set(false);
171188
shardsUnassigned.set(false);
172189
expectedSetting.set("index.max_result_window");
173190
expectedSettingValue.set("1500");
174191
// Making this request ought to add this new setting but not unassign the shards:
175-
metadataUpdateSettingsService.updateSettings(request, new ActionListener<>() {
176-
@Override
177-
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
178-
success.set(true);
179-
}
192+
metadataUpdateSettingsService.updateSettings(
193+
// Same request, except now we'll also set the dynamic "index.max_result_window" setting:
194+
requestFactory.apply(
195+
Settings.builder().put("index.codec", "FastDecompressionCompressingStoredFieldsData").put("index.max_result_window", "1500")
196+
),
197+
new ActionListener<>() {
198+
@Override
199+
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
200+
success.set(true);
201+
}
180202

181-
@Override
182-
public void onFailure(Exception e) {
183-
fail(e);
203+
@Override
204+
public void onFailure(Exception e) {
205+
fail(e);
206+
}
184207
}
185-
});
208+
);
186209

187210
assertBusy(() -> assertThat(success.get(), equalTo(true)));
188211
assertBusy(() -> assertThat(expectedSettingsChangeInClusterState.get(), equalTo(true)));

server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/TransportUpdateSettingsAction.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,24 @@ protected void masterOperation(
124124
return;
125125
}
126126

127-
UpdateSettingsClusterStateUpdateRequest clusterStateUpdateRequest = new UpdateSettingsClusterStateUpdateRequest().indices(
128-
concreteIndices
129-
)
130-
.settings(requestSettings)
131-
.setPreserveExisting(request.isPreserveExisting())
132-
.reopenShards(request.reopen())
133-
.ackTimeout(request.ackTimeout())
134-
.masterNodeTimeout(request.masterNodeTimeout());
135-
136-
updateSettingsService.updateSettings(clusterStateUpdateRequest, listener.delegateResponse((l, e) -> {
137-
logger.debug(() -> "failed to update settings on indices [" + Arrays.toString(concreteIndices) + "]", e);
138-
l.onFailure(e);
139-
}));
127+
updateSettingsService.updateSettings(
128+
new UpdateSettingsClusterStateUpdateRequest(
129+
request.masterNodeTimeout(),
130+
request.ackTimeout(),
131+
requestSettings,
132+
request.isPreserveExisting()
133+
? UpdateSettingsClusterStateUpdateRequest.OnExisting.PRESERVE
134+
: UpdateSettingsClusterStateUpdateRequest.OnExisting.OVERWRITE,
135+
request.reopen()
136+
? UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REOPEN_INDICES
137+
: UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REJECT,
138+
concreteIndices
139+
),
140+
listener.delegateResponse((l, e) -> {
141+
logger.debug(() -> "failed to update settings on indices [" + Arrays.toString(concreteIndices) + "]", e);
142+
l.onFailure(e);
143+
})
144+
);
140145
}
141146

142147
/**

server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsClusterStateUpdateRequest.java

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@
99

1010
package org.elasticsearch.action.admin.indices.settings.put;
1111

12-
import org.elasticsearch.cluster.ack.IndicesClusterStateUpdateRequest;
1312
import org.elasticsearch.common.settings.Settings;
1413
import org.elasticsearch.core.TimeValue;
1514
import org.elasticsearch.index.Index;
1615

17-
import java.util.Arrays;
16+
import java.util.Objects;
1817

1918
/**
2019
* Cluster state update request that allows to update settings for some indices
2120
*/
22-
public class UpdateSettingsClusterStateUpdateRequest extends IndicesClusterStateUpdateRequest<UpdateSettingsClusterStateUpdateRequest> {
21+
public record UpdateSettingsClusterStateUpdateRequest(
22+
TimeValue masterNodeTimeout,
23+
TimeValue ackTimeout,
24+
Settings settings,
25+
OnExisting onExisting,
26+
OnStaticSetting onStaticSetting,
27+
Index... indices
28+
) {
2329

2430
/**
2531
* Specifies the behaviour of an update-settings action on existing settings.
@@ -53,79 +59,10 @@ public enum OnStaticSetting {
5359
REOPEN_INDICES
5460
}
5561

56-
private Settings settings;
57-
58-
private boolean preserveExisting = false;
59-
60-
private boolean reopenShards = false;
61-
62-
public UpdateSettingsClusterStateUpdateRequest() {}
63-
64-
@SuppressWarnings("this-escape")
65-
public UpdateSettingsClusterStateUpdateRequest(
66-
TimeValue masterNodeTimeout,
67-
TimeValue ackTimeout,
68-
Settings settings,
69-
OnExisting onExisting,
70-
OnStaticSetting onStaticSetting,
71-
Index... indices
72-
) {
73-
masterNodeTimeout(masterNodeTimeout);
74-
ackTimeout(ackTimeout);
75-
settings(settings);
76-
setPreserveExisting(onExisting == OnExisting.PRESERVE);
77-
reopenShards(onStaticSetting == OnStaticSetting.REOPEN_INDICES);
78-
indices(indices);
79-
}
80-
81-
/**
82-
* Returns <code>true</code> iff the settings update should only add but not update settings. If the setting already exists
83-
* it should not be overwritten by this update. The default is <code>false</code>
84-
*/
85-
public boolean isPreserveExisting() {
86-
return preserveExisting;
87-
}
88-
89-
/**
90-
* Returns <code>true</code> if non-dynamic setting updates should go through, by automatically unassigning shards in the same cluster
91-
* state change as the setting update. The shards will be automatically reassigned after the cluster state update is made. The
92-
* default is <code>false</code>.
93-
*/
94-
public boolean reopenShards() {
95-
return reopenShards;
96-
}
97-
98-
public UpdateSettingsClusterStateUpdateRequest reopenShards(boolean reopenShards) {
99-
this.reopenShards = reopenShards;
100-
return this;
101-
}
102-
103-
/**
104-
* Iff set to <code>true</code> this settings update will only add settings not already set on an index. Existing settings remain
105-
* unchanged.
106-
*/
107-
public UpdateSettingsClusterStateUpdateRequest setPreserveExisting(boolean preserveExisting) {
108-
this.preserveExisting = preserveExisting;
109-
return this;
110-
}
111-
112-
/**
113-
* Returns the {@link Settings} to update
114-
*/
115-
public Settings settings() {
116-
return settings;
117-
}
118-
119-
/**
120-
* Sets the {@link Settings} to update
121-
*/
122-
public UpdateSettingsClusterStateUpdateRequest settings(Settings settings) {
123-
this.settings = settings;
124-
return this;
125-
}
126-
127-
@Override
128-
public String toString() {
129-
return Arrays.toString(indices()) + settings;
62+
public UpdateSettingsClusterStateUpdateRequest {
63+
Objects.requireNonNull(masterNodeTimeout);
64+
Objects.requireNonNull(ackTimeout);
65+
Objects.requireNonNull(settings);
66+
Objects.requireNonNull(indices);
13067
}
13168
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ ClusterState execute(ClusterState currentState) {
176176
}
177177
final Settings closedSettings = settingsForClosedIndices.build();
178178
final Settings openSettings = settingsForOpenIndices.build();
179-
final boolean preserveExisting = request.isPreserveExisting();
179+
final boolean preserveExisting = request.onExisting() == UpdateSettingsClusterStateUpdateRequest.OnExisting.PRESERVE;
180180

181181
RoutingTable.Builder routingTableBuilder = null;
182182
Metadata.Builder metadataBuilder = Metadata.builder(currentState.metadata());
@@ -199,7 +199,7 @@ ClusterState execute(ClusterState currentState) {
199199
}
200200

201201
if (skippedSettings.isEmpty() == false && openIndices.isEmpty() == false) {
202-
if (request.reopenShards()) {
202+
if (request.onStaticSetting() == UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REOPEN_INDICES) {
203203
// We have non-dynamic settings and open indices. We will unassign all of the shards in these indices so that the new
204204
// changed settings are applied when the shards are re-assigned.
205205
routingTableBuilder = RoutingTable.builder(

server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,18 @@ private CheckedBiConsumer<ActionListener<BulkByScrollResponse>, AcknowledgedResp
544544
*/
545545
private void setWriteBlock(Index index, boolean readOnlyValue, ActionListener<AcknowledgedResponse> listener) {
546546
final Settings readOnlySettings = Settings.builder().put(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey(), readOnlyValue).build();
547-
UpdateSettingsClusterStateUpdateRequest updateSettingsRequest = new UpdateSettingsClusterStateUpdateRequest().indices(
548-
new Index[] { index }
549-
).settings(readOnlySettings).setPreserveExisting(false).ackTimeout(TimeValue.ZERO);
550547

551-
metadataUpdateSettingsService.updateSettings(updateSettingsRequest, listener);
548+
metadataUpdateSettingsService.updateSettings(
549+
new UpdateSettingsClusterStateUpdateRequest(
550+
MasterNodeRequest.INFINITE_MASTER_NODE_TIMEOUT,
551+
TimeValue.ZERO,
552+
readOnlySettings,
553+
UpdateSettingsClusterStateUpdateRequest.OnExisting.OVERWRITE,
554+
UpdateSettingsClusterStateUpdateRequest.OnStaticSetting.REJECT,
555+
index
556+
),
557+
listener
558+
);
552559
}
553560

554561
private void reindex(SystemIndexMigrationInfo migrationInfo, ActionListener<BulkByScrollResponse> listener) {

0 commit comments

Comments
 (0)