diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 9fd59823..9ec393ef 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -97,6 +97,10 @@ var ( Name: "zip", Usage: "Extract from remote zip file (MinIO server source only)", }, + cli.IntFlag{ + Name: "max-workers", + Usage: "maximum number of concurrent copies (default: autodetect)", + }, checksumFlag, } ) @@ -366,7 +370,7 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli. quitCh := make(chan struct{}) statusCh := make(chan URLs) - parallel := newParallelManager(statusCh) + parallel := newParallelManager(statusCh, cli.Int("max-workers")) go func() { gracefulStop := func() { diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 5eeb3ede..b1204e44 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -144,6 +144,10 @@ var ( Name: "skip-errors", Usage: "skip any errors when mirroring", }, + cli.IntFlag{ + Name: "max-workers", + Usage: "maximum number of concurrent copies (default: autodetect)", + }, checksumFlag, } ) @@ -907,7 +911,7 @@ func newMirrorJob(srcURL, dstURL string, opts mirrorOptions) *mirrorJob { watcher: NewWatcher(UTCNow()), } - mj.parallel = newParallelManager(mj.statusCh) + mj.parallel = newParallelManager(mj.statusCh, opts.maxWorkers) // we'll define the status to use here, // do we want the quiet status? or the progressbar @@ -1025,6 +1029,7 @@ func runMirror(ctx context.Context, srcURL, dstURL string, cli *cli.Context, enc userMetadata: userMetadata, encKeyDB: encKeyDB, activeActive: isActiveActive, + maxWorkers: cli.Int("max-workers"), } // If we are not using active/active and we are not removing diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index 1c71fb89..4f15a6a0 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -278,6 +278,7 @@ type mirrorOptions struct { userMetadata map[string]string checksum minio.ChecksumType sourceListingOnly bool + maxWorkers int } // Prepares urls that need to be copied or removed based on requested options. diff --git a/cmd/parallel-manager.go b/cmd/parallel-manager.go index 9ba01ab2..b4d2050b 100644 --- a/cmd/parallel-manager.go +++ b/cmd/parallel-manager.go @@ -76,11 +76,18 @@ type ParallelManager struct { // The maximum memory to use maxMem uint64 + + // The maximum workers will be created + maxWorkers int } // addWorker creates a new worker to process tasks func (p *ParallelManager) addWorker() { - if atomic.LoadUint32(&p.workersNum) >= maxParallelWorkers { + maxWorkers := maxParallelWorkers + if p.maxWorkers > 0 { + maxWorkers = p.maxWorkers + } + if int(atomic.LoadUint32(&p.workersNum)) >= maxWorkers { // Number of maximum workers is reached, no need to // to create a new one. return @@ -266,7 +273,7 @@ func availableMemory() (available uint64) { } // newParallelManager starts new workers waiting for executing tasks -func newParallelManager(resultCh chan URLs) *ParallelManager { +func newParallelManager(resultCh chan URLs, maxWorkers int) *ParallelManager { p := &ParallelManager{ wg: &sync.WaitGroup{}, workersNum: 0, @@ -274,6 +281,7 @@ func newParallelManager(resultCh chan URLs) *ParallelManager { queueCh: make(chan task), resultCh: resultCh, maxMem: availableMemory(), + maxWorkers: maxWorkers, } // Start with runtime.NumCPU().