-
Notifications
You must be signed in to change notification settings - Fork 350
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
[#5019] feat: (hadoop-catalog): Add a framework to support multi-storage in a pluggable manner for fileset catalog #5020
Merged
Merged
Changes from 9 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
d2447a2
Add a framework to support multi-storage in a pluginized manner for …
yuqi1129 7e5a8b5
Fix compile distribution error.
yuqi1129 f53c5ef
fix
yuqi1129 36fedcd
fix
yuqi1129 e93fba5
fix
yuqi1129 b1e04b6
fix
yuqi1129 db00e65
Changed according to comments.
yuqi1129 c793582
fix
yuqi1129 013f5cb
fix
yuqi1129 dba5753
Merge branch 'main' of github.com:datastrato/graviton into issue_5019
yuqi1129 16dfc73
resolve comments.
yuqi1129 278fcd8
Polish code.
yuqi1129 3fb55ad
fix
yuqi1129 cd04666
fix
yuqi1129 ffaa064
Change gvfs accordingly.
yuqi1129 32d7f3d
Merge remote-tracking branch 'me/issue_5019' into issue_5019
yuqi1129 d82bf76
Update Java doc for FileSystemProvider
yuqi1129 dfdb772
Fix
yuqi1129 8708a8a
Fix
yuqi1129 ba9f8fa
Fix test error.
yuqi1129 dae99f7
Polish.
yuqi1129 e22053b
Polish
yuqi1129 4fb89e0
Polish
yuqi1129 e5746c0
Rename `AbstractIT` to `BaseIT`
yuqi1129 b2d7bed
Fix
yuqi1129 380717b
Merge branch 'apache:main' into issue_5019
yuqi1129 f4041ec
Fix python ut error again.
yuqi1129 66247ab
Merge branch 'issue_5019' of github.com:yuqi1129/gravitino into issue…
yuqi1129 3cfb94f
Fix test error again.
yuqi1129 7d1150f
Fix minor.
yuqi1129 608081b
fix
yuqi1129 9edfe82
Fix
yuqi1129 3079bf0
Fix
yuqi1129 da49e60
Fix
yuqi1129 b621d89
Fix
yuqi1129 e58f9a0
Fix
yuqi1129 c521daf
resolve comments
yuqi1129 46e996a
Resolve comments again.
yuqi1129 da0b7ca
Polish again.
yuqi1129 5dbca5f
fix
yuqi1129 f27520a
Update code.
yuqi1129 27bc2ab
Fix
yuqi1129 9dc0f5a
Fix
yuqi1129 2ee1709
Fix
yuqi1129 05e5d20
Fix
yuqi1129 8f28211
Fix
yuqi1129 35cba1e
Fix
yuqi1129 bcf2f12
Fix
yuqi1129 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/FileSystemProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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.gravitino.catalog.hadoop; | ||
|
||
import java.io.IOException; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
|
||
public interface FileSystemProvider { | ||
|
||
/** | ||
* Get the FileSystem instance according to the configuration and the path. | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* <p>Compared to the FileSystem.get method, this method allows the provider to create a | ||
* FileSystem instance with a specific configuration and path and do further initialization if | ||
* needed. | ||
* | ||
* <p>For example, we can check endpoint configurations for S3AFileSystem, or set the default one. | ||
* | ||
* @param configuration The configuration. | ||
* @param path The path. | ||
* @return The FileSystem instance. | ||
* @throws IOException If the FileSystem instance cannot be created. | ||
*/ | ||
FileSystem getFileSystem(Configuration configuration, Path path) throws IOException; | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Get the scheme of this FileSystem provider. | ||
* | ||
* @return The scheme of this FileSystem provider. | ||
*/ | ||
default String getScheme() { | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return "file"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ | |
import org.apache.gravitino.audit.CallerContext; | ||
import org.apache.gravitino.audit.FilesetAuditConstants; | ||
import org.apache.gravitino.audit.FilesetDataOperation; | ||
import org.apache.gravitino.catalog.hadoop.fs.HDFSFileSystemProvider; | ||
import org.apache.gravitino.catalog.hadoop.fs.LocalFileSystemProvider; | ||
import org.apache.gravitino.connector.CatalogInfo; | ||
import org.apache.gravitino.connector.CatalogOperations; | ||
import org.apache.gravitino.connector.HasPropertyMetadata; | ||
|
@@ -77,6 +79,7 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem | |
private static final String SLASH = "/"; | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(HadoopCatalogOperations.class); | ||
public static final Map<String, FileSystemProvider> FILE_SYSTEM_PROVIDERS = Maps.newHashMap(); | ||
|
||
private final EntityStore store; | ||
|
||
|
@@ -90,6 +93,14 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem | |
|
||
private CatalogInfo catalogInfo; | ||
|
||
static { | ||
FileSystemProvider localFileSystemProvider = new LocalFileSystemProvider(); | ||
FileSystemProvider hdfsFileSystemProvider = new HDFSFileSystemProvider(); | ||
|
||
FILE_SYSTEM_PROVIDERS.put(localFileSystemProvider.getScheme(), localFileSystemProvider); | ||
FILE_SYSTEM_PROVIDERS.put(hdfsFileSystemProvider.getScheme(), hdfsFileSystemProvider); | ||
} | ||
|
||
HadoopCatalogOperations(EntityStore store) { | ||
this.store = store; | ||
} | ||
|
@@ -119,32 +130,57 @@ public void initialize( | |
Map<String, String> config, CatalogInfo info, HasPropertyMetadata propertiesMetadata) | ||
throws RuntimeException { | ||
this.propertiesMetadata = propertiesMetadata; | ||
this.catalogInfo = info; | ||
|
||
// Initialize Hadoop Configuration. | ||
this.conf = config; | ||
this.hadoopConf = new Configuration(); | ||
this.catalogInfo = info; | ||
|
||
hadoopConf = new Configuration(); | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Map<String, String> bypassConfigs = | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config.entrySet().stream() | ||
conf.entrySet().stream() | ||
.filter(e -> e.getKey().startsWith(CATALOG_BYPASS_PREFIX)) | ||
.collect( | ||
Collectors.toMap( | ||
e -> e.getKey().substring(CATALOG_BYPASS_PREFIX.length()), | ||
Map.Entry::getValue)); | ||
bypassConfigs.forEach(hadoopConf::set); | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
initPluginFileSystem(conf); | ||
|
||
String catalogLocation = | ||
(String) | ||
propertiesMetadata | ||
.catalogPropertiesMetadata() | ||
.getOrDefault(config, HadoopCatalogPropertiesMetadata.LOCATION); | ||
conf.forEach(hadoopConf::set); | ||
|
||
this.catalogStorageLocation = | ||
StringUtils.isNotBlank(catalogLocation) | ||
? Optional.of(catalogLocation).map(Path::new) | ||
: Optional.empty(); | ||
} | ||
|
||
private void initPluginFileSystem(Map<String, String> config) { | ||
String fileSystemProviders = | ||
(String) | ||
propertiesMetadata | ||
.catalogPropertiesMetadata() | ||
.getOrDefault(config, HadoopCatalogPropertiesMetadata.FILESYSTEM_PROVIDER); | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (StringUtils.isNotBlank(fileSystemProviders)) { | ||
String[] providers = fileSystemProviders.split(","); | ||
for (String provider : providers) { | ||
try { | ||
FileSystemProvider fileSystemProvider = | ||
(FileSystemProvider) | ||
Class.forName(provider.trim()).getDeclaredConstructor().newInstance(); | ||
FILE_SYSTEM_PROVIDERS.put(fileSystemProvider.getScheme(), fileSystemProvider); | ||
} catch (Exception e) { | ||
throw new GravitinoRuntimeException( | ||
e, "Failed to initialize file system provider: %s", provider); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public NameIdentifier[] listFilesets(Namespace namespace) throws NoSuchSchemaException { | ||
try { | ||
|
@@ -236,7 +272,8 @@ public Fileset createFileset( | |
try { | ||
// formalize the path to avoid path without scheme, uri, authority, etc. | ||
filesetPath = formalizePath(filesetPath, hadoopConf); | ||
FileSystem fs = filesetPath.getFileSystem(hadoopConf); | ||
|
||
FileSystem fs = getFileSystem(filesetPath, hadoopConf); | ||
if (!fs.exists(filesetPath)) { | ||
if (!fs.mkdirs(filesetPath)) { | ||
throw new RuntimeException( | ||
|
@@ -339,7 +376,7 @@ public boolean dropFileset(NameIdentifier ident) { | |
|
||
// For managed fileset, we should delete the related files. | ||
if (filesetEntity.filesetType() == Fileset.Type.MANAGED) { | ||
FileSystem fs = filesetPath.getFileSystem(hadoopConf); | ||
FileSystem fs = getFileSystem(filesetPath, hadoopConf); | ||
if (fs.exists(filesetPath)) { | ||
if (!fs.delete(filesetPath, true)) { | ||
LOG.warn("Failed to delete fileset {} location {}", ident, filesetPath); | ||
|
@@ -459,7 +496,7 @@ public Schema createSchema(NameIdentifier ident, String comment, Map<String, Str | |
Path schemaPath = getSchemaPath(ident.name(), properties); | ||
if (schemaPath != null) { | ||
try { | ||
FileSystem fs = schemaPath.getFileSystem(hadoopConf); | ||
FileSystem fs = getFileSystem(schemaPath, hadoopConf); | ||
if (!fs.exists(schemaPath)) { | ||
if (!fs.mkdirs(schemaPath)) { | ||
// Fail the operation when failed to create the schema path. | ||
|
@@ -577,8 +614,7 @@ public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmpty | |
if (schemaPath == null) { | ||
return false; | ||
} | ||
|
||
FileSystem fs = schemaPath.getFileSystem(hadoopConf); | ||
FileSystem fs = getFileSystem(schemaPath, hadoopConf); | ||
// Nothing to delete if the schema path does not exist. | ||
if (!fs.exists(schemaPath)) { | ||
return false; | ||
|
@@ -731,7 +767,7 @@ private boolean hasCallerContext() { | |
private boolean checkSingleFile(Fileset fileset) { | ||
try { | ||
Path locationPath = new Path(fileset.storageLocation()); | ||
return locationPath.getFileSystem(hadoopConf).getFileStatus(locationPath).isFile(); | ||
return getFileSystem(locationPath, hadoopConf).getFileStatus(locationPath).isFile(); | ||
} catch (FileNotFoundException e) { | ||
// We should always return false here, same with the logic in `FileSystem.isFile(Path f)`. | ||
return false; | ||
|
@@ -742,4 +778,27 @@ private boolean checkSingleFile(Fileset fileset) { | |
fileset.name()); | ||
} | ||
} | ||
|
||
static FileSystem getFileSystem(Path path, Configuration conf) throws IOException { | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (path == null) { | ||
String defaultFS = conf.get("fs.defaultFS"); | ||
if (defaultFS != null) { | ||
path = new Path(defaultFS); | ||
} | ||
} | ||
|
||
String scheme; | ||
if (path == null || path.toUri().getScheme() == null) { | ||
scheme = "file"; | ||
} else { | ||
scheme = path.toUri().getScheme(); | ||
} | ||
|
||
FileSystemProvider provider = FILE_SYSTEM_PROVIDERS.get(scheme); | ||
if (provider == null) { | ||
throw new IllegalArgumentException("Unsupported scheme: " + scheme); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should clearly tell user why the exception is happened, the code here is not easy for user to know the actual reason. |
||
} | ||
|
||
return provider.getFileSystem(conf, path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...g-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/fs/HDFSFileSystemProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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.gravitino.catalog.hadoop.fs; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.gravitino.catalog.hadoop.FileSystemProvider; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
|
||
public class HDFSFileSystemProvider implements FileSystemProvider { | ||
|
||
@Override | ||
public FileSystem getFileSystem(Configuration configuration, Path path) throws IOException { | ||
yuqi1129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Path fileSystemPath = path; | ||
if (fileSystemPath == null) { | ||
String pathString = configuration.get("fs.defaultFS"); | ||
if (StringUtils.isNotBlank(pathString)) { | ||
fileSystemPath = new Path(pathString); | ||
} | ||
} | ||
|
||
if (fileSystemPath == null) { | ||
throw new IllegalArgumentException("The path should be specified."); | ||
} | ||
|
||
URI uri = fileSystemPath.toUri(); | ||
if (uri.getScheme() != null && !uri.getScheme().equals("hdfs")) { | ||
throw new IllegalArgumentException("The path should be a HDFS path."); | ||
} | ||
|
||
// Should we call DistributedFileSystem to create file system instance explicitly? If we | ||
// explicitly create a HDFS file system here, we can't reuse the file system cache in the | ||
// FileSystem class. | ||
String impl = configuration.get("fs.hdfs.impl"); | ||
if (impl == null) { | ||
configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); | ||
} else { | ||
if (!impl.equals("org.apache.hadoop.hdfs.DistributedFileSystem")) { | ||
throw new IllegalArgumentException( | ||
"The HDFS file system implementation class should be 'org.apache.hadoop.hdfs.DistributedFileSystem'."); | ||
} | ||
} | ||
|
||
try { | ||
if (HDFSFileSystemProvider.class.getClassLoader().loadClass(configuration.get("fs.hdfs.impl")) | ||
== null) { | ||
throw new IllegalArgumentException( | ||
"The HDFS file system implementation class is not found."); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalArgumentException("The HDFS file system implementation class is not found."); | ||
} | ||
|
||
return FileSystem.get(uri, configuration); | ||
} | ||
|
||
@Override | ||
public String getScheme() { | ||
return "hdfs"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/fs/LocalFileSystemProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.gravitino.catalog.hadoop.fs; | ||
|
||
import java.io.IOException; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.gravitino.catalog.hadoop.FileSystemProvider; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
|
||
public class LocalFileSystemProvider implements FileSystemProvider { | ||
|
||
@Override | ||
public FileSystem getFileSystem(Configuration configuration, Path path) throws IOException { | ||
Path fileSystemPath = path; | ||
if (fileSystemPath == null) { | ||
String pathString = configuration.get("fs.defaultFS"); | ||
if (StringUtils.isNotBlank(pathString)) { | ||
fileSystemPath = new Path(pathString); | ||
} | ||
} | ||
|
||
if (fileSystemPath == null) { | ||
fileSystemPath = new Path("file:///"); | ||
} | ||
|
||
return FileSystem.get(fileSystemPath.toUri(), configuration); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to extend the
AutoCloeable
and add theclose
method in the provider impl.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, there seems to be no resources managed by providers. Providers only provide file system instances that have already implemented the close interface.
Furthermore, I don't think
FileSystemProvider
could manage the life cycle of a file system. Please let me know if you have any further thoughts on this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.