Skip to content

Reduce use of deprecated Metadata builder method #124290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.GlobalRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
Expand Down Expand Up @@ -126,19 +129,20 @@ public void setUp() throws Exception {
Settings.builder().put("cluster.routing.allocation.awareness.attributes", "tag").build()
);

Metadata.Builder mb = Metadata.builder();
final ProjectId projectId = ProjectId.DEFAULT;
ProjectMetadata.Builder pmb = ProjectMetadata.builder(projectId);
for (int i = 1; i <= numIndices; i++) {
mb.put(
pmb.put(
IndexMetadata.builder("test_" + i)
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()))
.numberOfShards(numShards)
.numberOfReplicas(numReplicas)
);
}
Metadata metadata = mb.build();
Metadata metadata = Metadata.builder().put(pmb).build();
RoutingTable.Builder rb = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY);
for (int i = 1; i <= numIndices; i++) {
rb.addAsNew(metadata.getProject().index("test_" + i));
rb.addAsNew(metadata.getProject(projectId).index("test_" + i));
}
RoutingTable routingTable = rb.build();
DiscoveryNodes.Builder nb = DiscoveryNodes.builder();
Expand All @@ -151,7 +155,7 @@ public void setUp() throws Exception {
}
initialClusterState = ClusterState.builder(ClusterName.DEFAULT)
.metadata(metadata)
.routingTable(routingTable)
.routingTable(GlobalRoutingTable.builder().put(projectId, routingTable).build())
.nodes(nb)
.nodeIdsToCompatibilityVersions(compatibilityVersions)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,10 @@ public ClusterState copyAndUpdateMetadata(Consumer<Metadata.Builder> updater) {
return copyAndUpdate(builder -> builder.metadata(metadata().copyAndUpdate(updater)));
}

public ClusterState copyAndUpdateProject(ProjectId projectId, Consumer<ProjectMetadata.Builder> updater) {
return copyAndUpdate(builder -> builder.putProjectMetadata(metadata().getProject(projectId).copyAndUpdate(updater)));
}

@SuppressForbidden(reason = "directly reading ClusterState#clusterFeatures")
private static Map<String, Set<String>> getNodeFeatures(ClusterFeatures features) {
return features.nodeFeatures();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.elasticsearch.cli.UserException;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Tuple;
Expand Down Expand Up @@ -58,9 +58,10 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
final ClusterState oldClusterState = termAndClusterState.v2();
final Metadata.Builder newMetadataBuilder = Metadata.builder(oldClusterState.metadata());
final ProjectMetadata oldProject = oldClusterState.metadata().getProject();
final ProjectMetadata.Builder newProjectBuilder = ProjectMetadata.builder(oldProject);
int changes = 0;
for (IndexMetadata indexMetadata : oldClusterState.metadata().getProject()) {
for (IndexMetadata indexMetadata : oldProject) {
Settings oldSettings = indexMetadata.getSettings();
Settings.Builder newSettings = Settings.builder().put(oldSettings);
boolean removed = false;
Expand All @@ -76,15 +77,15 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
}
}
if (removed) {
newMetadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(newSettings));
newProjectBuilder.put(IndexMetadata.builder(indexMetadata).settings(newSettings));
changes++;
}
}
if (changes == 0) {
throw new UserException(ExitCodes.USAGE, "No index setting matching " + settingsToRemove + " were found on this node");
}

final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(newMetadataBuilder).build();
final ClusterState newClusterState = ClusterState.builder(oldClusterState).putProjectMetadata(newProjectBuilder).build();
terminal.println(
Terminal.Verbosity.VERBOSE,
"[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.UUIDs;
Expand Down Expand Up @@ -104,8 +105,10 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
.clusterUUIDCommitted(true)
.persistentSettings(persistentSettings)
.coordinationMetadata(newCoordinationMetadata);

final ProjectMetadata.Builder newProject = ProjectMetadata.builder(metadata.getProject());
for (IndexMetadata indexMetadata : metadata.getProject().indices().values()) {
newMetadata.put(
newProject.put(
IndexMetadata.builder(indexMetadata)
.settings(
Settings.builder()
Expand All @@ -114,6 +117,7 @@ protected void processDataPaths(Terminal terminal, Path[] dataPaths, OptionSet o
)
);
}
newMetadata.put(newProject);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1117,6 +1118,12 @@ public static ProjectMetadata.Builder builder(ProjectMetadata projectMetadata) {
return new ProjectMetadata.Builder(projectMetadata);
}

public ProjectMetadata copyAndUpdate(Consumer<Builder> updater) {
var builder = builder(this);
updater.accept(builder);
return builder.build();
}

public static class Builder {

private final ImmutableOpenMap.Builder<String, IndexMetadata> indices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.GlobalRoutingTable;
import org.elasticsearch.cluster.routing.GlobalRoutingTableTestHelper;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
Expand Down Expand Up @@ -149,6 +150,24 @@ public void testCopyAndUpdateMetadata() throws IOException {
assertThat(copy.metadata().clusterUUID(), equalTo(newClusterUuid));
}

public void testCopyAndUpdateProject() throws IOException {
var projectId = randomProjectIdOrDefault();
var state = buildClusterState(projectId);
var indexName = getTestName();

assertThat(state.metadata().getProject(projectId).hasIndex(indexName), equalTo(false));

var copy = state.copyAndUpdateProject(
projectId,
project -> project.put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1)))
);

assertThat(copy, not(sameInstance(state)));
assertThat(copy.metadata(), not(sameInstance(state.metadata())));
assertThat(copy.metadata().getProject(projectId), not(sameInstance(state.metadata().getProject(projectId))));
assertThat(copy.metadata().getProject(projectId).hasIndex(indexName), equalTo(true));
}

public void testGetNonExistingProjectStateThrows() {
final List<ProjectMetadata> projects = IntStream.range(0, between(1, 3))
.mapToObj(i -> MetadataTests.randomProject(ProjectId.fromId("p_" + i), between(0, 5)))
Expand Down Expand Up @@ -1862,6 +1881,10 @@ public void testNodeFeaturesSorted() throws IOException {
}

private ClusterState buildClusterState() throws IOException {
return buildClusterState(ProjectId.DEFAULT);
}

private static ClusterState buildClusterState(ProjectId projectId) throws IOException {
IndexMetadata indexMetadata = IndexMetadata.builder("index")
.state(IndexMetadata.State.OPEN)
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
Expand Down Expand Up @@ -1925,29 +1948,38 @@ private ClusterState buildClusterState() throws IOException {
)
.persistentSettings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()).build())
.transientSettings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()).build())
.put(indexMetadata, false)
.put(
IndexTemplateMetadata.builder("template")
.patterns(List.of("pattern1", "pattern2"))
.order(0)
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
.putMapping("type", "{ \"key1\": {} }")
ProjectMetadata.builder(projectId)
.put(indexMetadata, false)
.put(
IndexTemplateMetadata.builder("template")
.patterns(List.of("pattern1", "pattern2"))
.order(0)
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
.putMapping("type", "{ \"key1\": {} }")
.build()
)
.build()
)
)
.routingTable(
RoutingTable.builder()
.add(
IndexRoutingTable.builder(new Index("index", "indexUUID"))
.addIndexShard(
new IndexShardRoutingTable.Builder(new ShardId("index", "indexUUID", 0)).addShard(
TestShardRouting.newShardRouting(
new ShardId("index", "indexUUID", 0),
"nodeId2",
true,
ShardRoutingState.STARTED
GlobalRoutingTable.builder()
.put(
projectId,
RoutingTable.builder()
.add(
IndexRoutingTable.builder(new Index("index", "indexUUID"))
.addIndexShard(
new IndexShardRoutingTable.Builder(new ShardId("index", "indexUUID", 0)).addShard(
TestShardRouting.newShardRouting(
new ShardId("index", "indexUUID", 0),
"nodeId2",
true,
ShardRoutingState.STARTED
)
)
)
)
.build()
)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import static org.elasticsearch.cluster.metadata.MetadataTests.count;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;

public class ProjectMetadataTests extends ESTestCase {

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

public void testCopyAndUpdate() {
var initialIndexUUID = randomUUID();
final String indexName = randomAlphaOfLengthBetween(4, 12);
final ProjectMetadata before = ProjectMetadata.builder(randomProjectIdOrDefault())
.put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), initialIndexUUID, 1, 1)))
.build();

var alteredIndexUUID = randomUUID();
assertThat(alteredIndexUUID, not(equalTo(initialIndexUUID)));
final ProjectMetadata after = before.copyAndUpdate(
builder -> builder.put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), alteredIndexUUID, 1, 1)))
);

assertThat(after, not(sameInstance(before)));
assertThat(after.index(indexName).getIndexUUID(), equalTo(alteredIndexUUID));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.FixForMultiProject;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexNotFoundException;
Expand Down Expand Up @@ -287,6 +289,8 @@ static ClusterState unfollow(String followerIndex, ClusterState current) {
// Remove ccr custom metadata
newIndexMetadata.removeCustom(Ccr.CCR_CUSTOM_METADATA_KEY);

return current.copyAndUpdateMetadata(metadata -> metadata.put(newIndexMetadata));
@FixForMultiProject
final ProjectId projectId = current.metadata().getProject().id();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+1 I like this pattern

return current.copyAndUpdateProject(projectId, project -> project.put(newIndexMetadata));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.Lucene;
Expand Down Expand Up @@ -110,9 +111,13 @@ public void finalizeSnapshot(FinalizeSnapshotContext finalizeSnapshotContext) {
}

private static Metadata metadataToSnapshot(Collection<IndexId> indices, Metadata metadata) {
Metadata.Builder builder = Metadata.builder(metadata);
return Metadata.builder(metadata).put(projectMetadataToSnapshot(indices, metadata.getProject())).build();
}

private static ProjectMetadata projectMetadataToSnapshot(Collection<IndexId> indices, ProjectMetadata projectMetadata) {
ProjectMetadata.Builder builder = ProjectMetadata.builder(projectMetadata);
for (IndexId indexId : indices) {
IndexMetadata index = metadata.getProject().index(indexId.getName());
IndexMetadata index = projectMetadata.index(indexId.getName());
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(index);
// for a minimal restore we basically disable indexing on all fields and only create an index
// that is valid from an operational perspective. ie. it will have all metadata fields like version/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -94,11 +94,11 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
settings.put(key, value);
}

Metadata.Builder newMetaData = Metadata.builder(clusterState.getMetadata())
ProjectMetadata.Builder newProject = ProjectMetadata.builder(clusterState.getMetadata().getProject())
.put(
IndexMetadata.builder(targetIndexMetadata).settingsVersion(targetIndexMetadata.getSettingsVersion() + 1).settings(settings)
);
return ClusterState.builder(clusterState).metadata(newMetaData).build();
return ClusterState.builder(clusterState).putProjectMetadata(newProject).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexSettings;
Expand Down Expand Up @@ -79,7 +79,9 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
.build()
);
builder.putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.asMap());
return ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).put(builder).build()).build();
return ClusterState.builder(clusterState)
.putProjectMetadata(ProjectMetadata.builder(clusterState.metadata().getProject()).put(builder).build())
.build();
}
}

Expand Down
Loading