Skip to content
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

[#4246] improvement(core): Remove role local cache #4901

Merged
merged 2 commits into from
Sep 12, 2024
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
7 changes: 0 additions & 7 deletions core/src/main/java/org/apache/gravitino/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ private Configs() {}
ConfigConstants.NOT_BLANK_ERROR_MSG)
.create();

public static final ConfigEntry<Long> ROLE_CACHE_EVICTION_INTERVAL_MS =
new ConfigBuilder("gravitino.authorization.roleCacheEvictionIntervalMs")
.doc("The interval in milliseconds to evict the role cache")
.version(ConfigConstants.VERSION_0_5_0)
.longConf()
.createWithDefault(60 * 60 * 1000L);

public static final int DEFAULT_METRICS_TIME_SLIDING_WINDOW_SECONDS = 60;
public static final ConfigEntry<Integer> METRICS_TIME_SLIDING_WINDOW_SECONDS =
new ConfigBuilder("gravitino.metrics.timeSlidingWindowSecs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class AccessControlManager implements AccessControlDispatcher {
private final List<String> serviceAdmins;

public AccessControlManager(EntityStore store, IdGenerator idGenerator, Config config) {
this.roleManager = new RoleManager(store, idGenerator, config);
this.roleManager = new RoleManager(store, idGenerator);
this.userGroupManager = new UserGroupManager(store, idGenerator);
this.permissionManager = new PermissionManager(store, roleManager);
this.serviceAdmins = config.get(Configs.SERVICE_ADMINS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,10 @@

package org.apache.gravitino.authorization;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Scheduler;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.gravitino.Config;
import org.apache.gravitino.Configs;
import org.apache.gravitino.Entity;
import org.apache.gravitino.EntityAlreadyExistsException;
import org.apache.gravitino.EntityStore;
Expand All @@ -57,32 +48,10 @@ class RoleManager {
private static final Logger LOG = LoggerFactory.getLogger(RoleManager.class);
private final EntityStore store;
private final IdGenerator idGenerator;
private final Cache<NameIdentifier, RoleEntity> cache;

RoleManager(EntityStore store, IdGenerator idGenerator, Config config) {
RoleManager(EntityStore store, IdGenerator idGenerator) {
this.store = store;
this.idGenerator = idGenerator;

long cacheEvictionIntervalInMs = config.get(Configs.ROLE_CACHE_EVICTION_INTERVAL_MS);
// One role entity is about 40 bytes using jol estimate, there are usually about 100w+
// roles in the production environment, this won't bring too much memory cost, but it
// can improve the performance significantly.
this.cache =
Caffeine.newBuilder()
.expireAfterAccess(cacheEvictionIntervalInMs, TimeUnit.MILLISECONDS)
.removalListener(
(k, v, c) -> {
LOG.info("Remove role {} from the cache.", k);
})
.scheduler(
Scheduler.forScheduledExecutorService(
new ScheduledThreadPoolExecutor(
1,
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("role-cleaner-%d")
.build())))
.build();
}

RoleEntity createRole(
Expand All @@ -107,7 +76,6 @@ RoleEntity createRole(
.build();
try {
store.put(roleEntity, false /* overwritten */);
cache.put(roleEntity.nameIdentifier(), roleEntity);

AuthorizationUtils.callAuthorizationPluginForSecurableObjects(
metalake,
Expand Down Expand Up @@ -141,7 +109,6 @@ boolean deleteRole(String metalake, String role) {
try {
AuthorizationUtils.checkMetalakeExists(metalake);
NameIdentifier ident = AuthorizationUtils.ofRole(metalake, role);
cache.invalidate(ident);

try {
RoleEntity roleEntity = store.get(ident, Entity.EntityType.ROLE, RoleEntity.class);
Expand All @@ -163,20 +130,11 @@ boolean deleteRole(String metalake, String role) {
}

private RoleEntity getRoleEntity(NameIdentifier identifier) {
return cache.get(
identifier,
id -> {
try {
return store.get(identifier, Entity.EntityType.ROLE, RoleEntity.class);
} catch (IOException ioe) {
LOG.error("Failed to get roles {} due to storage issues", identifier, ioe);
throw new RuntimeException(ioe);
}
});
}

@VisibleForTesting
Cache<NameIdentifier, RoleEntity> getCache() {
return cache;
try {
return store.get(identifier, Entity.EntityType.ROLE, RoleEntity.class);
} catch (IOException ioe) {
LOG.error("Failed to get roles {} due to storage issues", identifier, ioe);
throw new RuntimeException(ioe);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,8 @@ public void testLoadRole() {
SecurableObjects.ofCatalog(
"catalog", Lists.newArrayList(Privileges.UseCatalog.allow()))));

Role cachedRole = accessControlManager.getRole("metalake", "loadRole");
accessControlManager.getRoleManager().getCache().invalidateAll();
Role role = accessControlManager.getRole("metalake", "loadRole");

// Verify the cached roleEntity is correct
Assertions.assertEquals(role, cachedRole);

Assertions.assertEquals("loadRole", role.name());
testProperties(props, role.properties());

Expand Down
Loading