2019-01-10 13:13:27 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-11 11:26:39 +00:00
|
|
|
pstore "storj.io/storj/pkg/piecestore"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver"
|
2019-01-23 10:39:03 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver/agreementsender"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
|
|
|
"storj.io/storj/pkg/server"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-02-06 12:37:17 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DB is the master database for Storage Node
|
|
|
|
type DB interface {
|
2019-01-24 20:28:06 +00:00
|
|
|
// CreateTables initializes the database
|
|
|
|
CreateTables() error
|
|
|
|
// Close closes the database
|
|
|
|
Close() error
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// TODO: use better interfaces
|
2019-01-11 11:26:39 +00:00
|
|
|
Storage() *pstore.Storage
|
2019-01-10 13:13:27 +00:00
|
|
|
PSDB() *psdb.DB
|
|
|
|
RoutingTable() (kdb, ndb storage.KeyValueStore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is all the configuration parameters for a Storage Node
|
|
|
|
type Config struct {
|
2019-01-25 14:54:54 +00:00
|
|
|
Identity identity.Config
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
Server server.Config
|
|
|
|
Kademlia kademlia.Config
|
|
|
|
Storage psserver.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 {
|
|
|
|
return config.Kademlia.Verify(log)
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peer is the representation of a Storage Node.
|
|
|
|
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-23 10:39:03 +00:00
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// servers
|
|
|
|
Public struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Server *server.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// services and endpoints
|
2019-01-23 10:39:03 +00:00
|
|
|
// TODO: similar grouping to satellite.Peer
|
2019-01-24 20:28:06 +00:00
|
|
|
Kademlia struct {
|
|
|
|
RoutingTable *kademlia.RoutingTable
|
|
|
|
Service *kademlia.Kademlia
|
2019-02-06 12:37:17 +00:00
|
|
|
Endpoint *kademlia.Endpoint
|
2019-01-24 20:28:06 +00:00
|
|
|
Inspector *kademlia.Inspector
|
|
|
|
}
|
2019-01-10 13:13:27 +00:00
|
|
|
|
2019-01-24 20:28:06 +00:00
|
|
|
Storage struct {
|
2019-01-29 15:41:01 +00:00
|
|
|
Endpoint *psserver.Server // TODO: separate into endpoint and service
|
|
|
|
Monitor *psserver.Monitor
|
|
|
|
Collector *psserver.Collector
|
2019-01-24 20:28:06 +00:00
|
|
|
}
|
2019-01-23 10:39:03 +00:00
|
|
|
|
|
|
|
Agreements struct {
|
|
|
|
Sender *agreementsender.AgreementSender
|
|
|
|
}
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Storage Node.
|
|
|
|
func New(log *zap.Logger, full *identity.FullIdentity, db DB, config Config) (*Peer, error) {
|
|
|
|
peer := &Peer{
|
2019-02-06 12:37:17 +00:00
|
|
|
Log: log,
|
|
|
|
Identity: full,
|
|
|
|
DB: db,
|
|
|
|
Transport: transport.NewClient(full),
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
{ // setup listener and server
|
2019-01-23 10:39:03 +00:00
|
|
|
peer.Public.Listener, err = net.Listen("tcp", config.Server.Address)
|
2019-01-10 13:13:27 +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-01-10 13:13:27 +00:00
|
|
|
publicOptions, err := server.NewOptions(peer.Identity, publicConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2019-01-28 15:04:53 +00:00
|
|
|
peer.Public.Server, err = server.New(publicOptions, peer.Public.Listener, nil)
|
2019-01-10 13:13:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup kademlia
|
|
|
|
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_STORAGE,
|
|
|
|
Address: &pb.NodeAddress{
|
2019-01-23 10:39:03 +00:00
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: config.ExternalAddress,
|
2019-01-10 13:13:27 +00:00
|
|
|
},
|
|
|
|
Metadata: &pb.NodeMetadata{
|
|
|
|
Email: config.Operator.Email,
|
|
|
|
Wallet: config.Operator.Wallet,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
kdb, ndb := peer.DB.RoutingTable()
|
2019-01-29 06:51:07 +00:00
|
|
|
peer.Kademlia.RoutingTable, err = kademlia.NewRoutingTable(peer.Log.Named("routing"), self, kdb, ndb, &config.RoutingTableConfig)
|
2019-01-10 13:13:27 +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-10 13:13:27 +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-10 13:13:27 +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-24 20:28:06 +00:00
|
|
|
pb.RegisterNodesServer(peer.Public.Server.GRPC(), peer.Kademlia.Endpoint)
|
2019-01-23 10:39:03 +00:00
|
|
|
|
2019-01-24 20:28:06 +00:00
|
|
|
peer.Kademlia.Inspector = kademlia.NewInspector(peer.Kademlia.Service, peer.Identity)
|
|
|
|
pb.RegisterKadInspectorServer(peer.Public.Server.GRPC(), peer.Kademlia.Inspector)
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // setup piecestore
|
|
|
|
// TODO: move this setup logic into psstore package
|
|
|
|
config := config.Storage
|
|
|
|
|
|
|
|
// TODO: psserver shouldn't need the private key
|
2019-01-24 20:28:06 +00:00
|
|
|
peer.Storage.Endpoint, err = psserver.NewEndpoint(peer.Log.Named("piecestore"), config, peer.DB.Storage(), peer.DB.PSDB(), peer.Identity.Key, peer.Kademlia.Service)
|
2019-01-23 10:39:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
2019-01-24 20:28:06 +00:00
|
|
|
pb.RegisterPieceStoreRoutesServer(peer.Public.Server.GRPC(), peer.Storage.Endpoint)
|
2019-01-23 10:39:03 +00:00
|
|
|
|
|
|
|
// TODO: organize better
|
2019-01-24 20:28:06 +00:00
|
|
|
peer.Storage.Monitor = psserver.NewMonitor(peer.Log.Named("piecestore:monitor"), config.KBucketRefreshInterval, peer.Kademlia.RoutingTable, peer.Storage.Endpoint)
|
2019-01-29 15:41:01 +00:00
|
|
|
peer.Storage.Collector = psserver.NewCollector(peer.Log.Named("piecestore:collector"), peer.DB.PSDB(), peer.DB.Storage(), config.CollectorInterval)
|
2019-01-23 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // agreements
|
|
|
|
config := config.Storage // TODO: separate config
|
|
|
|
peer.Agreements.Sender = agreementsender.New(
|
|
|
|
peer.Log.Named("agreements"),
|
2019-01-24 20:28:06 +00:00
|
|
|
peer.DB.PSDB(), peer.Identity, peer.Kademlia.Service,
|
2019-01-23 10:39:03 +00:00
|
|
|
config.AgreementSenderCheckInterval,
|
|
|
|
)
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10 13:13:27 +00:00
|
|
|
|
|
|
|
group.Go(func() error {
|
2019-01-24 20:28:06 +00:00
|
|
|
return ignoreCancel(peer.Kademlia.Service.Bootstrap(ctx))
|
2019-01-10 13:13:27 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-02-08 09:25:13 +00:00
|
|
|
return ignoreCancel(peer.Kademlia.Service.Run(ctx))
|
2019-01-10 13:13:27 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-01-23 10:39:03 +00:00
|
|
|
return ignoreCancel(peer.Agreements.Sender.Run(ctx))
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-01-24 20:28:06 +00:00
|
|
|
return ignoreCancel(peer.Storage.Monitor.Run(ctx))
|
2019-01-23 10:39:03 +00:00
|
|
|
})
|
2019-01-29 15:41:01 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
return ignoreCancel(peer.Storage.Collector.Run(ctx))
|
|
|
|
})
|
2019-01-23 10:39:03 +00:00
|
|
|
group.Go(func() error {
|
|
|
|
// TODO: move the message into Server instead
|
|
|
|
peer.Log.Sugar().Infof("Node %s started on %s", peer.Identity.ID, peer.Public.Server.Addr().String())
|
|
|
|
return ignoreCancel(peer.Public.Server.Run(ctx))
|
2019-01-10 13:13:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
func ignoreCancel(err error) error {
|
|
|
|
if err == context.Canceled || err == grpc.ErrServerStopped {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// 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-10 13:13:27 +00:00
|
|
|
// close services in reverse initialization order
|
2019-01-24 20:28:06 +00:00
|
|
|
if peer.Storage.Endpoint != nil {
|
|
|
|
errlist.Add(peer.Storage.Endpoint.Close())
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
2019-01-24 20:28:06 +00:00
|
|
|
if peer.Kademlia.Service != nil {
|
|
|
|
errlist.Add(peer.Kademlia.Service.Close())
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
2019-01-24 20:28:06 +00:00
|
|
|
if peer.Kademlia.RoutingTable != nil {
|
2019-01-29 06:51:07 +00:00
|
|
|
errlist.Add(peer.Kademlia.RoutingTable.Close())
|
2019-01-10 13:13:27 +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.
|
2019-01-24 20:28:06 +00:00
|
|
|
func (peer *Peer) Local() pb.Node { return peer.Kademlia.RoutingTable.Local() }
|
2019-01-10 13:13:27 +00:00
|
|
|
|
|
|
|
// Addr returns the public address.
|
|
|
|
func (peer *Peer) Addr() string { return peer.Public.Server.Addr().String() }
|