-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
149 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package lazy | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type Lazy[T any] struct { | ||
Get func() (T, error) | ||
|
||
once sync.Once | ||
|
||
val T | ||
err error | ||
} | ||
|
||
func MakeLazy[T any](get func() (T, error)) *Lazy[T] { | ||
return &Lazy[T]{ | ||
Get: get, | ||
} | ||
} | ||
|
||
func (l *Lazy[T]) Val() (T, error) { | ||
l.once.Do(func() { | ||
l.val, l.err = l.Get() | ||
}) | ||
return l.val, l.err | ||
} | ||
|
||
type LazyCtx[T any] struct { | ||
Get func(context.Context) (T, error) | ||
|
||
once sync.Once | ||
|
||
val T | ||
err error | ||
} | ||
|
||
func MakeLazyCtx[T any](get func(ctx context.Context) (T, error)) *LazyCtx[T] { | ||
return &LazyCtx[T]{ | ||
Get: get, | ||
} | ||
} | ||
|
||
func (l *LazyCtx[T]) Val(ctx context.Context) (T, error) { | ||
l.once.Do(func() { | ||
l.val, l.err = l.Get(ctx) | ||
}) | ||
return l.val, l.err | ||
} |
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,69 @@ | ||
package sealer | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/filecoin-project/lotus/lib/lazy" | ||
"github.com/filecoin-project/lotus/storage/sealer/sealtasks" | ||
"github.com/filecoin-project/lotus/storage/sealer/storiface" | ||
) | ||
|
||
// schedWorkerCache caches scheduling-related calls to workers | ||
type schedWorkerCache struct { | ||
Workers map[storiface.WorkerID]*WorkerHandle | ||
|
||
lk sync.Mutex | ||
cached map[storiface.WorkerID]*cachedSchedWorker | ||
} | ||
|
||
func (s *schedWorkerCache) Get(id storiface.WorkerID) (*cachedSchedWorker, bool) { | ||
s.lk.Lock() | ||
defer s.lk.Unlock() | ||
|
||
if _, found := s.cached[id]; !found { | ||
if _, found := s.Workers[id]; !found { | ||
return nil, false | ||
} | ||
|
||
whnd := s.Workers[id] | ||
|
||
s.cached[id] = &cachedSchedWorker{ | ||
tt: lazy.MakeLazyCtx(whnd.workerRpc.TaskTypes), | ||
paths: lazy.MakeLazyCtx(whnd.workerRpc.Paths), | ||
utilization: lazy.MakeLazy(func() (float64, error) { | ||
return whnd.Utilization(), nil | ||
}), | ||
|
||
Enabled: whnd.Enabled, | ||
Info: whnd.Info, | ||
} | ||
} | ||
|
||
return s.cached[id], true | ||
} | ||
|
||
type cachedSchedWorker struct { | ||
tt *lazy.LazyCtx[map[sealtasks.TaskType]struct{}] | ||
paths *lazy.LazyCtx[[]storiface.StoragePath] | ||
utilization *lazy.Lazy[float64] | ||
|
||
Enabled bool | ||
Info storiface.WorkerInfo | ||
} | ||
|
||
func (c *cachedSchedWorker) TaskTypes(ctx context.Context) (map[sealtasks.TaskType]struct{}, error) { | ||
return c.tt.Val(ctx) | ||
} | ||
|
||
func (c *cachedSchedWorker) Paths(ctx context.Context) ([]storiface.StoragePath, error) { | ||
return c.paths.Get(ctx) | ||
} | ||
|
||
func (c *cachedSchedWorker) Utilization() float64 { | ||
// can't error | ||
v, _ := c.utilization.Val() | ||
return v | ||
} | ||
|
||
var _ SchedWorker = &cachedSchedWorker{} |
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