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
Move `shell.ErrorWaitGroup` into a common file, to cleanly reuse across `weed shell` commands. (#6780)
Lisandro Pinba1d82db9023
diff --git a/weed/shell/command_ec_common.go b/weed/shell/command_ec_common.goindex 209d8a7335..a6f27232e0 100644--- a/weed/shell/command_ec_common.go+++ b/weed/shell/command_ec_common.go@@ -7,7 +7,6 @@ import ("math/rand/v2""slices""sort"- "sync""time""github.com/seaweedfs/seaweedfs/weed/glog"@@ -113,61 +112,6 @@ var (getDefaultReplicaPlacement = _getDefaultReplicaPlacement)-type ErrorWaitGroup struct {- maxConcurrency int- wg *sync.WaitGroup- wgSem chan bool- errors []error- errorsMu sync.Mutex-}-type ErrorWaitGroupTask func() error--func NewErrorWaitGroup(maxConcurrency int) *ErrorWaitGroup {- if maxConcurrency <= 0 {- // No concurrency = one task at the time- maxConcurrency = 1- }- return &ErrorWaitGroup{- maxConcurrency: maxConcurrency,- wg: &sync.WaitGroup{},- wgSem: make(chan bool, maxConcurrency),- }-}--func (ewg *ErrorWaitGroup) Reset() {- close(ewg.wgSem)-- ewg.wg = &sync.WaitGroup{}- ewg.wgSem = make(chan bool, ewg.maxConcurrency)- ewg.errors = nil-}--func (ewg *ErrorWaitGroup) Add(f ErrorWaitGroupTask) {- if ewg.maxConcurrency <= 1 {- // Keep run order deterministic when parallelization is off- ewg.errors = append(ewg.errors, f())- return- }-- ewg.wg.Add(1)- go func() {- ewg.wgSem <- true-- err := f()- ewg.errorsMu.Lock()- ewg.errors = append(ewg.errors, err)- ewg.errorsMu.Unlock()-- <-ewg.wgSem- ewg.wg.Done()- }()-}--func (ewg *ErrorWaitGroup) Wait() error {- ewg.wg.Wait()- return errors.Join(ewg.errors...)-}-func _getDefaultReplicaPlacement(commandEnv *CommandEnv) (*super_block.ReplicaPlacement, error) {var resp *master_pb.GetMasterConfigurationResponsevar err errordiff --git a/weed/shell/common.go b/weed/shell/common.gonew file mode 100644index 0000000000..9fe190b314--- /dev/null+++ b/weed/shell/common.go@@ -0,0 +1,66 @@+package shell++import (+ "errors"+ "sync"+)++// ErrorWaitGroup implements a goroutine wait group which aggregates errors, if any.+type ErrorWaitGroup struct {+ maxConcurrency int+ wg *sync.WaitGroup+ wgSem chan bool+ errors []error+ errorsMu sync.Mutex+}++type ErrorWaitGroupTask func() error++func NewErrorWaitGroup(maxConcurrency int) *ErrorWaitGroup {+ if maxConcurrency <= 0 {+ // no concurrency = one task at the time+ maxConcurrency = 1+ }+ return &ErrorWaitGroup{+ maxConcurrency: maxConcurrency,+ wg: &sync.WaitGroup{},+ wgSem: make(chan bool, maxConcurrency),+ }+}++// Reset restarts an ErrorWaitGroup, keeping original settings. Errors and pending goroutines, if any, are flushed.+func (ewg *ErrorWaitGroup) Reset() {+ close(ewg.wgSem)++ ewg.wg = &sync.WaitGroup{}+ ewg.wgSem = make(chan bool, ewg.maxConcurrency)+ ewg.errors = nil+}++// Add queues an ErrorWaitGroupTask to be executed as a goroutine.+func (ewg *ErrorWaitGroup) Add(f ErrorWaitGroupTask) {+ if ewg.maxConcurrency <= 1 {+ // keep run order deterministic when parallelization is off+ ewg.errors = append(ewg.errors, f())+ return+ }++ ewg.wg.Add(1)+ go func() {+ ewg.wgSem <- true++ err := f()+ ewg.errorsMu.Lock()+ ewg.errors = append(ewg.errors, err)+ ewg.errorsMu.Unlock()++ <-ewg.wgSem+ ewg.wg.Done()+ }()+}++// Wait sleeps until all ErrorWaitGroupTasks are completed, then returns errors for them.+func (ewg *ErrorWaitGroup) Wait() error {+ ewg.wg.Wait()+ return errors.Join(ewg.errors...)+}