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-30 19:16:40 +00:00
|
|
|
"storj.io/storj/storage/redis"
|
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-14 15:55:45 +00:00
|
|
|
QueueAddress string `help:"data checker queue address" default:"redis://127.0.0.1:6378?db=1&password=abc123"`
|
|
|
|
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-14 20:17:30 +00:00
|
|
|
sdb, ok := ctx.Value("masterdb").(interface {
|
|
|
|
StatDB() statdb.DB
|
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
return nil, Error.New("unable to get master db instance")
|
2018-12-06 18:51:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
db, ok := ctx.Value("masterdb").(interface {
|
|
|
|
Irreparable() irreparable.DB
|
|
|
|
})
|
|
|
|
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-07 05:42:16 +00:00
|
|
|
o := overlay.LoadServerFromContext(ctx)
|
2018-11-08 13:53:27 +00:00
|
|
|
redisQ, err := redis.NewQueueFrom(c.QueueAddress)
|
2018-10-30 19:16:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2018-11-08 13:53:27 +00:00
|
|
|
repairQueue := queue.NewQueue(redisQ)
|
2018-12-14 20:17:30 +00:00
|
|
|
return newChecker(pdb, sdb.StatDB(), 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()
|
2018-10-31 16:22:35 +00:00
|
|
|
zap.L().Error("Error running checker", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return server.Run(ctx)
|
2018-10-12 18:49:49 +01:00
|
|
|
}
|