2019-10-15 16:29:47 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gracefulexit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/sync2"
|
2019-10-22 21:42:21 +01:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
2019-10-15 16:29:47 +01:00
|
|
|
"storj.io/storj/storagenode/satellites"
|
2019-10-22 21:42:21 +01:00
|
|
|
"storj.io/storj/storagenode/trust"
|
2019-10-15 16:29:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Chore checks for satellites that the node is exiting and creates a worker per satellite to complete the process.
|
|
|
|
//
|
|
|
|
// architecture: Chore
|
|
|
|
type Chore struct {
|
|
|
|
log *zap.Logger
|
2019-10-22 21:42:21 +01:00
|
|
|
store *pieces.Store
|
2019-10-15 16:29:47 +01:00
|
|
|
satelliteDB satellites.DB
|
2019-10-22 21:42:21 +01:00
|
|
|
trust *trust.Pool
|
|
|
|
dialer rpc.Dialer
|
2019-10-15 16:29:47 +01:00
|
|
|
|
|
|
|
config Config
|
|
|
|
|
|
|
|
exitingMap sync.Map
|
2020-01-29 15:37:50 +00:00
|
|
|
Loop *sync2.Cycle
|
|
|
|
limiter *sync2.Limiter
|
2019-10-15 16:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChore instantiates Chore.
|
2019-10-22 21:42:21 +01:00
|
|
|
func NewChore(log *zap.Logger, config Config, store *pieces.Store, trust *trust.Pool, dialer rpc.Dialer, satelliteDB satellites.DB) *Chore {
|
2019-10-15 16:29:47 +01:00
|
|
|
return &Chore{
|
|
|
|
log: log,
|
2019-10-22 21:42:21 +01:00
|
|
|
store: store,
|
2019-10-15 16:29:47 +01:00
|
|
|
satelliteDB: satelliteDB,
|
2019-10-22 21:42:21 +01:00
|
|
|
trust: trust,
|
|
|
|
dialer: dialer,
|
2019-10-15 16:29:47 +01:00
|
|
|
config: config,
|
2020-01-29 15:37:50 +00:00
|
|
|
Loop: sync2.NewCycle(config.ChoreInterval),
|
|
|
|
limiter: sync2.NewLimiter(config.NumWorkers),
|
2019-10-15 16:29:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the chore.
|
|
|
|
func (chore *Chore) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
err = chore.Loop.Run(ctx, func(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-11-19 21:11:31 +00:00
|
|
|
|
2019-10-15 16:29:47 +01:00
|
|
|
satellites, err := chore.satelliteDB.ListGracefulExits(ctx)
|
|
|
|
if err != nil {
|
|
|
|
chore.log.Error("error retrieving satellites.", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(satellites) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-19 20:36:13 +00:00
|
|
|
chore.log.Debug("exiting", zap.Int("satellites", len(satellites)))
|
2019-10-15 16:29:47 +01:00
|
|
|
|
|
|
|
for _, satellite := range satellites {
|
2019-11-19 21:11:31 +00:00
|
|
|
mon.Meter("satellite_gracefulexit_request").Mark(1) //locked
|
2019-10-28 17:59:45 +00:00
|
|
|
if satellite.FinishedAt != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-15 16:29:47 +01:00
|
|
|
satelliteID := satellite.SatelliteID
|
2019-10-22 21:42:21 +01:00
|
|
|
addr, err := chore.trust.GetAddress(ctx, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
chore.log.Error("failed to get satellite address.", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:40:57 +00:00
|
|
|
worker := NewWorker(chore.log, chore.store, chore.satelliteDB, chore.dialer, satelliteID, addr, chore.config)
|
2019-10-15 16:29:47 +01:00
|
|
|
if _, ok := chore.exitingMap.LoadOrStore(satelliteID, worker); ok {
|
|
|
|
// already running a worker for this satellite
|
2019-11-05 21:04:07 +00:00
|
|
|
chore.log.Debug("skipping for satellite, worker already exists.", zap.Stringer("Satellite ID", satelliteID))
|
2019-10-15 16:29:47 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
chore.limiter.Go(ctx, func() {
|
2019-10-22 21:42:21 +01:00
|
|
|
err := worker.Run(ctx, func() {
|
2019-11-05 21:04:07 +00:00
|
|
|
chore.log.Debug("finished for satellite.", zap.Stringer("Satellite ID", satelliteID))
|
2019-10-15 16:29:47 +01:00
|
|
|
chore.exitingMap.Delete(satelliteID)
|
|
|
|
})
|
2020-01-08 02:33:41 +00:00
|
|
|
|
2019-10-15 16:29:47 +01:00
|
|
|
if err != nil {
|
2019-10-30 08:21:27 +00:00
|
|
|
chore.log.Error("worker failed", zap.Error(err))
|
2019-10-15 16:29:47 +01:00
|
|
|
}
|
2019-12-17 15:06:47 +00:00
|
|
|
|
|
|
|
if err := worker.Close(); err != nil {
|
|
|
|
chore.log.Error("closing worker failed", zap.Error(err))
|
|
|
|
}
|
2019-10-15 16:29:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
chore.limiter.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes chore.
|
|
|
|
func (chore *Chore) Close() error {
|
|
|
|
chore.Loop.Close()
|
|
|
|
chore.exitingMap.Range(func(key interface{}, value interface{}) bool {
|
|
|
|
worker := value.(*Worker)
|
|
|
|
err := worker.Close()
|
|
|
|
if err != nil {
|
|
|
|
worker.log.Error("worker failed on close.", zap.Error(err))
|
|
|
|
}
|
|
|
|
chore.exitingMap.Delete(key)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|