2018-11-03 12:17:14 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
// Package testplanet implements the full network wiring for testing
|
|
|
|
package testplanet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-19 20:39:25 +00:00
|
|
|
"errors"
|
2018-11-03 12:17:14 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-15 08:57:47 +00:00
|
|
|
"strconv"
|
2018-11-20 16:54:52 +00:00
|
|
|
"time"
|
2018-11-03 12:17:14 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-11-15 08:57:47 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-11-03 12:17:14 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/memory"
|
2018-11-19 14:40:01 +00:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-11-19 20:39:25 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-11-03 12:17:14 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceserver "storj.io/storj/pkg/piecestore/psserver"
|
|
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
2018-11-03 12:17:14 +00:00
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Planet is a full storj system setup.
|
|
|
|
type Planet struct {
|
2018-11-15 08:57:47 +00:00
|
|
|
log *zap.Logger
|
2018-11-03 12:17:14 +00:00
|
|
|
directory string // TODO: ensure that everything is in-memory to speed things up
|
2018-11-19 20:39:25 +00:00
|
|
|
started bool
|
2018-11-03 12:17:14 +00:00
|
|
|
|
|
|
|
nodeInfos []pb.Node
|
|
|
|
nodeLinks []string
|
|
|
|
nodes []*Node
|
|
|
|
|
|
|
|
Satellites []*Node
|
|
|
|
StorageNodes []*Node
|
|
|
|
Uplinks []*Node
|
|
|
|
|
|
|
|
identities *Identities
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new full system with the given number of nodes.
|
2018-11-15 08:57:47 +00:00
|
|
|
func New(t zaptest.TestingT, satelliteCount, storageNodeCount, uplinkCount int) (*Planet, error) {
|
|
|
|
var log *zap.Logger
|
|
|
|
if t == nil {
|
|
|
|
log = zap.NewNop()
|
|
|
|
} else {
|
|
|
|
log = zaptest.NewLogger(t)
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:17:14 +00:00
|
|
|
planet := &Planet{
|
2018-11-15 08:57:47 +00:00
|
|
|
log: log,
|
2018-11-03 12:17:14 +00:00
|
|
|
identities: pregeneratedIdentities.Clone(),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
planet.directory, err = ioutil.TempDir("", "planet")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:57:00 +00:00
|
|
|
planet.Satellites, err = planet.newNodes("satellite", satelliteCount, pb.NodeType_ADMIN)
|
2018-11-03 12:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, utils.CombineErrors(err, planet.Shutdown())
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:57:00 +00:00
|
|
|
planet.StorageNodes, err = planet.newNodes("storage", storageNodeCount, pb.NodeType_STORAGE)
|
2018-11-03 12:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, utils.CombineErrors(err, planet.Shutdown())
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:57:00 +00:00
|
|
|
planet.Uplinks, err = planet.newNodes("uplink", uplinkCount, pb.NodeType_ADMIN) // TODO: fix the node type here
|
2018-11-03 12:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, utils.CombineErrors(err, planet.Shutdown())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range planet.nodes {
|
2018-11-27 17:46:12 +00:00
|
|
|
err = node.initOverlay(planet)
|
2018-11-03 12:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, utils.CombineErrors(err, planet.Shutdown())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
for _, n := range planet.nodes {
|
2018-12-12 15:40:33 +00:00
|
|
|
server := node.NewServer(n.Log.Named("node"), n.Kademlia)
|
2018-11-19 14:40:01 +00:00
|
|
|
pb.RegisterNodesServer(n.Provider.GRPC(), server)
|
|
|
|
// TODO: shutdown
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:17:14 +00:00
|
|
|
// init Satellites
|
|
|
|
for _, node := range planet.Satellites {
|
2018-11-19 20:39:25 +00:00
|
|
|
pointerServer := pointerdb.NewServer(
|
2018-11-03 12:17:14 +00:00
|
|
|
teststore.New(), node.Overlay,
|
2018-11-15 08:57:47 +00:00
|
|
|
node.Log.Named("pdb"),
|
2018-11-03 12:17:14 +00:00
|
|
|
pointerdb.Config{
|
|
|
|
MinRemoteSegmentSize: 1240,
|
|
|
|
MaxInlineSegmentSize: 8000,
|
|
|
|
Overlay: true,
|
|
|
|
},
|
|
|
|
node.Identity)
|
2018-11-19 20:39:25 +00:00
|
|
|
pb.RegisterPointerDBServer(node.Provider.GRPC(), pointerServer)
|
2018-11-20 16:54:52 +00:00
|
|
|
// bootstrap satellite kademlia node
|
|
|
|
go func(n *Node) {
|
|
|
|
if err := n.Kademlia.Bootstrap(context.Background()); err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}(node)
|
2018-11-19 20:39:25 +00:00
|
|
|
|
|
|
|
overlayServer := overlay.NewServer(node.Log.Named("overlay"), node.Overlay, node.Kademlia)
|
|
|
|
pb.RegisterOverlayServer(node.Provider.GRPC(), overlayServer)
|
2018-11-03 12:17:14 +00:00
|
|
|
|
|
|
|
node.Dependencies = append(node.Dependencies,
|
|
|
|
closerFunc(func() error {
|
|
|
|
// TODO: implement
|
|
|
|
return nil
|
|
|
|
}))
|
2018-11-20 16:54:52 +00:00
|
|
|
|
|
|
|
go func(n *Node) {
|
|
|
|
// refresh the interval every 500ms
|
|
|
|
t := time.NewTicker(500 * time.Millisecond).C
|
|
|
|
for {
|
|
|
|
<-t
|
|
|
|
if err := n.Overlay.Refresh(context.Background()); err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(node)
|
2018-11-03 12:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init storage nodes
|
|
|
|
for _, node := range planet.StorageNodes {
|
2018-11-29 18:39:27 +00:00
|
|
|
storageDir := filepath.Join(planet.directory, node.ID().String())
|
2018-11-03 12:17:14 +00:00
|
|
|
|
|
|
|
serverdb, err := psdb.OpenInMemory(context.Background(), storageDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, utils.CombineErrors(err, planet.Shutdown())
|
|
|
|
}
|
|
|
|
|
|
|
|
server := pieceserver.New(storageDir, serverdb, pieceserver.Config{
|
|
|
|
Path: storageDir,
|
|
|
|
AllocatedDiskSpace: memory.GB.Int64(),
|
|
|
|
AllocatedBandwidth: 100 * memory.GB.Int64(),
|
|
|
|
}, node.Identity.Key)
|
|
|
|
|
|
|
|
pb.RegisterPieceStoreRoutesServer(node.Provider.GRPC(), server)
|
|
|
|
|
|
|
|
node.Dependencies = append(node.Dependencies,
|
|
|
|
closerFunc(func() error {
|
|
|
|
return server.Stop(context.Background())
|
|
|
|
}))
|
2018-11-20 16:54:52 +00:00
|
|
|
// bootstrap all the kademlia nodes
|
|
|
|
go func(n *Node) {
|
|
|
|
if err := n.Kademlia.Bootstrap(context.Background()); err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}(node)
|
2018-11-03 12:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init Uplinks
|
|
|
|
for _, uplink := range planet.Uplinks {
|
|
|
|
// TODO: do we need here anything?
|
|
|
|
_ = uplink
|
|
|
|
}
|
|
|
|
|
|
|
|
return planet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts all the nodes.
|
|
|
|
func (planet *Planet) Start(ctx context.Context) {
|
|
|
|
for _, node := range planet.nodes {
|
|
|
|
go func(node *Node) {
|
|
|
|
err := node.Provider.Run(ctx)
|
|
|
|
if err == grpc.ErrServerStopped {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// TODO: better error handling
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}(node)
|
|
|
|
}
|
2018-11-19 20:39:25 +00:00
|
|
|
planet.started = true
|
2018-11-03 12:17:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
// Size returns number of nodes in the network
|
|
|
|
func (planet *Planet) Size() int { return len(planet.nodes) }
|
|
|
|
|
2018-11-03 12:17:14 +00:00
|
|
|
// Shutdown shuts down all the nodes and deletes temporary directories.
|
|
|
|
func (planet *Planet) Shutdown() error {
|
|
|
|
var errs []error
|
2018-11-19 20:39:25 +00:00
|
|
|
if !planet.started {
|
|
|
|
errs = append(errs, errors.New("Start was never called"))
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:17:14 +00:00
|
|
|
// shutdown in reverse order
|
|
|
|
for i := len(planet.nodes) - 1; i >= 0; i-- {
|
|
|
|
node := planet.nodes[i]
|
|
|
|
errs = append(errs, node.Shutdown())
|
|
|
|
}
|
|
|
|
errs = append(errs, os.RemoveAll(planet.directory))
|
|
|
|
return utils.CombineErrors(errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNodes creates initializes multiple nodes
|
2018-11-29 14:57:00 +00:00
|
|
|
func (planet *Planet) newNodes(prefix string, count int, nodeType pb.NodeType) ([]*Node, error) {
|
2018-11-03 12:17:14 +00:00
|
|
|
var xs []*Node
|
|
|
|
for i := 0; i < count; i++ {
|
2018-11-29 14:57:00 +00:00
|
|
|
node, err := planet.newNode(prefix+strconv.Itoa(i), nodeType)
|
2018-11-03 12:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xs = append(xs, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newIdentity creates a new identity for a node
|
|
|
|
func (planet *Planet) newIdentity() (*provider.FullIdentity, error) {
|
|
|
|
return planet.identities.NewIdentity()
|
|
|
|
}
|
|
|
|
|
|
|
|
// newListener creates a new listener
|
|
|
|
func (planet *Planet) newListener() (net.Listener, error) {
|
|
|
|
return net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
}
|