2019-01-10 13:13:27 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-29 23:04:30 +00:00
|
|
|
"errors"
|
2019-06-20 12:52:32 +01:00
|
|
|
"net"
|
2019-10-08 09:52:19 +01:00
|
|
|
"net/http"
|
2019-11-26 16:25:21 +00:00
|
|
|
"time"
|
2019-01-10 13:13:27 +00:00
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-01-10 13:13:27 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/pb"
|
2020-02-26 13:59:46 +00:00
|
|
|
"storj.io/common/pb/pbgrpc"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/peertls/extensions"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/signing"
|
|
|
|
"storj.io/common/storj"
|
2020-01-28 17:35:45 +00:00
|
|
|
"storj.io/storj/pkg/debug"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/pkg/server"
|
2020-01-28 23:13:59 +00:00
|
|
|
"storj.io/storj/private/lifecycle"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/version"
|
|
|
|
"storj.io/storj/private/version/checker"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/storage"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/bandwidth"
|
2019-05-08 12:11:59 +01:00
|
|
|
"storj.io/storj/storagenode/collector"
|
2019-06-20 12:52:32 +01:00
|
|
|
"storj.io/storj/storagenode/console"
|
2019-10-08 09:52:19 +01:00
|
|
|
"storj.io/storj/storagenode/console/consoleassets"
|
2019-06-20 12:52:32 +01:00
|
|
|
"storj.io/storj/storagenode/console/consoleserver"
|
2019-09-04 20:04:18 +01:00
|
|
|
"storj.io/storj/storagenode/contact"
|
2019-10-11 14:58:12 +01:00
|
|
|
"storj.io/storj/storagenode/gracefulexit"
|
2020-03-12 14:42:01 +00:00
|
|
|
"storj.io/storj/storagenode/heldamount"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/inspector"
|
|
|
|
"storj.io/storj/storagenode/monitor"
|
2019-06-26 19:55:22 +01:00
|
|
|
"storj.io/storj/storagenode/nodestats"
|
2019-12-16 17:59:01 +00:00
|
|
|
"storj.io/storj/storagenode/notifications"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/orders"
|
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
"storj.io/storj/storagenode/piecestore"
|
2020-01-10 01:58:59 +00:00
|
|
|
"storj.io/storj/storagenode/preflight"
|
2019-08-08 14:47:04 +01:00
|
|
|
"storj.io/storj/storagenode/reputation"
|
2019-08-19 19:52:47 +01:00
|
|
|
"storj.io/storj/storagenode/retain"
|
2019-10-01 15:34:03 +01:00
|
|
|
"storj.io/storj/storagenode/satellites"
|
2019-08-08 14:47:04 +01:00
|
|
|
"storj.io/storj/storagenode/storageusage"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/trust"
|
2020-02-21 17:41:54 +00:00
|
|
|
version2 "storj.io/storj/storagenode/version"
|
2019-01-10 13:13:27 +00:00
|
|
|
)
|
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// DB is the master database for Storage Node
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Master Database
|
2019-01-10 13:13:27 +00:00
|
|
|
type DB interface {
|
2019-01-24 20:28:06 +00:00
|
|
|
// CreateTables initializes the database
|
2019-09-23 20:36:46 +01:00
|
|
|
CreateTables(ctx context.Context) error
|
2019-01-24 20:28:06 +00:00
|
|
|
// Close closes the database
|
|
|
|
Close() error
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
Pieces() storage.Blobs
|
|
|
|
|
|
|
|
Orders() orders.DB
|
2019-08-08 02:47:30 +01:00
|
|
|
V0PieceInfo() pieces.V0PieceInfoDB
|
|
|
|
PieceExpirationDB() pieces.PieceExpirationDB
|
2019-08-12 22:43:05 +01:00
|
|
|
PieceSpaceUsedDB() pieces.PieceSpaceUsedDB
|
2019-03-18 10:55:06 +00:00
|
|
|
Bandwidth() bandwidth.DB
|
|
|
|
UsedSerials() piecestore.UsedSerials
|
2019-08-08 14:47:04 +01:00
|
|
|
Reputation() reputation.DB
|
|
|
|
StorageUsage() storageusage.DB
|
2019-10-01 15:34:03 +01:00
|
|
|
Satellites() satellites.DB
|
2019-12-16 17:59:01 +00:00
|
|
|
Notifications() notifications.DB
|
2020-03-12 14:42:01 +00:00
|
|
|
HeldAmount() heldamount.DB
|
2020-03-02 22:33:55 +00:00
|
|
|
|
|
|
|
Preflight(ctx context.Context) error
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 22:16:56 +00:00
|
|
|
// Config is all the configuration parameters for a Storage Node.
|
2019-01-10 13:13:27 +00:00
|
|
|
type Config struct {
|
2019-01-25 14:54:54 +00:00
|
|
|
Identity identity.Config
|
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
Server server.Config
|
2020-01-28 17:35:45 +00:00
|
|
|
Debug debug.Config
|
2019-09-19 20:56:34 +01:00
|
|
|
|
2020-01-10 01:58:59 +00:00
|
|
|
Preflight preflight.Config
|
|
|
|
Contact contact.Config
|
|
|
|
Operator OperatorConfig
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
// TODO: flatten storage config and only keep the new one
|
|
|
|
Storage piecestore.OldConfig
|
|
|
|
Storage2 piecestore.Config
|
|
|
|
Collector collector.Config
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-08-19 19:52:47 +01:00
|
|
|
Retain retain.Config
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
Nodestats nodestats.Config
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
Console consoleserver.Config
|
|
|
|
|
2019-10-20 08:56:23 +01:00
|
|
|
Version checker.Config
|
2019-07-29 15:07:52 +01:00
|
|
|
|
|
|
|
Bandwidth bandwidth.Config
|
2019-10-15 16:29:47 +01:00
|
|
|
|
|
|
|
GracefulExit gracefulexit.Config
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify verifies whether configuration is consistent and acceptable.
|
2019-01-23 10:39:03 +00:00
|
|
|
func (config *Config) Verify(log *zap.Logger) error {
|
2020-01-20 18:58:03 +00:00
|
|
|
err := config.Operator.Verify(log)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Contact.ExternalAddress != "" {
|
|
|
|
err := isAddressValid(config.Contact.ExternalAddress)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("invalid contact.external-address: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Server.Address != "" {
|
|
|
|
err := isAddressValid(config.Server.Address)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("invalid server.address: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAddressValid(addrstring string) error {
|
|
|
|
addr, port, err := net.SplitHostPort(addrstring)
|
|
|
|
if err != nil || port == "" {
|
|
|
|
return errs.New("split host-port %q failed: %+v", addrstring, err)
|
|
|
|
}
|
2020-01-28 14:25:17 +00:00
|
|
|
if addr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-20 18:58:03 +00:00
|
|
|
resolvedhosts, err := net.LookupHost(addr)
|
|
|
|
if err != nil || len(resolvedhosts) == 0 {
|
|
|
|
return errs.New("lookup %q failed: %+v", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peer is the representation of a Storage Node.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Peer
|
2019-01-10 13:13:27 +00:00
|
|
|
type Peer struct {
|
|
|
|
// core dependencies
|
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
DB DB
|
|
|
|
|
2020-01-28 23:13:59 +00:00
|
|
|
Servers *lifecycle.Group
|
|
|
|
Services *lifecycle.Group
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
Dialer rpc.Dialer
|
2019-01-23 10:39:03 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
Server *server.Server
|
2019-01-10 13:13:27 +00:00
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
Version struct {
|
|
|
|
Chore *version2.Chore
|
|
|
|
Service *checker.Service
|
|
|
|
}
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2020-01-28 17:35:45 +00:00
|
|
|
Debug struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Server *debug.Server
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// services and endpoints
|
2019-11-04 19:01:02 +00:00
|
|
|
// TODO: similar grouping to satellite.Core
|
2019-01-10 13:13:27 +00:00
|
|
|
|
2020-01-10 01:58:59 +00:00
|
|
|
Preflight struct {
|
|
|
|
LocalTime *preflight.LocalTime
|
|
|
|
}
|
|
|
|
|
2019-09-04 20:04:18 +01:00
|
|
|
Contact struct {
|
2019-09-19 20:56:34 +01:00
|
|
|
Service *contact.Service
|
2019-09-06 17:14:03 +01:00
|
|
|
Chore *contact.Chore
|
2019-09-19 20:56:34 +01:00
|
|
|
Endpoint *contact.Endpoint
|
2019-09-06 17:14:03 +01:00
|
|
|
PingStats *contact.PingStats
|
2019-09-04 20:04:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
Storage2 struct {
|
2019-05-08 12:11:59 +01:00
|
|
|
// TODO: lift things outside of it to organize better
|
2019-08-19 19:52:47 +01:00
|
|
|
Trust *trust.Pool
|
|
|
|
Store *pieces.Store
|
2019-11-26 16:25:21 +00:00
|
|
|
TrashChore *pieces.TrashChore
|
2019-08-19 19:52:47 +01:00
|
|
|
BlobsCache *pieces.BlobsUsageCache
|
|
|
|
CacheService *pieces.CacheService
|
|
|
|
RetainService *retain.Service
|
|
|
|
Endpoint *piecestore.Endpoint
|
|
|
|
Inspector *inspector.Endpoint
|
|
|
|
Monitor *monitor.Service
|
2019-08-22 15:33:14 +01:00
|
|
|
Orders *orders.Service
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-05-08 12:11:59 +01:00
|
|
|
|
|
|
|
Collector *collector.Service
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
NodeStats struct {
|
|
|
|
Service *nodestats.Service
|
|
|
|
Cache *nodestats.Cache
|
|
|
|
}
|
2019-06-26 19:55:22 +01:00
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
// Web server with web UI
|
|
|
|
Console struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Service *console.Service
|
|
|
|
Endpoint *consoleserver.Server
|
|
|
|
}
|
2019-07-29 15:07:52 +01:00
|
|
|
|
2019-10-11 14:58:12 +01:00
|
|
|
GracefulExit struct {
|
|
|
|
Endpoint *gracefulexit.Endpoint
|
2019-10-15 16:29:47 +01:00
|
|
|
Chore *gracefulexit.Chore
|
2019-10-11 14:58:12 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 15:38:55 +00:00
|
|
|
Notifications struct {
|
|
|
|
Service *notifications.Service
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:01:12 +00:00
|
|
|
Heldamount struct {
|
|
|
|
Service *heldamount.Service
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:07:52 +01:00
|
|
|
Bandwidth *bandwidth.Service
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Storage Node.
|
2019-08-20 16:04:17 +01:00
|
|
|
func New(log *zap.Logger, full *identity.FullIdentity, db DB, revocationDB extensions.RevocationDB, config Config, versionInfo version.Info) (*Peer, error) {
|
2019-01-10 13:13:27 +00:00
|
|
|
peer := &Peer{
|
2019-02-11 11:17:32 +00:00
|
|
|
Log: log,
|
|
|
|
Identity: full,
|
|
|
|
DB: db,
|
2020-01-28 23:13:59 +00:00
|
|
|
|
|
|
|
Servers: lifecycle.NewGroup(log.Named("servers")),
|
|
|
|
Services: lifecycle.NewGroup(log.Named("services")),
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
{ // setup notification service.
|
|
|
|
peer.Notifications.Service = notifications.NewService(peer.Log, peer.DB.Notifications())
|
|
|
|
}
|
|
|
|
|
2020-01-28 17:35:45 +00:00
|
|
|
{ // setup debug
|
|
|
|
var err error
|
|
|
|
if config.Debug.Address != "" {
|
|
|
|
peer.Debug.Listener, err = net.Listen("tcp", config.Debug.Address)
|
|
|
|
if err != nil {
|
2020-01-29 23:04:30 +00:00
|
|
|
withoutStack := errors.New(err.Error())
|
|
|
|
peer.Log.Debug("failed to start debug endpoints", zap.Error(withoutStack))
|
|
|
|
err = nil
|
2020-01-28 17:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 12:49:40 +00:00
|
|
|
debugConfig := config.Debug
|
|
|
|
debugConfig.ControlTitle = "Storage Node"
|
|
|
|
peer.Debug.Server = debug.NewServer(log.Named("debug"), peer.Debug.Listener, monkit.Default, debugConfig)
|
2020-01-28 17:35:45 +00:00
|
|
|
peer.Servers.Add(lifecycle.Item{
|
|
|
|
Name: "debug",
|
|
|
|
Run: peer.Debug.Server.Run,
|
|
|
|
Close: peer.Debug.Server.Close,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
var err error
|
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
{
|
2019-10-21 11:50:59 +01:00
|
|
|
if !versionInfo.IsZero() {
|
2019-04-03 20:13:39 +01:00
|
|
|
peer.Log.Sugar().Debugf("Binary Version: %s with CommitHash %s, built at %s as Release %v",
|
|
|
|
versionInfo.Version.String(), versionInfo.CommitHash, versionInfo.Timestamp.String(), versionInfo.Release)
|
|
|
|
}
|
2020-01-28 23:13:59 +00:00
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
peer.Version.Service = checker.NewService(log.Named("version"), config.Version, versionInfo, "Storagenode")
|
|
|
|
versionCheckInterval := 24 * time.Hour
|
|
|
|
peer.Version.Chore = version2.NewChore(peer.Version.Service, peer.Notifications.Service, peer.Identity.ID, versionCheckInterval)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "version",
|
2020-02-21 17:41:54 +00:00
|
|
|
Run: peer.Version.Chore.Run,
|
2020-01-28 23:13:59 +00:00
|
|
|
})
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
{ // setup listener and server
|
2019-03-07 18:19:37 +00:00
|
|
|
sc := config.Server
|
2019-08-19 23:10:38 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(peer.Identity, sc.Config, revocationDB)
|
2019-01-10 13:13:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
peer.Dialer = rpc.NewDefaultDialer(tlsOptions)
|
2019-01-10 13:13:27 +00:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
peer.Server, err = server.New(log.Named("server"), tlsOptions, sc.Address, sc.PrivateAddress, nil)
|
2019-01-10 13:13:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2020-01-28 23:13:59 +00:00
|
|
|
|
|
|
|
peer.Servers.Add(lifecycle.Item{
|
|
|
|
Name: "server",
|
|
|
|
Run: func(ctx context.Context) error {
|
|
|
|
peer.Log.Sugar().Infof("Node %s started", peer.Identity.ID)
|
|
|
|
peer.Log.Sugar().Infof("Public server started on %s", peer.Addr())
|
|
|
|
peer.Log.Sugar().Infof("Private server started on %s", peer.PrivateAddr())
|
|
|
|
return peer.Server.Run(ctx)
|
|
|
|
},
|
|
|
|
Close: peer.Server.Close,
|
|
|
|
})
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
{ // setup trust pool
|
2019-11-16 00:59:32 +00:00
|
|
|
peer.Storage2.Trust, err = trust.NewPool(log.Named("trust"), trust.Dialer(peer.Dialer), config.Storage2.Trust)
|
2019-08-09 10:21:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "trust",
|
|
|
|
Run: peer.Storage2.Trust.Run,
|
|
|
|
})
|
2019-08-09 10:21:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 01:58:59 +00:00
|
|
|
{
|
|
|
|
peer.Preflight.LocalTime = preflight.NewLocalTime(peer.Log.Named("preflight:localtime"), config.Preflight, peer.Storage2.Trust, peer.Dialer)
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
{ // setup contact service
|
|
|
|
c := config.Contact
|
|
|
|
if c.ExternalAddress == "" {
|
|
|
|
c.ExternalAddress = peer.Addr()
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
pbVersion, err := versionInfo.Proto()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2019-04-22 10:07:50 +01:00
|
|
|
self := &overlay.NodeDossier{
|
|
|
|
Node: pb.Node{
|
|
|
|
Id: peer.ID(),
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
2019-09-19 20:56:34 +01:00
|
|
|
Address: c.ExternalAddress,
|
2019-04-22 10:07:50 +01:00
|
|
|
},
|
2019-01-10 13:13:27 +00:00
|
|
|
},
|
2019-04-22 10:07:50 +01:00
|
|
|
Type: pb.NodeType_STORAGE,
|
|
|
|
Operator: pb.NodeOperator{
|
2019-10-01 00:33:00 +01:00
|
|
|
Email: config.Operator.Email,
|
|
|
|
Wallet: config.Operator.Wallet,
|
2019-01-10 13:13:27 +00:00
|
|
|
},
|
2019-04-22 10:07:50 +01:00
|
|
|
Version: *pbVersion,
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
2019-09-19 20:56:34 +01:00
|
|
|
peer.Contact.PingStats = new(contact.PingStats)
|
2020-02-26 02:39:44 +00:00
|
|
|
peer.Contact.Service = contact.NewService(peer.Log.Named("contact:service"), peer.Dialer, self, peer.Storage2.Trust)
|
2020-01-28 23:13:59 +00:00
|
|
|
|
2020-02-26 02:39:44 +00:00
|
|
|
peer.Contact.Chore = contact.NewChore(peer.Log.Named("contact:chore"), config.Contact.Interval, peer.Contact.Service)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "contact:chore",
|
|
|
|
Run: peer.Contact.Chore.Run,
|
|
|
|
Close: peer.Contact.Chore.Close,
|
|
|
|
})
|
|
|
|
|
2019-09-06 17:14:03 +01:00
|
|
|
peer.Contact.Endpoint = contact.NewEndpoint(peer.Log.Named("contact:endpoint"), peer.Contact.PingStats)
|
2020-02-26 13:59:46 +00:00
|
|
|
pbgrpc.RegisterContactServer(peer.Server.GRPC(), peer.Contact.Endpoint)
|
2019-09-12 22:09:46 +01:00
|
|
|
pb.DRPCRegisterContact(peer.Server.DRPC(), peer.Contact.Endpoint)
|
2020-01-28 23:13:59 +00:00
|
|
|
|
2019-09-04 20:04:18 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
{ // setup storage
|
2020-01-30 18:01:50 +00:00
|
|
|
peer.Storage2.BlobsCache = pieces.NewBlobsUsageCache(peer.Log.Named("blobscache"), peer.DB.Pieces())
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
peer.Storage2.Store = pieces.NewStore(peer.Log.Named("pieces"),
|
|
|
|
peer.Storage2.BlobsCache,
|
|
|
|
peer.DB.V0PieceInfo(),
|
|
|
|
peer.DB.PieceExpirationDB(),
|
|
|
|
peer.DB.PieceSpaceUsedDB(),
|
|
|
|
)
|
|
|
|
|
2019-11-26 16:25:21 +00:00
|
|
|
peer.Storage2.TrashChore = pieces.NewTrashChore(
|
2020-01-06 12:34:54 +00:00
|
|
|
log.Named("pieces:trash"),
|
2019-11-26 16:25:21 +00:00
|
|
|
24*time.Hour, // choreInterval: how often to run the chore
|
|
|
|
7*24*time.Hour, // trashExpiryInterval: when items in the trash should be deleted
|
|
|
|
peer.Storage2.Trust,
|
|
|
|
peer.Storage2.Store,
|
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "pieces:trash",
|
|
|
|
Run: peer.Storage2.TrashChore.Run,
|
|
|
|
Close: peer.Storage2.TrashChore.Close,
|
|
|
|
})
|
2019-11-26 16:25:21 +00:00
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
peer.Storage2.CacheService = pieces.NewService(
|
2020-01-06 12:34:54 +00:00
|
|
|
log.Named("piecestore:cache"),
|
2019-08-12 22:43:05 +01:00
|
|
|
peer.Storage2.BlobsCache,
|
|
|
|
peer.Storage2.Store,
|
|
|
|
config.Storage2.CacheSyncInterval,
|
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "piecestore:cache",
|
|
|
|
Run: peer.Storage2.CacheService.Run,
|
|
|
|
Close: peer.Storage2.CacheService.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Piecestore Cache", peer.Storage2.CacheService.Loop))
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-04-15 11:12:22 +01:00
|
|
|
peer.Storage2.Monitor = monitor.NewService(
|
|
|
|
log.Named("piecestore:monitor"),
|
|
|
|
peer.Storage2.Store,
|
2019-09-19 20:56:34 +01:00
|
|
|
peer.Contact.Service,
|
2019-04-15 11:12:22 +01:00
|
|
|
peer.DB.Bandwidth(),
|
|
|
|
config.Storage.AllocatedDiskSpace.Int64(),
|
|
|
|
//TODO use config.Storage.Monitor.Interval, but for some reason is not set
|
|
|
|
config.Storage.KBucketRefreshInterval,
|
2020-02-26 02:39:44 +00:00
|
|
|
peer.Contact.Chore.Trigger,
|
2019-06-10 11:14:50 +01:00
|
|
|
config.Storage2.Monitor,
|
2019-04-15 11:12:22 +01:00
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "piecestore:monitor",
|
|
|
|
Run: peer.Storage2.Monitor.Run,
|
|
|
|
Close: peer.Storage2.Monitor.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Piecestore Monitor", peer.Storage2.Monitor.Loop))
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2019-08-19 19:52:47 +01:00
|
|
|
peer.Storage2.RetainService = retain.NewService(
|
|
|
|
peer.Log.Named("retain"),
|
|
|
|
peer.Storage2.Store,
|
|
|
|
config.Retain,
|
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "retain",
|
|
|
|
Run: peer.Storage2.RetainService.Run,
|
|
|
|
Close: peer.Storage2.RetainService.Close,
|
|
|
|
})
|
2019-08-19 19:52:47 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
peer.Storage2.Endpoint, err = piecestore.NewEndpoint(
|
|
|
|
peer.Log.Named("piecestore"),
|
|
|
|
signing.SignerFromFullIdentity(peer.Identity),
|
|
|
|
peer.Storage2.Trust,
|
2019-04-15 11:12:22 +01:00
|
|
|
peer.Storage2.Monitor,
|
2019-08-19 19:52:47 +01:00
|
|
|
peer.Storage2.RetainService,
|
2019-10-03 19:31:39 +01:00
|
|
|
peer.Contact.PingStats,
|
2019-03-18 10:55:06 +00:00
|
|
|
peer.Storage2.Store,
|
|
|
|
peer.DB.Orders(),
|
|
|
|
peer.DB.Bandwidth(),
|
|
|
|
peer.DB.UsedSerials(),
|
|
|
|
config.Storage2,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2020-02-20 17:56:47 +00:00
|
|
|
|
2020-02-26 13:59:46 +00:00
|
|
|
pbgrpc.RegisterPiecestoreServer(peer.Server.GRPC(), peer.Storage2.Endpoint)
|
2019-09-12 22:09:46 +01:00
|
|
|
pb.DRPCRegisterPiecestore(peer.Server.DRPC(), peer.Storage2.Endpoint.DRPC())
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
// TODO workaround for custom timeout for order sending request (read/write)
|
2019-09-02 12:24:02 +01:00
|
|
|
sc := config.Server
|
2019-09-19 05:46:39 +01:00
|
|
|
|
|
|
|
tlsOptions, err := tlsopts.NewOptions(peer.Identity, sc.Config, revocationDB)
|
2019-09-02 12:24:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
dialer.DialTimeout = config.Storage2.Orders.SenderDialTimeout
|
2019-09-02 12:24:02 +01:00
|
|
|
|
2019-08-22 15:33:14 +01:00
|
|
|
peer.Storage2.Orders = orders.NewService(
|
|
|
|
log.Named("orders"),
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer,
|
2019-03-27 10:24:35 +00:00
|
|
|
peer.DB.Orders(),
|
2019-07-18 18:15:09 +01:00
|
|
|
peer.Storage2.Trust,
|
2019-08-22 15:33:14 +01:00
|
|
|
config.Storage2.Orders,
|
2019-03-27 10:24:35 +00:00
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "orders",
|
|
|
|
Run: peer.Storage2.Orders.Run,
|
|
|
|
Close: peer.Storage2.Orders.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Orders Sender", peer.Storage2.Orders.Sender))
|
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Orders Cleanup", peer.Storage2.Orders.Cleanup))
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 22:16:56 +00:00
|
|
|
{ // setup heldamount service.
|
2020-03-13 14:01:12 +00:00
|
|
|
peer.Heldamount.Service = heldamount.NewService(
|
|
|
|
peer.Log.Named("heldamount:service"),
|
2020-03-15 23:24:04 +00:00
|
|
|
peer.DB.HeldAmount(),
|
2020-03-13 14:01:12 +00:00
|
|
|
peer.Dialer,
|
|
|
|
peer.Storage2.Trust,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-26 19:55:22 +01:00
|
|
|
{ // setup node stats service
|
2019-08-08 14:47:04 +01:00
|
|
|
peer.NodeStats.Service = nodestats.NewService(
|
|
|
|
peer.Log.Named("nodestats:service"),
|
2019-09-19 05:46:39 +01:00
|
|
|
peer.Dialer,
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Storage2.Trust,
|
|
|
|
)
|
2019-08-08 14:47:04 +01:00
|
|
|
|
|
|
|
peer.NodeStats.Cache = nodestats.NewCache(
|
|
|
|
peer.Log.Named("nodestats:cache"),
|
|
|
|
config.Nodestats,
|
|
|
|
nodestats.CacheStorage{
|
|
|
|
Reputation: peer.DB.Reputation(),
|
|
|
|
StorageUsage: peer.DB.StorageUsage(),
|
2020-03-15 22:16:56 +00:00
|
|
|
HeldAmount: peer.DB.HeldAmount(),
|
2019-08-08 14:47:04 +01:00
|
|
|
},
|
|
|
|
peer.NodeStats.Service,
|
2020-03-15 22:16:56 +00:00
|
|
|
peer.Heldamount.Service,
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Storage2.Trust,
|
|
|
|
)
|
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "nodestats:cache",
|
|
|
|
Run: peer.NodeStats.Cache.Run,
|
|
|
|
Close: peer.NodeStats.Cache.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Node Stats Cache Reputation", peer.NodeStats.Cache.Reputation))
|
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Node Stats Cache Storage", peer.NodeStats.Cache.Storage))
|
2019-06-26 19:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup storage node operator dashboard
|
2019-06-20 12:52:32 +01:00
|
|
|
peer.Console.Service, err = console.NewService(
|
|
|
|
peer.Log.Named("console:service"),
|
|
|
|
peer.DB.Bandwidth(),
|
2019-08-08 02:47:30 +01:00
|
|
|
peer.Storage2.Store,
|
2020-02-21 17:41:54 +00:00
|
|
|
peer.Version.Service,
|
2019-06-20 12:52:32 +01:00
|
|
|
config.Storage.AllocatedDiskSpace,
|
2019-10-01 00:33:00 +01:00
|
|
|
config.Operator.Wallet,
|
2019-08-14 13:17:11 +01:00
|
|
|
versionInfo,
|
|
|
|
peer.Storage2.Trust,
|
|
|
|
peer.DB.Reputation(),
|
2019-09-11 21:41:43 +01:00
|
|
|
peer.DB.StorageUsage(),
|
|
|
|
peer.Contact.PingStats,
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Contact.Service,
|
|
|
|
)
|
2019-06-20 12:52:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Console.Listener, err = net.Listen("tcp", config.Console.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:52:19 +01:00
|
|
|
assets := consoleassets.FileSystem
|
|
|
|
if config.Console.StaticDir != "" {
|
|
|
|
// a specific directory has been configured. use it
|
|
|
|
assets = http.Dir(config.Console.StaticDir)
|
|
|
|
}
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
peer.Console.Endpoint = consoleserver.NewServer(
|
|
|
|
peer.Log.Named("console:endpoint"),
|
2019-10-08 09:52:19 +01:00
|
|
|
assets,
|
2019-12-17 15:38:55 +00:00
|
|
|
peer.Notifications.Service,
|
2019-06-20 12:52:32 +01:00
|
|
|
peer.Console.Service,
|
2020-03-13 14:01:12 +00:00
|
|
|
peer.Heldamount.Service,
|
2019-06-20 12:52:32 +01:00
|
|
|
peer.Console.Listener,
|
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "console:endpoint",
|
|
|
|
Run: peer.Console.Endpoint.Run,
|
|
|
|
Close: peer.Console.Endpoint.Close,
|
|
|
|
})
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
|
|
|
|
2019-07-06 14:40:58 +01:00
|
|
|
{ // setup storage inspector
|
|
|
|
peer.Storage2.Inspector = inspector.NewEndpoint(
|
|
|
|
peer.Log.Named("pieces:inspector"),
|
2019-08-08 02:47:30 +01:00
|
|
|
peer.Storage2.Store,
|
2019-09-19 20:56:34 +01:00
|
|
|
peer.Contact.Service,
|
2019-09-11 21:41:43 +01:00
|
|
|
peer.Contact.PingStats,
|
2019-07-06 14:40:58 +01:00
|
|
|
peer.DB.Bandwidth(),
|
|
|
|
config.Storage,
|
|
|
|
peer.Console.Listener.Addr(),
|
2019-09-19 20:56:34 +01:00
|
|
|
config.Contact.ExternalAddress,
|
2019-07-06 14:40:58 +01:00
|
|
|
)
|
2020-02-26 13:59:46 +00:00
|
|
|
pbgrpc.RegisterPieceStoreInspectorServer(peer.Server.PrivateGRPC(), peer.Storage2.Inspector)
|
2019-09-12 22:09:46 +01:00
|
|
|
pb.DRPCRegisterPieceStoreInspector(peer.Server.PrivateDRPC(), peer.Storage2.Inspector)
|
2019-07-06 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:58:12 +01:00
|
|
|
{ // setup graceful exit service
|
|
|
|
peer.GracefulExit.Endpoint = gracefulexit.NewEndpoint(
|
|
|
|
peer.Log.Named("gracefulexit:endpoint"),
|
|
|
|
peer.Storage2.Trust,
|
|
|
|
peer.DB.Satellites(),
|
|
|
|
peer.Storage2.BlobsCache,
|
|
|
|
)
|
2020-02-26 13:59:46 +00:00
|
|
|
pbgrpc.RegisterNodeGracefulExitServer(peer.Server.PrivateGRPC(), peer.GracefulExit.Endpoint)
|
2019-10-11 14:58:12 +01:00
|
|
|
pb.DRPCRegisterNodeGracefulExit(peer.Server.PrivateDRPC(), peer.GracefulExit.Endpoint)
|
2019-10-15 16:29:47 +01:00
|
|
|
|
|
|
|
peer.GracefulExit.Chore = gracefulexit.NewChore(
|
|
|
|
peer.Log.Named("gracefulexit:chore"),
|
|
|
|
config.GracefulExit,
|
2019-10-22 21:42:21 +01:00
|
|
|
peer.Storage2.Store,
|
|
|
|
peer.Storage2.Trust,
|
|
|
|
peer.Dialer,
|
2019-10-15 16:29:47 +01:00
|
|
|
peer.DB.Satellites(),
|
|
|
|
)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "gracefulexit:chore",
|
|
|
|
Run: peer.GracefulExit.Chore.Run,
|
|
|
|
Close: peer.GracefulExit.Chore.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Graceful Exit", peer.GracefulExit.Chore.Loop))
|
2019-10-11 14:58:12 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
peer.Collector = collector.NewService(peer.Log.Named("collector"), peer.Storage2.Store, peer.DB.UsedSerials(), config.Collector)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "collector",
|
|
|
|
Run: peer.Collector.Run,
|
|
|
|
Close: peer.Collector.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Collector", peer.Collector.Loop))
|
2019-05-08 12:11:59 +01:00
|
|
|
|
2019-07-29 15:07:52 +01:00
|
|
|
peer.Bandwidth = bandwidth.NewService(peer.Log.Named("bandwidth"), peer.DB.Bandwidth(), config.Bandwidth)
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "bandwidth",
|
|
|
|
Run: peer.Bandwidth.Run,
|
|
|
|
Close: peer.Bandwidth.Close,
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
peer.Debug.Server.Panel.Add(
|
|
|
|
debug.Cycle("Bandwidth", peer.Bandwidth.Loop))
|
2019-07-29 15:07:52 +01:00
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs storage node until it's either closed or it errors.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (peer *Peer) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-16 00:59:32 +00:00
|
|
|
// Refresh the trust pool first. It will be updated periodically via
|
|
|
|
// Run() below.
|
|
|
|
if err := peer.Storage2.Trust.Refresh(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 01:58:59 +00:00
|
|
|
if err := peer.Preflight.LocalTime.Check(ctx); err != nil {
|
|
|
|
peer.Log.Fatal("failed preflight check", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:19:14 +00:00
|
|
|
group, ctx := errgroup.WithContext(ctx)
|
2020-01-28 17:35:45 +00:00
|
|
|
|
2020-01-28 23:13:59 +00:00
|
|
|
peer.Servers.Run(ctx, group)
|
|
|
|
peer.Services.Run(ctx, group)
|
2019-10-15 16:29:47 +01:00
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the resources.
|
|
|
|
func (peer *Peer) Close() error {
|
2020-01-28 23:13:59 +00:00
|
|
|
return errs.Combine(
|
|
|
|
peer.Servers.Close(),
|
|
|
|
peer.Services.Close(),
|
|
|
|
)
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the peer ID.
|
|
|
|
func (peer *Peer) ID() storj.NodeID { return peer.Identity.ID }
|
|
|
|
|
|
|
|
// Local returns the peer local node info.
|
2019-09-19 20:56:34 +01:00
|
|
|
func (peer *Peer) Local() overlay.NodeDossier { return peer.Contact.Service.Local() }
|
2019-01-10 13:13:27 +00:00
|
|
|
|
|
|
|
// Addr returns the public address.
|
2019-03-07 18:19:37 +00:00
|
|
|
func (peer *Peer) Addr() string { return peer.Server.Addr().String() }
|
|
|
|
|
2019-07-03 18:29:18 +01:00
|
|
|
// URL returns the storj.NodeURL.
|
|
|
|
func (peer *Peer) URL() storj.NodeURL { return storj.NodeURL{ID: peer.ID(), Address: peer.Addr()} }
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
// PrivateAddr returns the private address.
|
|
|
|
func (peer *Peer) PrivateAddr() string { return peer.Server.PrivateAddr().String() }
|