Skip to content

Commit a0dd4e7

Browse files
authored
Reduce use of deprecated Metadata builder method (#124290)
This removes all non-test usage of Metadata.Builder.put(IndexMetadata.Builder) And replaces it with appropriate calls to the equivalent method on `ProjectMetadata.Builder` In most cases this _does not_ make the code project aware, but does reduce the number of deprecated methods in use
1 parent 788cd26 commit a0dd4e7

File tree

12 files changed

+149
-49
lines changed

12 files changed

+149
-49
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/routing/allocation/AllocationBenchmark.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
import org.elasticsearch.cluster.ClusterState;
1515
import org.elasticsearch.cluster.metadata.IndexMetadata;
1616
import org.elasticsearch.cluster.metadata.Metadata;
17+
import org.elasticsearch.cluster.metadata.ProjectId;
18+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1719
import org.elasticsearch.cluster.node.DiscoveryNodes;
20+
import org.elasticsearch.cluster.routing.GlobalRoutingTable;
1821
import org.elasticsearch.cluster.routing.RoutingTable;
1922
import org.elasticsearch.cluster.routing.ShardRouting;
2023
import org.elasticsearch.cluster.routing.allocation.AllocationService;
@@ -126,19 +129,20 @@ public void setUp() throws Exception {
126129
Settings.builder().put("cluster.routing.allocation.awareness.attributes", "tag").build()
127130
);
128131

129-
Metadata.Builder mb = Metadata.builder();
132+
final ProjectId projectId = ProjectId.DEFAULT;
133+
ProjectMetadata.Builder pmb = ProjectMetadata.builder(projectId);
130134
for (int i = 1; i <= numIndices; i++) {
131-
mb.put(
135+
pmb.put(
132136
IndexMetadata.builder("test_" + i)
133137
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()))
134138
.numberOfShards(numShards)
135139
.numberOfReplicas(numReplicas)
136140
);
137141
}
138-
Metadata metadata = mb.build();
142+
Metadata metadata = Metadata.builder().put(pmb).build();
139143
RoutingTable.Builder rb = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY);
140144
for (int i = 1; i <= numIndices; i++) {
141-
rb.addAsNew(metadata.getProject().index("test_" + i));
145+
rb.addAsNew(metadata.getProject(projectId).index("test_" + i));
142146
}
143147
RoutingTable routingTable = rb.build();
144148
DiscoveryNodes.Builder nb = DiscoveryNodes.builder();
@@ -151,7 +155,7 @@ public void setUp() throws Exception {
151155
}
152156
initialClusterState = ClusterState.builder(ClusterName.DEFAULT)
153157
.metadata(metadata)
154-
.routingTable(routingTable)
158+
.routingTable(GlobalRoutingTable.builder().put(projectId, routingTable).build())
155159
.nodes(nb)
156160
.nodeIdsToCompatibilityVersions(compatibilityVersions)
157161
.build();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,10 @@ public ClusterState copyAndUpdateMetadata(Consumer<Metadata.Builder> updater) {
983983
return copyAndUpdate(builder -> builder.metadata(metadata().copyAndUpdate(updater)));
984984
}
985985

986+
public ClusterState copyAndUpdateProject(ProjectId projectId, Consumer<ProjectMetadata.Builder> updater) {
987+
return copyAndUpdate(builder -> builder.putProjectMetadata(metadata().getProject(projectId).copyAndUpdate(updater)));
988+
}
989+
986990
@SuppressForbidden(reason = "directly reading ClusterState#clusterFeatures")
987991
private static Map<String, Set<String>> getNodeFeatures(ClusterFeatures features) {
988992
return features.nodeFeatures();

server/src/main/java/org/elasticsearch/cluster/coordination/RemoveIndexSettingsCommand.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.elasticsearch.cli.UserException;
1717
import org.elasticsearch.cluster.ClusterState;
1818
import org.elasticsearch.cluster.metadata.IndexMetadata;
19-
import org.elasticsearch.cluster.metadata.Metadata;
19+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2020
import org.elasticsearch.common.regex.Regex;
2121
import org.elasticsearch.common.settings.Settings;
2222
import org.elasticsearch.core.Tuple;
@@ -58,9 +58,10 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
5858
terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
5959
final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
6060
final ClusterState oldClusterState = termAndClusterState.v2();
61-
final Metadata.Builder newMetadataBuilder = Metadata.builder(oldClusterState.metadata());
61+
final ProjectMetadata oldProject = oldClusterState.metadata().getProject();
62+
final ProjectMetadata.Builder newProjectBuilder = ProjectMetadata.builder(oldProject);
6263
int changes = 0;
63-
for (IndexMetadata indexMetadata : oldClusterState.metadata().getProject()) {
64+
for (IndexMetadata indexMetadata : oldProject) {
6465
Settings oldSettings = indexMetadata.getSettings();
6566
Settings.Builder newSettings = Settings.builder().put(oldSettings);
6667
boolean removed = false;
@@ -76,15 +77,15 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
7677
}
7778
}
7879
if (removed) {
79-
newMetadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(newSettings));
80+
newProjectBuilder.put(IndexMetadata.builder(indexMetadata).settings(newSettings));
8081
changes++;
8182
}
8283
}
8384
if (changes == 0) {
8485
throw new UserException(ExitCodes.USAGE, "No index setting matching " + settingsToRemove + " were found on this node");
8586
}
8687

87-
final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(newMetadataBuilder).build();
88+
final ClusterState newClusterState = ClusterState.builder(oldClusterState).putProjectMetadata(newProjectBuilder).build();
8889
terminal.println(
8990
Terminal.Verbosity.VERBOSE,
9091
"[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]"

server/src/main/java/org/elasticsearch/cluster/coordination/UnsafeBootstrapMasterCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.cluster.ClusterState;
1616
import org.elasticsearch.cluster.metadata.IndexMetadata;
1717
import org.elasticsearch.cluster.metadata.Metadata;
18+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1819
import org.elasticsearch.cluster.node.DiscoveryNode;
1920
import org.elasticsearch.cluster.service.ClusterService;
2021
import org.elasticsearch.common.UUIDs;
@@ -104,8 +105,10 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
104105
.clusterUUIDCommitted(true)
105106
.persistentSettings(persistentSettings)
106107
.coordinationMetadata(newCoordinationMetadata);
108+
109+
final ProjectMetadata.Builder newProject = ProjectMetadata.builder(metadata.getProject());
107110
for (IndexMetadata indexMetadata : metadata.getProject().indices().values()) {
108-
newMetadata.put(
111+
newProject.put(
109112
IndexMetadata.builder(indexMetadata)
110113
.settings(
111114
Settings.builder()
@@ -114,6 +117,7 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
114117
)
115118
);
116119
}
120+
newMetadata.put(newProject);
117121

118122
final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(newMetadata).build();
119123

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import java.util.TreeMap;
6767
import java.util.function.BiConsumer;
6868
import java.util.function.BiPredicate;
69+
import java.util.function.Consumer;
6970
import java.util.function.Function;
7071
import java.util.function.Predicate;
7172
import java.util.stream.Collectors;
@@ -1117,6 +1118,12 @@ public static ProjectMetadata.Builder builder(ProjectMetadata projectMetadata) {
11171118
return new ProjectMetadata.Builder(projectMetadata);
11181119
}
11191120

1121+
public ProjectMetadata copyAndUpdate(Consumer<Builder> updater) {
1122+
var builder = builder(this);
1123+
updater.accept(builder);
1124+
return builder.build();
1125+
}
1126+
11201127
public static class Builder {
11211128

11221129
private final ImmutableOpenMap.Builder<String, IndexMetadata> indices;

server/src/test/java/org/elasticsearch/cluster/ClusterStateTests.java

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.cluster.node.DiscoveryNode;
3030
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
3131
import org.elasticsearch.cluster.node.DiscoveryNodes;
32+
import org.elasticsearch.cluster.routing.GlobalRoutingTable;
3233
import org.elasticsearch.cluster.routing.GlobalRoutingTableTestHelper;
3334
import org.elasticsearch.cluster.routing.IndexRoutingTable;
3435
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
@@ -149,6 +150,24 @@ public void testCopyAndUpdateMetadata() throws IOException {
149150
assertThat(copy.metadata().clusterUUID(), equalTo(newClusterUuid));
150151
}
151152

153+
public void testCopyAndUpdateProject() throws IOException {
154+
var projectId = randomProjectIdOrDefault();
155+
var state = buildClusterState(projectId);
156+
var indexName = getTestName();
157+
158+
assertThat(state.metadata().getProject(projectId).hasIndex(indexName), equalTo(false));
159+
160+
var copy = state.copyAndUpdateProject(
161+
projectId,
162+
project -> project.put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1)))
163+
);
164+
165+
assertThat(copy, not(sameInstance(state)));
166+
assertThat(copy.metadata(), not(sameInstance(state.metadata())));
167+
assertThat(copy.metadata().getProject(projectId), not(sameInstance(state.metadata().getProject(projectId))));
168+
assertThat(copy.metadata().getProject(projectId).hasIndex(indexName), equalTo(true));
169+
}
170+
152171
public void testGetNonExistingProjectStateThrows() {
153172
final List<ProjectMetadata> projects = IntStream.range(0, between(1, 3))
154173
.mapToObj(i -> MetadataTests.randomProject(ProjectId.fromId("p_" + i), between(0, 5)))
@@ -1862,6 +1881,10 @@ public void testNodeFeaturesSorted() throws IOException {
18621881
}
18631882

18641883
private ClusterState buildClusterState() throws IOException {
1884+
return buildClusterState(ProjectId.DEFAULT);
1885+
}
1886+
1887+
private static ClusterState buildClusterState(ProjectId projectId) throws IOException {
18651888
IndexMetadata indexMetadata = IndexMetadata.builder("index")
18661889
.state(IndexMetadata.State.OPEN)
18671890
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
@@ -1925,29 +1948,38 @@ private ClusterState buildClusterState() throws IOException {
19251948
)
19261949
.persistentSettings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()).build())
19271950
.transientSettings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()).build())
1928-
.put(indexMetadata, false)
19291951
.put(
1930-
IndexTemplateMetadata.builder("template")
1931-
.patterns(List.of("pattern1", "pattern2"))
1932-
.order(0)
1933-
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
1934-
.putMapping("type", "{ \"key1\": {} }")
1952+
ProjectMetadata.builder(projectId)
1953+
.put(indexMetadata, false)
1954+
.put(
1955+
IndexTemplateMetadata.builder("template")
1956+
.patterns(List.of("pattern1", "pattern2"))
1957+
.order(0)
1958+
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
1959+
.putMapping("type", "{ \"key1\": {} }")
1960+
.build()
1961+
)
19351962
.build()
19361963
)
19371964
)
19381965
.routingTable(
1939-
RoutingTable.builder()
1940-
.add(
1941-
IndexRoutingTable.builder(new Index("index", "indexUUID"))
1942-
.addIndexShard(
1943-
new IndexShardRoutingTable.Builder(new ShardId("index", "indexUUID", 0)).addShard(
1944-
TestShardRouting.newShardRouting(
1945-
new ShardId("index", "indexUUID", 0),
1946-
"nodeId2",
1947-
true,
1948-
ShardRoutingState.STARTED
1966+
GlobalRoutingTable.builder()
1967+
.put(
1968+
projectId,
1969+
RoutingTable.builder()
1970+
.add(
1971+
IndexRoutingTable.builder(new Index("index", "indexUUID"))
1972+
.addIndexShard(
1973+
new IndexShardRoutingTable.Builder(new ShardId("index", "indexUUID", 0)).addShard(
1974+
TestShardRouting.newShardRouting(
1975+
new ShardId("index", "indexUUID", 0),
1976+
"nodeId2",
1977+
true,
1978+
ShardRoutingState.STARTED
1979+
)
1980+
)
19491981
)
1950-
)
1982+
.build()
19511983
)
19521984
.build()
19531985
)

server/src/test/java/org/elasticsearch/cluster/metadata/ProjectMetadataTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import static org.elasticsearch.cluster.metadata.MetadataTests.count;
3030
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
3131
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
32+
import static org.hamcrest.Matchers.equalTo;
33+
import static org.hamcrest.Matchers.not;
34+
import static org.hamcrest.Matchers.sameInstance;
3235

3336
public class ProjectMetadataTests extends ESTestCase {
3437

@@ -384,4 +387,21 @@ static int expectedChunkCount(ToXContent.Params params, ProjectMetadata project)
384387
return Math.toIntExact(chunkCount);
385388
}
386389

390+
public void testCopyAndUpdate() {
391+
var initialIndexUUID = randomUUID();
392+
final String indexName = randomAlphaOfLengthBetween(4, 12);
393+
final ProjectMetadata before = ProjectMetadata.builder(randomProjectIdOrDefault())
394+
.put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), initialIndexUUID, 1, 1)))
395+
.build();
396+
397+
var alteredIndexUUID = randomUUID();
398+
assertThat(alteredIndexUUID, not(equalTo(initialIndexUUID)));
399+
final ProjectMetadata after = before.copyAndUpdate(
400+
builder -> builder.put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), alteredIndexUUID, 1, 1)))
401+
);
402+
403+
assertThat(after, not(sameInstance(before)));
404+
assertThat(after.index(indexName).getIndexUUID(), equalTo(alteredIndexUUID));
405+
}
406+
387407
}

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowAction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import org.elasticsearch.cluster.block.ClusterBlockException;
2727
import org.elasticsearch.cluster.block.ClusterBlockLevel;
2828
import org.elasticsearch.cluster.metadata.IndexMetadata;
29+
import org.elasticsearch.cluster.metadata.ProjectId;
2930
import org.elasticsearch.cluster.service.ClusterService;
3031
import org.elasticsearch.common.settings.Settings;
3132
import org.elasticsearch.common.util.concurrent.EsExecutors;
3233
import org.elasticsearch.common.util.concurrent.ThreadContext;
34+
import org.elasticsearch.core.FixForMultiProject;
3335
import org.elasticsearch.core.SuppressForbidden;
3436
import org.elasticsearch.index.Index;
3537
import org.elasticsearch.index.IndexNotFoundException;
@@ -287,6 +289,8 @@ static ClusterState unfollow(String followerIndex, ClusterState current) {
287289
// Remove ccr custom metadata
288290
newIndexMetadata.removeCustom(Ccr.CCR_CUSTOM_METADATA_KEY);
289291

290-
return current.copyAndUpdateMetadata(metadata -> metadata.put(newIndexMetadata));
292+
@FixForMultiProject
293+
final ProjectId projectId = current.metadata().getProject().id();
294+
return current.copyAndUpdateProject(projectId, project -> project.put(newIndexMetadata));
291295
}
292296
}

x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotRepository.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.cluster.metadata.IndexMetadata;
2121
import org.elasticsearch.cluster.metadata.MappingMetadata;
2222
import org.elasticsearch.cluster.metadata.Metadata;
23+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2324
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
2425
import org.elasticsearch.common.Strings;
2526
import org.elasticsearch.common.lucene.Lucene;
@@ -110,9 +111,13 @@ public void finalizeSnapshot(FinalizeSnapshotContext finalizeSnapshotContext) {
110111
}
111112

112113
private static Metadata metadataToSnapshot(Collection<IndexId> indices, Metadata metadata) {
113-
Metadata.Builder builder = Metadata.builder(metadata);
114+
return Metadata.builder(metadata).put(projectMetadataToSnapshot(indices, metadata.getProject())).build();
115+
}
116+
117+
private static ProjectMetadata projectMetadataToSnapshot(Collection<IndexId> indices, ProjectMetadata projectMetadata) {
118+
ProjectMetadata.Builder builder = ProjectMetadata.builder(projectMetadata);
114119
for (IndexId indexId : indices) {
115-
IndexMetadata index = metadata.getProject().index(indexId.getName());
120+
IndexMetadata index = projectMetadata.index(indexId.getName());
116121
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(index);
117122
// for a minimal restore we basically disable indexing on all fields and only create an index
118123
// that is valid from an operational perspective. ie. it will have all metadata fields like version/

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.elasticsearch.cluster.ClusterState;
1212
import org.elasticsearch.cluster.metadata.IndexMetadata;
1313
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
14-
import org.elasticsearch.cluster.metadata.Metadata;
14+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1515
import org.elasticsearch.common.Strings;
1616
import org.elasticsearch.common.settings.Settings;
1717
import org.elasticsearch.index.Index;
@@ -94,11 +94,11 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
9494
settings.put(key, value);
9595
}
9696

97-
Metadata.Builder newMetaData = Metadata.builder(clusterState.getMetadata())
97+
ProjectMetadata.Builder newProject = ProjectMetadata.builder(clusterState.getMetadata().getProject())
9898
.put(
9999
IndexMetadata.builder(targetIndexMetadata).settingsVersion(targetIndexMetadata.getSettingsVersion() + 1).settings(settings)
100100
);
101-
return ClusterState.builder(clusterState).metadata(newMetaData).build();
101+
return ClusterState.builder(clusterState).putProjectMetadata(newProject).build();
102102
}
103103

104104
@Override

0 commit comments

Comments
 (0)