Skip to content

Commit 3927772

Browse files
committed
fix
1 parent d239323 commit 3927772

File tree

11 files changed

+473
-26
lines changed

11 files changed

+473
-26
lines changed

paimon-core/src/main/java/org/apache/paimon/rest/RESTClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.apache.paimon.rest.auth.RESTAuthFunction;
2222

23-
import java.io.Closeable;
2423
import java.util.Map;
2524

2625
/** Interface for a basic HTTP Client for interfacing with the REST catalog. */

paimon-core/src/main/java/org/apache/paimon/rest/RESTTokenFileIO.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,13 @@ private void tryToRefreshToken() {
192192
}
193193

194194
private boolean shouldRefresh() {
195-
return token == null || token.expireAtMillis() - System.currentTimeMillis() < TOKEN_EXPIRATION_SAFE_TIME_MILLIS;
195+
return token == null
196+
|| token.expireAtMillis() - System.currentTimeMillis()
197+
< TOKEN_EXPIRATION_SAFE_TIME_MILLIS;
196198
}
197199

198200
private void refreshToken() {
199-
LOG.info("begin refresh token for identifier [{}]", identifier);
201+
LOG.info("begin refresh data token for identifier [{}]", identifier);
200202
GetTableTokenResponse response;
201203
if (catalogInstance != null) {
202204
try {
@@ -212,7 +214,7 @@ private void refreshToken() {
212214
}
213215
}
214216
LOG.info(
215-
"end refresh token for identifier [{}] expiresAtMillis [{}]",
217+
"end refresh data token for identifier [{}] expiresAtMillis [{}]",
216218
identifier,
217219
response.getExpiresAtMillis());
218220

paimon-core/src/main/java/org/apache/paimon/rest/auth/AuthProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
/** Authentication provider. */
2424
public interface AuthProvider {
2525

26-
Map<String, String> mergeAuthHeader(Map<String, String> baseHeader, RESTAuthParameter restAuthParameter);
26+
Map<String, String> mergeAuthHeader(
27+
Map<String, String> baseHeader, RESTAuthParameter restAuthParameter);
2728
}

paimon-core/src/main/java/org/apache/paimon/rest/auth/AuthProviderFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
import org.apache.paimon.factories.Factory;
2222
import org.apache.paimon.factories.FactoryUtil;
2323
import org.apache.paimon.options.Options;
24-
import org.apache.paimon.rest.RESTCatalogOptions;
2524
import org.apache.paimon.utils.StringUtils;
2625

26+
import static org.apache.paimon.rest.RESTCatalogOptions.TOKEN_PROVIDER;
27+
2728
/** Factory for {@link AuthProvider}. */
2829
public interface AuthProviderFactory extends Factory {
2930

3031
AuthProvider create(Options options);
3132

3233
static AuthProvider createAuthProvider(Options options) {
33-
String tokenProvider = options.get(RESTCatalogOptions.TOKEN_PROVIDER);
34+
String tokenProvider = options.get(TOKEN_PROVIDER);
3435
if (StringUtils.isEmpty(tokenProvider)) {
3536
throw new IllegalArgumentException("token.provider is not set.");
3637
}

paimon-core/src/main/java/org/apache/paimon/rest/auth/DLFAuthProvider.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@
1818

1919
package org.apache.paimon.rest.auth;
2020

21+
import org.apache.paimon.annotation.VisibleForTesting;
22+
2123
import okhttp3.MediaType;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2226

2327
import javax.annotation.Nullable;
28+
2429
import java.time.ZoneOffset;
2530
import java.time.ZonedDateTime;
2631
import java.time.format.DateTimeFormatter;
2732
import java.util.HashMap;
2833
import java.util.Map;
2934

3035
import static org.apache.paimon.rest.RESTCatalog.TOKEN_EXPIRATION_SAFE_TIME_MILLIS;
36+
import static org.apache.paimon.utils.Preconditions.checkNotNull;
3137

3238
/** Auth provider for <b>Ali CLoud</b> DLF. */
3339
public class DLFAuthProvider implements AuthProvider {
3440

41+
private static final Logger LOG = LoggerFactory.getLogger(DLFAuthProvider.class);
42+
3543
public static final String DLF_AUTHORIZATION_HEADER_KEY = "Authorization";
3644
public static final String DLF_CONTENT_MD5_HEADER_KEY = "Content-MD5";
3745
public static final String DLF_CONTENT_TYPE_KEY = "Content-Type";
@@ -40,20 +48,16 @@ public class DLFAuthProvider implements AuthProvider {
4048
public static final String DLF_AUTH_VERSION_HEADER_KEY = "x-dlf-version";
4149
public static final String DLF_CONTENT_SHA56_HEADER_KEY = "x-dlf-content-sha256";
4250
public static final String DLF_CONTENT_SHA56_VALUE = "UNSIGNED-PAYLOAD";
43-
public static final double EXPIRED_FACTOR = 0.4;
4451

4552
public static final DateTimeFormatter AUTH_DATE_TIME_FORMATTER =
4653
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'");
4754
protected static final MediaType MEDIA_TYPE = MediaType.parse("application/json");
4855

49-
@Nullable
50-
private final DLFTokenLoader tokenLoader;
51-
@Nullable
52-
protected DLFToken token;
56+
@Nullable private final DLFTokenLoader tokenLoader;
57+
@Nullable protected DLFToken token;
5358
private final String region;
5459

55-
public static DLFAuthProvider fromTokenLoader(
56-
DLFTokenLoader tokenLoader, String region) {
60+
public static DLFAuthProvider fromTokenLoader(DLFTokenLoader tokenLoader, String region) {
5761
return new DLFAuthProvider(tokenLoader, null, region);
5862
}
5963

@@ -64,9 +68,7 @@ public static DLFAuthProvider fromAccessKey(
6468
}
6569

6670
public DLFAuthProvider(
67-
@Nullable DLFTokenLoader tokenLoader,
68-
@Nullable DLFToken token,
69-
String region) {
71+
@Nullable DLFTokenLoader tokenLoader, @Nullable DLFToken token, String region) {
7072
this.tokenLoader = tokenLoader;
7173
this.token = token;
7274
this.region = region;
@@ -75,7 +77,7 @@ public DLFAuthProvider(
7577
@Override
7678
public Map<String, String> mergeAuthHeader(
7779
Map<String, String> baseHeader, RESTAuthParameter restAuthParameter) {
78-
tryToRefreshToken();
80+
DLFToken token = getFreshToken();
7981
try {
8082
String dateTime =
8183
baseHeader.getOrDefault(
@@ -97,14 +99,27 @@ public Map<String, String> mergeAuthHeader(
9799
}
98100
}
99101

100-
private void tryToRefreshToken() {
102+
@VisibleForTesting
103+
DLFToken getFreshToken() {
101104
if (shouldRefresh()) {
102105
synchronized (this) {
103106
if (shouldRefresh()) {
104-
this.token = tokenLoader.loadToken();
107+
refreshToken();
105108
}
106109
}
107110
}
111+
return token;
112+
}
113+
114+
private void refreshToken() {
115+
checkNotNull(tokenLoader);
116+
LOG.info("begin refresh meta token for loader [{}]", tokenLoader.description());
117+
this.token = tokenLoader.loadToken();
118+
checkNotNull(token);
119+
LOG.info(
120+
"end refresh meta token for loader [{}] expiresAtMillis [{}]",
121+
tokenLoader.description(),
122+
token.getExpirationAtMills());
108123
}
109124

110125
private boolean shouldRefresh() {

paimon-core/src/main/java/org/apache/paimon/rest/auth/DLFECSTokenLoader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class DLFECSTokenLoader implements DLFTokenLoader {
5353
.readTimeout(Duration.ofMinutes(3))
5454
.build();
5555

56-
private String ecsMetadataURL;
56+
private final String ecsMetadataURL;
5757

5858
private String roleName;
5959

@@ -70,6 +70,11 @@ public DLFToken loadToken() {
7070
return getToken(ecsMetadataURL + roleName);
7171
}
7272

73+
@Override
74+
public String description() {
75+
return ecsMetadataURL;
76+
}
77+
7378
private static String getRole(String url) {
7479
try {
7580
return getResponseBody(url);

paimon-core/src/main/java/org/apache/paimon/rest/auth/DLFLocalFileTokenLoader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public DLFToken loadToken() {
4343
return readToken(tokenFilePath, 0);
4444
}
4545

46+
@Override
47+
public String description() {
48+
return tokenFilePath;
49+
}
50+
4651
protected static DLFToken readToken(String tokenFilePath, int retryTimes) {
4752
try {
4853
File tokenFile = new File(tokenFilePath);

paimon-core/src/main/java/org/apache/paimon/rest/auth/DLFToken.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonProperty;
2525

2626
import javax.annotation.Nullable;
27+
2728
import java.time.LocalDateTime;
28-
import java.time.ZoneOffset;
29+
import java.time.ZoneId;
2930
import java.time.format.DateTimeFormatter;
3031
import java.util.Objects;
3132

@@ -55,8 +56,7 @@ public class DLFToken {
5556
@Nullable
5657
private final String expiration;
5758

58-
@Nullable
59-
private final Long expirationAtMills;
59+
@Nullable private final Long expirationAtMills;
6060

6161
@JsonCreator
6262
public DLFToken(
@@ -72,7 +72,8 @@ public DLFToken(
7272
this.expirationAtMills = null;
7373
} else {
7474
LocalDateTime dateTime = LocalDateTime.parse(expiration, TOKEN_DATE_FORMATTER);
75-
this.expirationAtMills = dateTime.atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
75+
this.expirationAtMills =
76+
dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
7677
}
7778
}
7879

paimon-core/src/main/java/org/apache/paimon/rest/auth/DLFTokenLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
public interface DLFTokenLoader {
2323

2424
DLFToken loadToken();
25+
26+
String description();
2527
}

0 commit comments

Comments
 (0)