Skip to content

Commit 9bab4d1

Browse files
authored
Add methods to ProjectStateRegistry and its builder (elastic#131790)
This PR adds some new methods to `ProjectStateRegistry` and its builder that are needed for changes in other parts of the code.
1 parent b506f0a commit 9bab4d1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

server/src/main/java/org/elasticsearch/cluster/project/ProjectStateRegistry.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public class ProjectStateRegistry extends AbstractNamedDiffable<Custom> implemen
5454
// A counter that is incremented each time one or more projects are marked for deletion.
5555
private final long projectsMarkedForDeletionGeneration;
5656

57+
public static ProjectStateRegistry get(ClusterState clusterState) {
58+
return clusterState.custom(TYPE, EMPTY);
59+
}
60+
5761
public ProjectStateRegistry(StreamInput in) throws IOException {
5862
if (in.getTransportVersion().onOrAfter(TransportVersions.PROJECT_STATE_REGISTRY_ENTRY)) {
5963
projectsEntries = in.readMap(ProjectId::readFrom, Entry::readFrom);
@@ -80,6 +84,10 @@ private ProjectStateRegistry(
8084
this.projectsMarkedForDeletionGeneration = projectsMarkedForDeletionGeneration;
8185
}
8286

87+
public boolean hasProject(ProjectId projectId) {
88+
return projectsEntries.containsKey(projectId);
89+
}
90+
8391
/**
8492
* Retrieves the settings for a specific project based on its project ID from the specified cluster state without creating a new object.
8593
* If you need a full state of the project rather than just its setting, please use {@link ClusterState#projectState(ProjectId)}
@@ -97,6 +105,10 @@ public Settings getProjectSettings(ProjectId projectId) {
97105
return projectsEntries.getOrDefault(projectId, EMPTY_ENTRY).settings;
98106
}
99107

108+
public Set<ProjectId> getProjectsMarkedForDeletion() {
109+
return projectsMarkedForDeletion;
110+
}
111+
100112
public boolean isProjectMarkedForDeletion(ProjectId projectId) {
101113
return projectsMarkedForDeletion.contains(projectId);
102114
}
@@ -310,6 +322,12 @@ public Builder markProjectForDeletion(ProjectId projectId) {
310322
return this;
311323
}
312324

325+
public Builder removeProject(ProjectId projectId) {
326+
projectsEntries.remove(projectId);
327+
projectsMarkedForDeletion.remove(projectId);
328+
return this;
329+
}
330+
313331
public ProjectStateRegistry build() {
314332
final var unknownButUnderDeletion = Sets.difference(projectsMarkedForDeletion, projectsEntries.keys());
315333
if (unknownButUnderDeletion.isEmpty() == false) {
@@ -319,7 +337,7 @@ public ProjectStateRegistry build() {
319337
}
320338
return new ProjectStateRegistry(
321339
projectsEntries.build(),
322-
projectsMarkedForDeletion,
340+
Collections.unmodifiableSet(projectsMarkedForDeletion),
323341
newProjectMarkedForDeletion ? projectsMarkedForDeletionGeneration + 1 : projectsMarkedForDeletionGeneration
324342
);
325343
}

server/src/test/java/org/elasticsearch/cluster/project/ProjectStateRegistryTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public void testBuilder() {
4646
var unknownProjectId = randomUniqueProjectId();
4747
var throwingBuilder = ProjectStateRegistry.builder(projectStateRegistry).markProjectForDeletion(unknownProjectId);
4848
assertThrows(IllegalArgumentException.class, throwingBuilder::build);
49+
50+
var projectToRemove = randomFrom(projectStateRegistry.knownProjects());
51+
projectStateRegistry = ProjectStateRegistry.builder(projectStateRegistry).removeProject(projectToRemove).build();
52+
assertFalse(projectStateRegistry.hasProject(projectToRemove));
53+
assertFalse(projectStateRegistry.isProjectMarkedForDeletion(projectToRemove));
4954
}
5055

5156
public void testDiff() {

0 commit comments

Comments
 (0)