2018-04-23 16:54:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-04-12 14:50:22 +01:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-03 14:15:52 +01:00
|
|
|
"fmt"
|
2018-04-12 14:50:22 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
protob "github.com/gogo/protobuf/proto"
|
2018-06-05 22:06:37 +01:00
|
|
|
"go.uber.org/zap"
|
2018-08-03 14:15:52 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-06-13 19:22:32 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2018-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
|
|
|
|
2018-04-23 16:54:22 +01:00
|
|
|
proto "storj.io/storj/protos/overlay" // naming proto to avoid confusion with this package
|
2018-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
// Server implements our overlay RPC service
|
|
|
|
type Server struct {
|
2018-06-22 14:33:57 +01:00
|
|
|
dht dht.DHT
|
2018-06-13 19:22:32 +01:00
|
|
|
cache *Cache
|
2018-06-05 22:06:37 +01:00
|
|
|
logger *zap.Logger
|
|
|
|
metrics *monkit.Registry
|
|
|
|
}
|
2018-04-12 14:50:22 +01:00
|
|
|
|
|
|
|
// Lookup finds the address of a node in our overlay network
|
2018-06-19 15:00:15 +01:00
|
|
|
func (o *Server) Lookup(ctx context.Context, req *proto.LookupRequest) (*proto.LookupResponse, error) {
|
2018-06-13 19:22:32 +01:00
|
|
|
na, err := o.cache.Get(ctx, req.NodeID)
|
2018-06-19 15:00:15 +01:00
|
|
|
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
|
|
|
o.logger.Error("Error looking up node", zap.Error(err), zap.String("nodeID", req.NodeID))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.LookupResponse{
|
2018-08-01 15:15:38 +01:00
|
|
|
Node: na,
|
2018-06-05 22:06:37 +01:00
|
|
|
}, nil
|
2018-04-12 14:50:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindStorageNodes searches the overlay network for nodes that meet the provided requirements
|
2018-08-01 15:15:38 +01:00
|
|
|
func (o *Server) FindStorageNodes(ctx context.Context, req *proto.FindStorageNodesRequest) (resp *proto.FindStorageNodesResponse, err error) {
|
|
|
|
opts := req.GetOpts()
|
2018-08-03 14:15:52 +01:00
|
|
|
maxNodes := opts.GetAmount()
|
2018-08-01 15:15:38 +01:00
|
|
|
restrictions := opts.GetRestrictions()
|
|
|
|
restrictedBandwidth := restrictions.GetFreeBandwidth()
|
|
|
|
restrictedSpace := restrictions.GetFreeDisk()
|
|
|
|
|
|
|
|
var start storage.Key
|
|
|
|
result := []*proto.Node{}
|
|
|
|
for {
|
|
|
|
var nodes []*proto.Node
|
|
|
|
nodes, start, err = o.populate(ctx, start, maxNodes, restrictedBandwidth, restrictedSpace)
|
|
|
|
if err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodes) <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, nodes...)
|
|
|
|
|
|
|
|
if len(result) >= int(maxNodes) || start == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:15:52 +01:00
|
|
|
if len(result) < int(maxNodes) {
|
2018-08-13 09:39:45 +01:00
|
|
|
fmt.Printf("result %v", result)
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, status.Errorf(codes.ResourceExhausted, fmt.Sprintf("requested %d nodes, only %d nodes matched the criteria requested", maxNodes, len(result)))
|
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
if len(result) > int(maxNodes) {
|
|
|
|
result = result[:maxNodes]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.FindStorageNodesResponse{
|
|
|
|
Nodes: result,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Server) getNodes(ctx context.Context, keys storage.Keys) ([]*proto.Node, error) {
|
|
|
|
values, err := o.cache.DB.GetAll(keys)
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
nodes := []*proto.Node{}
|
|
|
|
for _, v := range values {
|
|
|
|
n := &proto.Node{}
|
|
|
|
if err := protob.Unmarshal(v, n); err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes, n)
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
return nodes, nil
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-04-12 14:50:22 +01:00
|
|
|
}
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
func (o *Server) populate(ctx context.Context, starting storage.Key, maxNodes, restrictedBandwidth, restrictedSpace int64) ([]*proto.Node, storage.Key, error) {
|
|
|
|
limit := storage.Limit(maxNodes) * 2
|
2018-08-03 14:15:52 +01:00
|
|
|
keys, err := o.cache.DB.List(starting, limit)
|
2018-08-09 20:20:39 +01:00
|
|
|
fmt.Printf("keys from populate %v\n", keys)
|
2018-08-01 15:15:38 +01:00
|
|
|
if err != nil {
|
|
|
|
o.logger.Error("Error listing nodes", zap.Error(err))
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, nil, Error.Wrap(err)
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) <= 0 {
|
|
|
|
o.logger.Info("No Keys returned from List operation")
|
|
|
|
return []*proto.Node{}, starting, nil
|
|
|
|
}
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
result := []*proto.Node{}
|
|
|
|
nodes, err := o.getNodes(ctx, keys)
|
2018-08-09 20:20:39 +01:00
|
|
|
fmt.Printf("nodes from populate %v\n", len(nodes))
|
2018-08-01 15:15:38 +01:00
|
|
|
if err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
o.logger.Error("Error getting nodes", zap.Error(err))
|
|
|
|
return nil, nil, Error.Wrap(err)
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
for _, v := range nodes {
|
|
|
|
rest := v.GetRestrictions()
|
|
|
|
if rest.GetFreeBandwidth() < restrictedBandwidth || rest.GetFreeDisk() < restrictedSpace {
|
|
|
|
continue
|
|
|
|
}
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
result = append(result, v)
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
nextStart := keys[len(keys)-1]
|
|
|
|
if len(keys) < int(limit) {
|
|
|
|
nextStart = nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
return result, nextStart, nil
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|