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 (
|
2018-11-29 18:39:27 +00:00
|
|
|
"bytes"
|
2018-04-12 14:50:22 +01:00
|
|
|
"context"
|
2018-08-03 14:15:52 +01:00
|
|
|
"fmt"
|
2018-09-18 05:39:06 +01:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-09-11 05:52:14 +01:00
|
|
|
"github.com/zeebo/errs"
|
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-12-20 13:57:54 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
// ServerError creates class of errors for stack traces
|
|
|
|
var ServerError = errs.Class("Server Error")
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
// Server implements our overlay RPC service
|
|
|
|
type Server struct {
|
2018-12-20 13:57:54 +00:00
|
|
|
log *zap.Logger
|
2018-12-17 21:05:05 +00:00
|
|
|
cache *Cache
|
|
|
|
metrics *monkit.Registry
|
|
|
|
nodeStats *pb.NodeStats
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
2018-04-12 14:50:22 +01:00
|
|
|
|
2018-11-19 20:39:25 +00:00
|
|
|
// NewServer creates a new Overlay Server
|
2018-12-20 13:57:54 +00:00
|
|
|
func NewServer(log *zap.Logger, cache *Cache, nodeStats *pb.NodeStats) *Server {
|
2018-11-19 20:39:25 +00:00
|
|
|
return &Server{
|
2018-12-17 21:05:05 +00:00
|
|
|
cache: cache,
|
2018-12-20 13:57:54 +00:00
|
|
|
log: log,
|
2018-12-17 21:05:05 +00:00
|
|
|
metrics: monkit.Default,
|
|
|
|
nodeStats: nodeStats,
|
2018-11-19 20:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:50:22 +01:00
|
|
|
// Lookup finds the address of a node in our overlay network
|
2018-12-20 13:57:54 +00:00
|
|
|
func (server *Server) Lookup(ctx context.Context, req *pb.LookupRequest) (*pb.LookupResponse, error) {
|
|
|
|
na, err := server.cache.Get(ctx, req.NodeId)
|
2018-06-19 15:00:15 +01:00
|
|
|
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
2018-12-20 13:57:54 +00:00
|
|
|
server.log.Error("Error looking up node", zap.Error(err), zap.String("nodeID", req.NodeId.String()))
|
2018-06-05 22:06:37 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
return &pb.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
|
|
|
}
|
|
|
|
|
2018-11-20 16:54:52 +00:00
|
|
|
// BulkLookup finds the addresses of nodes in our overlay network
|
2018-12-20 13:57:54 +00:00
|
|
|
func (server *Server) BulkLookup(ctx context.Context, reqs *pb.LookupRequests) (*pb.LookupResponses, error) {
|
|
|
|
ns, err := server.cache.GetAll(ctx, lookupRequestsToNodeIDs(reqs))
|
2018-09-11 05:52:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, ServerError.New("could not get nodes requested %s\n", err)
|
|
|
|
}
|
|
|
|
return nodesToLookupResponses(ns), nil
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:50:22 +01:00
|
|
|
// FindStorageNodes searches the overlay network for nodes that meet the provided requirements
|
2018-12-20 13:57:54 +00:00
|
|
|
func (server *Server) FindStorageNodes(ctx context.Context, req *pb.FindStorageNodesRequest) (resp *pb.FindStorageNodesResponse, err error) {
|
2018-08-01 15:15:38 +01:00
|
|
|
opts := req.GetOpts()
|
2018-11-02 18:50:28 +00:00
|
|
|
maxNodes := req.GetMaxNodes()
|
|
|
|
if maxNodes <= 0 {
|
|
|
|
maxNodes = opts.GetAmount()
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
excluded := opts.ExcludedNodes
|
2018-08-01 15:15:38 +01:00
|
|
|
restrictions := opts.GetRestrictions()
|
2018-12-20 13:57:54 +00:00
|
|
|
reputation := server.nodeStats
|
2018-08-01 15:15:38 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
var startID storj.NodeID
|
2018-09-18 05:39:06 +01:00
|
|
|
result := []*pb.Node{}
|
2018-08-01 15:15:38 +01:00
|
|
|
for {
|
2018-09-18 05:39:06 +01:00
|
|
|
var nodes []*pb.Node
|
2018-12-20 13:57:54 +00:00
|
|
|
nodes, startID, err = server.populate(ctx, req.Start, maxNodes, restrictions, reputation, excluded)
|
2018-08-01 15:15:38 +01:00
|
|
|
if err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
resultNodes := []*pb.Node{}
|
|
|
|
usedAddrs := make(map[string]bool)
|
|
|
|
for _, n := range nodes {
|
|
|
|
addr := n.Address.GetAddress()
|
|
|
|
excluded = append(excluded, n.Id) // exclude all nodes on next iteration
|
|
|
|
if !usedAddrs[addr] {
|
|
|
|
resultNodes = append(resultNodes, n)
|
|
|
|
usedAddrs[addr] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(resultNodes) <= 0 {
|
2018-08-01 15:15:38 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
result = append(result, resultNodes...)
|
2018-08-01 15:15:38 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
if len(result) >= int(maxNodes) || startID == (storj.NodeID{}) {
|
2018-08-01 15:15:38 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:15:52 +01:00
|
|
|
if len(result) < int(maxNodes) {
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
return &pb.FindStorageNodesResponse{
|
2018-08-01 15:15:38 +01:00
|
|
|
Nodes: result,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
func (server *Server) getNodes(ctx context.Context, keys storage.Keys) ([]*pb.Node, error) {
|
|
|
|
values, err := server.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-09-18 05:39:06 +01:00
|
|
|
nodes := []*pb.Node{}
|
2018-08-01 15:15:38 +01:00
|
|
|
for _, v := range values {
|
2018-09-18 05:39:06 +01:00
|
|
|
n := &pb.Node{}
|
|
|
|
if err := proto.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-12-20 13:57:54 +00:00
|
|
|
func (server *Server) populate(ctx context.Context, startID storj.NodeID, maxNodes int64,
|
2018-12-04 20:18:26 +00:00
|
|
|
minRestrictions *pb.NodeRestrictions, minReputation *pb.NodeStats,
|
|
|
|
excluded storj.NodeIDList) ([]*pb.Node, storj.NodeID, error) {
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
limit := int(maxNodes * 2)
|
2018-12-20 13:57:54 +00:00
|
|
|
keys, err := server.cache.db.List(startID.Bytes(), limit)
|
2018-08-01 15:15:38 +01:00
|
|
|
if err != nil {
|
2018-12-20 13:57:54 +00:00
|
|
|
server.log.Error("Error listing nodes", zap.Error(err))
|
2018-11-29 18:39:27 +00:00
|
|
|
return nil, storj.NodeID{}, Error.Wrap(err)
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) <= 0 {
|
2018-12-20 13:57:54 +00:00
|
|
|
server.log.Info("No Keys returned from List operation")
|
2018-11-29 18:39:27 +00:00
|
|
|
return []*pb.Node{}, startID, nil
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
2018-06-22 14:33:57 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
// TODO: should this be `var result []*pb.Node` ?
|
2018-09-18 05:39:06 +01:00
|
|
|
result := []*pb.Node{}
|
2018-12-20 13:57:54 +00:00
|
|
|
nodes, err := server.getNodes(ctx, keys)
|
2018-08-01 15:15:38 +01:00
|
|
|
if err != nil {
|
2018-12-20 13:57:54 +00:00
|
|
|
server.log.Error("Error getting nodes", zap.Error(err))
|
2018-11-29 18:39:27 +00:00
|
|
|
return nil, storj.NodeID{}, 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 {
|
2018-11-29 14:57:00 +00:00
|
|
|
if v.Type != pb.NodeType_STORAGE {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-15 18:42:36 +01:00
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
restrictions := v.GetRestrictions()
|
|
|
|
reputation := v.GetReputation()
|
2018-12-04 20:18:26 +00:00
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
if restrictions.GetFreeBandwidth() < minRestrictions.GetFreeBandwidth() ||
|
|
|
|
restrictions.GetFreeDisk() < minRestrictions.GetFreeDisk() ||
|
|
|
|
reputation.GetUptimeRatio() < minReputation.GetUptimeRatio() ||
|
|
|
|
reputation.GetUptimeCount() < minReputation.GetUptimeCount() ||
|
|
|
|
reputation.GetAuditSuccessRatio() < minReputation.GetAuditSuccessRatio() ||
|
|
|
|
reputation.GetAuditCount() < minReputation.GetAuditCount() ||
|
2018-12-04 20:18:26 +00:00
|
|
|
contains(excluded, v.Id) {
|
2018-08-01 15:15:38 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, v)
|
2018-06-22 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
var nextStart storj.NodeID
|
2018-09-07 15:20:15 +01:00
|
|
|
if len(keys) < limit {
|
2018-11-29 18:39:27 +00:00
|
|
|
nextStart = storj.NodeID{}
|
|
|
|
} else {
|
|
|
|
nextStart, err = storj.NodeIDFromBytes(keys[len(keys)-1])
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.NodeID{}, Error.Wrap(err)
|
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
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
|
2018-10-15 18:42:36 +01:00
|
|
|
// contains checks if item exists in list
|
2018-11-29 18:39:27 +00:00
|
|
|
func contains(nodeIDs storj.NodeIDList, searchID storj.NodeID) bool {
|
|
|
|
for _, id := range nodeIDs {
|
|
|
|
if bytes.Equal(id.Bytes(), searchID.Bytes()) {
|
2018-10-15 18:42:36 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
// lookupRequestsToNodeIDs returns the nodeIDs from the LookupRequests
|
|
|
|
func lookupRequestsToNodeIDs(reqs *pb.LookupRequests) (ids storj.NodeIDList) {
|
2018-11-24 02:46:53 +00:00
|
|
|
for _, v := range reqs.LookupRequest {
|
2018-11-29 18:39:27 +00:00
|
|
|
ids = append(ids, v.NodeId)
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:09:33 +01:00
|
|
|
// nodesToLookupResponses returns LookupResponses from the nodes
|
2018-09-18 05:39:06 +01:00
|
|
|
func nodesToLookupResponses(nodes []*pb.Node) *pb.LookupResponses {
|
|
|
|
var rs []*pb.LookupResponse
|
2018-09-11 05:52:14 +01:00
|
|
|
for _, v := range nodes {
|
2018-09-18 05:39:06 +01:00
|
|
|
r := &pb.LookupResponse{Node: v}
|
2018-09-11 05:52:14 +01:00
|
|
|
rs = append(rs, r)
|
|
|
|
}
|
2018-11-24 02:46:53 +00:00
|
|
|
return &pb.LookupResponses{LookupResponse: rs}
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|