-
Notifications
You must be signed in to change notification settings - Fork 735
support folders in S3 uri in CS tiers #13337
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c04c91
extend s3 uri parsing for CS tiers
swalrus1 8a74ee6
fix test
swalrus1 6cd42ba
Update s3_uri.h
swalrus1 443a8ca
remove regex
swalrus1 e5d8540
Merge branch 'parse-s3-uri' of github.com:swalrus1/ydb into parse-s3-uri
swalrus1 eade249
fix
swalrus1 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 hidden or 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 hidden or 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,4 @@ | ||
| #include "s3_uri.h" | ||
|
|
||
| namespace NKikimr::NColumnShard::NTiers { | ||
| } |
This file contains hidden or 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,179 @@ | ||
| #include <ydb/core/protos/flat_scheme_op.pb.h> | ||
|
|
||
| #include <ydb/library/accessor/accessor.h> | ||
| #include <ydb/library/conclusion/result.h> | ||
|
|
||
| #include <library/cpp/uri/uri.h> | ||
| #include <util/string/builder.h> | ||
|
|
||
| namespace NKikimr::NColumnShard::NTiers { | ||
|
|
||
| class TS3Uri { | ||
| private: | ||
| YDB_READONLY_DEF(std::optional<NKikimrSchemeOp::TS3Settings_EScheme>, Scheme); | ||
| YDB_READONLY_DEF(TString, Bucket); | ||
| YDB_READONLY_DEF(TString, Host); | ||
| YDB_READONLY_DEF(std::optional<ui16>, Port); | ||
| YDB_READONLY_DEF(std::optional<TString>, Folder); | ||
|
|
||
| enum TUriStyle { | ||
| PATH_STYLE = 1, | ||
| VIRTUAL_HOSTED_STYLE = 2, | ||
| }; | ||
|
|
||
| inline static const std::vector<TString> BucketHostSeparators = { ".s3.", ".s3-" }; | ||
|
|
||
| private: | ||
| static TStringBuf StripPath(const TStringBuf& path) { | ||
| TStringBuf stripped = path; | ||
| while (stripped.SkipPrefix("/")) { | ||
| } | ||
| while (stripped.ChopSuffix("/")) { | ||
| } | ||
| return stripped; | ||
| } | ||
|
|
||
| static std::optional<TUriStyle> DeduceUriStyle(const NUri::TUri& uri) { | ||
| const bool hasSubdomain = std::count(uri.GetHost().begin(), uri.GetHost().end(), '.') >= 2; | ||
| const bool hasPath = !StripPath(uri.GetField(NUri::TField::FieldPath)).Empty(); | ||
| if (hasSubdomain && !hasPath) { | ||
| return VIRTUAL_HOSTED_STYLE; | ||
| } | ||
| if (!hasSubdomain && hasPath) { | ||
| return PATH_STYLE; | ||
| } | ||
|
|
||
| // URI style deduction copied from AWS SDK for Java | ||
| for (const TString& sep : BucketHostSeparators) { | ||
| if (uri.GetHost().StartsWith(sep.substr(1))) { | ||
| return PATH_STYLE; | ||
| } | ||
| if (uri.GetHost().Contains(sep)) { | ||
| return VIRTUAL_HOSTED_STYLE; | ||
| } | ||
| } | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| static TConclusion<TS3Uri> ParsePathStyleUri(const NUri::TUri& input) { | ||
| TS3Uri result; | ||
|
|
||
| TStringBuf path = StripPath(input.GetField(NUri::TField::FieldPath)); | ||
|
|
||
| if (path.Empty()) { | ||
| return TConclusionStatus::Fail(TStringBuilder() << "Missing bucket in path-style S3 uri: " << input.Serialize()); | ||
| } | ||
|
|
||
| TStringBuf folder; | ||
| TStringBuf bucket; | ||
| if (path.TryRSplit('/', folder, bucket)) { | ||
| result.Folder = folder; | ||
| result.Bucket = bucket; | ||
| } else { | ||
| result.Bucket = path; | ||
| } | ||
|
|
||
| result.Host = input.GetHost(); | ||
|
|
||
| if (auto status = result.FillStyleAgnosticFields(input); status.IsFail()) { | ||
| return status; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| static TConclusion<TS3Uri> ParseVirtualHostedStyleUri(const NUri::TUri& input) { | ||
| TS3Uri result; | ||
|
|
||
| for (const TString& sep : BucketHostSeparators) { | ||
| if (const ui64 findSep = input.GetHost().find(sep); findSep != TStringBuf::npos) { | ||
| result.Bucket = input.GetHost().SubStr(0, findSep); | ||
| result.Host = input.GetHost().SubStr(findSep + 1); | ||
| break; | ||
| } | ||
| } | ||
| if (result.Host.empty()) { | ||
| TStringBuf host; | ||
| TStringBuf bucket; | ||
| if (input.GetHost().TrySplit('.', bucket, host)) { | ||
| result.Host = host; | ||
| result.Bucket = bucket; | ||
| } else { | ||
| return TConclusionStatus::Fail(TStringBuilder() << "Missing bucket in virtual-hosted style S3 uri: " << input.Serialize()); | ||
| } | ||
| } | ||
|
|
||
| if (TStringBuf path = StripPath(input.GetField(NUri::TField::FieldPath))) { | ||
| result.Folder = path; | ||
| } | ||
|
|
||
| if (auto status = result.FillStyleAgnosticFields(input); status.IsFail()) { | ||
| return status; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| TConclusionStatus FillStyleAgnosticFields(const NUri::TUri& from) { | ||
| if (from.GetField(NUri::TField::FieldPort)) { | ||
| Port = from.GetPort(); | ||
| } | ||
|
|
||
| switch (from.GetScheme()) { | ||
| case NUri::TScheme::SchemeEmpty: | ||
| break; | ||
| case NUri::TScheme::SchemeHTTP: | ||
| Scheme = NKikimrSchemeOp::TS3Settings_EScheme_HTTP; | ||
| break; | ||
| case NUri::TScheme::SchemeHTTPS: | ||
| Scheme = NKikimrSchemeOp::TS3Settings_EScheme_HTTPS; | ||
| break; | ||
| default: | ||
| return TConclusionStatus::Fail(TStringBuilder() << "Unexpected scheme in url: " << from.Serialize()); | ||
| } | ||
|
|
||
| return TConclusionStatus::Success(); | ||
| } | ||
|
|
||
| public: | ||
| static TConclusion<TS3Uri> ParseUri(const TString& input) { | ||
| NUri::TUri uri; | ||
| if (uri.Parse(input, NUri::TFeature::NewFeaturesRecommended) != NUri::TState::EParsed::ParsedOK) { | ||
| return TConclusionStatus::Fail("Cannot parse URI: " + input); | ||
| } | ||
|
|
||
| TUriStyle uriStyle; | ||
| if (const auto deducedStyle = DeduceUriStyle(uri)) { | ||
| uriStyle = *deducedStyle; | ||
| } else { | ||
| uriStyle = PATH_STYLE; | ||
| } | ||
|
|
||
| switch (uriStyle) { | ||
| case PATH_STYLE: | ||
| return ParsePathStyleUri(uri); | ||
| case VIRTUAL_HOSTED_STYLE: | ||
| return ParseVirtualHostedStyleUri(uri); | ||
| } | ||
| } | ||
|
|
||
| TString GetEndpoint() const { | ||
| TString endpoint = Host; | ||
| if (Port) { | ||
| endpoint += TStringBuilder() << ':' << *Port; | ||
| } | ||
| if (Folder) { | ||
| endpoint += TStringBuilder() << '/' << *Folder; | ||
| } | ||
| return endpoint; | ||
| } | ||
|
|
||
| void FillSettings(NKikimrSchemeOp::TS3Settings& settings) const { | ||
| settings.SetEndpoint(GetEndpoint()); | ||
| settings.SetBucket(Bucket); | ||
| if (Scheme) { | ||
| settings.SetScheme(*Scheme); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace NKikimr::NColumnShard::NTiers | ||
This file contains hidden or 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 hidden or 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.
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.
DeduceUriStyle(uri).value_or(PATH_STYLE)