2020-03-12 15:40:22 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-05-06 22:04:07 +01:00
|
|
|
"runtime/pprof"
|
2020-03-12 15:40:22 +00:00
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/peertls/extensions"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/debug"
|
2020-03-23 19:30:31 +00:00
|
|
|
"storj.io/private/version"
|
2020-03-12 15:40:22 +00:00
|
|
|
"storj.io/storj/private/lifecycle"
|
|
|
|
version_checker "storj.io/storj/private/version/checker"
|
2022-08-29 10:44:54 +01:00
|
|
|
"storj.io/storj/satellite/gc/sender"
|
2021-05-13 09:14:18 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2020-03-12 15:40:22 +00:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// GarbageCollection is the satellite garbage collection process.
|
2020-03-12 15:40:22 +00:00
|
|
|
//
|
|
|
|
// architecture: Peer
|
|
|
|
type GarbageCollection struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
DB DB
|
|
|
|
|
|
|
|
Servers *lifecycle.Group
|
|
|
|
Services *lifecycle.Group
|
|
|
|
|
|
|
|
Dialer rpc.Dialer
|
|
|
|
|
|
|
|
Version struct {
|
|
|
|
Chore *version_checker.Chore
|
|
|
|
Service *version_checker.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Server *debug.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
Overlay struct {
|
|
|
|
DB overlay.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
GarbageCollection struct {
|
2022-08-29 10:44:54 +01:00
|
|
|
Sender *sender.Service
|
2020-03-12 15:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewGarbageCollection creates a new satellite garbage collection process.
|
2020-03-12 15:40:22 +00:00
|
|
|
func NewGarbageCollection(log *zap.Logger, full *identity.FullIdentity, db DB,
|
2021-05-13 09:14:18 +01:00
|
|
|
metabaseDB *metabase.DB, revocationDB extensions.RevocationDB,
|
2020-05-12 20:10:32 +01:00
|
|
|
versionInfo version.Info, config *Config, atomicLogLevel *zap.AtomicLevel) (*GarbageCollection, error) {
|
2020-03-12 15:40:22 +00:00
|
|
|
peer := &GarbageCollection{
|
|
|
|
Log: log,
|
|
|
|
Identity: full,
|
|
|
|
DB: db,
|
|
|
|
|
|
|
|
Servers: lifecycle.NewGroup(log.Named("servers")),
|
|
|
|
Services: lifecycle.NewGroup(log.Named("services")),
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup debug
|
|
|
|
var err error
|
2023-11-27 15:13:05 +00:00
|
|
|
if config.Debug.Addr != "" {
|
|
|
|
peer.Debug.Listener, err = net.Listen("tcp", config.Debug.Addr)
|
2020-03-12 15:40:22 +00:00
|
|
|
if err != nil {
|
|
|
|
withoutStack := errors.New(err.Error())
|
|
|
|
peer.Log.Debug("failed to start debug endpoints", zap.Error(withoutStack))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debugConfig := config.Debug
|
|
|
|
debugConfig.ControlTitle = "GC"
|
2020-05-12 20:10:32 +01:00
|
|
|
peer.Debug.Server = debug.NewServerWithAtomicLevel(log.Named("debug"), peer.Debug.Listener, monkit.Default, debugConfig, atomicLogLevel)
|
2020-03-12 15:40:22 +00:00
|
|
|
peer.Servers.Add(lifecycle.Item{
|
|
|
|
Name: "debug",
|
|
|
|
Run: peer.Debug.Server.Run,
|
|
|
|
Close: peer.Debug.Server.Close,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup version control
|
2020-04-15 20:32:22 +01:00
|
|
|
peer.Log.Info("Version info",
|
|
|
|
zap.Stringer("Version", versionInfo.Version.Version),
|
|
|
|
zap.String("Commit Hash", versionInfo.CommitHash),
|
|
|
|
zap.Stringer("Build Timestamp", versionInfo.Timestamp),
|
|
|
|
zap.Bool("Release Build", versionInfo.Release),
|
|
|
|
)
|
2020-03-12 15:40:22 +00:00
|
|
|
peer.Version.Service = version_checker.NewService(log.Named("version"), config.Version, versionInfo, "Satellite")
|
|
|
|
peer.Version.Chore = version_checker.NewChore(peer.Version.Service, config.Version.CheckInterval)
|
|
|
|
|
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "version",
|
|
|
|
Run: peer.Version.Chore.Run,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup listener and server
|
|
|
|
sc := config.Server
|
|
|
|
|
|
|
|
tlsOptions, err := tlsopts.NewOptions(peer.Identity, sc.Config, revocationDB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Dialer = rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup overlay
|
|
|
|
peer.Overlay.DB = peer.DB.OverlayCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup garbage collection
|
2022-08-29 10:44:54 +01:00
|
|
|
peer.GarbageCollection.Sender = sender.NewService(
|
|
|
|
peer.Log.Named("gc-sender"),
|
2020-03-12 15:40:22 +00:00
|
|
|
config.GarbageCollection,
|
|
|
|
peer.Dialer,
|
|
|
|
peer.Overlay.DB,
|
|
|
|
)
|
2022-08-29 10:44:54 +01:00
|
|
|
|
2020-03-12 15:40:22 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
2022-08-29 10:44:54 +01:00
|
|
|
Name: "gc-sender",
|
|
|
|
Run: peer.GarbageCollection.Sender.Run,
|
2020-03-12 15:40:22 +00:00
|
|
|
})
|
|
|
|
peer.Debug.Server.Panel.Add(
|
2022-08-29 10:44:54 +01:00
|
|
|
debug.Cycle("Garbage Collection", peer.GarbageCollection.Sender.Loop))
|
2020-03-12 15:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs satellite garbage collection until it's either closed or it errors.
|
|
|
|
func (peer *GarbageCollection) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
group, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
2022-05-06 22:04:07 +01:00
|
|
|
pprof.Do(ctx, pprof.Labels("subsystem", "gc"), func(ctx context.Context) {
|
|
|
|
peer.Servers.Run(ctx, group)
|
|
|
|
peer.Services.Run(ctx, group)
|
2020-03-12 15:40:22 +00:00
|
|
|
|
2022-05-06 22:04:07 +01:00
|
|
|
pprof.Do(ctx, pprof.Labels("name", "subsystem-wait"), func(ctx context.Context) {
|
|
|
|
err = group.Wait()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return err
|
2020-03-12 15:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the resources.
|
|
|
|
func (peer *GarbageCollection) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
peer.Servers.Close(),
|
|
|
|
peer.Services.Close(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the peer ID.
|
|
|
|
func (peer *GarbageCollection) ID() storj.NodeID { return peer.Identity.ID }
|