SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.
Go blob-storagecloud-drivedistributed-file-systemdistributed-storageerasure-codingfusehadoop-hdfshdfskuberneteslancelancedbobject-storageposixs3s3-storageseaweedfstiered-file-systemvector-search
upload: upload -dir files concurrently behind a new -c flag (#11918)
Chris Lu164a7d8b5604
diff --git a/weed/command/upload.go b/weed/command/upload.goindex bb542a030c..def64adc77 100644--- a/weed/command/upload.go+++ b/weed/command/upload.go@@ -6,6 +6,7 @@ import ("fmt""os""path/filepath"+ "sync""github.com/seaweedfs/seaweedfs/weed/pb""github.com/seaweedfs/seaweedfs/weed/pb/master_pb"@@ -31,6 +32,7 @@ type UploadOptions struct {diskType *stringmaxMB *intusePublicUrl *bool+ concurrency *int}func init() {@@ -46,6 +48,7 @@ func init() {upload.ttl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")upload.maxMB = cmdUpload.Flag.Int("maxMB", 4, "split files larger than the limit")upload.usePublicUrl = cmdUpload.Flag.Bool("usePublicUrl", false, "upload to public url from volume server")+ upload.concurrency = cmdUpload.Flag.Int("c", 8, "concurrent file upload goroutines when uploading a folder")}var cmdUpload = &Command{@@ -85,6 +88,7 @@ func runUpload(cmd *Command, args []string) bool {if *upload.dir == "" {return false}+ var filePaths []stringerr = filepath.Walk(util.ResolvePath(*upload.dir), func(path string, info os.FileInfo, err error) error {if err == nil {if !info.IsDir() {@@ -93,23 +97,7 @@ func runUpload(cmd *Command, args []string) bool {return nil}}- parts, e := operation.NewFileParts([]string{path})- if e != nil {- return e- }- results, e := operation.SubmitFiles(func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*upload.master) }, grpcDialOption, parts, operation.StoragePreference{- Replication: *upload.replication,- Collection: *upload.collection,- DataCenter: *upload.dataCenter,- Ttl: *upload.ttl,- DiskType: *upload.diskType,- MaxMB: *upload.maxMB,- }, *upload.usePublicUrl)- bytes, _ := json.Marshal(results)- fmt.Println(string(bytes))- if e != nil {- return e- }+ filePaths = append(filePaths, path)}} else {fmt.Println(err)@@ -118,6 +106,72 @@ func runUpload(cmd *Command, args []string) bool {})if err != nil {fmt.Println(err.Error())+ }++ concurrency := *upload.concurrency+ if concurrency < 1 {+ concurrency = 1+ }+ masterFn := func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*upload.master) }+ pref := operation.StoragePreference{+ Replication: *upload.replication,+ Collection: *upload.collection,+ DataCenter: *upload.dataCenter,+ Ttl: *upload.ttl,+ DiskType: *upload.diskType,+ MaxMB: *upload.maxMB,+ }+ results := make([][]operation.SubmitResult, len(filePaths))+ ready := make([]chan struct{}, len(filePaths))+ for index := range ready {+ ready[index] = make(chan struct{})+ }+ printDone := make(chan bool, 1)+ go func() {+ failed := false+ for index := range ready {+ <-ready[index]+ bytes, _ := json.Marshal(results[index])+ fmt.Println(string(bytes))+ for _, r := range results[index] {+ if r.Error != "" {+ failed = true+ }+ }+ }+ printDone <- failed+ }()++ sem := make(chan struct{}, concurrency)+ var wg sync.WaitGroup+ for index, filePath := range filePaths {+ sem <- struct{}{}+ wg.Add(1)+ go func(index int, filePath string) {+ defer close(ready[index])+ defer wg.Done()+ defer func() { <-sem }()+ parts, e := operation.NewFileParts([]string{filePath})+ if e != nil {+ results[index] = []operation.SubmitResult{{FileName: filepath.Base(filePath), Error: e.Error()}}+ return+ }+ fileResults, e := operation.SubmitFiles(masterFn, grpcDialOption, parts, pref, *upload.usePublicUrl)+ if e != nil {+ for i := range fileResults {+ if fileResults[i].Error == "" {+ fileResults[i].Error = e.Error()+ }+ }+ }+ results[index] = fileResults+ }(index, filePath)+ }+ wg.Wait()+ if <-printDone {+ return false+ }+ if err != nil {return false}} else {@@ -134,12 +188,12 @@ func runUpload(cmd *Command, args []string) bool {DiskType: *upload.diskType,MaxMB: *upload.maxMB,}, *upload.usePublicUrl)+ bytes, _ := json.Marshal(results)+ fmt.Println(string(bytes))if err != nil {fmt.Println(err.Error())return false}- bytes, _ := json.Marshal(results)- fmt.Println(string(bytes))}return true}diff --git a/weed/operation/submit.go b/weed/operation/submit.goindex 1d94f5bab3..347b1647fd 100644--- a/weed/operation/submit.go+++ b/weed/operation/submit.go@@ -10,6 +10,7 @@ import ("path""strconv""strings"+ "sync""github.com/seaweedfs/seaweedfs/weed/pb"@@ -50,6 +51,10 @@ type StoragePreference struct {type GetMasterFn func(ctx context.Context) pb.ServerAddress+// submitUploadWorkers bounds how many files one SubmitFiles call uploads at+// once; the batch assign above already fetched all their file ids.+const submitUploadWorkers = 4+func SubmitFiles(masterFn GetMasterFn, grpcDialOption grpc.DialOption, files []*FilePart, pref StoragePreference, usePublicUrl bool) ([]SubmitResult, error) {results := make([]SubmitResult, len(files))var totalBytes int64@@ -72,8 +77,11 @@ func SubmitFiles(masterFn GetMasterFn, grpcDialOption grpc.DialOption, files []*}ret, err := Assign(context.Background(), masterFn, grpcDialOption, ar)if err != nil {- for index := range files {+ for index, file := range files {results[index].Error = err.Error()+ if closer, ok := file.Reader.(io.Closer); ok {+ closer.Close()+ }}return results, err}@@ -87,14 +95,35 @@ func SubmitFiles(masterFn GetMasterFn, grpcDialOption grpc.DialOption, files []*file.Server = ret.PublicUrl}file.Pref = pref- results[index].Size, err = file.Upload(pref.MaxMB, masterFn, usePublicUrl, ret.Auth, grpcDialOption)- if err != nil {- results[index].Error = err.Error()- }results[index].Fid = file.Fidresults[index].FileUrl = ret.PublicUrl + "/" + file.Fid}- return results, nil++ var wg sync.WaitGroup+ var lastErrLock sync.Mutex+ jobs := make(chan int)+ for w := 0; w < min(submitUploadWorkers, len(files)); w++ {+ wg.Add(1)+ go func() {+ defer wg.Done()+ for index := range jobs {+ size, uploadErr := files[index].Upload(pref.MaxMB, masterFn, usePublicUrl, ret.Auth, grpcDialOption)+ results[index].Size = size+ if uploadErr != nil {+ results[index].Error = uploadErr.Error()+ lastErrLock.Lock()+ err = uploadErr+ lastErrLock.Unlock()+ }+ }+ }()+ }+ for index := range files {+ jobs <- index+ }+ close(jobs)+ wg.Wait()+ return results, err}func NewFileParts(fullPathFilenames []string) (ret []*FilePart, err error) {@@ -118,6 +147,8 @@ func newFilePart(fullPathFilename string) (ret *FilePart, err error) {fi, fiErr := fh.Stat()if fiErr != nil {glog.V(0).Info("Failed to stat file:", fullPathFilename)+ fh.Close()+ ret.Reader = nilreturn ret, fiErr}ret.ModTime = fi.ModTime().UTC().Unix()