2023-05-05 22:26:09 +01:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package internalcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"runtime"
|
|
|
|
|
2023-05-24 22:26:33 +01:00
|
|
|
"github.com/spf13/cobra"
|
2023-05-05 22:26:09 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-05-24 22:26:33 +01:00
|
|
|
"storj.io/private/process"
|
2023-05-05 22:26:09 +01:00
|
|
|
"storj.io/storj/storagenode/iopriority"
|
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
"storj.io/storj/storagenode/pieces/lazyfilewalker"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb"
|
|
|
|
)
|
|
|
|
|
2023-05-24 22:26:33 +01:00
|
|
|
// NewUsedSpaceFilewalkerCmd creates a new cobra command for running used-space calculation filewalker.
|
|
|
|
func NewUsedSpaceFilewalkerCmd() *LazyFilewalkerCmd {
|
|
|
|
var cfg FilewalkerCfg
|
|
|
|
var runOpts RunOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: lazyfilewalker.UsedSpaceFilewalkerCmdName,
|
|
|
|
Short: "An internal subcommand used to run used-space calculation filewalker as a separate subprocess with lower IO priority",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
runOpts.normalize(cmd)
|
|
|
|
runOpts.config = &cfg
|
|
|
|
|
|
|
|
return usedSpaceCmdRun(&runOpts)
|
|
|
|
},
|
|
|
|
FParseErrWhitelist: cobra.FParseErrWhitelist{
|
|
|
|
UnknownFlags: true,
|
|
|
|
},
|
|
|
|
Hidden: true,
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
}
|
2023-05-05 22:26:09 +01:00
|
|
|
|
2023-05-24 22:26:33 +01:00
|
|
|
process.Bind(cmd, &cfg)
|
2023-05-05 22:26:09 +01:00
|
|
|
|
2023-05-24 22:26:33 +01:00
|
|
|
return &LazyFilewalkerCmd{
|
|
|
|
Command: cmd,
|
|
|
|
RunOptions: &runOpts,
|
2023-05-05 22:26:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the UsedSpaceLazyFileWalker.
|
2023-05-24 22:26:33 +01:00
|
|
|
func usedSpaceCmdRun(opts *RunOptions) (err error) {
|
|
|
|
if opts.config.LowerIOPriority {
|
2023-05-05 22:26:09 +01:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
// Pin the current goroutine to the current OS thread, so we can set the IO priority
|
|
|
|
// for the current thread.
|
|
|
|
// This is necessary because Go does use CLONE_IO when creating new threads,
|
|
|
|
// so they do not share a single IO context.
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = iopriority.SetLowIOPriority()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 22:26:33 +01:00
|
|
|
log := opts.Logger
|
2023-05-05 22:26:09 +01:00
|
|
|
|
|
|
|
// Decode the data struct received from the main process
|
|
|
|
var req lazyfilewalker.UsedSpaceRequest
|
2023-05-24 22:26:33 +01:00
|
|
|
if err = json.NewDecoder(opts.stdin).Decode(&req); err != nil {
|
2023-05-05 22:26:09 +01:00
|
|
|
return errs.New("Error decoding data from stdin: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.SatelliteID.IsZero() {
|
|
|
|
return errs.New("SatelliteID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We still need the DB in this case because we still have to deal with v0 pieces.
|
|
|
|
// Once we drop support for v0 pieces, we can remove this.
|
2023-05-24 22:26:33 +01:00
|
|
|
db, err := storagenodedb.OpenExisting(opts.Ctx, log.Named("db"), opts.config.DatabaseConfig())
|
2023-05-05 22:26:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error starting master database on storage node: %v", err)
|
|
|
|
}
|
|
|
|
log.Info("Database started")
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Info("used-space-filewalker started")
|
|
|
|
|
|
|
|
filewalker := pieces.NewFileWalker(log, db.Pieces(), db.V0PieceInfo())
|
2023-05-24 22:26:33 +01:00
|
|
|
total, contentSize, err := filewalker.WalkAndComputeSpaceUsedBySatellite(opts.Ctx, req.SatelliteID)
|
2023-05-05 22:26:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp := lazyfilewalker.UsedSpaceResponse{PiecesTotal: total, PiecesContentSize: contentSize}
|
|
|
|
|
2023-05-05 22:26:53 +01:00
|
|
|
log.Info("used-space-filewalker completed", zap.Int64("piecesTotal", total), zap.Int64("piecesContentSize", contentSize))
|
2023-05-05 22:26:09 +01:00
|
|
|
|
|
|
|
// encode the response struct and write it to stdout
|
2023-05-24 22:26:33 +01:00
|
|
|
return json.NewEncoder(opts.stdout).Encode(resp)
|
2023-05-05 22:26:09 +01:00
|
|
|
}
|