2018-10-12 18:49:49 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2018-10-30 19:16:40 +00:00
|
|
|
"go.uber.org/zap"
|
2018-11-20 15:54:22 +00:00
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/irreparable"
|
2018-10-30 19:16:40 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-10-12 18:49:49 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-12-06 18:51:23 +00:00
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-10-12 18:49:49 +01:00
|
|
|
)
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
// Config contains configurable values for checker
|
2018-10-12 18:49:49 +01:00
|
|
|
type Config struct {
|
2018-12-21 15:11:19 +00:00
|
|
|
Interval time.Duration `help:"how frequently checker should audit segments" default:"30s"`
|
2018-10-12 18:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a Checker struct
|
|
|
|
func (c Config) initialize(ctx context.Context) (Checker, error) {
|
2018-11-20 15:54:22 +00:00
|
|
|
pdb := pointerdb.LoadFromContext(ctx)
|
2018-12-06 18:51:23 +00:00
|
|
|
if pdb == nil {
|
|
|
|
return nil, Error.New("failed to load pointerdb from context")
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
db, ok := ctx.Value("masterdb").(interface {
|
2018-12-21 15:11:19 +00:00
|
|
|
StatDB() statdb.DB
|
2018-12-10 19:08:45 +00:00
|
|
|
Irreparable() irreparable.DB
|
2018-12-21 15:11:19 +00:00
|
|
|
RepairQueue() queue.RepairQueue
|
2018-12-10 19:08:45 +00:00
|
|
|
})
|
|
|
|
if !ok {
|
2018-12-14 20:17:30 +00:00
|
|
|
return nil, Error.New("unable to get master db instance")
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2018-12-07 05:42:16 +00:00
|
|
|
o := overlay.LoadServerFromContext(ctx)
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
return newChecker(pdb, db.StatDB(), db.RepairQueue(), o, db.Irreparable(), 0, zap.L(), c.Interval), nil
|
2018-10-12 18:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the checker with configured values
|
|
|
|
func (c Config) Run(ctx context.Context, server *provider.Provider) (err error) {
|
|
|
|
check, err := c.initialize(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-08 16:18:28 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-10-31 16:22:35 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := check.Run(ctx); err != nil {
|
2018-11-08 16:18:28 +00:00
|
|
|
defer cancel()
|
2019-01-16 19:30:33 +00:00
|
|
|
zap.L().Debug("Checker is shutting down", zap.Error(err))
|
2018-10-31 16:22:35 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return server.Run(ctx)
|
2018-10-12 18:49:49 +01:00
|
|
|
}
|