storj/cmd/segment-reaper/detect.go
Michal Niewrzal f0aeda3091 storj: remove from storj/pkg packages moved to storj/private repo
* debug
* traces
* cfgstruct
* process

Package `storj/private/version` will be removed as a separate change.

Change-Id: Iadc40faa782e6225513b28218952f02d9c240a9f
2020-03-24 09:56:29 +01:00

102 lines
2.4 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/csv"
"os"
"time"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/private/cfgstruct"
"storj.io/private/process"
"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://"`
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:""`
File string `help:"location of file with report" default:"zombie-segments.csv"`
}
)
func init() {
rootCmd.AddCommand(detectCmd)
defaults := cfgstruct.DefaultsFlag(rootCmd)
process.Bind(detectCmd, &detectCfg, defaults)
}
func cmdDetect(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
log := zap.L()
db, err := metainfo.NewStore(log.Named("pointerdb"), detectCfg.DatabaseURL)
if err != nil {
return errs.New("error connecting database: %+v", err)
}
defer func() {
err = errs.Combine(err, db.Close())
}()
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())
}()
var from, to *time.Time
if detectCfg.From != "" {
fromTime, err := time.Parse(time.RFC3339, detectCfg.From)
if err != nil {
return err
}
from = &fromTime
}
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)
if err != nil {
return err
}
err = observer.detectZombieSegments(ctx)
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))
log.Info("number of zombie segments", zap.Int("segments", observer.zombieSegments))
return nil
}