diff options
author | Valery Piashchynski <[email protected]> | 2021-02-08 23:21:54 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-02-08 23:21:54 +0300 |
commit | da64d9fbab7d73e203e7dbbb9503f4d422feaab0 (patch) | |
tree | 3dc3d5dd4a8c4de7d4b57baf2eeb1089f831bc1c /pkg/worker_watcher | |
parent | 3e92e3df723ca1c4f152d8526eebfd7184e6fcec (diff) |
BaseProcess interface as a return type in the worker_watcher,pool and
worker interface
Diffstat (limited to 'pkg/worker_watcher')
-rw-r--r-- | pkg/worker_watcher/interface.go | 10 | ||||
-rw-r--r-- | pkg/worker_watcher/stack.go | 58 | ||||
-rw-r--r-- | pkg/worker_watcher/stack_test.go | 2 | ||||
-rwxr-xr-x | pkg/worker_watcher/worker_watcher.go | 25 |
4 files changed, 48 insertions, 47 deletions
diff --git a/pkg/worker_watcher/interface.go b/pkg/worker_watcher/interface.go index ce5011c0..a3552e7e 100644 --- a/pkg/worker_watcher/interface.go +++ b/pkg/worker_watcher/interface.go @@ -9,13 +9,13 @@ import ( // Watcher is an interface for the Sync workers lifecycle type Watcher interface { // Watch used to add workers to the stack - Watch(workers []worker.SyncWorker) error + Watch(workers []worker.BaseProcess) error // Get provide first free worker - Get(ctx context.Context) (worker.SyncWorker, error) + Get(ctx context.Context) (worker.BaseProcess, error) // Push enqueues worker back - Push(w worker.SyncWorker) + Push(w worker.BaseProcess) // Allocate - allocates new worker and put it into the WorkerWatcher Allocate() error @@ -24,8 +24,8 @@ type Watcher interface { Destroy(ctx context.Context) // WorkersList return all stack w/o removing it from internal storage - List() []worker.SyncWorker + List() []worker.BaseProcess // RemoveWorker remove worker from the stack - Remove(wb worker.SyncWorker) error + Remove(wb worker.BaseProcess) error } diff --git a/pkg/worker_watcher/stack.go b/pkg/worker_watcher/stack.go index 9a0bc6a4..69e2024b 100644 --- a/pkg/worker_watcher/stack.go +++ b/pkg/worker_watcher/stack.go @@ -9,8 +9,8 @@ import ( ) type Stack struct { - workers []*worker.SyncWorkerImpl - mutex sync.RWMutex + sync.RWMutex + workers []worker.BaseProcess destroy bool actualNumOfWorkers uint64 initialNumOfWorkers uint64 @@ -19,15 +19,15 @@ type Stack struct { func NewWorkersStack(initialNumOfWorkers uint64) *Stack { w := runtime.NumCPU() return &Stack{ - workers: make([]*worker.SyncWorkerImpl, 0, w), + workers: make([]worker.BaseProcess, 0, w), actualNumOfWorkers: 0, initialNumOfWorkers: initialNumOfWorkers, } } func (stack *Stack) Reset() { - stack.mutex.Lock() - defer stack.mutex.Unlock() + stack.Lock() + defer stack.Unlock() stack.actualNumOfWorkers = 0 stack.workers = nil } @@ -35,21 +35,21 @@ func (stack *Stack) Reset() { // Push worker back to the stack // If stack in destroy state, Push will provide 100ms window to unlock the mutex func (stack *Stack) Push(w worker.BaseProcess) { - stack.mutex.Lock() - defer stack.mutex.Unlock() + stack.Lock() + defer stack.Unlock() stack.actualNumOfWorkers++ - stack.workers = append(stack.workers, w.(*worker.SyncWorkerImpl)) + stack.workers = append(stack.workers, w) } func (stack *Stack) IsEmpty() bool { - stack.mutex.Lock() - defer stack.mutex.Unlock() + stack.Lock() + defer stack.Unlock() return len(stack.workers) == 0 } -func (stack *Stack) Pop() (*worker.SyncWorkerImpl, bool) { - stack.mutex.Lock() - defer stack.mutex.Unlock() +func (stack *Stack) Pop() (worker.BaseProcess, bool) { + stack.Lock() + defer stack.Unlock() // do not release new stack if stack.destroy { @@ -68,8 +68,8 @@ func (stack *Stack) Pop() (*worker.SyncWorkerImpl, bool) { } func (stack *Stack) FindAndRemoveByPid(pid int64) bool { - stack.mutex.Lock() - defer stack.mutex.Unlock() + stack.Lock() + defer stack.Unlock() for i := 0; i < len(stack.workers); i++ { // worker in the stack, reallocating if stack.workers[i].Pid() == pid { @@ -84,10 +84,10 @@ func (stack *Stack) FindAndRemoveByPid(pid int64) bool { } // Workers return copy of the workers in the stack -func (stack *Stack) Workers() []worker.SyncWorker { - stack.mutex.Lock() - defer stack.mutex.Unlock() - workersCopy := make([]worker.SyncWorker, 0, 1) +func (stack *Stack) Workers() []worker.BaseProcess { + stack.Lock() + defer stack.Unlock() + workersCopy := make([]worker.BaseProcess, 0, 1) // copy // TODO pointers, copy have no sense for _, v := range stack.workers { @@ -100,40 +100,40 @@ func (stack *Stack) Workers() []worker.SyncWorker { } func (stack *Stack) isDestroying() bool { - stack.mutex.Lock() - defer stack.mutex.Unlock() + stack.Lock() + defer stack.Unlock() return stack.destroy } // we also have to give a chance to pool to Push worker (return it) -func (stack *Stack) Destroy(ctx context.Context) { - stack.mutex.Lock() +func (stack *Stack) Destroy(_ context.Context) { + stack.Lock() stack.destroy = true - stack.mutex.Unlock() + stack.Unlock() tt := time.NewTicker(time.Millisecond * 500) defer tt.Stop() for { select { case <-tt.C: - stack.mutex.Lock() + stack.Lock() // that might be one of the workers is working if stack.initialNumOfWorkers != stack.actualNumOfWorkers { - stack.mutex.Unlock() + stack.Unlock() continue } - stack.mutex.Unlock() + stack.Unlock() // unnecessary mutex, but // just to make sure. All stack at this moment are in the stack // Pop operation is blocked, push can't be done, since it's not possible to pop - stack.mutex.Lock() + stack.Lock() for i := 0; i < len(stack.workers); i++ { // set state for the stack in the stack (unused at the moment) stack.workers[i].State().Set(worker.StateDestroyed) // kill the worker _ = stack.workers[i].Kill() } - stack.mutex.Unlock() + stack.Unlock() // clear stack.Reset() return diff --git a/pkg/worker_watcher/stack_test.go b/pkg/worker_watcher/stack_test.go index 5287a6dc..769419e4 100644 --- a/pkg/worker_watcher/stack_test.go +++ b/pkg/worker_watcher/stack_test.go @@ -12,7 +12,7 @@ import ( func TestNewWorkersStack(t *testing.T) { stack := NewWorkersStack(0) assert.Equal(t, uint64(0), stack.actualNumOfWorkers) - assert.Equal(t, []*worker.SyncWorkerImpl{}, stack.workers) + assert.Equal(t, []worker.BaseProcess{}, stack.workers) } func TestStack_Push(t *testing.T) { diff --git a/pkg/worker_watcher/worker_watcher.go b/pkg/worker_watcher/worker_watcher.go index d065bae5..2380c190 100755 --- a/pkg/worker_watcher/worker_watcher.go +++ b/pkg/worker_watcher/worker_watcher.go @@ -27,11 +27,11 @@ type workerWatcher struct { events events.Handler } -func (ww *workerWatcher) Watch(workers []worker.SyncWorker) error { +func (ww *workerWatcher) Watch(workers []worker.BaseProcess) error { for i := 0; i < len(workers); i++ { ww.stack.Push(workers[i]) - go func(swc worker.SyncWorker) { + go func(swc worker.BaseProcess) { ww.wait(swc) }(workers[i]) } @@ -39,7 +39,7 @@ func (ww *workerWatcher) Watch(workers []worker.SyncWorker) error { } // Get is not a thread safe operation -func (ww *workerWatcher) Get(ctx context.Context) (worker.SyncWorker, error) { +func (ww *workerWatcher) Get(ctx context.Context) (worker.BaseProcess, error) { const op = errors.Op("worker_watcher_get_free_worker") // FAST PATH // thread safe operation @@ -72,6 +72,10 @@ func (ww *workerWatcher) Get(ctx context.Context) (worker.SyncWorker, error) { } switch w.State().Value() { + // return only workers in the Ready state + // check first + case worker.StateReady: + return w, nil case worker.StateRemove: err := ww.Remove(w) if err != nil { @@ -94,9 +98,6 @@ func (ww *workerWatcher) Get(ctx context.Context) (worker.SyncWorker, error) { _ = w.Kill() // try to get new worker continue - // return only workers in the Ready state - case worker.StateReady: - return w, nil } case <-ctx.Done(): return nil, errors.E(op, errors.NoFreeWorkers, errors.Str("no free workers in the stack, timeout exceed")) @@ -105,7 +106,7 @@ func (ww *workerWatcher) Get(ctx context.Context) (worker.SyncWorker, error) { } func (ww *workerWatcher) Allocate() error { - ww.stack.mutex.Lock() + ww.mutex.Lock() const op = errors.Op("worker_watcher_allocate_new") sw, err := ww.allocator() if err != nil { @@ -113,14 +114,14 @@ func (ww *workerWatcher) Allocate() error { } ww.addToWatch(sw) - ww.stack.mutex.Unlock() + ww.mutex.Unlock() ww.Push(sw) return nil } // Remove -func (ww *workerWatcher) Remove(wb worker.SyncWorker) error { +func (ww *workerWatcher) Remove(wb worker.BaseProcess) error { ww.mutex.Lock() defer ww.mutex.Unlock() @@ -139,7 +140,7 @@ func (ww *workerWatcher) Remove(wb worker.SyncWorker) error { } // O(1) operation -func (ww *workerWatcher) Push(w worker.SyncWorker) { +func (ww *workerWatcher) Push(w worker.BaseProcess) { ww.mutex.Lock() defer ww.mutex.Unlock() ww.stack.Push(w) @@ -152,7 +153,7 @@ func (ww *workerWatcher) Destroy(ctx context.Context) { } // Warning, this is O(n) operation, and it will return copy of the actual workers -func (ww *workerWatcher) List() []worker.SyncWorker { +func (ww *workerWatcher) List() []worker.BaseProcess { return ww.stack.Workers() } @@ -183,7 +184,7 @@ func (ww *workerWatcher) wait(w worker.BaseProcess) { } } -func (ww *workerWatcher) addToWatch(wb worker.SyncWorker) { +func (ww *workerWatcher) addToWatch(wb worker.BaseProcess) { go func() { ww.wait(wb) }() |