Skip to content

Add optimized updateSingleProject on Metadata #129585

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
merged 3 commits into from
Jun 18, 2025
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 @@ -1005,7 +1005,7 @@ public ClusterState copyAndUpdateMetadata(Consumer<Metadata.Builder> updater) {
}

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

@SuppressForbidden(reason = "directly reading ClusterState#clusterFeatures")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public int hashCode() {
public ClusterState updatedState(Consumer<ProjectMetadata.Builder> projectBuilderConsumer) {
ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(metadata());
projectBuilderConsumer.accept(projectBuilder);
return ClusterState.builder(cluster).putProjectMetadata(projectBuilder).build();
return updatedState(projectBuilder.build());
}

/**
* Build a new {@link ClusterState} with the updated project.
*/
public ClusterState updatedState(ProjectMetadata updatedProject) {
return ClusterState.builder(cluster).putProjectMetadata(updatedProject).build();
return ClusterState.builder(cluster).metadata(cluster.metadata().withUpdatedProject(updatedProject)).build();
}

/**
Expand All @@ -100,6 +100,6 @@ public ProjectState updateProject(ProjectMetadata updatedProject) {
)
);
}
return new ProjectState(ClusterState.builder(cluster).putProjectMetadata(updatedProject).build(), project);
return new ProjectState(updatedState(updatedProject), project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,44 @@ private Metadata updateSingleProject(Function<ProjectMetadata, ProjectMetadata>
}
}

/**
* Updates a single project in the metadata. This offers a more performant way of updating a single project compared to the Builder.
*/
@FixForMultiProject // We should reconsider whether this method is valuable once we update Metadata.Builder to hold constructed projects
// instead of project builders.
public Metadata withUpdatedProject(ProjectMetadata updatedProject) {
final var existingProject = projectMetadata.get(updatedProject.id());
if (existingProject == null) {
throw new IllegalArgumentException(
"Can only update existing project, cannot add a new project [" + updatedProject.id() + "]. Use the builder instead"
);
}
if (updatedProject == existingProject) {
return this;
}
final Map<ProjectId, ProjectMetadata> updatedMap;
if (projects().size() == 1) {
updatedMap = Map.of(updatedProject.id(), updatedProject);
} else {
final var hashMap = new HashMap<>(projectMetadata);
hashMap.put(updatedProject.id(), updatedProject);
updatedMap = Collections.unmodifiableMap(hashMap);
}
return new Metadata(
clusterUUID,
clusterUUIDCommitted,
version,
coordinationMetadata,
updatedMap,
transientSettings,
persistentSettings,
settings,
hashesOfConsistentSettings,
customs,
reservedStateMetadata
);
}

public long version() {
return this.version;
}
Expand Down Expand Up @@ -1487,6 +1525,18 @@ public Metadata copyAndUpdate(Consumer<Builder> updater) {
return builder.build();
}

public Metadata copyAndUpdateProject(ProjectId projectId, Consumer<ProjectMetadata.Builder> updater) {
Copy link
Member

Choose a reason for hiding this comment

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

Aside: I think the name of this one is okay because it's following the precedent of the existing copyAndUpdate. (And because you omitted the Single here!) I guess it's a little odd that it's not following the 'wither' convention, and IIUC it's also misleading in the sense that it won't do a copy if the updater was a noop, but I think that's okay given the precedent.

final var existingProject = projectMetadata.get(projectId);
if (existingProject == null) {
throw new IllegalArgumentException(
"Can only update existing project, cannot add a new project [" + projectId + "]. Use the builder instead"
);
}
final var builder = ProjectMetadata.builder(existingProject);
updater.accept(builder);
return withUpdatedProject(builder.build());
}

public static class Builder {

private String clusterUUID;
Expand Down