Skip to content

Use PolarisImmutable for StorageCredentialCacheKey #2029

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
Jul 15, 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 @@ -114,7 +114,7 @@ public AccessConfig getOrGenerateSubScopeCreds(
.fail("entity_type_not_suppported_to_scope_creds", "type={}", polarisEntity.getType());
}
StorageCredentialCacheKey key =
new StorageCredentialCacheKey(
StorageCredentialCacheKey.of(
callCtx.getRealmContext().getRealmIdentifier(),
polarisEntity,
allowListOperation,
Expand All @@ -127,12 +127,12 @@ public AccessConfig getOrGenerateSubScopeCreds(
ScopedCredentialsResult scopedCredentialsResult =
credentialVendor.getSubscopedCredsForEntity(
callCtx,
k.getCatalogId(),
k.getEntityId(),
k.catalogId(),
polarisEntity.getId(),
polarisEntity.getType(),
k.isAllowedListAction(),
k.getAllowedReadLocations(),
k.getAllowedWriteLocations());
k.allowedListAction(),
k.allowedReadLocations(),
k.allowedWriteLocations());
if (scopedCredentialsResult.isSuccess()) {
long maxCacheDurationMs = maxCacheDurationMs(callCtx.getRealmConfig());
return new StorageCredentialCacheEntry(scopedCredentialsResult, maxCacheDurationMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,118 +18,51 @@
*/
package org.apache.polaris.core.storage.cache;

import java.util.Objects;
import jakarta.annotation.Nullable;
import java.util.Set;
import org.apache.polaris.core.entity.PolarisEntity;
import org.apache.polaris.core.entity.PolarisEntityConstants;
import org.apache.polaris.immutables.PolarisImmutable;
import org.immutables.value.Value;

public class StorageCredentialCacheKey {
@PolarisImmutable
public interface StorageCredentialCacheKey {

private final String realmId;
private final long catalogId;
@Value.Parameter(order = 1)
String realmId();

/** The serialized string of the storage config. */
private final String storageConfigSerializedStr;
@Value.Parameter(order = 2)
long catalogId();

/**
* The entity id is passed to be used to fetch subscoped creds, but is not used to do hash/equals
* as part of the cache key.
*/
Comment on lines -34 to -37
Copy link
Contributor

Choose a reason for hiding this comment

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

I know that this PR does not change this behaviour, but conceptually if the entity ID is a substantial parameter to computing the cached value, I believe it should be included in the key.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure whether I already mentioned it, but I'm not sure the StorageCredentialCache works as expected, because when credentials are retrieved from the cache, we only know that those are valid at the time of retrieval, but not how long those will be valid (for a client), it could be 0.1ms or 30days.

Copy link
Member

Choose a reason for hiding this comment

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

(But that's not a problem of this PR)

Copy link
Member

Choose a reason for hiding this comment

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

(Created #2046)

private final long entityId;
@Value.Parameter(order = 3)
@Nullable
String storageConfigSerializedStr();

private final boolean allowedListAction;
private final Set<String> allowedReadLocations;
@Value.Parameter(order = 4)
boolean allowedListAction();

private final Set<String> allowedWriteLocations;
@Value.Parameter(order = 5)
Set<String> allowedReadLocations();

public StorageCredentialCacheKey(
@Value.Parameter(order = 6)
Set<String> allowedWriteLocations();

static StorageCredentialCacheKey of(
String realmId,
PolarisEntity entity,
boolean allowedListAction,
Set<String> allowedReadLocations,
Set<String> allowedWriteLocations) {
this.realmId = realmId;
this.catalogId = entity.getCatalogId();
this.storageConfigSerializedStr =
String storageConfigSerializedStr =
entity
.getInternalPropertiesAsMap()
.get(PolarisEntityConstants.getStorageConfigInfoPropertyName());
this.entityId = entity.getId();
this.allowedListAction = allowedListAction;
this.allowedReadLocations = allowedReadLocations;
this.allowedWriteLocations = allowedWriteLocations;
}

public String getRealmId() {
return realmId;
}

public long getCatalogId() {
return catalogId;
}

public String getStorageConfigSerializedStr() {
return storageConfigSerializedStr;
}

public long getEntityId() {
return entityId;
}

public boolean isAllowedListAction() {
return allowedListAction;
}

public Set<String> getAllowedReadLocations() {
return allowedReadLocations;
}

public Set<String> getAllowedWriteLocations() {
return allowedWriteLocations;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StorageCredentialCacheKey cacheKey = (StorageCredentialCacheKey) o;
return Objects.equals(realmId, cacheKey.getRealmId())
&& catalogId == cacheKey.getCatalogId()
&& Objects.equals(storageConfigSerializedStr, cacheKey.getStorageConfigSerializedStr())
&& allowedListAction == cacheKey.allowedListAction
&& Objects.equals(allowedReadLocations, cacheKey.allowedReadLocations)
&& Objects.equals(allowedWriteLocations, cacheKey.allowedWriteLocations);
}

@Override
public int hashCode() {
return Objects.hash(
return ImmutableStorageCredentialCacheKey.of(
realmId,
catalogId,
entity.getCatalogId(),
storageConfigSerializedStr,
allowedListAction,
allowedReadLocations,
allowedWriteLocations);
}

@Override
public String toString() {
return "StorageCredentialCacheKey{"
+ "realmId="
+ realmId
+ ", catalogId="
+ catalogId
+ ", storageConfigSerializedStr='"
+ storageConfigSerializedStr
+ '\''
+ ", entityId="
+ entityId
+ ", allowedListAction="
+ allowedListAction
+ ", allowedReadLocations="
+ allowedReadLocations
+ ", allowedWriteLocations="
+ allowedWriteLocations
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testCacheEvict() throws InterruptedException {
1, 2, PolarisEntityType.CATALOG, PolarisEntitySubType.ICEBERG_TABLE, 0, "name");
PolarisEntity polarisEntity = new PolarisEntity(baseEntity);
StorageCredentialCacheKey cacheKey =
new StorageCredentialCacheKey(
StorageCredentialCacheKey.of(
callCtx.getRealmContext().getRealmIdentifier(),
polarisEntity,
true,
Expand Down
Loading