Skip to content

Commit 240d1a9

Browse files
Move project settings to cluster state custom
1 parent a0ec70a commit 240d1a9

File tree

7 files changed

+162
-90
lines changed

7 files changed

+162
-90
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ private ClusterStateResponse buildResponse(final ClusterStateRequest request, fi
201201
ClusterState.Builder builder = ClusterState.builder(currentState.getClusterName());
202202
builder.version(currentState.version());
203203
builder.stateUUID(currentState.stateUUID());
204-
builder.projectsSettings(currentState.projectsSettings());
205204

206205
if (request.nodes()) {
207206
builder.nodes(currentState.nodes());

server/src/main/java/org/elasticsearch/cluster/ClusterModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.cluster.metadata.NodesShutdownMetadata;
2828
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
2929
import org.elasticsearch.cluster.project.ProjectResolver;
30+
import org.elasticsearch.cluster.project.ProjectsStateRegistry;
3031
import org.elasticsearch.cluster.routing.DelayedAllocationService;
3132
import org.elasticsearch.cluster.routing.ShardRouting;
3233
import org.elasticsearch.cluster.routing.ShardRoutingRoleStrategy;
@@ -291,6 +292,7 @@ public static List<Entry> getNamedWriteables() {
291292
RegisteredPolicySnapshots::new,
292293
RegisteredPolicySnapshots.RegisteredSnapshotsDiff::new
293294
);
295+
registerClusterCustom(entries, ProjectsStateRegistry.TYPE, ProjectsStateRegistry::new, ProjectsStateRegistry::readDiffFrom);
294296
// Secrets
295297
registerClusterCustom(entries, ClusterSecrets.TYPE, ClusterSecrets::new, ClusterSecrets::readDiffFrom);
296298
registerProjectCustom(entries, ProjectSecrets.TYPE, ProjectSecrets::new, ProjectSecrets::readDiffFrom);

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ public CompatibilityVersions read(StreamInput in, String key) throws IOException
193193

194194
private final boolean wasReadFromDiff;
195195

196-
private final Map<ProjectId, Settings> projectsSettings;
197-
198196
// built on demand
199197
private volatile RoutingNodes routingNodes;
200198

@@ -211,7 +209,6 @@ public ClusterState(long version, String stateUUID, ClusterState state) {
211209
state.blocks(),
212210
state.customs(),
213211
false,
214-
state.projectsSettings,
215212
state.routingNodes
216213
);
217214
}
@@ -228,7 +225,6 @@ public ClusterState(
228225
ClusterBlocks blocks,
229226
Map<String, Custom> customs,
230227
boolean wasReadFromDiff,
231-
Map<ProjectId, Settings> projectsSettings,
232228
@Nullable RoutingNodes routingNodes
233229
) {
234230
this.version = version;
@@ -242,7 +238,6 @@ public ClusterState(
242238
this.blocks = blocks;
243239
this.customs = customs;
244240
this.wasReadFromDiff = wasReadFromDiff;
245-
this.projectsSettings = projectsSettings;
246241
this.routingNodes = routingNodes;
247242
assert assertConsistentRoutingNodes(routingTable, nodes, routingNodes);
248243
assert assertConsistentProjectState(routingTable, metadata);
@@ -410,14 +405,6 @@ public RoutingTable routingTable(ProjectId projectId) {
410405
return routingTable.routingTable(projectId);
411406
}
412407

413-
public Settings projectSettings(ProjectId projectId) {
414-
return projectsSettings.getOrDefault(projectId, Settings.EMPTY);
415-
}
416-
417-
public Map<ProjectId, Settings> projectsSettings() {
418-
return projectsSettings;
419-
}
420-
421408
@Deprecated(forRemoval = true)
422409
public RoutingTable routingTable() {
423410
return routingTable.getRoutingTable();
@@ -686,8 +673,7 @@ public enum Metric {
686673
METADATA("metadata"),
687674
ROUTING_TABLE("routing_table"),
688675
ROUTING_NODES("routing_nodes"),
689-
CUSTOMS("customs"),
690-
PROJECTS_SETTINGS("projects_settings");
676+
CUSTOMS("customs");
691677

692678
private static final Map<String, Metric> valueToEnum;
693679

@@ -864,22 +850,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
864850
customs.entrySet().iterator(),
865851
e -> ChunkedToXContentHelper.object(e.getKey(), e.getValue().toXContentChunked(outerParams))
866852
)
867-
: Collections.emptyIterator(),
868-
869-
chunkedSection(
870-
multiProject && metrics.contains(Metric.PROJECTS_SETTINGS),
871-
(builder, params) -> builder.startArray("projects_settings"),
872-
projectsSettings.entrySet().iterator(),
873-
874-
entry -> Iterators.single((builder, params) -> {
875-
builder.startObject();
876-
builder.field("id", entry.getKey());
877-
builder.startObject("settings");
878-
entry.getValue().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true")));
879-
return builder.endObject().endObject();
880-
}),
881-
(builder, params) -> builder.endArray()
882-
)
853+
: Collections.emptyIterator()
883854
);
884855
}
885856

@@ -1057,7 +1028,6 @@ public static class Builder {
10571028
private final Map<String, Set<String>> nodeFeatures;
10581029
private ClusterBlocks blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
10591030
private final ImmutableOpenMap.Builder<String, Custom> customs;
1060-
private final ImmutableOpenMap.Builder<ProjectId, Settings> projectsSettings;
10611031
private boolean fromDiff;
10621032

10631033
public Builder(ClusterState state) {
@@ -1073,14 +1043,12 @@ public Builder(ClusterState state) {
10731043
this.blocks = state.blocks();
10741044
this.customs = ImmutableOpenMap.builder(state.customs());
10751045
this.fromDiff = false;
1076-
this.projectsSettings = ImmutableOpenMap.builder(state.projectsSettings);
10771046
}
10781047

10791048
public Builder(ClusterName clusterName) {
10801049
this.compatibilityVersions = new HashMap<>();
10811050
this.nodeFeatures = new HashMap<>();
1082-
this.projectsSettings = ImmutableOpenMap.builder();
1083-
customs = ImmutableOpenMap.builder();
1051+
this.customs = ImmutableOpenMap.builder();
10841052
this.clusterName = clusterName;
10851053
}
10861054

@@ -1184,16 +1152,6 @@ public Builder putRoutingTable(ProjectId projectId, RoutingTable routingTable) {
11841152
return routingTable(globalRoutingTableBuilder.put(projectId, routingTable).build());
11851153
}
11861154

1187-
public Builder putProjectSettings(ProjectId projectId, Settings settings) {
1188-
projectsSettings.put(projectId, settings);
1189-
return this;
1190-
}
1191-
1192-
public Builder projectsSettings(Map<ProjectId, Settings> projectsSettings) {
1193-
this.projectsSettings.putAllFromMap(projectsSettings);
1194-
return this;
1195-
}
1196-
11971155
public Builder metadata(Metadata.Builder metadataBuilder) {
11981156
return metadata(metadataBuilder.build());
11991157
}
@@ -1298,7 +1256,6 @@ public ClusterState build() {
12981256
metadata != null ? blocks.initializeProjects(metadata.projects().keySet()) : blocks,
12991257
customs.build(),
13001258
fromDiff,
1301-
projectsSettings.build(),
13021259
routingNodes
13031260
);
13041261
}
@@ -1350,9 +1307,6 @@ public static ClusterState readFrom(StreamInput in, DiscoveryNode localNode) thr
13501307
Custom customIndexMetadata = in.readNamedWriteable(Custom.class);
13511308
builder.putCustom(customIndexMetadata.getWriteableName(), customIndexMetadata);
13521309
}
1353-
if (in.getTransportVersion().onOrAfter(TransportVersions.CLUSTER_STATE_PROJECTS_SETTINGS)) {
1354-
builder.projectsSettings(in.readMap(ProjectId::readFrom, Settings::readSettingsFromStream));
1355-
}
13561310
return builder.build();
13571311
}
13581312

@@ -1374,9 +1328,6 @@ public void writeTo(StreamOutput out) throws IOException {
13741328
clusterFeatures.writeTo(out);
13751329
blocks.writeTo(out);
13761330
VersionedNamedWriteable.writeVersionedWritables(out, customs);
1377-
if (out.getTransportVersion().onOrAfter(TransportVersions.CLUSTER_STATE_PROJECTS_SETTINGS)) {
1378-
out.writeMap(projectsSettings);
1379-
}
13801331
}
13811332

13821333
private static class ClusterStateDiff implements Diff<ClusterState> {
@@ -1414,8 +1365,6 @@ public Diff<Settings> readDiff(StreamInput in, ProjectId key) throws IOException
14141365

14151366
private final Diff<Map<String, Custom>> customs;
14161367

1417-
private final DiffableUtils.MapDiff<ProjectId, Settings, Map<ProjectId, Settings>> projectsSettings;
1418-
14191368
ClusterStateDiff(ClusterState before, ClusterState after) {
14201369
fromUuid = before.stateUUID;
14211370
toUuid = after.stateUUID;
@@ -1433,12 +1382,6 @@ public Diff<Settings> readDiff(StreamInput in, ProjectId key) throws IOException
14331382
metadata = after.metadata.diff(before.metadata);
14341383
blocks = after.blocks.diff(before.blocks);
14351384
customs = DiffableUtils.diff(before.customs, after.customs, DiffableUtils.getStringKeySerializer(), CUSTOM_VALUE_SERIALIZER);
1436-
projectsSettings = DiffableUtils.diff(
1437-
before.projectsSettings,
1438-
after.projectsSettings,
1439-
ProjectId.PROJECT_ID_SERIALIZER,
1440-
SETTINGS_SERIALIZER
1441-
);
14421385
}
14431386

14441387
ClusterStateDiff(StreamInput in, DiscoveryNode localNode) throws IOException {
@@ -1455,11 +1398,6 @@ public Diff<Settings> readDiff(StreamInput in, ProjectId key) throws IOException
14551398
metadata = Metadata.readDiffFrom(in);
14561399
blocks = ClusterBlocks.readDiffFrom(in);
14571400
customs = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), CUSTOM_VALUE_SERIALIZER);
1458-
if (in.getTransportVersion().onOrAfter(TransportVersions.CLUSTER_STATE_PROJECTS_SETTINGS)) {
1459-
projectsSettings = DiffableUtils.readJdkMapDiff(in, ProjectId.PROJECT_ID_SERIALIZER, SETTINGS_SERIALIZER);
1460-
} else {
1461-
projectsSettings = DiffableUtils.emptyDiff();
1462-
}
14631401
}
14641402

14651403
@Override
@@ -1476,9 +1414,6 @@ public void writeTo(StreamOutput out) throws IOException {
14761414
metadata.writeTo(out);
14771415
blocks.writeTo(out);
14781416
customs.writeTo(out);
1479-
if (out.getTransportVersion().onOrAfter(TransportVersions.CLUSTER_STATE_PROJECTS_SETTINGS)) {
1480-
projectsSettings.writeTo(out);
1481-
}
14821417
}
14831418

14841419
@Override
@@ -1500,7 +1435,6 @@ public ClusterState apply(ClusterState state) {
15001435
builder.metadata(metadata.apply(state.metadata));
15011436
builder.blocks(blocks.apply(state.blocks));
15021437
builder.customs(customs.apply(state.customs));
1503-
builder.projectsSettings(this.projectsSettings.apply(state.projectsSettings));
15041438
builder.fromDiff(state);
15051439
return builder.build();
15061440
}

server/src/main/java/org/elasticsearch/cluster/ProjectState.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.cluster.block.ClusterBlocks;
1313
import org.elasticsearch.cluster.metadata.ProjectId;
1414
import org.elasticsearch.cluster.metadata.ProjectMetadata;
15+
import org.elasticsearch.cluster.project.ProjectsStateRegistry;
1516
import org.elasticsearch.cluster.routing.RoutingTable;
1617
import org.elasticsearch.common.Strings;
1718
import org.elasticsearch.common.settings.Settings;
@@ -36,7 +37,7 @@ public final class ProjectState {
3637
this.cluster = clusterState;
3738
this.project = projectId;
3839
this.projectMetadata = clusterState.metadata().getProject(projectId);
39-
this.projectSettings = clusterState.projectSettings(projectId);
40+
this.projectSettings = ProjectsStateRegistry.getProjectSettings(projectId, clusterState);
4041
this.routingTable = clusterState.routingTable(projectId);
4142
}
4243

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.cluster.project;
11+
12+
import org.elasticsearch.TransportVersion;
13+
import org.elasticsearch.TransportVersions;
14+
import org.elasticsearch.cluster.AbstractNamedDiffable;
15+
import org.elasticsearch.cluster.ClusterState;
16+
import org.elasticsearch.cluster.NamedDiff;
17+
import org.elasticsearch.cluster.metadata.ProjectId;
18+
import org.elasticsearch.common.collect.ImmutableOpenMap;
19+
import org.elasticsearch.common.collect.Iterators;
20+
import org.elasticsearch.common.io.stream.StreamInput;
21+
import org.elasticsearch.common.io.stream.StreamOutput;
22+
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.xcontent.ToXContent;
24+
25+
import java.io.IOException;
26+
import java.util.Collections;
27+
import java.util.Iterator;
28+
import java.util.Map;
29+
30+
public class ProjectsStateRegistry extends AbstractNamedDiffable<ClusterState.Custom> implements ClusterState.Custom {
31+
public static final String TYPE = "projects_registry";
32+
public static final ProjectsStateRegistry EMPTY = new ProjectsStateRegistry(Collections.emptyMap());
33+
34+
private final Map<ProjectId, Settings> projectsSettings;
35+
36+
public ProjectsStateRegistry(StreamInput in) throws IOException {
37+
projectsSettings = in.readMap(ProjectId::readFrom, Settings::readSettingsFromStream);
38+
}
39+
40+
public ProjectsStateRegistry(Map<ProjectId, Settings> projectsSettings) {
41+
this.projectsSettings = projectsSettings;
42+
}
43+
44+
public static Settings getProjectSettings(ProjectId projectId, ClusterState clusterState) {
45+
ProjectsStateRegistry registry = clusterState.custom(TYPE, EMPTY);
46+
return registry.projectsSettings.getOrDefault(projectId, Settings.EMPTY);
47+
}
48+
49+
@Override
50+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
51+
boolean multiProject = params.paramAsBoolean("multi-project", false);
52+
if (multiProject == false) {
53+
return Collections.emptyIterator();
54+
}
55+
56+
return Iterators.concat(
57+
Iterators.single((builder, p) -> builder.startArray("projects")),
58+
Iterators.map(projectsSettings.entrySet().iterator(), entry -> (builder, p) -> {
59+
builder.startObject();
60+
builder.field("id", entry.getKey());
61+
builder.startObject("settings");
62+
entry.getValue().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true")));
63+
builder.endObject();
64+
return builder.endObject();
65+
}),
66+
Iterators.single((builder, p) -> builder.endArray())
67+
);
68+
}
69+
70+
public static NamedDiff<ClusterState.Custom> readDiffFrom(StreamInput in) throws IOException {
71+
return readDiffFrom(ClusterState.Custom.class, TYPE, in);
72+
}
73+
74+
@Override
75+
public String getWriteableName() {
76+
return TYPE;
77+
}
78+
79+
@Override
80+
public TransportVersion getMinimalSupportedVersion() {
81+
return TransportVersions.CLUSTER_STATE_PROJECTS_SETTINGS;
82+
}
83+
84+
@Override
85+
public void writeTo(StreamOutput out) throws IOException {
86+
out.writeMap(projectsSettings);
87+
}
88+
89+
public int size() {
90+
return projectsSettings.size();
91+
}
92+
93+
/**
94+
* Returns a new instance of ProjectsStateRegistry containing all projects from the cluster state and settings for the specified project
95+
*/
96+
public static ProjectsStateRegistry setProjectSettings(ClusterState clusterState, ProjectId projectId, Settings settings) {
97+
return ProjectsStateRegistry.builder(clusterState).putProjectSettings(projectId, settings).build();
98+
}
99+
100+
public static Builder builder(ClusterState original) {
101+
ProjectsStateRegistry projectRegistry = original.custom(TYPE, EMPTY);
102+
return builder(projectRegistry);
103+
}
104+
105+
public static Builder builder(ProjectsStateRegistry projectRegistry) {
106+
return new Builder(projectRegistry);
107+
}
108+
109+
public static Builder builder() {
110+
return new Builder();
111+
}
112+
113+
public static class Builder {
114+
private final ImmutableOpenMap.Builder<ProjectId, Settings> projectsSettings;
115+
116+
private Builder() {
117+
this.projectsSettings = ImmutableOpenMap.builder();
118+
}
119+
120+
private Builder(ProjectsStateRegistry original) {
121+
this.projectsSettings = ImmutableOpenMap.builder(original.projectsSettings);
122+
}
123+
124+
public Builder putProjectSettings(ProjectId projectId, Settings settings) {
125+
projectsSettings.put(projectId, settings);
126+
return this;
127+
}
128+
129+
public ProjectsStateRegistry build() {
130+
return new ProjectsStateRegistry(projectsSettings.build());
131+
}
132+
}
133+
134+
}

server/src/main/java/org/elasticsearch/reservedstate/service/ReservedProjectStateUpdateTask.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.ClusterState;
1515
import org.elasticsearch.cluster.metadata.ProjectId;
1616
import org.elasticsearch.cluster.metadata.ProjectMetadata;
17+
import org.elasticsearch.cluster.project.ProjectsStateRegistry;
1718
import org.elasticsearch.common.settings.Settings;
1819
import org.elasticsearch.gateway.GatewayService;
1920
import org.elasticsearch.reservedstate.ReservedProjectStateHandler;
@@ -72,10 +73,10 @@ protected ClusterState execute(ClusterState currentState) {
7273
}
7374

7475
ClusterState updatedClusterState = result.v1();
75-
Settings updatedSettings = updatedClusterState.projectSettings(projectId);
76+
Settings updatedSettings = ProjectsStateRegistry.getProjectSettings(projectId, updatedClusterState);
7677
ProjectMetadata updatedProject = updatedClusterState.getMetadata().getProject(projectId);
7778
return ClusterState.builder(currentState)
78-
.putProjectSettings(projectId, updatedSettings)
79+
.putCustom(ProjectsStateRegistry.TYPE, ProjectsStateRegistry.setProjectSettings(currentState, projectId, updatedSettings))
7980
.putProjectMetadata(ProjectMetadata.builder(updatedProject).put(result.v2()))
8081
.build();
8182
}

0 commit comments

Comments
 (0)