Skip to content
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 @@ -15,6 +15,11 @@ private UCHadoopConf() {
public static final String UC_URI_KEY = "unitycatalog.uri";
public static final String UC_TOKEN_KEY = "unitycatalog.token";

// Key representing a unique credential ID. It identifies a job-level credential for a specific
// table, meaning that the same job–table combination shares the same credential. Cached
// credentials are indexed by this key and are not reused across different jobs.
public static final String UC_CREDENTIALS_UID_KEY = "unitycatalog.credentials.uid";
Comment thread
tdas marked this conversation as resolved.
Resolved

// Keys for table based temporary credential requests
public static final String UC_TABLE_ID_KEY = "unitycatalog.table.id";
public static final String UC_TABLE_OPERATION_KEY = "unitycatalog.table.operation";
Expand All @@ -27,4 +32,9 @@ private UCHadoopConf() {
public static final String UC_CREDENTIALS_TYPE_KEY = "unitycatalog.credentials.type";
public static final String UC_CREDENTIALS_TYPE_TABLE_VALUE = "table";
public static final String UC_CREDENTIALS_TYPE_PATH_VALUE = "path";

// Key to enable the credential cache.
public static final String UC_CREDENTIAL_CACHE_ENABLED_KEY =
"unitycatalog.credential.cache.enabled";
public static final boolean UC_CREDENTIAL_CACHE_ENABLED_DEFAULT_VALUE = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.unitycatalog.spark.UCHadoopConf;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.com.google.common.base.Preconditions;
import org.sparkproject.guava.base.Preconditions;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.unitycatalog.client.model.AwsCredentials;
import io.unitycatalog.client.model.TemporaryCredentials;
import java.util.Objects;

public class GenericCredential {
private final TemporaryCredentials tempCred;
Expand Down Expand Up @@ -44,4 +45,23 @@ public boolean readyToRenew(long renewalLeadTimeMillis) {
return tempCred.getExpirationTime() != null &&
tempCred.getExpirationTime() <= System.currentTimeMillis() + renewalLeadTimeMillis;
}

@Override
public int hashCode() {
return Objects.hashCode(tempCred);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

GenericCredential that = (GenericCredential) o;
return Objects.equals(tempCred, that.tempCred);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@
import io.unitycatalog.spark.UCHadoopConf;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.com.google.common.base.Preconditions;
import org.sparkproject.guava.base.Preconditions;
import org.sparkproject.guava.cache.Cache;
import org.sparkproject.guava.cache.CacheBuilder;

public abstract class GenericCredentialProvider {
// The time remaining until expiration, we will try to renew the credential before the expiration
// time.
// The remaining time before expiration, used to trigger renewal in advance.
private static final long DEFAULT_RENEWAL_LEAD_TIME_MILLIS = 30 * 1000;

// The credential cache, for saving QPS to unity catalog server.
static final Cache<String, GenericCredential> globalCache;
private static final String UC_CREDENTIAL_CACHE_MAX_SIZE =
"unitycatalog.credential.cache.max.size";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"unitycatalog.credential.cache.max.size";
"unitycatalog.credential.cache.maxSize";

typically we each .<string>. is a scope under which there can be many more confs like <scope>.<key1>m <scope>.<key2>. so its usually a component or a concept, like cache or credential, and sub keys are tied to that component or concept.

max. is a not a component or concept. rather "max size" is tied to the cache component.

private static final long UC_CREDENTIAL_CACHE_MAX_SIZE_DEFAULT = 1024;

static {
long maxSize = Long.getLong(UC_CREDENTIAL_CACHE_MAX_SIZE, UC_CREDENTIAL_CACHE_MAX_SIZE_DEFAULT);
globalCache = CacheBuilder.newBuilder().maximumSize(maxSize).build();
}

private final Configuration conf;
private final URI ucUri;
private final String ucToken;
private final String credUid;
private final boolean credCacheEnabled;

private volatile long renewalLeadTimeMillis = DEFAULT_RENEWAL_LEAD_TIME_MILLIS;
private volatile GenericCredential credential;
Expand All @@ -39,6 +53,15 @@ public GenericCredentialProvider(Configuration conf) {
"'%s' is not set in hadoop configuration", UCHadoopConf.UC_TOKEN_KEY);
this.ucToken = conf.get(UCHadoopConf.UC_TOKEN_KEY);

this.credUid = conf.get(UCHadoopConf.UC_CREDENTIALS_UID_KEY);
Preconditions.checkState(credUid != null && !credUid.isEmpty(),
"Credential UID cannot be null or empty, '%s' is not set in hadoop configuration",
UCHadoopConf.UC_CREDENTIALS_UID_KEY);

this.credCacheEnabled = conf.getBoolean(
UCHadoopConf.UC_CREDENTIAL_CACHE_ENABLED_KEY,
UCHadoopConf.UC_CREDENTIAL_CACHE_ENABLED_DEFAULT_VALUE);

// The initialized credentials passing-through the hadoop configuration.
this.credential = initGenericCredential(conf);
}
Expand All @@ -50,8 +73,8 @@ public GenericCredential accessCredentials() {
synchronized (this) {
if (credential == null || credential.readyToRenew(renewalLeadTimeMillis)) {
try {
credential = createGenericCredentials();
} catch (Exception e) {
credential = renewCredential();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Different threads access the same provider, should be safe here since we've the synchronzied(this). However the Cache is global and process level, so we still need to lock the CACHE for its renewal implementation. Please see: https://github.com/unitycatalog/unitycatalog/pull/1108/files#diff-68d2e51ab5a4e6186ba02fecda9958db316feac1f8397a17c1da4cba334be188R102

} catch (ApiException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -79,6 +102,24 @@ protected TemporaryCredentialsApi temporaryCredentialsApi() {
return tempCredApi;
}

private GenericCredential renewCredential() throws ApiException {
if (credCacheEnabled) {
synchronized (globalCache) {
GenericCredential cached = globalCache.getIfPresent(credUid);
// Use the cached one if existing and valid.
if (cached != null && !cached.readyToRenew(renewalLeadTimeMillis)) {
return cached;
}
// Renew the credential and update the cache.
GenericCredential renewed = createGenericCredentials();
globalCache.put(credUid, renewed);
return renewed;
}
} else {
return createGenericCredentials();
}
}

private GenericCredential createGenericCredentials() throws ApiException {
TemporaryCredentialsApi tempCredApi = temporaryCredentialsApi();

Expand Down
Loading