-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Drop role cache entries when project is deleted #131908
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
tvernum
merged 4 commits into
elastic:main
from
tvernum:multi-project/role-cache-cleanup
Jul 30, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
server/src/main/java/org/elasticsearch/cluster/project/ProjectDeletedListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.cluster.project; | ||
|
||
import org.elasticsearch.cluster.ClusterChangedEvent; | ||
import org.elasticsearch.cluster.metadata.ProjectId; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Utility class to make it easy to run a block of code whenever a project is deleted (e.g. to cleanup cache entries) | ||
*/ | ||
public class ProjectDeletedListener { | ||
|
||
private final Consumer<ProjectId> consumer; | ||
|
||
public ProjectDeletedListener(Consumer<ProjectId> consumer) { | ||
this.consumer = consumer; | ||
} | ||
|
||
public void attach(ClusterService clusterService) { | ||
clusterService.addListener(event -> { | ||
final ClusterChangedEvent.ProjectsDelta delta = event.projectDelta(); | ||
delta.removed().forEach(consumer); | ||
}); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
server/src/test/java/org/elasticsearch/cluster/project/ProjectDeletedListenerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.cluster.project; | ||
|
||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.Metadata; | ||
import org.elasticsearch.cluster.metadata.ProjectId; | ||
import org.elasticsearch.cluster.metadata.ProjectMetadata; | ||
import org.elasticsearch.cluster.routing.GlobalRoutingTableTestHelper; | ||
import org.elasticsearch.cluster.routing.RoutingTable; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue; | ||
import org.elasticsearch.test.ClusterServiceUtils; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class ProjectDeletedListenerTests extends ESTestCase { | ||
|
||
public void testInvocation() { | ||
final List<ProjectId> existingProjects = randomList(5, 15, ESTestCase::randomUniqueProjectId); | ||
|
||
try (ClusterService clusterService = ClusterServiceUtils.createClusterService(new DeterministicTaskQueue().getThreadPool())) { | ||
final ClusterState.Builder csBuilder = ClusterState.builder(ClusterName.DEFAULT); | ||
existingProjects.forEach(p -> csBuilder.putProjectMetadata(ProjectMetadata.builder(p).build())); | ||
final ClusterState cs0 = csBuilder.build(); | ||
|
||
ClusterServiceUtils.setState(clusterService, cs0); | ||
|
||
final Set<ProjectId> notifiedProjects = new HashSet<>(); | ||
var pdl = new ProjectDeletedListener(notifiedProjects::add); | ||
pdl.attach(clusterService); | ||
|
||
final Set<ProjectId> projectsToDelete = Set.copyOf( | ||
randomSubsetOf(randomIntBetween(1, existingProjects.size() / 2), existingProjects) | ||
); | ||
final List<ProjectId> projectsToCreate = randomList(0, 3, ESTestCase::randomUniqueProjectId); | ||
|
||
final var mdBuilder = Metadata.builder(cs0.metadata()); | ||
projectsToDelete.forEach(mdBuilder::removeProject); | ||
projectsToCreate.forEach(p -> mdBuilder.put(ProjectMetadata.builder(p).build())); | ||
var md = mdBuilder.build(); | ||
var cs1 = ClusterState.builder(cs0) | ||
.metadata(md) | ||
.routingTable(GlobalRoutingTableTestHelper.buildRoutingTable(md, RoutingTable.Builder::addAsNew)) | ||
.build(); | ||
ClusterServiceUtils.setState(clusterService, cs1); | ||
|
||
assertThat(notifiedProjects, equalTo(projectsToDelete)); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ | |
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.ActionRunnable; | ||
import org.elasticsearch.cluster.metadata.ProjectId; | ||
import org.elasticsearch.cluster.project.ProjectDeletedListener; | ||
import org.elasticsearch.cluster.project.ProjectResolver; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.cache.Cache; | ||
|
@@ -23,7 +25,6 @@ | |
import org.elasticsearch.common.util.concurrent.ReleasableLock; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.common.util.set.Sets; | ||
import org.elasticsearch.core.FixForMultiProject; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.core.Tuple; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
|
@@ -113,7 +114,6 @@ public class CompositeRolesStore { | |
private final DocumentSubsetBitsetCache dlsBitsetCache; | ||
private final AnonymousUser anonymousUser; | ||
|
||
@FixForMultiProject(description = "Deleted projects are never cleared from this map") | ||
private final Map<ProjectId, Long> numInvalidation = new ConcurrentHashMap<>(); | ||
private final RoleDescriptorStore roleReferenceResolver; | ||
private final Role superuserRole; | ||
|
@@ -124,6 +124,7 @@ public class CompositeRolesStore { | |
|
||
public CompositeRolesStore( | ||
Settings settings, | ||
ClusterService clusterService, | ||
RoleProviders roleProviders, | ||
NativePrivilegeStore privilegeStore, | ||
ThreadContext threadContext, | ||
|
@@ -137,6 +138,8 @@ public CompositeRolesStore( | |
Executor roleBuildingExecutor, | ||
Consumer<Collection<RoleDescriptor>> effectiveRoleDescriptorsConsumer | ||
) { | ||
new ProjectDeletedListener(this::removeProject).attach(clusterService); | ||
|
||
this.roleProviders = roleProviders; | ||
roleProviders.addChangeListener(new RoleProviders.ChangeListener() { | ||
@Override | ||
|
@@ -633,6 +636,12 @@ public void invalidateProject(ProjectId projectId) { | |
} | ||
} | ||
|
||
final void removeProject(ProjectId projectId) { | ||
numInvalidation.remove(projectId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a fix annotation for this field on line 116 which can now be removed. |
||
negativeLookupCacheHelper.removeKeysIf(key -> key.projectId().equals(projectId)); | ||
roleCacheHelper.removeKeysIf(key -> key.projectId().equals(projectId)); | ||
} | ||
|
||
public void invalidateAll() { | ||
numInvalidation.replaceAll((p, num) -> num + 1); | ||
negativeLookupCache.invalidateAll(); | ||
|
@@ -655,6 +664,11 @@ public void invalidateClusterScopedRoles(Set<String> roles) { | |
negativeLookupCacheHelper.removeKeysIf(key -> roles.contains(key.value())); | ||
} | ||
|
||
// for testing | ||
Iterable<ProjectScoped<RoleKey>> cachedRoles() { | ||
return this.roleCache.keys(); | ||
} | ||
|
||
public void usageStats(ActionListener<Map<String, Object>> listener) { | ||
final Map<String, Object> usage = new HashMap<>(); | ||
usage.put("dls", Map.of("bit_set_cache", dlsBitsetCache.usageStats())); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class doesn't do a lot, but it makes the code that uses it a lot clearer.
The 1 line in
CompositeRolesStore
becomes really obvious:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's useful if the class does not already listen for cluster state changes. I guess there are probably a number of such classes.