2019-01-18 13:54:08 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-01-24 16:26:36 +00:00
|
|
|
"net/http"
|
2019-01-23 19:58:44 +00:00
|
|
|
"os"
|
2019-01-18 13:54:08 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/accounting"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/pkg/accounting/rollup"
|
|
|
|
"storj.io/storj/pkg/accounting/tally"
|
|
|
|
"storj.io/storj/pkg/audit"
|
|
|
|
"storj.io/storj/pkg/auth/grpcauth"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2019-02-07 19:22:49 +00:00
|
|
|
"storj.io/storj/pkg/certdb"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/checker"
|
|
|
|
"storj.io/storj/pkg/datarepair/irreparable"
|
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/datarepair/repairer"
|
|
|
|
"storj.io/storj/pkg/discovery"
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/server"
|
|
|
|
"storj.io/storj/pkg/statdb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-24 16:26:36 +00:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
|
|
|
"storj.io/storj/satellite/console/consoleweb"
|
2019-01-18 13:54:08 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/storelogger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DB is the master database for the satellite
|
|
|
|
type DB interface {
|
|
|
|
// CreateTables initializes the database
|
|
|
|
CreateTables() error
|
|
|
|
// Close closes the database
|
|
|
|
Close() error
|
|
|
|
|
2019-02-04 20:37:46 +00:00
|
|
|
// CreateSchema sets the schema
|
|
|
|
CreateSchema(schema string) error
|
2019-01-31 19:17:12 +00:00
|
|
|
// DropSchema drops the schema
|
|
|
|
DropSchema(schema string) error
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// BandwidthAgreement returns database for storing bandwidth agreements
|
|
|
|
BandwidthAgreement() bwagreement.DB
|
2019-02-07 19:22:49 +00:00
|
|
|
// CertDB returns database for storing uplink's public key & ID
|
|
|
|
CertDB() certdb.DB
|
2019-01-18 13:54:08 +00:00
|
|
|
// StatDB returns database for storing node statistics
|
|
|
|
StatDB() statdb.DB
|
|
|
|
// OverlayCache returns database for caching overlay information
|
|
|
|
OverlayCache() overlay.DB
|
|
|
|
// Accounting returns database for storing information about data use
|
|
|
|
Accounting() accounting.DB
|
|
|
|
// RepairQueue returns queue for segments that need repairing
|
|
|
|
RepairQueue() queue.RepairQueue
|
|
|
|
// Irreparable returns database for failed repairs
|
|
|
|
Irreparable() irreparable.DB
|
|
|
|
// Console returns database for satellite console
|
|
|
|
Console() console.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the global config satellite
|
|
|
|
type Config struct {
|
|
|
|
Identity identity.Config
|
|
|
|
|
|
|
|
// TODO: switch to using server.Config when Identity has been removed from it
|
2019-01-23 19:58:44 +00:00
|
|
|
Server server.Config
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
Kademlia kademlia.Config
|
|
|
|
Overlay overlay.Config
|
|
|
|
Discovery discovery.Config
|
|
|
|
|
|
|
|
PointerDB pointerdb.Config
|
2019-01-23 19:58:44 +00:00
|
|
|
BwAgreement bwagreement.Config // TODO: decide whether to keep empty configs for consistency
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
Checker checker.Config
|
|
|
|
Repairer repairer.Config
|
2019-01-23 19:58:44 +00:00
|
|
|
Audit audit.Config
|
|
|
|
|
2019-01-25 16:23:25 +00:00
|
|
|
Tally tally.Config
|
|
|
|
Rollup rollup.Config
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
Console consoleweb.Config
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peer is the satellite
|
|
|
|
type Peer struct {
|
|
|
|
// core dependencies
|
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
DB DB
|
|
|
|
|
2019-02-06 12:37:17 +00:00
|
|
|
Transport transport.Client
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// servers
|
|
|
|
Public struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Server *server.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// services and endpoints
|
|
|
|
Kademlia struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
kdb, ndb storage.KeyValueStore // TODO: move these into DB
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
RoutingTable *kademlia.RoutingTable
|
|
|
|
Service *kademlia.Kademlia
|
2019-02-06 12:37:17 +00:00
|
|
|
Endpoint *kademlia.Endpoint
|
2019-01-23 19:58:44 +00:00
|
|
|
Inspector *kademlia.Inspector
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Overlay struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
Service *overlay.Cache
|
|
|
|
Endpoint *overlay.Server
|
|
|
|
Inspector *overlay.Inspector
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Discovery struct {
|
|
|
|
Service *discovery.Discovery
|
|
|
|
}
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
Reputation struct {
|
|
|
|
Inspector *statdb.Inspector
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
Metainfo struct {
|
2019-01-19 18:58:53 +00:00
|
|
|
Database storage.KeyValueStore // TODO: move into pointerDB
|
|
|
|
Allocation *pointerdb.AllocationSigner
|
|
|
|
Service *pointerdb.Service
|
|
|
|
Endpoint *pointerdb.Server
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Agreements struct {
|
|
|
|
Endpoint *bwagreement.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
Repair struct {
|
2019-02-11 21:06:39 +00:00
|
|
|
Checker *checker.Checker
|
2019-01-18 13:54:08 +00:00
|
|
|
Repairer *repairer.Service
|
|
|
|
}
|
|
|
|
Audit struct {
|
2019-01-23 19:58:44 +00:00
|
|
|
Service *audit.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
Accounting struct {
|
|
|
|
Tally *tally.Tally
|
|
|
|
Rollup *rollup.Rollup
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
Console struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Service *console.Service
|
|
|
|
Endpoint *consoleweb.Server
|
|
|
|
}
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new satellite
|
|
|
|
func New(log *zap.Logger, full *identity.FullIdentity, db DB, config *Config) (*Peer, error) {
|
|
|
|
peer := &Peer{
|
2019-02-11 11:17:32 +00:00
|
|
|
Log: log,
|
|
|
|
Identity: full,
|
|
|
|
DB: db,
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
{ // setup listener and server
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Starting listener and server")
|
2019-01-23 19:58:44 +00:00
|
|
|
peer.Public.Listener, err = net.Listen("tcp", config.Server.Address)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-02-08 18:57:17 +00:00
|
|
|
publicConfig := config.Server
|
|
|
|
publicConfig.Address = peer.Public.Listener.Addr().String()
|
2019-02-11 11:17:32 +00:00
|
|
|
publicOptions, err := tlsopts.NewOptions(peer.Identity, publicConfig.Config)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
peer.Transport = transport.NewClient(publicOptions)
|
|
|
|
|
2019-01-28 15:04:53 +00:00
|
|
|
peer.Public.Server, err = server.New(publicOptions, peer.Public.Listener, grpcauth.NewAPIKeyInterceptor())
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
{ // setup overlay
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Starting overlay")
|
2019-02-08 09:25:13 +00:00
|
|
|
config := config.Overlay
|
|
|
|
peer.Overlay.Service = overlay.NewCache(peer.DB.OverlayCache(), peer.DB.StatDB())
|
|
|
|
|
2019-02-08 20:35:59 +00:00
|
|
|
peer.Transport = peer.Transport.WithObservers(peer.Overlay.Service)
|
2019-02-08 09:25:13 +00:00
|
|
|
|
|
|
|
nodeSelectionConfig := &overlay.NodeSelectionConfig{
|
|
|
|
UptimeCount: config.Node.UptimeCount,
|
|
|
|
UptimeRatio: config.Node.UptimeRatio,
|
|
|
|
AuditSuccessRatio: config.Node.AuditSuccessRatio,
|
|
|
|
AuditCount: config.Node.AuditCount,
|
|
|
|
NewNodeAuditThreshold: config.Node.NewNodeAuditThreshold,
|
|
|
|
NewNodePercentage: config.Node.NewNodePercentage,
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Overlay.Endpoint = overlay.NewServer(peer.Log.Named("overlay:endpoint"), peer.Overlay.Service, nodeSelectionConfig)
|
|
|
|
pb.RegisterOverlayServer(peer.Public.Server.GRPC(), peer.Overlay.Endpoint)
|
|
|
|
|
|
|
|
peer.Overlay.Inspector = overlay.NewInspector(peer.Overlay.Service)
|
|
|
|
pb.RegisterOverlayInspectorServer(peer.Public.Server.GRPC(), peer.Overlay.Inspector)
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
{ // setup kademlia
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up Kademlia")
|
2019-01-18 13:54:08 +00:00
|
|
|
config := config.Kademlia
|
|
|
|
// TODO: move this setup logic into kademlia package
|
|
|
|
if config.ExternalAddress == "" {
|
|
|
|
config.ExternalAddress = peer.Public.Server.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
self := pb.Node{
|
|
|
|
Id: peer.ID(),
|
|
|
|
Type: pb.NodeType_SATELLITE,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: config.ExternalAddress,
|
|
|
|
},
|
|
|
|
Metadata: &pb.NodeMetadata{
|
|
|
|
Email: config.Operator.Email,
|
|
|
|
Wallet: config.Operator.Wallet,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup routing table
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: clean this up, should be part of database
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up routing table")
|
2019-01-18 13:54:08 +00:00
|
|
|
bucketIdentifier := peer.ID().String()[:5] // need a way to differentiate between nodes if running more than one simultaneously
|
|
|
|
dbpath := filepath.Join(config.DBPath, fmt.Sprintf("kademlia_%s.db", bucketIdentifier))
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
if err := os.MkdirAll(config.DBPath, 0777); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
dbs, err := boltdb.NewShared(dbpath, kademlia.KademliaBucket, kademlia.NodeBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2019-01-23 19:58:44 +00:00
|
|
|
peer.Kademlia.kdb, peer.Kademlia.ndb = dbs[0], dbs[1]
|
2019-01-18 13:54:08 +00:00
|
|
|
|
2019-01-29 06:51:07 +00:00
|
|
|
peer.Kademlia.RoutingTable, err = kademlia.NewRoutingTable(peer.Log.Named("routing"), self, peer.Kademlia.kdb, peer.Kademlia.ndb, &config.RoutingTableConfig)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2019-02-08 09:25:13 +00:00
|
|
|
|
|
|
|
peer.Transport = peer.Transport.WithObservers(peer.Kademlia.RoutingTable)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: reduce number of arguments
|
2019-02-08 09:25:13 +00:00
|
|
|
peer.Kademlia.Service, err = kademlia.NewService(peer.Log.Named("kademlia"), self, config.BootstrapNodes(), peer.Transport, config.Alpha, peer.Kademlia.RoutingTable)
|
2019-01-18 13:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:37:17 +00:00
|
|
|
peer.Kademlia.Endpoint = kademlia.NewEndpoint(peer.Log.Named("kademlia:endpoint"), peer.Kademlia.Service, peer.Kademlia.RoutingTable)
|
2019-01-18 13:54:08 +00:00
|
|
|
pb.RegisterNodesServer(peer.Public.Server.GRPC(), peer.Kademlia.Endpoint)
|
2019-01-23 19:58:44 +00:00
|
|
|
|
|
|
|
peer.Kademlia.Inspector = kademlia.NewInspector(peer.Kademlia.Service, peer.Identity)
|
|
|
|
pb.RegisterKadInspectorServer(peer.Public.Server.GRPC(), peer.Kademlia.Inspector)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
{ // setup reputation
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up reputation")
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: find better structure with overlay
|
|
|
|
peer.Reputation.Inspector = statdb.NewInspector(peer.DB.StatDB())
|
|
|
|
pb.RegisterStatDBInspectorServer(peer.Public.Server.GRPC(), peer.Reputation.Inspector)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup discovery
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up discovery")
|
2019-01-18 13:54:08 +00:00
|
|
|
config := config.Discovery
|
2019-01-30 16:29:18 +00:00
|
|
|
peer.Discovery.Service = discovery.New(peer.Log.Named("discovery"), peer.Overlay.Service, peer.Kademlia.Service, peer.DB.StatDB(), config)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup metainfo
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up metainfo")
|
2019-01-18 13:54:08 +00:00
|
|
|
db, err := pointerdb.NewStore(config.PointerDB.DatabaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Metainfo.Database = storelogger.New(peer.Log.Named("pdb"), db)
|
2019-01-18 15:10:21 +00:00
|
|
|
peer.Metainfo.Service = pointerdb.NewService(peer.Log.Named("pointerdb"), peer.Metainfo.Database)
|
2019-02-07 19:22:49 +00:00
|
|
|
peer.Metainfo.Allocation = pointerdb.NewAllocationSigner(peer.Identity, config.PointerDB.BwExpiration, peer.DB.CertDB())
|
2019-02-05 17:22:17 +00:00
|
|
|
peer.Metainfo.Endpoint = pointerdb.NewServer(peer.Log.Named("pointerdb:endpoint"),
|
|
|
|
peer.Metainfo.Service,
|
|
|
|
peer.Metainfo.Allocation,
|
|
|
|
peer.Overlay.Service,
|
|
|
|
config.PointerDB,
|
|
|
|
peer.Identity, peer.DB.Console().APIKeys())
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
pb.RegisterPointerDBServer(peer.Public.Server.GRPC(), peer.Metainfo.Endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup agreements
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up agreements")
|
2019-02-07 19:22:49 +00:00
|
|
|
bwServer := bwagreement.NewServer(peer.DB.BandwidthAgreement(), peer.DB.CertDB(), peer.Identity.Leaf.PublicKey, peer.Log.Named("agreements"), peer.Identity.ID)
|
2019-01-25 18:05:21 +00:00
|
|
|
peer.Agreements.Endpoint = bwServer
|
2019-01-18 13:54:08 +00:00
|
|
|
pb.RegisterBandwidthServer(peer.Public.Server.GRPC(), peer.Agreements.Endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup datarepair
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up datarepair")
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: simplify argument list somehow
|
|
|
|
peer.Repair.Checker = checker.NewChecker(
|
2019-01-19 18:58:53 +00:00
|
|
|
peer.Metainfo.Service,
|
2019-01-18 13:54:08 +00:00
|
|
|
peer.DB.StatDB(), peer.DB.RepairQueue(),
|
|
|
|
peer.Overlay.Endpoint, peer.DB.Irreparable(),
|
|
|
|
0, peer.Log.Named("checker"),
|
|
|
|
config.Checker.Interval)
|
|
|
|
|
2019-02-11 11:02:49 +00:00
|
|
|
if config.Repairer.OverlayAddr == "" {
|
|
|
|
config.Repairer.OverlayAddr = peer.Addr()
|
|
|
|
}
|
|
|
|
if config.Repairer.PointerDBAddr == "" {
|
|
|
|
config.Repairer.PointerDBAddr = peer.Addr()
|
|
|
|
}
|
2019-02-11 11:17:32 +00:00
|
|
|
peer.Repair.Repairer = repairer.NewService(peer.DB.RepairQueue(), &config.Repairer, peer.Transport, config.Repairer.Interval, config.Repairer.MaxRepair)
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup audit
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up audits")
|
2019-01-23 19:58:44 +00:00
|
|
|
config := config.Audit
|
|
|
|
|
|
|
|
peer.Audit.Service, err = audit.NewService(peer.Log.Named("audit"),
|
|
|
|
peer.DB.StatDB(),
|
|
|
|
config.Interval, config.MaxRetriesStatDB,
|
|
|
|
peer.Metainfo.Service, peer.Metainfo.Allocation,
|
2019-02-11 11:17:32 +00:00
|
|
|
peer.Transport, peer.Overlay.Service,
|
2019-01-23 19:58:44 +00:00
|
|
|
peer.Identity,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup accounting
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up accounting")
|
2019-01-23 19:58:44 +00:00
|
|
|
peer.Accounting.Tally = tally.New(peer.Log.Named("tally"), peer.DB.Accounting(), peer.DB.BandwidthAgreement(), peer.Metainfo.Service, peer.Overlay.Endpoint, 0, config.Tally.Interval)
|
|
|
|
peer.Accounting.Rollup = rollup.New(peer.Log.Named("rollup"), peer.DB.Accounting(), config.Rollup.Interval)
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
{ // setup console
|
2019-02-28 20:12:52 +00:00
|
|
|
log.Debug("Setting up console")
|
2019-01-24 16:26:36 +00:00
|
|
|
config := config.Console
|
|
|
|
|
|
|
|
peer.Console.Listener, err = net.Listen("tcp", config.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Console.Service, err = console.NewService(peer.Log.Named("console:service"),
|
|
|
|
// TODO: use satellite key
|
|
|
|
&consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")},
|
2019-02-05 17:31:53 +00:00
|
|
|
peer.DB.Console(),
|
|
|
|
config.PasswordCost,
|
|
|
|
)
|
2019-01-24 16:26:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Console.Endpoint = consoleweb.NewServer(peer.Log.Named("console:endpoint"),
|
|
|
|
config,
|
|
|
|
peer.Console.Service,
|
|
|
|
peer.Console.Listener)
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ignoreCancel(err error) error {
|
2019-01-24 16:26:36 +00:00
|
|
|
if err == context.Canceled || err == grpc.ErrServerStopped || err == http.ErrServerClosed {
|
2019-01-18 13:54:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs storage node until it's either closed or it errors.
|
|
|
|
func (peer *Peer) Run(ctx context.Context) error {
|
2019-02-06 13:19:14 +00:00
|
|
|
group, ctx := errgroup.WithContext(ctx)
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Kademlia.Service.Bootstrap(ctx))
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-02-08 09:25:13 +00:00
|
|
|
return ignoreCancel(peer.Kademlia.Service.Run(ctx))
|
2019-01-18 13:54:08 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Discovery.Service.Run(ctx))
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Repair.Checker.Run(ctx))
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Repair.Repairer.Run(ctx))
|
|
|
|
})
|
2019-01-24 18:44:14 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Accounting.Tally.Run(ctx))
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Accounting.Rollup.Run(ctx))
|
|
|
|
})
|
2019-01-24 19:52:16 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Audit.Service.Run(ctx))
|
|
|
|
})
|
2019-01-18 13:54:08 +00:00
|
|
|
group.Go(func() error {
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: move the message into Server instead
|
|
|
|
peer.Log.Sugar().Infof("Node %s started on %s", peer.Identity.ID, peer.Public.Server.Addr().String())
|
2019-01-18 13:54:08 +00:00
|
|
|
return ignoreCancel(peer.Public.Server.Run(ctx))
|
|
|
|
})
|
2019-01-24 16:26:36 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Console.Endpoint.Run(ctx))
|
|
|
|
})
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the resources.
|
|
|
|
func (peer *Peer) Close() error {
|
|
|
|
var errlist errs.Group
|
|
|
|
|
|
|
|
// TODO: ensure that Close can be called on nil-s that way this code won't need the checks.
|
|
|
|
|
2019-01-28 19:04:42 +00:00
|
|
|
// close servers, to avoid new connections to closing subsystems
|
|
|
|
if peer.Public.Server != nil {
|
|
|
|
errlist.Add(peer.Public.Server.Close())
|
|
|
|
} else {
|
|
|
|
// peer.Public.Server automatically closes listener
|
|
|
|
if peer.Public.Listener != nil {
|
|
|
|
errlist.Add(peer.Public.Listener.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
if peer.Console.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Console.Endpoint.Close())
|
|
|
|
} else {
|
|
|
|
if peer.Console.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Public.Listener.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 19:04:42 +00:00
|
|
|
// close services in reverse initialization order
|
2019-01-18 13:54:08 +00:00
|
|
|
if peer.Repair.Repairer != nil {
|
|
|
|
errlist.Add(peer.Repair.Repairer.Close())
|
|
|
|
}
|
|
|
|
if peer.Repair.Checker != nil {
|
|
|
|
errlist.Add(peer.Repair.Checker.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Agreements.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Agreements.Endpoint.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Metainfo.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Metainfo.Endpoint.Close())
|
|
|
|
}
|
|
|
|
if peer.Metainfo.Database != nil {
|
|
|
|
errlist.Add(peer.Metainfo.Database.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Discovery.Service != nil {
|
|
|
|
errlist.Add(peer.Discovery.Service.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add kademlia.Endpoint for consistency
|
|
|
|
if peer.Kademlia.Service != nil {
|
|
|
|
errlist.Add(peer.Kademlia.Service.Close())
|
|
|
|
}
|
|
|
|
if peer.Kademlia.RoutingTable != nil {
|
2019-01-29 06:51:07 +00:00
|
|
|
errlist.Add(peer.Kademlia.RoutingTable.Close())
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
if peer.Overlay.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Overlay.Endpoint.Close())
|
|
|
|
}
|
|
|
|
if peer.Overlay.Service != nil {
|
|
|
|
errlist.Add(peer.Overlay.Service.Close())
|
|
|
|
}
|
|
|
|
|
2019-01-25 22:33:20 +00:00
|
|
|
if peer.Kademlia.ndb != nil || peer.Kademlia.kdb != nil {
|
|
|
|
errlist.Add(peer.Kademlia.kdb.Close())
|
|
|
|
errlist.Add(peer.Kademlia.ndb.Close())
|
|
|
|
}
|
2019-01-23 19:58:44 +00:00
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
return errlist.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the peer ID.
|
|
|
|
func (peer *Peer) ID() storj.NodeID { return peer.Identity.ID }
|
|
|
|
|
|
|
|
// Local returns the peer local node info.
|
|
|
|
func (peer *Peer) Local() pb.Node { return peer.Kademlia.RoutingTable.Local() }
|
|
|
|
|
|
|
|
// Addr returns the public address.
|
|
|
|
func (peer *Peer) Addr() string { return peer.Public.Server.Addr().String() }
|