-
Notifications
You must be signed in to change notification settings - Fork 319
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
improve(filesystem-hadoop): support path without scheme for gvfs api #2779
Merged
jerryshao
merged 6 commits into
apache:main
from
coolderli:support-fileset-withoutscheme
Apr 15, 2024
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a42ca1
improve(filesystem-hadoop3): support releative path for fileset
coolderli 50a92af
refactor some code
coolderli 53e236a
refactor some code
coolderli 2dec27b
address comment
coolderli 99a8c66
fix ut
coolderli 264615a
fix description
coolderli 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,13 @@ | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FSDataInputStream; | ||
import org.apache.hadoop.fs.FSDataOutputStream; | ||
import org.apache.hadoop.fs.FileStatus; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.InvalidPathException; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.fs.permission.FsPermission; | ||
import org.apache.hadoop.util.Progressable; | ||
|
@@ -51,6 +52,13 @@ public class GravitinoVirtualFileSystem extends FileSystem { | |
private Cache<NameIdentifier, Pair<Fileset, FileSystem>> filesetCache; | ||
private ScheduledThreadPoolExecutor scheduler; | ||
|
||
// The pattern is used to match gvfs path. The scheme prefix (gvfs://) is optional. | ||
// The following path can be match: | ||
// gvfs://fileset/fileset_catalog/fileset_schema/fileset1/file.txt | ||
// /fileset_catalog/fileset_schema/fileset1/sub_dir/ | ||
private static final Pattern IDENTIFIER_PATTERN = | ||
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. Could you add a description for this pattern which string will be matched? 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. done |
||
Pattern.compile("^(?:gvfs://fileset)?/([^/]+)/([^/]+)/([^/]+)(?:[/[^/]+]*)$"); | ||
|
||
@Override | ||
public void initialize(URI name, Configuration configuration) throws IOException { | ||
if (!name.toString().startsWith(GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX)) { | ||
|
@@ -214,39 +222,33 @@ private void checkAuthConfig(String authType, String configKey, String configVal | |
authType); | ||
} | ||
|
||
private String concatVirtualPrefix(NameIdentifier identifier) { | ||
return GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX | ||
+ identifier.namespace().level(1) | ||
+ "/" | ||
+ identifier.namespace().level(2) | ||
+ "/" | ||
+ identifier.name(); | ||
private String getVirtualLocation(NameIdentifier identifier, boolean withScheme) { | ||
return String.format( | ||
"%s/%s/%s/%s", | ||
withScheme ? GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX : "", | ||
identifier.namespace().level(1), | ||
identifier.namespace().level(2), | ||
identifier.name()); | ||
} | ||
|
||
private Path getActualPathByIdentifier( | ||
NameIdentifier identifier, Pair<Fileset, FileSystem> filesetPair, Path path) { | ||
String virtualPath = path.toString(); | ||
if (!virtualPath.startsWith(GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX)) { | ||
throw new InvalidPathException( | ||
String.format( | ||
"Path %s doesn't start with the scheme \"%s\".", | ||
virtualPath, GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX)); | ||
} | ||
boolean withScheme = | ||
virtualPath.startsWith(GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX); | ||
String virtualLocation = getVirtualLocation(identifier, withScheme); | ||
String storageLocation = filesetPair.getLeft().storageLocation(); | ||
try { | ||
if (checkMountsSingleFile(filesetPair)) { | ||
String virtualPrefix = concatVirtualPrefix(identifier); | ||
Preconditions.checkArgument( | ||
virtualPath.equals(virtualPrefix), | ||
virtualPath.equals(virtualLocation), | ||
"Path: %s should be same with the virtual prefix: %s, because the fileset only mounts a single file.", | ||
virtualPath, | ||
virtualPrefix); | ||
virtualLocation); | ||
|
||
return new Path(filesetPair.getLeft().storageLocation()); | ||
return new Path(storageLocation); | ||
} else { | ||
return new Path( | ||
virtualPath.replaceFirst( | ||
concatVirtualPrefix(identifier), | ||
new Path(filesetPair.getLeft().storageLocation()).toString())); | ||
return new Path(virtualPath.replaceFirst(virtualLocation, storageLocation)); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
|
@@ -275,37 +277,32 @@ private boolean checkMountsSingleFile(Pair<Fileset, FileSystem> filesetPair) { | |
private FileStatus convertFileStatusPathPrefix( | ||
FileStatus fileStatus, String actualPrefix, String virtualPrefix) { | ||
String filePath = fileStatus.getPath().toString(); | ||
if (!filePath.startsWith(actualPrefix)) { | ||
throw new InvalidPathException( | ||
String.format("Path %s doesn't start with prefix \"%s\".", filePath, actualPrefix)); | ||
} | ||
Preconditions.checkArgument( | ||
filePath.startsWith(actualPrefix), | ||
"Path %s doesn't start with prefix \"%s\".", | ||
filePath, | ||
actualPrefix); | ||
Path path = new Path(filePath.replaceFirst(actualPrefix, virtualPrefix)); | ||
fileStatus.setPath(path); | ||
|
||
return fileStatus; | ||
} | ||
|
||
private NameIdentifier extractIdentifier(URI virtualUri) { | ||
@VisibleForTesting | ||
NameIdentifier extractIdentifier(URI virtualUri) { | ||
String virtualPath = virtualUri.toString(); | ||
Preconditions.checkArgument( | ||
virtualUri | ||
.toString() | ||
.startsWith(GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX), | ||
"Path %s doesn't start with scheme prefix \"%s\".", | ||
virtualUri, | ||
GravitinoVirtualFileSystemConfiguration.GVFS_FILESET_PREFIX); | ||
|
||
if (StringUtils.isBlank(virtualUri.toString())) { | ||
throw new InvalidPathException("Uri which need be extracted cannot be null or empty."); | ||
} | ||
StringUtils.isNotBlank(virtualPath), | ||
"Uri which need be extracted cannot be null or empty."); | ||
|
||
// remove first '/' symbol with empty string | ||
String[] reservedDirs = | ||
Arrays.stream(virtualUri.getPath().replaceFirst("/", "").split("/")).toArray(String[]::new); | ||
Matcher matcher = IDENTIFIER_PATTERN.matcher(virtualPath); | ||
Preconditions.checkArgument( | ||
reservedDirs.length >= 3, "URI %s doesn't contains valid identifier", virtualUri); | ||
matcher.matches() && matcher.groupCount() == 3, | ||
"URI %s doesn't contains valid identifier", | ||
virtualPath); | ||
|
||
return NameIdentifier.ofFileset( | ||
metalakeName, reservedDirs[0], reservedDirs[1], reservedDirs[2]); | ||
metalakeName, matcher.group(1), matcher.group(2), matcher.group(3)); | ||
} | ||
|
||
private FilesetContext getFilesetContext(Path virtualPath) { | ||
|
@@ -449,7 +446,7 @@ public FileStatus getFileStatus(Path path) throws IOException { | |
return convertFileStatusPathPrefix( | ||
fileStatus, | ||
context.getFileset().storageLocation(), | ||
concatVirtualPrefix(context.getIdentifier())); | ||
getVirtualLocation(context.getIdentifier(), true)); | ||
} | ||
|
||
@Override | ||
|
@@ -462,7 +459,7 @@ public FileStatus[] listStatus(Path path) throws IOException { | |
convertFileStatusPathPrefix( | ||
fileStatus, | ||
new Path(context.getFileset().storageLocation()).toString(), | ||
concatVirtualPrefix(context.getIdentifier()))) | ||
getVirtualLocation(context.getIdentifier(), true))) | ||
.toArray(FileStatus[]::new); | ||
} | ||
|
||
|
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
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
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.
The scheme prefix should be "gvfs://fileset", not "gvfs://", right? If so, can you please clarify it?
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.
@jerryshao You are right. I have fixed it. Please take a look. Thanks.