2018-06-19 15:00:15 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2018-07-09 18:43:13 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-06-19 15:00:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is the interface that defines an overlay client.
|
|
|
|
//
|
|
|
|
// Choose returns a list of storage NodeID's that fit the provided criteria.
|
|
|
|
// limit is the maximum number of nodes to be returned.
|
|
|
|
// space is the storage and bandwidth requested consumption in bytes.
|
|
|
|
//
|
|
|
|
// Lookup finds a Node with the provided identifier.
|
2018-09-11 05:52:14 +01:00
|
|
|
|
|
|
|
// ClientError creates class of errors for stack traces
|
|
|
|
var ClientError = errs.Class("Client Error")
|
|
|
|
|
|
|
|
//Client implements the Overlay Client interface
|
2018-06-19 15:00:15 +01:00
|
|
|
type Client interface {
|
2018-10-15 18:42:36 +01:00
|
|
|
Choose(ctx context.Context, op Options) ([]*pb.Node, error)
|
2018-09-18 05:39:06 +01:00
|
|
|
Lookup(ctx context.Context, nodeID dht.NodeID) (*pb.Node, error)
|
|
|
|
BulkLookup(ctx context.Context, nodeIDs []dht.NodeID) ([]*pb.Node, error)
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overlay is the overlay concrete implementation of the client interface
|
|
|
|
type Overlay struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
client pb.OverlayClient
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 18:42:36 +01:00
|
|
|
// Options contains parameters for selecting nodes
|
|
|
|
type Options struct {
|
|
|
|
Amount int
|
|
|
|
Space int64
|
|
|
|
Excluded []dht.NodeID
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
// NewOverlayClient returns a new intialized Overlay Client
|
2018-08-24 05:01:03 +01:00
|
|
|
func NewOverlayClient(identity *provider.FullIdentity, address string) (*Overlay, error) {
|
|
|
|
dialOpt, err := identity.DialOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c, err := NewClient(address, dialOpt)
|
2018-06-19 15:00:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-03 14:15:52 +01:00
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
return &Overlay{
|
|
|
|
client: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:05:02 +01:00
|
|
|
// a compiler trick to make sure *Overlay implements Client
|
|
|
|
var _ Client = (*Overlay)(nil)
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
// Choose implements the client.Choose interface
|
2018-10-15 18:42:36 +01:00
|
|
|
func (o *Overlay) Choose(ctx context.Context, op Options) ([]*pb.Node, error) {
|
|
|
|
var exIDs []string
|
|
|
|
for _, id := range op.Excluded {
|
|
|
|
exIDs = append(exIDs, id.String())
|
|
|
|
}
|
2018-06-19 15:00:15 +01:00
|
|
|
// TODO(coyle): We will also need to communicate with the reputation service here
|
2018-09-18 05:39:06 +01:00
|
|
|
resp, err := o.client.FindStorageNodes(ctx, &pb.FindStorageNodesRequest{
|
2018-10-15 18:42:36 +01:00
|
|
|
Opts: &pb.OverlayOptions{
|
|
|
|
Amount: int64(op.Amount),
|
|
|
|
Restrictions: &pb.NodeRestrictions{FreeDisk: op.Space},
|
|
|
|
ExcludedNodes: exIDs,
|
|
|
|
},
|
2018-08-01 15:15:38 +01:00
|
|
|
})
|
2018-06-19 15:00:15 +01:00
|
|
|
if err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
return resp.GetNodes(), nil
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
// Lookup provides a Node with the given ID
|
2018-09-18 05:39:06 +01:00
|
|
|
func (o *Overlay) Lookup(ctx context.Context, nodeID dht.NodeID) (*pb.Node, error) {
|
|
|
|
resp, err := o.client.Lookup(ctx, &pb.LookupRequest{NodeID: nodeID.String()})
|
2018-06-19 15:00:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.GetNode(), nil
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
|
|
|
|
//BulkLookup provides a list of Nodes with the given IDs
|
2018-09-18 05:39:06 +01:00
|
|
|
func (o *Overlay) BulkLookup(ctx context.Context, nodeIDs []dht.NodeID) ([]*pb.Node, error) {
|
|
|
|
var reqs pb.LookupRequests
|
2018-09-11 05:52:14 +01:00
|
|
|
for _, v := range nodeIDs {
|
2018-09-18 05:39:06 +01:00
|
|
|
reqs.Lookuprequest = append(reqs.Lookuprequest, &pb.LookupRequest{NodeID: v.String()})
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
|
|
|
resp, err := o.client.BulkLookup(ctx, &reqs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, ClientError.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
var nodes []*pb.Node
|
2018-09-11 05:52:14 +01:00
|
|
|
for _, v := range resp.Lookupresponse {
|
|
|
|
nodes = append(nodes, v.Node)
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|