Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5188] feat(python-client): Support s3 fileset in python client #5209

Merged
merged 135 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
d2447a2
Add a framework to support multi-storage in a pluginized manner for …
yuqi1129 Sep 25, 2024
7e5a8b5
Fix compile distribution error.
yuqi1129 Sep 25, 2024
f53c5ef
fix
yuqi1129 Sep 25, 2024
36fedcd
fix
yuqi1129 Sep 25, 2024
e93fba5
fix
yuqi1129 Sep 25, 2024
b1e04b6
fix
yuqi1129 Sep 26, 2024
db00e65
Changed according to comments.
yuqi1129 Sep 29, 2024
c793582
fix
yuqi1129 Sep 29, 2024
013f5cb
fix
yuqi1129 Sep 30, 2024
dba5753
Merge branch 'main' of github.com:datastrato/graviton into issue_5019
yuqi1129 Oct 8, 2024
16dfc73
resolve comments.
yuqi1129 Oct 8, 2024
278fcd8
Polish code.
yuqi1129 Oct 9, 2024
3fb55ad
fix
yuqi1129 Oct 9, 2024
cd04666
fix
yuqi1129 Oct 9, 2024
d0bf13e
Support GCS fileset.
yuqi1129 Oct 9, 2024
ffaa064
Change gvfs accordingly.
yuqi1129 Oct 11, 2024
32d7f3d
Merge remote-tracking branch 'me/issue_5019' into issue_5019
yuqi1129 Oct 11, 2024
d82bf76
Update Java doc for FileSystemProvider
yuqi1129 Oct 11, 2024
dfdb772
Fix
yuqi1129 Oct 11, 2024
8708a8a
Fix
yuqi1129 Oct 11, 2024
ba9f8fa
Fix test error.
yuqi1129 Oct 11, 2024
dae99f7
Polish.
yuqi1129 Oct 11, 2024
e22053b
Polish
yuqi1129 Oct 12, 2024
4fb89e0
Polish
yuqi1129 Oct 12, 2024
e5746c0
Rename `AbstractIT` to `BaseIT`
yuqi1129 Oct 12, 2024
b2d7bed
Fix
yuqi1129 Oct 14, 2024
380717b
Merge branch 'apache:main' into issue_5019
yuqi1129 Oct 14, 2024
f4041ec
Fix python ut error again.
yuqi1129 Oct 14, 2024
66247ab
Merge branch 'issue_5019' of github.com:yuqi1129/gravitino into issue…
yuqi1129 Oct 14, 2024
3cfb94f
Fix test error again.
yuqi1129 Oct 14, 2024
7d1150f
Fix minor.
yuqi1129 Oct 14, 2024
608081b
fix
yuqi1129 Oct 14, 2024
9edfe82
Fix
yuqi1129 Oct 14, 2024
3079bf0
Fix
yuqi1129 Oct 14, 2024
da49e60
Fix
yuqi1129 Oct 14, 2024
b621d89
Fix
yuqi1129 Oct 14, 2024
05dd006
Merge branch 'issue_5019' into issue_5074
yuqi1129 Oct 15, 2024
9d5b8dc
rebase issue_5019
yuqi1129 Oct 15, 2024
e58f9a0
Fix
yuqi1129 Oct 15, 2024
c521daf
resolve comments
yuqi1129 Oct 15, 2024
46e996a
Resolve comments again.
yuqi1129 Oct 15, 2024
da0b7ca
Polish again.
yuqi1129 Oct 15, 2024
e9ccda4
Merge branch 'issue_5019' of github.com:yuqi1129/gravitino into issue…
yuqi1129 Oct 15, 2024
ba1fe5f
Rebase branch issue_5019
yuqi1129 Oct 15, 2024
4ffe389
Merge branch 'main' of github.com:datastrato/graviton into issue_5139
yuqi1129 Oct 15, 2024
7c44a57
Support python gvfs
yuqi1129 Oct 15, 2024
992ba0a
fix
yuqi1129 Oct 15, 2024
5dbca5f
fix
yuqi1129 Oct 15, 2024
f27520a
Update code.
yuqi1129 Oct 15, 2024
e29e47b
Merge branch 'issue_5019' into issue_5074
yuqi1129 Oct 15, 2024
bc1e76f
Rebase branch issue_5019
yuqi1129 Oct 15, 2024
8a9d3bf
Merge branch 'issue_5074' of github.com:yuqi1129/gravitino into issue…
yuqi1129 Oct 15, 2024
2115e31
fix
yuqi1129 Oct 15, 2024
c2e55d4
fix
yuqi1129 Oct 15, 2024
557aa02
fix
yuqi1129 Oct 15, 2024
5c3fa5c
fix
yuqi1129 Oct 15, 2024
408eca7
fix
yuqi1129 Oct 15, 2024
a02065d
fix
yuqi1129 Oct 15, 2024
8762bae
fix
yuqi1129 Oct 15, 2024
dc7a915
fix
yuqi1129 Oct 15, 2024
c230991
fix
yuqi1129 Oct 15, 2024
dc54880
skip some test.
yuqi1129 Oct 15, 2024
7ecc040
fix
yuqi1129 Oct 15, 2024
da46321
fix
yuqi1129 Oct 15, 2024
27bc2ab
Fix
yuqi1129 Oct 16, 2024
017c42e
Merge branch 'issue_5019' into issue_5074
yuqi1129 Oct 16, 2024
9dc0f5a
Fix
yuqi1129 Oct 16, 2024
41ff00d
Merge branch 'issue_5019' of github.com:yuqi1129/gravitino into issue…
yuqi1129 Oct 16, 2024
1fee1e4
Fix
yuqi1129 Oct 16, 2024
1789bd2
fix
yuqi1129 Oct 16, 2024
2ee1709
Fix
yuqi1129 Oct 16, 2024
05e5d20
Fix
yuqi1129 Oct 16, 2024
8f28211
Fix
yuqi1129 Oct 16, 2024
35cba1e
Fix
yuqi1129 Oct 16, 2024
bcf2f12
Fix
yuqi1129 Oct 16, 2024
f25a37d
Fix a problem
yuqi1129 Oct 16, 2024
a3da011
Merge branch 'issue_5019' into issue_5074
yuqi1129 Oct 16, 2024
3517996
Merge branch 'main' of github.com:apache/gravitino into issue_5074
yuqi1129 Oct 16, 2024
27a911a
fix
yuqi1129 Oct 16, 2024
e34dbea
Merge branch 'main' of github.com:apache/gravitino into issue_5074
yuqi1129 Oct 16, 2024
6bae7e5
Fix a problem
yuqi1129 Oct 16, 2024
fe13f5e
Fix a problem
yuqi1129 Oct 16, 2024
0181632
Fix a problem
yuqi1129 Oct 16, 2024
3ff9eef
Fix
yuqi1129 Oct 16, 2024
2ce660c
Fix
yuqi1129 Oct 16, 2024
f0fa87b
Fix
yuqi1129 Oct 16, 2024
d2921a8
Fix
yuqi1129 Oct 16, 2024
dc68dd1
Fix
yuqi1129 Oct 16, 2024
6431e2f
Fix
yuqi1129 Oct 16, 2024
242888f
Fix
yuqi1129 Oct 16, 2024
3ec2dcc
Merge branch 'issue_5074' of github.com:yuqi1129/gravitino into issue…
yuqi1129 Oct 16, 2024
f754997
Fix
yuqi1129 Oct 16, 2024
2cdfb35
Fix
yuqi1129 Oct 16, 2024
67dbc3a
Resolve comments.
yuqi1129 Oct 17, 2024
70a545e
Fix the java doc problem.
yuqi1129 Oct 17, 2024
4d54acf
Merge branch 'issue_5074' into issue_5139
yuqi1129 Oct 17, 2024
15bbf99
rebase issue_5074
yuqi1129 Oct 17, 2024
cfcc544
Optimize code.
yuqi1129 Oct 17, 2024
acf51e1
Merge branch 'issue_5074' of github.com:yuqi1129/gravitino into issue…
yuqi1129 Oct 17, 2024
11f9992
Merge branch 'main' of github.com:datastrato/graviton into issue_5139
yuqi1129 Oct 17, 2024
4f00a2f
Remove s3 related code.
yuqi1129 Oct 17, 2024
b9ef8f0
fix
yuqi1129 Oct 17, 2024
9f65fb5
try to import lazily.
yuqi1129 Oct 18, 2024
4defcc6
format code.
yuqi1129 Oct 18, 2024
3a907f4
fix
yuqi1129 Oct 18, 2024
76912b7
fix
yuqi1129 Oct 18, 2024
4478673
fix
yuqi1129 Oct 18, 2024
5a194df
Merge branch 'main' of github.com:datastrato/graviton into issue_5139
yuqi1129 Oct 18, 2024
c8b5c7c
Merge branch 'main' of github.com:datastrato/graviton into issue_5139
yuqi1129 Oct 21, 2024
6ff9353
Resolve comments
yuqi1129 Oct 21, 2024
1dac0f0
Merge branch 'main' of github.com:datastrato/graviton into issue_5188
yuqi1129 Oct 21, 2024
f592289
Support python client for S3.
yuqi1129 Oct 21, 2024
7069b6b
fix
yuqi1129 Oct 21, 2024
1281e72
fix
yuqi1129 Oct 21, 2024
f022d6e
fix
yuqi1129 Oct 21, 2024
0d2ccab
fix
yuqi1129 Oct 22, 2024
55633e8
Merge branch 'main' of github.com:datastrato/graviton into issue_5188
yuqi1129 Oct 22, 2024
217cc5f
fix
yuqi1129 Oct 22, 2024
6958aa8
fix
yuqi1129 Oct 22, 2024
8b9b8d7
Replace pyarrow gvfs with gcsfs.
yuqi1129 Oct 23, 2024
b4d5728
fix
yuqi1129 Oct 23, 2024
0306ac5
fix
yuqi1129 Oct 23, 2024
a35a40d
fix
yuqi1129 Oct 23, 2024
cfd8a89
fix
yuqi1129 Oct 23, 2024
a131564
fix
yuqi1129 Oct 23, 2024
34e3ff4
Replace ArrowFSWrapper with s3fs.S3FileSystem.
yuqi1129 Oct 23, 2024
2cc234a
fix
yuqi1129 Oct 23, 2024
ba0237e
fix
yuqi1129 Oct 23, 2024
4df5ea1
fix
yuqi1129 Oct 23, 2024
4e49e6e
fix
yuqi1129 Oct 23, 2024
015b788
fix comments.
yuqi1129 Oct 23, 2024
c414b4d
fix test error.
yuqi1129 Oct 23, 2024
798b4d1
fix
yuqi1129 Oct 23, 2024
aa96f63
fix
yuqi1129 Oct 23, 2024
804622c
fix
yuqi1129 Oct 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 91 additions & 23 deletions clients/client-python/gravitino/filesystem/gvfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
from enum import Enum
from pathlib import PurePosixPath
from typing import Dict, Tuple
Expand Down Expand Up @@ -49,6 +48,7 @@ class StorageType(Enum):
HDFS = "hdfs"
LOCAL = "file"
GCS = "gs"
S3A = "s3a"


class FilesetContextPair:
Expand Down Expand Up @@ -314,7 +314,11 @@ def mv(self, path1, path2, recursive=False, maxdepth=None, **kwargs):

# convert the following to in

if storage_type in [StorageType.HDFS, StorageType.GCS]:
if storage_type in [
StorageType.HDFS,
StorageType.GCS,
StorageType.S3A,
]:
src_context_pair.filesystem().mv(
self._strip_storage_protocol(storage_type, src_actual_path),
self._strip_storage_protocol(storage_type, dst_actual_path),
Expand All @@ -336,6 +340,10 @@ def _rm(self, path):
"Deprecated method, use `rm_file` method instead."
)

def lazy_load_class(self, module_name, class_name):
module = importlib.import_module(module_name)
return getattr(module, class_name)

def rm(self, path, recursive=False, maxdepth=None):
"""Remove a file or directory.
:param path: Virtual fileset path
Expand All @@ -348,11 +356,17 @@ def rm(self, path, recursive=False, maxdepth=None):
)
actual_path = context_pair.actual_file_location()
storage_type = self._recognize_storage_type(actual_path)
context_pair.filesystem().rm(
self._strip_storage_protocol(storage_type, actual_path),
recursive,
maxdepth,
)
fs = context_pair.filesystem()

# S3FileSystem doesn't support maxdepth
if isinstance(fs, self.lazy_load_class("s3fs", "S3FileSystem")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the latest doc, s3fs seems supports the max depth param(https://s3fs.readthedocs.io/en/latest/api.html#s3fs.core.S3FileSystem.rm), is there something wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually not and the error will be:

======================================================================
ERROR: test_rm (test_gvfs_with_s3.TestGvfsWithS3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ec2-user/gravitino/clients/client-python/tests/integration/test_gvfs_with_s3.py", line 195, in test_rm
    fs.rm(rm_file)
  File "/home/ec2-user/gravitino/clients/client-python/gravitino/filesystem/gvfs.py", line 355, in rm
    context_pair.filesystem().rm(
  File "/home/ec2-user/gravitino/.gradle/python/Linux/Miniforge3/envs/python-3.8/lib/python3.8/site-packages/fsspec/asyn.py", line 118, in wrapper
    return sync(self.loop, func, *args, **kwargs)
  File "/home/ec2-user/gravitino/.gradle/python/Linux/Miniforge3/envs/python-3.8/lib/python3.8/site-packages/fsspec/asyn.py", line 85, in sync
    coro = func(*args, **kwargs)
TypeError: _rm() takes from 2 to 3 positional arguments but 4 were given

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I checked the s3fs code, it does not support max depth indeed: https://github.com/fsspec/s3fs/blob/main/s3fs/core.py#L2001.

fs.rm(self._strip_storage_protocol(storage_type, actual_path), recursive)
else:
fs.rm(
self._strip_storage_protocol(storage_type, actual_path),
recursive,
maxdepth,
)

def rm_file(self, path):
"""Remove a file.
Expand Down Expand Up @@ -547,9 +561,11 @@ def _convert_actual_path(
"""

# If the storage path starts with hdfs, gcs, we should use the path as the prefix.
if storage_location.startswith(
f"{StorageType.HDFS.value}://"
) or storage_location.startswith(f"{StorageType.GCS.value}://"):
if (
storage_location.startswith(f"{StorageType.HDFS.value}://")
or storage_location.startswith(f"{StorageType.GCS.value}://")
or storage_location.startswith(f"{StorageType.S3A.value}://")
):
actual_prefix = infer_storage_options(storage_location)["path"]
elif storage_location.startswith(f"{StorageType.LOCAL.value}:/"):
actual_prefix = storage_location[len(f"{StorageType.LOCAL.value}:") :]
Expand Down Expand Up @@ -586,11 +602,34 @@ def _convert_actual_info(
path = self._convert_actual_path(
entry["name"], storage_location, virtual_location
)

# if entry contains 'mtime', then return the entry with 'mtime' else
# if entry contains 'LastModified', then return the entry with 'LastModified'

if "mtime" in entry:
# HDFS and GCS
return {
"name": path,
"size": entry["size"],
"type": entry["type"],
"mtime": entry["mtime"],
}

if "LastModified" in entry:
# S3 and OSS
return {
"name": path,
"size": entry["size"],
"type": entry["type"],
"mtime": entry["LastModified"],
}

# Unknown
return {
"name": path,
"size": entry["size"],
"type": entry["type"],
"mtime": entry["mtime"],
"mtime": None,
}

def _get_fileset_context(self, virtual_path: str, operation: FilesetDataOperation):
Expand Down Expand Up @@ -692,6 +731,8 @@ def _recognize_storage_type(path: str):
return StorageType.LOCAL
if path.startswith(f"{StorageType.GCS.value}://"):
return StorageType.GCS
if path.startswith(f"{StorageType.S3A.value}://"):
return StorageType.S3A
raise GravitinoRuntimeException(
f"Storage type doesn't support now. Path:{path}"
)
Expand All @@ -716,7 +757,7 @@ def _strip_storage_protocol(storage_type: StorageType, path: str):
:param path: The path
:return: The stripped path
"""
if storage_type in (StorageType.HDFS, StorageType.GCS):
if storage_type in (StorageType.HDFS, StorageType.GCS, StorageType.S3A):
return path
if storage_type == StorageType.LOCAL:
return path[len(f"{StorageType.LOCAL.value}:") :]
Expand Down Expand Up @@ -791,7 +832,9 @@ def _get_filesystem(self, actual_file_location: str):
elif storage_type == StorageType.LOCAL:
fs = LocalFileSystem()
elif storage_type == StorageType.GCS:
fs = ArrowFSWrapper(self._get_gcs_filesystem())
fs = self._get_gcs_filesystem()
elif storage_type == StorageType.S3A:
fs = self._get_s3_filesystem()
else:
raise GravitinoRuntimeException(
f"Storage type: `{storage_type}` doesn't support now."
Expand All @@ -802,22 +845,47 @@ def _get_filesystem(self, actual_file_location: str):
write_lock.release()

def _get_gcs_filesystem(self):
# get All keys from the options that start with 'gravitino.bypass.gcs.' and remove the prefix
gcs_options = {
key[len(GVFSConfig.GVFS_FILESYSTEM_BY_PASS_GCS) :]: value
for key, value in self._options.items()
if key.startswith(GVFSConfig.GVFS_FILESYSTEM_BY_PASS_GCS)
}

# get 'service-account-key' from gcs_options, if the key is not found, throw an exception
service_account_key_path = gcs_options.get(GVFSConfig.GVFS_FILESYSTEM_KEY_FILE)
service_account_key_path = self._options.get(
GVFSConfig.GVFS_FILESYSTEM_GCS_SERVICE_KEY_FILE
)
if service_account_key_path is None:
raise GravitinoRuntimeException(
"Service account key is not found in the options."
)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = service_account_key_path
return importlib.import_module("gcsfs").GCSFileSystem(
token=service_account_key_path
)

return importlib.import_module("pyarrow.fs").GcsFileSystem()
def _get_s3_filesystem(self):
# get 'aws_access_key_id' from s3_options, if the key is not found, throw an exception
aws_access_key_id = self._options.get(GVFSConfig.GVFS_FILESYSTEM_S3_ACCESS_KEY)
if aws_access_key_id is None:
raise GravitinoRuntimeException(
"AWS access key id is not found in the options."
)

# get 'aws_secret_access_key' from s3_options, if the key is not found, throw an exception
aws_secret_access_key = self._options.get(
GVFSConfig.GVFS_FILESYSTEM_S3_SECRET_KEY
)
if aws_secret_access_key is None:
raise GravitinoRuntimeException(
"AWS secret access key is not found in the options."
)

# get 'aws_endpoint_url' from s3_options, if the key is not found, throw an exception
aws_endpoint_url = self._options.get(GVFSConfig.GVFS_FILESYSTEM_S3_ENDPOINT)
if aws_endpoint_url is None:
raise GravitinoRuntimeException(
"AWS endpoint url is not found in the options."
)

return importlib.import_module("s3fs").S3FileSystem(
key=aws_access_key_id,
secret=aws_secret_access_key,
endpoint_url=aws_endpoint_url,
)


fsspec.register_implementation(PROTOCOL_NAME, GravitinoVirtualFileSystem)
8 changes: 5 additions & 3 deletions clients/client-python/gravitino/filesystem/gvfs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class GVFSConfig:
OAUTH2_PATH = "oauth2_path"
OAUTH2_SCOPE = "oauth2_scope"

GVFS_FILESYSTEM_BY_PASS = "gravitino.bypass"
GVFS_FILESYSTEM_BY_PASS_GCS = "gravitino.bypass.gcs."
GVFS_FILESYSTEM_KEY_FILE = "service-account-key-path"
GVFS_FILESYSTEM_GCS_SERVICE_KEY_FILE = "gcs_service_account_key_path"

GVFS_FILESYSTEM_S3_ACCESS_KEY = "s3_access_key"
GVFS_FILESYSTEM_S3_SECRET_KEY = "s3_secret_key"
GVFS_FILESYSTEM_S3_ENDPOINT = "s3_endpoint"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we should also redefine the Java side gvfs and Hadoop catalog s3/oss/gcs related configurations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do this in a separate PR.

3 changes: 2 additions & 1 deletion clients/client-python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ readerwriterlock==1.0.9
fsspec==2024.3.1
pyarrow==15.0.2
cachetools==5.3.3
google-auth==2.35.0
gcsfs==2024.3.1
s3fs==2024.3.1
Loading
Loading