-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jakob Möller <[email protected]>
- Loading branch information
1 parent
0729895
commit cbf1fac
Showing
6 changed files
with
197 additions
and
27 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,131 @@ | ||
package pvworker | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
var ErrWorkerAlreadySyncingPVs = errors.New("worker is already syncing PVs") | ||
|
||
type MoveExtentFn = func(ctx context.Context, from []string, to []string) error | ||
|
||
type AsyncExtentMover interface { | ||
Sync(from, to []string) error | ||
IsSyncing() bool | ||
Status() *SyncStatus | ||
Reset() error | ||
} | ||
|
||
type SyncStatus struct { | ||
state SyncState | ||
err error | ||
start time.Time | ||
end time.Time | ||
from, to []string | ||
} | ||
|
||
func (s *SyncStatus) State() SyncState { | ||
return s.state | ||
} | ||
|
||
func (s *SyncStatus) Error() error { | ||
return s.err | ||
} | ||
|
||
func (s *SyncStatus) Operation() (from []string, to []string) { | ||
return s.from, s.to | ||
} | ||
|
||
// SyncDuration returns the duration of the sync operation since it started and until it finished. | ||
func (s *SyncStatus) SyncDuration() time.Duration { | ||
if s.end.IsZero() { | ||
return time.Since(s.start) | ||
} | ||
return s.end.Sub(s.start) | ||
} | ||
|
||
type SyncState string | ||
|
||
const ( | ||
SyncStateSyncing SyncState = "syncing" | ||
SyncStateError SyncState = "error" | ||
SyncStateIdle SyncState = "idle" | ||
SyncStateDone SyncState = "done" | ||
) | ||
|
||
func NewAsyncExtentMover(move MoveExtentFn) AsyncExtentMover { | ||
worker := &asyncExtentMover{ | ||
syncStatus: atomic.Pointer[SyncStatus]{}, | ||
pvMove: move, | ||
} | ||
worker.syncStatus.Store(&SyncStatus{state: SyncStateIdle}) | ||
return worker | ||
} | ||
|
||
type asyncExtentMover struct { | ||
syncStatus atomic.Pointer[SyncStatus] | ||
pvMove MoveExtentFn | ||
} | ||
|
||
func (w *asyncExtentMover) Sync(from, to []string) error { | ||
if w.IsSyncing() { | ||
return ErrWorkerAlreadySyncingPVs | ||
} | ||
|
||
if w.syncStatus.Load().state == SyncStateError { | ||
return fmt.Errorf("cannot start new sync operation when previous one failed, "+ | ||
"explicit reset is necessary: %w", w.syncStatus.Load().err) | ||
} | ||
|
||
w.startSync(from, to) | ||
go func(ctx context.Context, from, to []string) { | ||
err := w.pvMove(ctx, from, to) | ||
w.finishSync(err) | ||
}(context.Background(), from, to) | ||
|
||
return nil | ||
} | ||
|
||
func (w *asyncExtentMover) Status() *SyncStatus { | ||
return w.syncStatus.Load() | ||
} | ||
|
||
func (w *asyncExtentMover) IsSyncing() bool { | ||
return w.syncStatus.Load().state == SyncStateSyncing | ||
} | ||
|
||
func (w *asyncExtentMover) startSync(from, to []string) { | ||
w.syncStatus.Store(&SyncStatus{ | ||
state: SyncStateSyncing, | ||
start: time.Now(), | ||
from: from, | ||
to: to, | ||
}) | ||
} | ||
|
||
func (w *asyncExtentMover) finishSync(err error) { | ||
var state SyncState | ||
if err != nil { | ||
state = SyncStateError | ||
} else { | ||
state = SyncStateDone | ||
} | ||
status := w.syncStatus.Load() | ||
status.err = err | ||
status.state = state | ||
status.end = time.Now() | ||
w.syncStatus.Store(status) | ||
} | ||
|
||
func (w *asyncExtentMover) Reset() error { | ||
if w.IsSyncing() { | ||
return fmt.Errorf("cannot reset running worker: %w", ErrWorkerAlreadySyncingPVs) | ||
} | ||
|
||
w.syncStatus.Store(&SyncStatus{state: SyncStateIdle}) | ||
|
||
return nil | ||
} |