924060929 commented on code in PR #71031:
URL: https://github.com/apache/doris/pull/71031#discussion_r4750853709
##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/io/NativeGcsResolvingFileIO.java:
##########
@@ -39,9 +52,146 @@ public class NativeGcsResolvingFileIO extends
ResolvingFileIO {
private static final String GCS_FILE_IO_IMPL =
"org.apache.iceberg.gcp.gcs.GCSFileIO";
private static final String HADOOP_FILE_IO_IMPL =
"org.apache.iceberg.hadoop.HadoopFileIO";
+ // Clients are bounded by credential prefixes, not file locations, and are
rebuilt after serialization.
+ private transient Map<String, DelegateFileIO> scopedIos;
+ private volatile Boolean hadoopGcsConfigured;
+ private boolean closed;
+
+ public static boolean hasSdkGcsProperties(Map<String, String> properties) {
Review Comment:
[P3] `hasSdkGcsProperties()` appears unused in both production code and
tests. Consider removing it.
##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/io/NativeGcsResolvingFileIO.java:
##########
@@ -39,9 +52,146 @@ public class NativeGcsResolvingFileIO extends
ResolvingFileIO {
private static final String GCS_FILE_IO_IMPL =
"org.apache.iceberg.gcp.gcs.GCSFileIO";
private static final String HADOOP_FILE_IO_IMPL =
"org.apache.iceberg.hadoop.HadoopFileIO";
+ // Clients are bounded by credential prefixes, not file locations, and are
rebuilt after serialization.
+ private transient Map<String, DelegateFileIO> scopedIos;
+ private volatile Boolean hadoopGcsConfigured;
+ private boolean closed;
+
+ public static boolean hasSdkGcsProperties(Map<String, String> properties) {
+ return properties.keySet().stream().anyMatch(key -> key != null &&
key.startsWith("gcs."));
+ }
+
@Override
- String implFromLocation(String location) {
+ synchronized String implFromLocation(String location) {
String impl = super.implFromLocation(location);
- return GCS_FILE_IO_IMPL.equals(impl) ? HADOOP_FILE_IO_IMPL : impl;
+ if (!GCS_FILE_IO_IMPL.equals(impl)) {
+ return impl;
+ }
+ StorageCredential matching = matchingCredential(location);
+ Map<String, String> scope = matching == null ? Collections.emptyMap()
: matching.config();
+ // Validate the effective scope before choosing an identity: a present
null token is not an absent token.
+ GcsOAuthTokenValidator.validate(properties(), scope);
+ Map<String, String> effective = effectiveProperties(scope);
+ String impersonation =
effective.get("gcs.impersonate.service-account");
+ boolean sdkIdentity = effective.containsKey("gcs.oauth2.token")
+ || Boolean.parseBoolean(effective.get("gcs.no-auth"))
+ || (impersonation != null && !impersonation.isEmpty());
+ if (sdkIdentity) {
+ return GCS_FILE_IO_IMPL;
+ }
+ // Project selection is not authentication. SDK-only options must
never silently replace HMAC with ADC.
+ if (properties().containsKey("gs.access_key") ||
hasHadoopGcsConfiguration()) {
+ for (String key : effective.keySet()) {
+ if (key.startsWith("gcs.") && !"gcs.project-id".equals(key)) {
+ throw new IllegalArgumentException("GCS option requires an
explicit SDK storage identity: " + key);
+ }
+ }
+ return HADOOP_FILE_IO_IMPL;
+ }
+ // With no configured Hadoop identity, retain the upstream SDK/ADC
default.
+ return GCS_FILE_IO_IMPL;
+ }
+
+ private boolean hasHadoopGcsConfiguration() {
+ if (hadoopGcsConfigured == null) {
+ boolean configured = false;
+ Configuration conf = getConf();
+ if (conf != null) {
+ for (Map.Entry<String, String> entry : conf) {
+ if (entry.getKey().startsWith("fs.gs.")) {
+ configured = true;
+ break;
+ }
+ }
+ }
+ hadoopGcsConfigured = configured;
+ }
+ return hadoopGcsConfigured;
+ }
+
+ @Override
+ public synchronized void setConf(Configuration conf) {
+ super.setConf(conf);
+ // Hadoop configurations can contain thousands of defaults; inspect
them once per configuration.
+ hadoopGcsConfigured = null;
+ }
+
+ @Override
+ public synchronized void initialize(Map<String, String> properties) {
+ super.initialize(properties);
+ closed = false;
+ }
+
+ @Override
+ public synchronized void setCredentials(List<StorageCredential>
credentials) {
+ boolean changed = !credentials().equals(credentials);
+ super.setCredentials(credentials);
+ if (changed && scopedIos != null) {
+ // Replacement credentials must not reuse a client initialized
with the previous effective root map.
+ scopedIos.values().forEach(DelegateFileIO::close);
+ scopedIos.clear();
+ }
+ }
+
+ private StorageCredential matchingCredential(String location) {
+ StorageCredential matching = null;
+ for (StorageCredential credential : credentials()) {
+ if (location.startsWith(credential.prefix())
+ && (matching == null || credential.prefix().length() >=
matching.prefix().length())) {
+ matching = credential;
+ }
+ }
+ return matching;
+ }
+
+ private Map<String, String> effectiveProperties(Map<String, String> scope)
{
+ Map<String, String> effective = new HashMap<>(properties());
+ effective.putAll(scope);
+ return effective;
+ }
+
+ @Override
+ synchronized DelegateFileIO io(String location) {
+ if (closed) {
+ throw new IllegalStateException("FileIO is closed");
+ }
+ if (!GCS_FILE_IO_IMPL.equals(implFromLocation(location))) {
+ return super.io(location);
+ }
+ StorageCredential matching = matchingCredential(location);
+ String prefix = matching == null ? "" : matching.prefix();
+ if (scopedIos == null) {
+ scopedIos = new HashMap<>();
+ }
+ return scopedIos.computeIfAbsent(prefix, ignored -> {
+ // GCSFileIO eagerly parses globals and every credential,
including unrelated/overridden expiries.
+ // Give its root client the validated effective map, and retain
only this scope for SDK refresh.
+ Map<String, String> effective = effectiveProperties(
+ matching == null ? Collections.emptyMap() :
matching.config());
+ if (matching != null &&
effective.containsKey(GCPProperties.GCS_OAUTH2_TOKEN)
+ &&
effective.containsKey(GCPProperties.GCS_OAUTH2_REFRESH_CREDENTIALS_ENDPOINT)
Review Comment:
[P2] Checking
`effective.containsKey(GCPProperties.GCS_OAUTH2_REFRESH_CREDENTIALS_ENDPOINT)`
will select `PrefixAwareGcsFileIO` even when the key is mapped to `null`,
whereas upstream `GCPAuthUtils` checks
`oauth2RefreshCredentialsEndpoint().isPresent()` (non-null). Consider checking
`effective.get(GCPProperties.GCS_OAUTH2_REFRESH_CREDENTIALS_ENDPOINT) != null`.
##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/io/NativeGcsResolvingFileIO.java:
##########
@@ -39,9 +52,146 @@ public class NativeGcsResolvingFileIO extends
ResolvingFileIO {
private static final String GCS_FILE_IO_IMPL =
"org.apache.iceberg.gcp.gcs.GCSFileIO";
private static final String HADOOP_FILE_IO_IMPL =
"org.apache.iceberg.hadoop.HadoopFileIO";
+ // Clients are bounded by credential prefixes, not file locations, and are
rebuilt after serialization.
+ private transient Map<String, DelegateFileIO> scopedIos;
+ private volatile Boolean hadoopGcsConfigured;
+ private boolean closed;
+
+ public static boolean hasSdkGcsProperties(Map<String, String> properties) {
+ return properties.keySet().stream().anyMatch(key -> key != null &&
key.startsWith("gcs."));
+ }
+
@Override
- String implFromLocation(String location) {
+ synchronized String implFromLocation(String location) {
String impl = super.implFromLocation(location);
- return GCS_FILE_IO_IMPL.equals(impl) ? HADOOP_FILE_IO_IMPL : impl;
+ if (!GCS_FILE_IO_IMPL.equals(impl)) {
+ return impl;
+ }
+ StorageCredential matching = matchingCredential(location);
+ Map<String, String> scope = matching == null ? Collections.emptyMap()
: matching.config();
+ // Validate the effective scope before choosing an identity: a present
null token is not an absent token.
+ GcsOAuthTokenValidator.validate(properties(), scope);
+ Map<String, String> effective = effectiveProperties(scope);
+ String impersonation =
effective.get("gcs.impersonate.service-account");
+ boolean sdkIdentity = effective.containsKey("gcs.oauth2.token")
+ || Boolean.parseBoolean(effective.get("gcs.no-auth"))
+ || (impersonation != null && !impersonation.isEmpty());
+ if (sdkIdentity) {
+ return GCS_FILE_IO_IMPL;
+ }
+ // Project selection is not authentication. SDK-only options must
never silently replace HMAC with ADC.
+ if (properties().containsKey("gs.access_key") ||
hasHadoopGcsConfiguration()) {
+ for (String key : effective.keySet()) {
+ if (key.startsWith("gcs.") && !"gcs.project-id".equals(key)) {
+ throw new IllegalArgumentException("GCS option requires an
explicit SDK storage identity: " + key);
+ }
+ }
+ return HADOOP_FILE_IO_IMPL;
+ }
+ // With no configured Hadoop identity, retain the upstream SDK/ADC
default.
+ return GCS_FILE_IO_IMPL;
+ }
+
+ private boolean hasHadoopGcsConfiguration() {
+ if (hadoopGcsConfigured == null) {
+ boolean configured = false;
+ Configuration conf = getConf();
+ if (conf != null) {
+ for (Map.Entry<String, String> entry : conf) {
+ if (entry.getKey().startsWith("fs.gs.")) {
+ configured = true;
+ break;
+ }
+ }
+ }
+ hadoopGcsConfigured = configured;
+ }
+ return hadoopGcsConfigured;
+ }
+
+ @Override
+ public synchronized void setConf(Configuration conf) {
+ super.setConf(conf);
+ // Hadoop configurations can contain thousands of defaults; inspect
them once per configuration.
+ hadoopGcsConfigured = null;
+ }
+
+ @Override
+ public synchronized void initialize(Map<String, String> properties) {
+ super.initialize(properties);
+ closed = false;
+ }
+
+ @Override
+ public synchronized void setCredentials(List<StorageCredential>
credentials) {
+ boolean changed = !credentials().equals(credentials);
+ super.setCredentials(credentials);
+ if (changed && scopedIos != null) {
+ // Replacement credentials must not reuse a client initialized
with the previous effective root map.
+ scopedIos.values().forEach(DelegateFileIO::close);
Review Comment:
[P2] If closing one delegate in `scopedIos` throws a runtime exception (e.g.
`UncheckedIOException` from `PrefixAwareGcsFileIO`), the remaining delegates
are leaked and `scopedIos.clear()` is skipped. In `setCredentials()`, this
leaves stale/closed clients in `scopedIos` after
`super.setCredentials(credentials)` has already updated the credentials (same
pattern in `close()`). Consider copying `scopedIos.values()` to a local list
and clearing `scopedIos` first (matching `ResolvingFileIO.close()`), then
closing all delegates while suppressing secondary failures.
##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/gcp/gcs/PrefixAwareGcsFileIO.java:
##########
@@ -0,0 +1,209 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.iceberg.gcp.gcs;
+
+import org.apache.iceberg.EnvironmentContext;
+import org.apache.iceberg.gcp.GCPProperties;
+import org.apache.iceberg.io.CloseableGroup;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.rest.credentials.Credential;
+import org.apache.iceberg.util.SerializableMap;
+import org.apache.iceberg.util.SerializableSupplier;
+
+import com.google.api.gax.rpc.FixedHeaderProvider;
+import com.google.auth.oauth2.AccessToken;
+import com.google.auth.oauth2.OAuth2CredentialsWithRefresh;
+import com.google.cloud.storage.Storage;
+import com.google.cloud.storage.StorageOptions;
+import org.apache.doris.filesystem.gcs.auth.GcsOAuthTokenValidator;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Keeps an SDK client bound to the same vended scope when its OAuth token
expires. */
+public final class PrefixAwareGcsFileIO extends GCSFileIO {
+ private static final long serialVersionUID = 1L;
+ private static final String USER_AGENT = "gcsfileio/" +
EnvironmentContext.get();
+ private final ScopedClient scopedClient;
+ private final SerializableMap<String, String> originalProperties;
+ private final GCPProperties options;
+
+ public PrefixAwareGcsFileIO(Map<String, String> properties, String prefix)
{
+ this(new ScopedClient(properties, prefix), properties);
+ }
+
+ private PrefixAwareGcsFileIO(ScopedClient client, Map<String, String>
properties) {
+ super(client);
+ this.scopedClient = client;
+ this.originalProperties = SerializableMap.copyOf(properties);
+ this.options = new GCPProperties(properties);
+ Map<String, String> internal = new HashMap<>(properties);
+ // The SDK analytics factory creates separate credentials; both
readers must share our scoped token.
+ internal.put(GCPProperties.GCS_ANALYTICS_CORE_ENABLED, "false");
+ super.initialize(internal);
+ // No SDK credential list: its scheduled refresh replaces the list but
leaves existing clients unchanged.
+ }
+
+ @Override
+ public Map<String, String> properties() {
+ return originalProperties.immutableMap();
+ }
+
+ @Override
+ public InputFile newInputFile(String path) {
+ GCSInputFile file = (GCSInputFile) super.newInputFile(path);
+ return new GCSInputFile(file.storage(), scopedClient.fileSystem(),
file.blobId(),
+ null, options, file.metrics());
+ }
+
+ @Override
+ public InputFile newInputFile(String path, long length) {
+ GCSInputFile file = (GCSInputFile) super.newInputFile(path, length);
+ return new GCSInputFile(file.storage(), scopedClient.fileSystem(),
file.blobId(),
+ length > 0 ? Long.valueOf(length) : null, options,
file.metrics());
+ }
+
+ @Override
+ public OutputFile newOutputFile(String path) {
+ GCSOutputFile file = (GCSOutputFile) super.newOutputFile(path);
+ return new GCSOutputFile(file.storage(), scopedClient.fileSystem(),
file.blobId(), options, file.metrics());
+ }
+
+ @Override
+ public void close() {
+ try {
+ super.close();
+ } finally {
+ scopedClient.close();
+ }
+ }
+
+ private static final class ScopedClient implements
SerializableSupplier<Storage>, Closeable {
+ private static final long serialVersionUID = 1L;
+ private final Map<String, String> properties;
+ private final String prefix;
+ private final GCPProperties options;
+ private transient Storage storage;
+ private transient OAuth2CredentialsWithRefresh credentials;
+ private transient OAuth2RefreshCredentialsHandler handler;
+ private transient AutoCloseable fileSystem;
+ private boolean closed;
+
+ private ScopedClient(Map<String, String> properties, String prefix) {
+ this.properties = SerializableMap.copyOf(properties);
+ this.prefix = prefix;
+ this.options = new GCPProperties(properties);
+ }
+
+ private OAuth2CredentialsWithRefresh credentials() {
+ if (credentials == null) {
+ credentials =
OAuth2CredentialsWithRefresh.newBuilder().setAccessToken(new AccessToken(
+ options.oauth2Token().orElseThrow(),
options.oauth2TokenExpiresAt().orElse(null)))
+ .setRefreshHandler(this::refreshAccessToken).build();
+ }
+ return credentials;
+ }
+
+ @Override
+ public synchronized Storage get() {
+ if (closed) {
+ throw new IllegalStateException("FileIO is closed");
+ }
+ if (storage == null) {
+ StorageOptions.Builder builder = StorageOptions.newBuilder();
+
builder.setHeaderProvider(FixedHeaderProvider.create(Map.of("User-agent",
USER_AGENT)));
+ options.projectId().ifPresent(builder::setProjectId);
+ options.clientLibToken().ifPresent(builder::setClientLibToken);
+ options.serviceHost().ifPresent(builder::setHost);
+ builder.setCredentials(credentials());
+ storage = builder.build().getService();
+ }
+ return storage;
+ }
+
+ private synchronized AutoCloseable fileSystem() {
+ if (!options.isGcsAnalyticsCoreEnabled()) {
+ return null;
+ }
+ if (closed) {
+ throw new IllegalStateException("FileIO is closed");
+ }
+ if (fileSystem == null) {
+ Map<String, String> analyticsProperties = new
HashMap<>(properties);
+ analyticsProperties.put("gcs.user-agent", USER_AGENT);
+ fileSystem =
AnalyticsCoreUtil.createFileSystem(analyticsProperties, credentials());
+ }
+ return fileSystem;
+ }
+
+ private synchronized AccessToken refreshAccessToken() {
Review Comment:
[P2] `refreshAccessToken()` performs a blocking HTTP call
(`handler.fetchCredentials()`) while holding the `ScopedClient` monitor, which
blocks `close()`/`get()`/`fileSystem()` during network latency and introduces a
potential lock-ordering inversion with `OAuth2Credentials.lock` if
`AnalyticsCoreUtil.createFileSystem()` ever triggers token acquisition under
`ScopedClient`. Consider initializing `handler` under the lock and invoking
`fetchCredentials()` outside `synchronized`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]