-
Notifications
You must be signed in to change notification settings - Fork 609
🌱 [cron] generalize some of the transfer logic so it is easy to build new transfer agents #2454
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright 2021 Security Scorecard Authors | ||
| // | ||
| // Licensed 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 data | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "google.golang.org/protobuf/encoding/protojson" | ||
|
|
||
| "github.com/ossf/scorecard/v4/cron/config" | ||
| ) | ||
|
|
||
| // ShardSummary is a summary of information about a set of shards with the same | ||
| // creation time. | ||
| type ShardSummary struct { | ||
| creationTime time.Time | ||
| shardMetadata []byte | ||
| shardsExpected int | ||
| shardsCreated int | ||
| isTransferred bool | ||
| } | ||
|
|
||
| // IsCompleted checks if the percentage of completed shards is over the desired completion threshold. | ||
| // It also returns false to prevent transfers in cases where the expected number of shards is 0, | ||
| // as either the .shard_metadata file is missing, or there is nothing to transfer anyway. | ||
| func (s *ShardSummary) IsCompleted(completionThreshold float64) bool { | ||
| completedPercentage := float64(s.shardsCreated) / float64(s.shardsExpected) | ||
| return s.shardsExpected > 0 && completedPercentage >= completionThreshold | ||
| } | ||
|
|
||
| // IsTransferred returns true if the shards have already been transferred. | ||
| // A true value indicates that a transfer should not occur, a false value | ||
| // indicates that a transfer should occur if IsCompleted() also returns true. | ||
| func (s *ShardSummary) IsTransferred() bool { | ||
| return s.isTransferred | ||
| } | ||
|
|
||
| // Metadata returns the raw metadata about the bucket. | ||
| func (s *ShardSummary) Metadata() []byte { | ||
| return s.shardMetadata | ||
| } | ||
|
|
||
| // CreationTime returns the time the shards were created. This corresponds to | ||
| // the job time generated by the controller. | ||
| func (s *ShardSummary) CreationTime() time.Time { | ||
| return s.creationTime | ||
| } | ||
|
|
||
| func (s *ShardSummary) MarkTransferred(ctx context.Context, bucketURL string) error { | ||
| transferStatusFilename := GetTransferStatusFilename(s.creationTime) | ||
| if err := WriteToBlobStore(ctx, bucketURL, transferStatusFilename, nil); err != nil { | ||
| return fmt.Errorf("error during WriteToBlobStore: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // BucketSummary contains details about all the shards in a bucket grouped by | ||
| // their creation time. | ||
| type BucketSummary struct { | ||
| shards map[time.Time]*ShardSummary | ||
| } | ||
|
|
||
| func (summary *BucketSummary) getOrCreate(t time.Time) *ShardSummary { | ||
| if summary.shards[t] == nil { | ||
| summary.shards[t] = &ShardSummary{ | ||
| creationTime: t, | ||
| } | ||
| } | ||
| return summary.shards[t] | ||
| } | ||
|
|
||
| // Shards returns a slice of ShardSummary instances for each shard creation time. | ||
| func (summary *BucketSummary) Shards() []*ShardSummary { | ||
| var shards []*ShardSummary | ||
| for _, s := range summary.shards { | ||
| shards = append(shards, s) | ||
| } | ||
| return shards | ||
| } | ||
|
|
||
| // GetBucketSummary iterates through all files in a bucket and returns a | ||
| // BucketSummary with details on each set of shards grouped by creation time. | ||
| func GetBucketSummary(ctx context.Context, bucketURL string) (*BucketSummary, error) { | ||
| keys, err := GetBlobKeys(ctx, bucketURL) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error getting BlobKeys: %w", err) | ||
| } | ||
|
|
||
| summary := BucketSummary{ | ||
| shards: make(map[time.Time]*ShardSummary), | ||
| } | ||
| for _, key := range keys { | ||
| creationTime, filename, err := ParseBlobFilename(key) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error parsing Blob key: %w", err) | ||
| } | ||
| switch { | ||
| case strings.HasPrefix(filename, "shard-"): | ||
| summary.getOrCreate(creationTime).shardsCreated++ | ||
| case filename == config.TransferStatusFilename: | ||
| summary.getOrCreate(creationTime).isTransferred = true | ||
| case filename == config.ShardMetadataFilename: | ||
| keyData, err := GetBlobContent(ctx, bucketURL, key) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error during GetBlobContent: %w", err) | ||
| } | ||
| var metadata ShardMetadata | ||
| if err := protojson.Unmarshal(keyData, &metadata); err != nil { | ||
| return nil, fmt.Errorf("error parsing data as ShardMetadata: %w", err) | ||
| } | ||
| summary.getOrCreate(creationTime).shardsExpected = int(metadata.GetNumShard()) | ||
| summary.getOrCreate(creationTime).shardMetadata = keyData | ||
| default: | ||
| //nolint: goerr113 | ||
| return nil, fmt.Errorf("found unrecognized file: %s", key) | ||
| } | ||
| } | ||
| return &summary, nil | ||
| } | ||
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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.
Uh oh!
There was an error while loading. Please reload this page.