Skip to content

Add GenericCredential cache to reuse the table-level / path-level credential in the vended token provider. - #1108

Merged
tdas merged 6 commits into
unitycatalog:mainfrom
openinx:1103-cred-cache
Oct 13, 2025
Merged

tdas merged 6 commits into
unitycatalog:mainfrom
openinx:1103-cred-cache

Conversation

@openinx

@openinx openinx commented Oct 9, 2025

Copy link
Copy Markdown
Collaborator

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

Related issue: #1103

private final String uri;
private final String token;
private final String tableId;
private final TableOperation tableOp;

@openinx openinx Oct 9, 2025 •

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.

The cache key for a temporary table cred, must identify by the following 4 fields:

  • uri --> identify UC server.
  • token --> identify the account.
  • tableId --> identify the table.
  • tableOp --> identify the table operation.

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.

how are we going to make sure that the same credential is not going to be reused across jobs? we want to ensure that every job users a new set of credentials... so that if permissions are revoked the next job immediately fails.

@openinx openinx Oct 10, 2025 •

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.

Thanks for the great question.

If the spark job is deployed in YARN, or k8s, per job per cluster. It should be fine, since for every job termination, the process level cache will be invalid.

But if the spark job is deployed in standalone, then all those jobs will share the same executors and driver. Then the shared cache from one job will be easily accessible for another job. The standalone cluster usually owned by one user, then it should be fine for different jobs from the same user to share the credential cache. I mean reusing the parallel jobs' refreshed credential for another job, to avoiding an extra credential refreshing.

As you said, the question is: what if the credential from one job is revoked, then other related jobs need to fail immediately. I think we can think the two cases:

  1. If people start a new job, it should immediately fail, since we always need to initialize the credentials for the first initializaiton ( and then set them in the hadoop conf, finaly dispatching from driver to executors ). For the initialization, it will fail fast.

  2. If one job (A)'s credential is revoked, another running job (B) is reusing this credentials. Then when the job B send http requests to the s3, the s3 should also detect the revoked credential, and fail the job B. (Another question is: will it make sense to fail the Job B because of the Job A's credential revocation ?).

In summary, for yarn & k8s, that's should be fine. for standalone cluster, fast-failure should also be fine. Seems like have some corner cases we need to address.

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.

Do you think we need to add the jobId dimenssion in those key ? If so, how can we corrently get the jobId and finally set it ?

UCHadoopConf.UC_GLOBAL_CREDENTIAL_CACHE_ENABLED_VALUE);

// Init the generic credential key for caching.
this.genericCredKey = createGenericCredKey(conf);

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.

For each GenericCredentialProvider, it should have only one key... for identifying the cache entry.

@openinx openinx closed this Oct 9, 2025
@openinx openinx reopened this Oct 9, 2025
@openinx openinx closed this Oct 9, 2025
@openinx openinx reopened this Oct 9, 2025
// Key to enable the global credential cache.
public static final String UC_GLOBAL_CREDENTIAL_CACHE_ENABLED_KEY =
"unitycatalog.global.credential.cache.enabled";
public static final boolean UC_GLOBAL_CREDENTIAL_CACHE_ENABLED_VALUE = true;

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.

did you meant this to be the default value? if so, then please put default in the name.

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.

Yes, it's the default value. Done, thanks!

public static final String UC_CREDENTIALS_TYPE_PATH_VALUE = "path";

// Key to enable the global credential cache.
public static final String UC_GLOBAL_CREDENTIAL_CACHE_ENABLED_KEY =

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.

nit: you can remove _GLOBAL .. just CREDENTIAL_CACHE is good enough. its understood that the cache is global.

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.

Done, thanks for the comment !

@tdas tdas left a comment

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.

havent gone through the tests in full detail yet. but have a few comments.

private final String uri;
private final String token;
private final String tableId;
private final TableOperation tableOp;

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.

how are we going to make sure that the same credential is not going to be reused across jobs? we want to ensure that every job users a new set of credentials... so that if permissions are revoked the next job immediately fails.

Comment on lines +22 to +28
public String tableId() {
return tableId;
}

public TableOperation tableOp() {
return tableOp;
}

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.

do you need these public methods? once you define the key, do you need to access the key's details?

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.


return new GenericCredential(tempCred);
private GenericCredential renewCredential() {
if (globalCredCacheEnabled) {

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.

why do we need a flag for enabling? when do we want it to be disabled?

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.

I'm considering if in future we want to enable the fs impl cache, the filesystem is mapping to one single provider. and the filesystem have already been cached, then we finally may no need this credential. Just like the current S3AFileSystem, which have a single credentialsProvider with having the latest valid credential only.

@openinx

openinx commented Oct 10, 2025

Copy link
Copy Markdown
Collaborator Author

The MLflow-UC integration test is timeout , https://github.com/unitycatalog/unitycatalog/actions/runs/18413247521/job/52470796766?pr=1108 , which I think it's unrelated to this PR. we can retry the CI.

Waiting for UC server... (42/48)
Waiting for UC server... (43/48)
Waiting for UC server... (44/48)
Waiting for UC server... (45/48)
Waiting for UC server... (46/48)
Waiting for UC server... (47/48)
Waiting for UC server... (48/48)
ai/dev/start_unitycatalog.sh: line 36: kill: (2152) - No such process
UC server failed to start within 240 seconds
Error: Process completed with exit code 1.

…talog#1098)

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [x] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Implement this aws token provider for this issue:
unitycatalog#1096. This provider
can accept the initialized credentials provider for the first time, and
then it will automatically renew the credentials before the expiration
time. So that the provider can always deliver the valid credentials to
the upper layer.

---------

Signed-off-by: Zheng Hu <[email protected]>
…dential in the vended token provider.

Signed-off-by: Zheng Hu <[email protected]>

@BeforeEach
public void before() {
GenericCredentialProvider.CACHE.invalidateAll();

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.

The invalidateAll will clean all the cached entries, for preparing a clean test evironment.

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

@tdas tdas left a comment

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.

only a few minor comments left

// The credential cache, for saving QPS to unity catalog server.
static final Cache<String, GenericCredential> CACHE = CacheBuilder
.newBuilder()
.maximumSize(1000)

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.

lets make this configurable in the conf.

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.

are you sure its LRU?

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.

lets make this configurable in the conf.

Sounds good to me, let me fix this.

are you sure its LRU?

https://github.com/google/guava/wiki/cachesexplained#size-based-eviction

According to the document, The cache will try to evict entries that haven't been used recently or very often. Warning: the cache may evict entries before this limit is exceeded -- typically when the cache size is approaching the limit. . so by default, guava evicts entries using an approximation of LRU (Least Recently Used) policy. I think it should be fine here.

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.

lets make this configurable in the conf.

Since the globalCache is a static global reference, so we cannot use the GenericCredentialProvider instance's Configuration to set the max size, otherwise different Configuration will set different max size for the globalCache, which may lead to confusion.

A better way is using the system's property to set the cache max size, then it should provide a consistent global cache size.

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> CACHE = CacheBuilder

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.

typically constants are capitalized .. isnt it?
maybe just call this globalCache

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.

Yes, make sense. for constant values, we usually capitalize it. while for a mutable object referenece we won't. Thanks for pointing it out !


TemporaryCredentialsApi tempCredApi = mock(TemporaryCredentialsApi.class);
// For TableA's 1st renewal
TemporaryCredentials cred1 = newAwsTempCred("1", System.currentTimeMillis() + 2000L);

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.

the path creds and table creds are completely independent of each other. rather than duplicate the code, why not put it in a loop. something like this

for (String isPath: {true, false}) {
   // define confA, confB, credA1, credB1, credA2, credB2
   if (isPath) {
      when(tempCredApi.generateTemporaryPathCredentials(any())) .... 
   } else {
      when(tempCredApi.generateTemporaryTableCredentials(any()))   
   }
   // further tests
}

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.

Hi @tdas , thanks for the comment. The path creds and table creds actually are not completely independent, since the cache is a global cache, we want to put the mixed creds with different types of path creds and table creds, into the global cache. And for each renewal, we expect the global cache can always get the correct cred corressponding to its type ( path cred or table cred).

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.

ahh.. good point.

but would have been easier to read if objects were named clearly.. which is path and which is table. like pathAConf, pathBConf, pathACred1, pathACred2, ... and similarly tableAConf, tableBConf, ...

// 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.

@tdas
tdas merged commit 0257c25 into unitycatalog:main Oct 13, 2025
@tdas

tdas commented Oct 13, 2025

Copy link
Copy Markdown
Contributor

merged your PR, but please address the last 2 comments in follow up PRs. thank you!

timothyw553 pushed a commit to timothyw553/unitycatalog that referenced this pull request Oct 24, 2025
…dential in the vended token provider. (unitycatalog#1108)

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [x] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Related issue: unitycatalog#1103

---------

Signed-off-by: Zheng Hu <[email protected]>
Signed-off-by: openinx <[email protected]>
Signed-off-by: TimothyW553 <[email protected]>
openinx added a commit to openinx/unitycatalog that referenced this pull request Nov 7, 2025
…dential in the vended token provider. (unitycatalog#1108)

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [x] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Related issue: unitycatalog#1103

---------

Signed-off-by: Zheng Hu <[email protected]>
Signed-off-by: openinx <[email protected]>
tdas pushed a commit that referenced this pull request Nov 10, 2025
…el credential in the vended token provider (#1174)

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [x] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

Related issues: 
* #1103
* #1163

Backport the PR #1108
to the branch-0.3

Signed-off-by: Zheng Hu <[email protected]>
Signed-off-by: openinx <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants