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

feat: enhances S3Download to filter by traced table names (backport #1374) #1380

Closed
Closed
Changes from all commits
Commits
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
72 changes: 44 additions & 28 deletions pkg/trace/fileserver.go
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@
defer w.Close()
defer m.Close()

part, err := m.CreateFormFile("filename", table+".jsonl")

Check failure on line 79 in pkg/trace/fileserver.go

GitHub Actions / golangci-lint

string `.jsonl` has 3 occurrences, make it a constant (goconst)
if err != nil {
return
}
@@ -263,7 +263,9 @@

// S3Download downloads files that match some prefix from an S3 bucket to a
// local directory dst.
func S3Download(dst, prefix string, cfg S3Config) error {
// fileNames is a list of traced jsonl file names to download. If it is empty, all traces are downloaded.
// fileNames should not have .jsonl suffix.
func S3Download(dst, prefix string, fileNames []string, cfg S3Config) error {
Copy link
Collaborator

@rootulp rootulp Jun 6, 2024

Choose a reason for hiding this comment

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

this is technically a Go API breaking change and pkg/trace is public. Are you sure this should be backported as is? @staheri14

Copy link
Collaborator

Choose a reason for hiding this comment

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

That is a good point, I'll defer it to @evan-forbes, do you think we should avoid merging this change? I can also re-implement it in a backward compatible way i.e., by introducing two different S3Download methods, with and without fileNames.

Copy link
Member

@evan-forbes evan-forbes Jun 7, 2024

Choose a reason for hiding this comment

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

nice let's do all things backwards compatible if we can out of good habit. If there significant cost to doing them backwards compatible then we can reevaluate.

ofc, we just need to add a deprecated note to this function

// Ensure local directory structure exists
err := os.MkdirAll(dst, os.ModePerm)
if err != nil {
@@ -292,37 +294,51 @@

err = s3Svc.ListObjectsV2Pages(input, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
for _, content := range page.Contents {
localFilePath := filepath.Join(dst, prefix, strings.TrimPrefix(*content.Key, prefix))
fmt.Printf("Downloading %s to %s\n", *content.Key, localFilePath)
key := *content.Key

// Create the directories in the path
if err := os.MkdirAll(filepath.Dir(localFilePath), os.ModePerm); err != nil {
return false
// If no fileNames are specified, download all files
if len(fileNames) == 0 {
fileNames = append(fileNames, strings.TrimPrefix(key, prefix))
}

// Create a file to write the S3 Object contents to.
f, err := os.Create(localFilePath)
if err != nil {
return false
}

resp, err := s3Svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(cfg.BucketName),
Key: aws.String(*content.Key),
})
if err != nil {
f.Close()
continue
for _, filename := range fileNames {
// Add .jsonl suffix to the fileNames
fullFilename := filename + ".jsonl"
if strings.HasSuffix(key, fullFilename) {
localFilePath := filepath.Join(dst, prefix, strings.TrimPrefix(key, prefix))
fmt.Printf("Downloading %s to %s\n", key, localFilePath)

// Create the directories in the path
if err := os.MkdirAll(filepath.Dir(localFilePath), os.ModePerm); err != nil {
return false
}

// Create a file to write the S3 Object contents to.
f, err := os.Create(localFilePath)
if err != nil {
return false
}

resp, err := s3Svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(cfg.BucketName),
Key: aws.String(key),
})
if err != nil {
f.Close()
continue
}
defer resp.Body.Close()

// Copy the contents of the S3 object to the local file
if _, err := io.Copy(f, resp.Body); err != nil {
f.Close()
return false
}

fmt.Printf("Successfully downloaded %s to %s\n", key, localFilePath)
f.Close()
}
}
defer resp.Body.Close()

// Copy the contents of the S3 object to the local file
if _, err := io.Copy(f, resp.Body); err != nil {
return false
}

fmt.Printf("Successfully downloaded %s to %s\n", *content.Key, localFilePath)
f.Close()
}
return !lastPage // continue paging
})
Loading