Skip to content

[Spark] Refresh the s3 credential automatically so that we can support long-running spark jobs. - #1087

Closed
openinx wants to merge 15 commits into
unitycatalog:mainfrom
openinx:refresh-s3-cred
Closed

openinx wants to merge 15 commits into
unitycatalog:mainfrom
openinx:refresh-s3-cred

Conversation

@openinx

@openinx openinx commented Sep 26, 2025

Copy link
Copy Markdown
Collaborator

The aws s3 credentials refresh solution for this issue: #1086

@openinx
openinx marked this pull request as draft September 26, 2025 23:55
TemporaryCredentialsApi tempCredApi = new TemporaryCredentialsApi(apiClient);

// Get the temp credential request.
String jsonRequest = conf.get("fs.s3a.unitycatalog.request");

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.

this has to be fs.s3a.unitycatalog.credential.request

@openinx
openinx marked this pull request as ready for review October 3, 2025 22:13
public class Constants {
public Constants() {}

public static final String UNITY_CATALOG_URI = "fs.unitycatalog.uri";

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: lets shorten these constant variable by

  • either UNITY_CATALOG_ --> UC_
  • or ... we name the class to UCConstants and remove the UC prefix completely. everything will use UCConstants.URI

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.

actually... on a different note. are all these constants tied to HadoopConf only?

then it might be worthwhile to put that information in the naming somewhere. Say UCHadoopConfs instead of generic UCConstants.

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 comments. I think it's fair, let me reflect in the new PR.

public AwsVendedTokenProvider(URI ignored, Configuration conf) {
this.conf = conf;
this.uri = URI.create(conf.get(Constants.UNITY_CATALOG_URI));
this.token = conf.get(Constants.UNITY_CATALOG_TOKEN);

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.

if these are not set, then how should we handle failures? does conf.get() return null if not set?

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 think here using Preconditions.checkNotNull should be enough, to throw the exception to the upper layer. Let me update it in the new PR. #1096

TemporaryCredentialsApi tempCredApi = temporaryCredentialsApi();

// Generate the temporary credential via requesting UnityCatalog.
TemporaryCredentials tempCred;

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.

where are you using the initial hadoop credential acquired before job start?

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 basic idea is that we request the credential in the spark driver side, and then set it in the hadoopConf, and finally pass them to the executors. The new PR implement this: https://github.com/unitycatalog/unitycatalog/pull/1098/files#diff-9116a150ee4790736eb5b39d5ef0f4c16f40db3286a997c4676a93562196af49R40-R52

public AwsCredentials resolveCredentials() {
if(awsS3Credentials == null || awsS3Credentials.readyToRenew()) {
synchronized (this) {
if(awsS3Credentials == null || awsS3Credentials.readyToRenew()) {

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.

this provider object will be recreated on every file access (since we disabled FS cache) ...isnt it? so we need to cache the credentials outside this objects lifecycle isnt it?

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.

Oh, thanks for the great point ! I think you are right, we may need a global credentials cache. Let me take a close look at this part.

public static final String UNITY_CATALOG_PATH = "fs.unitycatalog.path";
public static final String UNITY_CATALOG_PATH_OPERATION = "fs.unitycatalog.path.operation";

public static final String UNITY_CATALOG_CREDENTIALS_TYPE = "fs.unitycatalog.credentials.type";

@tdas tdas Oct 6, 2025 •

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 the conf key strings have _KEY at the end of constant var name. for example ... ...CREDENTIALS_TYPE_KEY.

this is to distinguish the keys string from the value strings

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.

Cool, that makes more clear !

Comment on lines 215 to 217
"fs.s3a.access.key" -> awsCredentials.getAccessKeyId,
"fs.s3a.secret.key" -> awsCredentials.getSecretAccessKey,
"fs.s3a.session.token" -> awsCredentials.getSessionToken,

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.

in a separate PR, it would be good to refactor these constant key values to the same place as the new keys like URI

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, will do that. Thank you for the comment !

public class AwsVendedTokenProviderTest {

@Test
public void testTableBasedTemporaryCredentialsRenew() throws Exception {

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 testing all the negative cases ... like url and token are not set in the hadoop conf. we should fail gracefully (e.g. keep using initial hadoop conf until expiry and fail only when renew needed).. and with good error messages (not null pointer exceptions, etc.)

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 pointing this out. Will work on the new PR to address those comments !

// Time-travel to before the last insert, we should only see the first inserted row.
validateTimeTravelDeltaTable(session.sql("SELECT * FROM " + t1 + " VERSION AS OF 1"));
validateTimeTravelDeltaTable(session.sql("SELECT * FROM " + t1 + " TIMESTAMP AS OF '" + timestamp + "'"));
validateTimeTravelDeltaTable(

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 the existing tests testing table level isolation? same query on separate tables use different credentials correctly?

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 think it's because we don't enforce the codestyle check before PR merging, so after I use the command to format the code, then it will automatically format this unrelated code.

./build/sbt javafmtAll

I have an issue for this before: #1099

Comment on lines +218 to +220
Constants.UNITY_CATALOG_URI -> unityCatalogURL,
Constants.UNITY_CATALOG_TOKEN -> unityCatalogToken,
"fs.s3a.aws.credentials.provider" -> classOf[AwsVendedTokenProvider].getName,

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.

there is risk that in some environment this new stuff can cause errors while loading the provider. lets use a separate spark conf to enable the renew for now. we can always set it to be enable by default by changing the default value of the flag after we have gained confidence.

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.

there is risk that in some environment this new stuff can cause errors while loading the provider

Agreed, there is risk. If someone encounter this error and if he/she want to acess the UC, then the only way that I can think of is to fix it, because finally they need this AwsVendedTokenProvider to create the temporary credentials. There is no other way. If this is correct, then seem no neccessary to introduce another switch to control enable the AwsVendedTokenProvider or not.

Please correct me if I misunderstood, thanks for the comment !

@openinx

openinx commented Nov 6, 2025

Copy link
Copy Markdown
Collaborator Author

All aws s3 credentials provider with credential renewal PRs was merged now, close this one. Thanks @tdas for the all the reviewing work.

@openinx openinx closed this Nov 6, 2025
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