2019-11-14 15:57:27 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-18 17:13:20 +00:00
|
|
|
"encoding/csv"
|
|
|
|
"os"
|
2019-12-09 11:30:52 +00:00
|
|
|
"time"
|
2019-11-18 17:13:20 +00:00
|
|
|
|
2019-11-14 15:57:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/cfgstruct"
|
|
|
|
"storj.io/private/process"
|
2019-11-14 15:57:27 +00:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
detectCmd = &cobra.Command{
|
|
|
|
Use: "detect",
|
|
|
|
Short: "Detects zombie segments in DB",
|
|
|
|
Args: cobra.OnlyValidArgs,
|
|
|
|
RunE: cmdDetect,
|
|
|
|
}
|
|
|
|
|
|
|
|
detectCfg struct {
|
|
|
|
DatabaseURL string `help:"the database connection string to use" default:"postgres://"`
|
2019-12-09 11:30:52 +00:00
|
|
|
From string `help:"begin of date range for detecting zombie segments (RFC3339)" default:""`
|
|
|
|
To string `help:"end of date range for detecting zombie segments (RFC3339)" default:""`
|
2019-11-26 13:01:31 +00:00
|
|
|
File string `help:"location of file with report" default:"zombie-segments.csv"`
|
2019-11-14 15:57:27 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(detectCmd)
|
|
|
|
|
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
|
|
|
process.Bind(detectCmd, &detectCfg, defaults)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdDetect(cmd *cobra.Command, args []string) (err error) {
|
2019-11-18 17:13:20 +00:00
|
|
|
ctx, _ := process.Ctx(cmd)
|
|
|
|
|
2019-11-14 15:57:27 +00:00
|
|
|
log := zap.L()
|
2020-08-19 15:59:20 +01:00
|
|
|
|
|
|
|
if err := process.InitMetricsWithHostname(ctx, log, nil); err != nil {
|
|
|
|
log.Warn("Failed to initialize telemetry batcher on segment reaper", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-12-04 10:24:39 +00:00
|
|
|
db, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), detectCfg.DatabaseURL, "satellite-reaper")
|
2019-11-14 15:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("error connecting database: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
2019-11-18 17:13:20 +00:00
|
|
|
|
|
|
|
file, err := os.Create(detectCfg.File)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("error creating result file: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, file.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
writer := csv.NewWriter(file)
|
|
|
|
defer func() {
|
|
|
|
writer.Flush()
|
|
|
|
err = errs.Combine(err, writer.Error())
|
|
|
|
}()
|
|
|
|
|
2019-12-09 11:30:52 +00:00
|
|
|
var from, to *time.Time
|
|
|
|
if detectCfg.From != "" {
|
|
|
|
fromTime, err := time.Parse(time.RFC3339, detectCfg.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
from = &fromTime
|
2019-11-18 17:13:20 +00:00
|
|
|
}
|
2019-12-09 11:30:52 +00:00
|
|
|
|
|
|
|
if detectCfg.To != "" {
|
|
|
|
toTime, err := time.Parse(time.RFC3339, detectCfg.To)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
to = &toTime
|
|
|
|
}
|
|
|
|
|
|
|
|
observer, err := newObserver(db, writer, from, to)
|
2019-11-18 17:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 14:28:46 +00:00
|
|
|
err = observer.detectZombieSegments(ctx)
|
2019-11-18 17:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("number of inline segments", zap.Int("segments", observer.inlineSegments))
|
|
|
|
log.Info("number of last inline segments", zap.Int("segments", observer.lastInlineSegments))
|
|
|
|
log.Info("number of remote segments", zap.Int("segments", observer.remoteSegments))
|
2019-12-09 11:30:52 +00:00
|
|
|
log.Info("number of zombie segments", zap.Int("segments", observer.zombieSegments))
|
2020-08-19 15:59:20 +01:00
|
|
|
|
2020-10-13 13:13:41 +01:00
|
|
|
mon.IntVal("zombie_segments").Observe(int64(observer.zombieSegments)) //mon:locked
|
2020-08-19 15:59:20 +01:00
|
|
|
|
|
|
|
return process.Report(ctx)
|
2019-11-14 15:57:27 +00:00
|
|
|
}
|