Repository navigation
Add GenericCredential cache to reuse the table-level / path-level credential in the vended token provider. #1108
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
Changes from all commits
860e0f5
3e5f48a
5d2b45c
da13b4f
3ebcfc6
45b48d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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"; | ||||||
|
Contributor
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.
Suggested change
typically we each
|
||||||
| 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; | ||||||
|
|
@@ -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); | ||||||
| } | ||||||
|
|
@@ -50,8 +73,8 @@ public GenericCredential accessCredentials() { | |||||
| synchronized (this) { | ||||||
| if (credential == null || credential.readyToRenew(renewalLeadTimeMillis)) { | ||||||
| try { | ||||||
| credential = createGenericCredentials(); | ||||||
| } catch (Exception e) { | ||||||
| credential = renewCredential(); | ||||||
|
Collaborator
Author
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. Different threads access the same provider, should be safe here since we've the |
||||||
| } catch (ApiException e) { | ||||||
| throw new RuntimeException(e); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -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(); | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.