-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parallelise epoch processing subtasks (#12191)
Parallelising some tasks by using worker pool. Worker pool will save (last)error if any task returns it and we return it by `Error()` call after closing worker pool we do: `wp := CreateWorkerPool(runtime.NumCPU())` `wp.AddWork(func() error { some work })` `wp.WaitAndClose()` `return wp.Error()` --------- Co-authored-by: shota.silagadze <[email protected]> Co-authored-by: Giulio <[email protected]>
- Loading branch information
1 parent
4a28b7f
commit e6a0caa
Showing
5 changed files
with
121 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,95 @@ | ||
// Copyright 2024 The Erigon Authors | ||
// This file is part of Erigon. | ||
// | ||
// Erigon is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Erigon is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with Erigon. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package statechange | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
type WorkerPool struct { | ||
work chan func() error | ||
wg sync.WaitGroup | ||
atomicErr unsafe.Pointer | ||
} | ||
|
||
// CreateWorkerPool initializes a pool of workers to process tasks. | ||
func CreateWorkerPool(numWorkers int) *WorkerPool { | ||
wp := WorkerPool{ | ||
work: make(chan func() error, 1000), | ||
} | ||
for i := 1; i <= numWorkers; i++ { | ||
go wp.StartWorker() | ||
} | ||
return &wp | ||
} | ||
|
||
// close work channel and finish | ||
func (wp *WorkerPool) WaitAndClose() { | ||
// Wait for all workers to finish. | ||
wp.wg.Wait() | ||
// Close the task channel to indicate no more tasks will be sent. | ||
close(wp.work) | ||
} | ||
|
||
// Worker is the worker that processes tasks. | ||
func (wp *WorkerPool) StartWorker() { | ||
for task := range wp.work { | ||
if err := task(); err != nil { | ||
atomic.StorePointer(&wp.atomicErr, unsafe.Pointer(&err)) | ||
} | ||
wp.wg.Done() | ||
} | ||
} | ||
|
||
func (wp *WorkerPool) Error() error { | ||
errPointer := atomic.LoadPointer(&wp.atomicErr) | ||
if errPointer == nil { | ||
return nil | ||
} | ||
return *(*error)(errPointer) | ||
} | ||
|
||
// enqueue work | ||
func (wp *WorkerPool) AddWork(f func() error) { | ||
wp.wg.Add(1) | ||
wp.work <- f | ||
} | ||
|
||
func ParallellForLoop(numWorkers int, from, to int, f func(int) error) error { | ||
// divide the work into numWorkers parts | ||
size := (to - from) / numWorkers | ||
wp := CreateWorkerPool(numWorkers) | ||
for i := 0; i < numWorkers; i++ { | ||
start := from + i*size | ||
end := start + size | ||
if i == numWorkers-1 { | ||
end = to | ||
} | ||
wp.AddWork(func() error { | ||
for j := start; j < end; j++ { | ||
if err := f(j); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
} | ||
wp.WaitAndClose() | ||
return wp.Error() | ||
} |