Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ private UCHadoopConf() {
public static final String UC_URI_KEY = "fs.unitycatalog.uri";
public static final String UC_TOKEN_KEY = "fs.unitycatalog.token";

// Key representing the remaining time before expiration, used to trigger credentials renewal in
// advance.
public static final String UC_RENEWAL_LEAD_TIME_KEY = "fs.unitycatalog.renewal.leadTimeMillis";
public static final long UC_RENEWAL_LEAD_TIME_DEFAULT_VALUE = 30_000L;

// Key for specifying the manual clock, for testing purpose.
public static final String UC_TEST_CLOCK_NAME = "fs.unitycatalog.test.clock.name";

// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private class UCProxy(
properties = t.getProperties.asScala.toMap ++ extraSerdeProps
),
schema = StructType(fields),
provider = Some(t.getDataSourceFormat.getValue),
provider = Some(t.getDataSourceFormat.getValue.toLowerCase()),

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.

If we don't lower case here, then we will encounter another problem: #1121 (comment)

createTime = t.getCreatedAt,
tracksPartitionsInCatalog = false,
partitionColumnNames = partitionCols.sortBy(_._2).map(_._1).toSeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.unitycatalog.client.model.AzureUserDelegationSAS;
import io.unitycatalog.spark.UCHadoopConf;
import io.unitycatalog.spark.utils.Clock;
import org.apache.hadoop.fs.azurebfs.extensions.SASTokenProvider;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.com.google.common.base.Preconditions;
Expand All @@ -13,10 +12,6 @@ public class AbfsVendedTokenProvider extends GenericCredentialProvider implement
public AbfsVendedTokenProvider() {
}

AbfsVendedTokenProvider(Clock clock, long renewalLeadTimeMillis) {
super(clock, renewalLeadTimeMillis);
}

@Override
public void initialize(Configuration conf, String accountName) {
initialize(conf);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.unitycatalog.spark.auth;

import io.unitycatalog.spark.UCHadoopConf;
import io.unitycatalog.spark.utils.Clock;
import org.apache.hadoop.conf.Configuration;
import org.sparkproject.guava.base.Preconditions;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
Expand All @@ -18,17 +17,11 @@ public AwsVendedTokenProvider(Configuration conf) {
initialize(conf);
}

AwsVendedTokenProvider(Configuration conf, Clock clock, long renewalLeadTimeMillis) {
super(clock, renewalLeadTimeMillis);
initialize(conf);
}

@Override
public GenericCredential initGenericCredential(Configuration conf) {
if (conf.get(UCHadoopConf.S3A_INIT_ACCESS_KEY) != null
&& conf.get(UCHadoopConf.S3A_INIT_SECRET_KEY) != null
&& conf.get(UCHadoopConf.S3A_INIT_SESSION_TOKEN) != null
&& conf.get(UCHadoopConf.S3A_INIT_CRED_EXPIRED_TIME) != null) {
&& conf.get(UCHadoopConf.S3A_INIT_SESSION_TOKEN) != null) {

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.

   && conf.get(UCHadoopConf.S3A_INIT_CRED_EXPIRED_TIME) != null) {

why is this removed?

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.

Because the expiredTime is allowed to be null. When I design this PR, and the original code did not set any expiration time when generating the credential from the UC server, Please see: https://github.com/unitycatalog/unitycatalog/pull/1123/files#diff-5084dc98e0f29a934bf2486de5be2667d7c6d375beb3e23add41a3bbd42554fbR72-R74

And I found we won't correctly initialize the GenericCredentials because of the conf.get(UCHadoopConf.S3A_INIT_CRED_EXPIRED_TIME) != null checking, since it's always null. (The UC server does not set it ).

The above is the code investigation. From another side, if the temporaryCredentialsApi just did not get the expiration time for credential, but actually the credentials is generated, then actually we will never use it a the initial GenericCredentials.


String accessKey = conf.get(UCHadoopConf.S3A_INIT_ACCESS_KEY);
String secretKey = conf.get(UCHadoopConf.S3A_INIT_SECRET_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import org.sparkproject.guava.cache.CacheBuilder;

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

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.

This can be removed, because we just introduced a hadoop configuration key, so that we can also set it the test suites. That will be more convinient to do the integration testing, since for the IT, we don't have API to explicitly use the GenericCredentialsProvider's constructor to pass renewalLeadTime to construct --- All the providers are initialized via the java reflection.

The only approach is using the hadoop configuration setting.


// 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 =
Expand All @@ -31,10 +28,9 @@ public abstract class GenericCredentialProvider {
globalCache = CacheBuilder.newBuilder().maximumSize(maxSize).build();
}

private final Clock clock;
private final long renewalLeadTimeMillis;

private Configuration conf;
private Clock clock;
private long renewalLeadTimeMillis;
private URI ucUri;
private String ucToken;
private String credUid;
Expand All @@ -43,18 +39,17 @@ public abstract class GenericCredentialProvider {
private volatile GenericCredential credential;
private volatile TemporaryCredentialsApi tempCredApi;

public GenericCredentialProvider() {
this(Clock.systemClock(), DEFAULT_RENEWAL_LEAD_TIME_MILLIS);
}

GenericCredentialProvider(Clock clock, long renewalLeadTimeMillis) {
this.clock = clock;
this.renewalLeadTimeMillis = renewalLeadTimeMillis;
}

protected void initialize(Configuration conf) {
this.conf = conf;

// Use the test clock if one is intentionally configured for testing.
String clockName = conf.get(UCHadoopConf.UC_TEST_CLOCK_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.

Use the intentionally configured clockName to access the manualClock from the globalManualClock, so that all of those infra can share the same manualClock, then we can use this shared manualClock to control all the infra's time.

  • OSS UC server: Need this clock to generate credentials.
  • AwsVendedTokenProvider: Need this clock to determine if it's time to renew.
  • IntegrationTest: Need this clock to advance the time step by step.

And considering we cannot pass the clock instance and make it go-through all the stack, so introducing a configure key to selectively access the clock from global should be the correct approach. --- But we need to remember to clean the newly allocated clock if we created one in the test. Otherwise, no big impact just accumulating some tesing clocks in the testing process.

this.clock = clockName != null ? Clock.getManualClock(clockName) : Clock.systemClock();

this.renewalLeadTimeMillis = conf.getLong(
UCHadoopConf.UC_RENEWAL_LEAD_TIME_KEY,
UCHadoopConf.UC_RENEWAL_LEAD_TIME_DEFAULT_VALUE);

String ucUriStr = conf.get(UCHadoopConf.UC_URI_KEY);
Preconditions.checkNotNull(ucUriStr,
"'%s' is not set in hadoop configuration", UCHadoopConf.UC_URI_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public interface Clock {
/**
Expand All @@ -23,6 +25,17 @@ static Clock manualClock(Instant now) {
return new ManualClock(now);
}


static Clock getManualClock(String name) {
return ManualClock.globalManualClock.compute(name, (clockName, clock) ->
clock == null ? manualClock(Instant.now()) : clock
);
}

static void removeManualClock(String name) {
ManualClock.globalManualClock.remove(name);
}

class SystemClock implements Clock {
private static final SystemClock SINGLETON = new SystemClock();

Expand All @@ -38,6 +51,8 @@ public void advance(Duration duration) {
}

class ManualClock implements Clock {
private static final Map<String, Clock> globalManualClock = new ConcurrentHashMap<>();

private volatile Instant now;

ManualClock(Instant now) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class AzureCredentialTestFileSystem extends CredentialTestFileSystem {
@Override
void checkCredentials(Path f) {
protected void checkCredentials(Path f) {
Configuration conf = getConf();
String host = f.toUri().getHost();
if (credentialCheckEnabled) {
Expand All @@ -24,7 +24,7 @@ void checkCredentials(Path f) {
}

@Override
String scheme() {
protected String scheme() {
return "abfs:";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public abstract class CredentialTestFileSystem extends RawLocalFileSystem {
public static boolean credentialCheckEnabled = true;

abstract String scheme();
protected abstract String scheme();

@Override
protected void checkPath(Path path) {
Expand Down Expand Up @@ -75,5 +75,5 @@ private Path toLocalPath(Path f) {
return new Path(f.toString().replaceAll(scheme() + "//.*?/", "file:///"));
}

abstract void checkCredentials(Path f);
protected abstract void checkCredentials(Path f);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class GCSCredentialTestFileSystem extends CredentialTestFileSystem {
@Override
void checkCredentials(Path f) {
protected void checkCredentials(Path f) {
Configuration conf = getConf();
String host = f.toUri().getHost();
if (credentialCheckEnabled) {
Expand All @@ -26,7 +26,7 @@ void checkCredentials(Path f) {
}

@Override
String scheme() {
protected String scheme() {
return "gs:";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void initialize(URI uri, Configuration conf) throws IOException {
}

@Override
void checkCredentials(Path f) {
protected void checkCredentials(Path f) {
Configuration conf = getConf();
String host = f.toUri().getHost();

Expand Down Expand Up @@ -72,7 +72,7 @@ private AwsCredentialsProvider accessProvider(Configuration conf) {
}

@Override
String scheme() {
protected String scheme() {
return "s3:";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.unitycatalog.client.model.AzureUserDelegationSAS;
import io.unitycatalog.client.model.TemporaryCredentials;
import io.unitycatalog.spark.UCHadoopConf;
import io.unitycatalog.spark.utils.Clock;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.azurebfs.AbfsConfiguration;
import org.apache.hadoop.fs.azurebfs.extensions.SASTokenProvider;
Expand All @@ -19,22 +18,14 @@
public class AbfsVendedTokenProviderTest extends BaseTokenProviderTest<AbfsVendedTokenProvider> {
@Override
protected AbfsVendedTokenProvider createTestProvider(
Clock clock,
long renewalLeadTimeMillis,

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.

we just changed to set configuration keys in the hadoop configuration (for both clock and renewLeadTimeMillis ). So we don't need those contructors any more.

As I saide above here: https://github.com/unitycatalog/unitycatalog/pull/1123/files#r2461390293

Configuration conf,
TemporaryCredentialsApi mockApi) {
return new TestAbfsVendedTokenProvider(clock, renewalLeadTimeMillis, conf, mockApi);
Configuration conf, TemporaryCredentialsApi mockApi) {
return new TestAbfsVendedTokenProvider(conf, mockApi);
}

static class TestAbfsVendedTokenProvider extends AbfsVendedTokenProvider {
private final TemporaryCredentialsApi mockApi;

TestAbfsVendedTokenProvider(
Clock clock,
long renewalLeadTimeMillis,
Configuration conf,
TemporaryCredentialsApi mockApi) {
super(clock, renewalLeadTimeMillis);
TestAbfsVendedTokenProvider(Configuration conf, TemporaryCredentialsApi mockApi) {
initialize(conf);
this.mockApi = mockApi;
}
Expand Down
Loading