You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by co...@apache.org on 2024/11/26 01:36:08 UTC
(polaris) 05/06: Add CacheDelegatingGrantCache
This is an automated email from the ASF dual-hosted git repository.
collado pushed a commit to branch mcollado-hk2-di
in repository https://gitbox.apache.org/repos/asf/polaris.git
commit 01c6928f188624bf0d260a2d79eb5983b480bfe7
Author: Michael Collado <co...@gmail.com>
AuthorDate: Mon Nov 25 17:35:14 2024 -0800
Add CacheDelegatingGrantCache
---
.../cache/CacheDelegatingGrantCache.java | 164 +++++++++++++++++++++
1 file changed, 164 insertions(+)
diff --git a/polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/CacheDelegatingGrantCache.java b/polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/CacheDelegatingGrantCache.java
new file mode 100644
index 00000000..8bf54845
--- /dev/null
+++ b/polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/CacheDelegatingGrantCache.java
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.persistence.cache;
+
+import jakarta.inject.Inject;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.auth.PolarisGrantManager;
+import org.apache.polaris.core.context.RealmScope;
+import org.apache.polaris.core.entity.PolarisEntityCore;
+import org.apache.polaris.core.entity.PolarisPrivilege;
+import org.apache.polaris.core.persistence.BaseResult;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@RealmScope
+public class CacheDelegatingGrantCache implements PolarisGrantManager {
+ private static final Logger LOGGER = LoggerFactory.getLogger(CacheDelegatingGrantCache.class);
+ private final EntityCache entityCache;
+ private final PolarisGrantManager delegateGrantManager;
+
+ @Inject
+ public CacheDelegatingGrantCache(
+ EntityCache entityCache, PolarisGrantManager delegateGrantManager) {
+ this.entityCache = entityCache;
+ this.delegateGrantManager = delegateGrantManager;
+ }
+
+ @Override
+ public @NotNull PrivilegeResult grantUsageOnRoleToGrantee(
+ @NotNull PolarisCallContext callCtx,
+ @Nullable PolarisEntityCore catalog,
+ @NotNull PolarisEntityCore role,
+ @NotNull PolarisEntityCore grantee) {
+ return delegateGrantManager.grantUsageOnRoleToGrantee(callCtx, catalog, role, grantee);
+ }
+
+ @Override
+ public @NotNull PrivilegeResult revokeUsageOnRoleFromGrantee(
+ @NotNull PolarisCallContext callCtx,
+ @Nullable PolarisEntityCore catalog,
+ @NotNull PolarisEntityCore role,
+ @NotNull PolarisEntityCore grantee) {
+ return delegateGrantManager.revokeUsageOnRoleFromGrantee(callCtx, catalog, role, grantee);
+ }
+
+ @Override
+ public @NotNull PrivilegeResult grantPrivilegeOnSecurableToRole(
+ @NotNull PolarisCallContext callCtx,
+ @NotNull PolarisEntityCore grantee,
+ @Nullable List<PolarisEntityCore> catalogPath,
+ @NotNull PolarisEntityCore securable,
+ @NotNull PolarisPrivilege privilege) {
+ return delegateGrantManager.grantPrivilegeOnSecurableToRole(
+ callCtx, grantee, catalogPath, securable, privilege);
+ }
+
+ @Override
+ public @NotNull PrivilegeResult revokePrivilegeOnSecurableFromRole(
+ @NotNull PolarisCallContext callCtx,
+ @NotNull PolarisEntityCore grantee,
+ @Nullable List<PolarisEntityCore> catalogPath,
+ @NotNull PolarisEntityCore securable,
+ @NotNull PolarisPrivilege privilege) {
+ return delegateGrantManager.revokePrivilegeOnSecurableFromRole(
+ callCtx, grantee, catalogPath, securable, privilege);
+ }
+
+ @Override
+ public @NotNull LoadGrantsResult loadGrantsOnSecurable(
+ @NotNull PolarisCallContext callCtx, long securableCatalogId, long securableId) {
+ EntityCacheLookupResult lookupResult =
+ entityCache.getOrLoadEntityById(callCtx, securableCatalogId, securableId);
+ if (lookupResult == null) {
+ LOGGER.warn(
+ "No cache hit or entity found for securable with catalogId {} and id {}",
+ securableCatalogId,
+ securableId);
+ return new LoadGrantsResult(BaseResult.ReturnStatus.ENTITY_NOT_FOUND, null);
+ }
+ callCtx
+ .getDiagServices()
+ .checkNotNull(lookupResult.getCacheEntry(), "Lookup result did not contain an entry");
+ LOGGER.debug(
+ "Returning grants from cache for securable with catalogId {} and id {}",
+ securableCatalogId,
+ securableId);
+ EntityCacheEntry cacheEntry = lookupResult.getCacheEntry();
+ callCtx.getDiagServices().checkNotNull(cacheEntry.getEntity(), "Entity not found in cache");
+ return new LoadGrantsResult(
+ cacheEntry.getEntity().getGrantRecordsVersion(),
+ cacheEntry.getGrantRecordsAsSecurable(),
+ cacheEntry.getGrantRecordsAsSecurable().stream()
+ .map(
+ gr ->
+ entityCache.getOrLoadEntityById(
+ callCtx, gr.getGranteeCatalogId(), gr.getGranteeId()))
+ .flatMap(
+ result ->
+ Optional.ofNullable(result)
+ .map(EntityCacheLookupResult::getCacheEntry)
+ .map(EntityCacheEntry::getEntity)
+ .stream())
+ .collect(Collectors.toList()));
+ }
+
+ @Override
+ public @NotNull LoadGrantsResult loadGrantsToGrantee(
+ PolarisCallContext callCtx, long granteeCatalogId, long granteeId) {
+ EntityCacheLookupResult lookupResult =
+ entityCache.getOrLoadEntityById(callCtx, granteeCatalogId, granteeId);
+ if (lookupResult == null) {
+ LOGGER.warn(
+ "No cache hit or entity found for grantee with catalogId {} and id {}",
+ granteeCatalogId,
+ granteeId);
+ return new LoadGrantsResult(BaseResult.ReturnStatus.ENTITY_NOT_FOUND, null);
+ }
+ callCtx
+ .getDiagServices()
+ .checkNotNull(lookupResult.getCacheEntry(), "Lookup result did not contain an entry");
+ LOGGER.debug(
+ "Returning grants from cache for grantee with catalogId {} and id {}",
+ granteeCatalogId,
+ granteeId);
+ EntityCacheEntry cacheEntry = lookupResult.getCacheEntry();
+ callCtx.getDiagServices().checkNotNull(cacheEntry.getEntity(), "Entity not found in cache");
+ return new LoadGrantsResult(
+ cacheEntry.getEntity().getGrantRecordsVersion(),
+ cacheEntry.getGrantRecordsAsGrantee(),
+ cacheEntry.getGrantRecordsAsGrantee().stream()
+ .map(
+ gr ->
+ entityCache.getOrLoadEntityById(
+ callCtx, gr.getSecurableCatalogId(), gr.getSecurableId()))
+ .flatMap(
+ result ->
+ Optional.ofNullable(result)
+ .map(EntityCacheLookupResult::getCacheEntry)
+ .map(EntityCacheEntry::getEntity)
+ .stream())
+ .collect(Collectors.toList()));
+ }
+}