Copilot commented on code in PR #9715:
URL: https://github.com/apache/paimon/pull/9715#discussion_r3977978369
##########
paimon-python/pypaimon/catalog/rest/rest_token_file_io.py:
##########
@@ -127,7 +126,7 @@ def file_io(self) -> FileIO:
merged_properties[OssOptions.OSS_ENDPOINT.key()] =
dlf_oss_endpoint
merged_options = Options(merged_properties)
- file_io = PyArrowFileIO(self.path, merged_options)
+ file_io = FileIO.get(self.path, merged_options)
cache[cache_key] = file_io
Review Comment:
`_FILE_IO_CACHE` is keyed only by `RESTToken`, but the newly cached
`OssFileIO` is bound to the bucket extracted from `self.path`. If two
tables/buckets receive the same OSS credential token and expiry, the second
`RESTTokenFileIO` reuses the first bucket's instance, and atomic writes now
fail the configured-bucket check (ordinary operations may also target the wrong
backend). Include the path/bucket in both cache lookups and insertion, with a
cross-bucket reuse test.
##########
paimon-python/pypaimon/filesystem/oss_file_io.py:
##########
@@ -0,0 +1,126 @@
+# 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.
+
+"""OSS conditional metadata creation, reusing the existing Arrow/Jindo
FileIO."""
+
+import re
+from urllib.parse import urlparse
+
+from pypaimon.common.options.config import OssOptions
+from pypaimon.filesystem.pyarrow_file_io import PyArrowFileIO
+
+
+class OssFileIO(PyArrowFileIO):
+ """Override atomic metadata creation; inherit all ordinary file
operations."""
+
+ def try_to_write_atomic(self, path: str, content: str) -> bool:
+ uri = urlparse(path)
+ if uri.scheme:
+ if uri.scheme != 'oss' or self._extract_oss_bucket(path) !=
self._oss_bucket:
+ raise ValueError("Atomic write must target the configured OSS
bucket")
+ key = re.sub(r'/+', '/', uri.path).lstrip('/')
+ if '@' in uri.netloc:
+ key = key.partition('/')[2]
+ else:
+ key = path
+ if not self._use_jindo and not self._oss_bucket_in_endpoint:
+ bucket, _, key = path.partition('/')
+ if bucket != self._oss_bucket:
+ raise ValueError("Atomic write must target the configured
OSS bucket")
+ if not key or key == '.' or key.endswith('/'):
+ return False
+
+ try:
+ import oss2
+ except ImportError as error:
+ raise ImportError(
+ "OSS atomic writes require oss2. Install pypaimon[oss] or
pypaimon[jindo]."
+ ) from error
+
+ access_key = self.properties.get(OssOptions.OSS_ACCESS_KEY_ID)
+ secret_key = self.properties.get(OssOptions.OSS_ACCESS_KEY_SECRET)
+ token = self.properties.get(OssOptions.OSS_SECURITY_TOKEN)
+ endpoint = self.properties.get(OssOptions.OSS_ENDPOINT)
+ if not access_key or not secret_key or not endpoint:
+ raise ValueError(
+ "OSS atomic writes require fs.oss.accessKeyId,
fs.oss.accessKeySecret "
+ "and fs.oss.endpoint; pass fs.oss.securityToken for STS
credentials.")
+ if '://' not in endpoint:
+ endpoint = 'https://' + endpoint
+ headers = self._sse_headers()
+ headers['x-oss-forbid-overwrite'] = 'true'
+ auth = oss2.StsAuth(access_key, secret_key, token) if token else
oss2.Auth(access_key, secret_key)
+
+ session = oss2.Session()
+ try:
+ bucket = oss2.Bucket(auth, endpoint, self._oss_bucket,
session=session,
+
region=self.properties.get(OssOptions.OSS_REGION))
+ try:
+ versioning = bucket.get_bucket_versioning().status
+ except oss2.exceptions.ServerError as error:
+ if error.status != 403 or error.code != 'AccessDenied':
+ raise
+ versioning = 'unknown (GetBucketVersioning denied)'
+ if versioning is not None:
+ self.logger.warning(
+ "Using legacy temporary-file-and-rename writes for OSS
bucket %s "
+ "(versioning: %s). Concurrent commits are not protected
against overwrites.",
+ self._oss_bucket, versioning)
+ return super().try_to_write_atomic(path, content)
Review Comment:
The compatibility fallback passes credential-style OSS URIs to
`PyArrowFileIO`, whose path conversion does not strip the URI's endpoint/bucket
prefix. For `oss://AK:SK@endpoint/bucket/key`, modern PyArrow resolves
`AK:SK@endpoint/bucket/key`, while legacy/Jindo resolves `bucket/key`, so
versioned or query-denied buckets write to the wrong location (or fail).
Convert this URI form to the backend-native key before invoking the inherited
implementation, and cover it in the fallback test.
--
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]