2018-11-21 17:31:27 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package inspector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2018-12-04 18:47:58 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-11-21 22:01:09 +00:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-11-21 17:31:27 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-27 22:50:52 +00:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-11-21 22:01:09 +00:00
|
|
|
"storj.io/storj/pkg/statdb"
|
|
|
|
statsproto "storj.io/storj/pkg/statdb/proto"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-21 17:31:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ServerError is a gRPC server error for Inspector
|
|
|
|
ServerError = errs.Class("inspector server error:")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server holds references to cache and kad
|
|
|
|
type Server struct {
|
2018-11-27 22:50:52 +00:00
|
|
|
dht dht.DHT
|
|
|
|
cache *overlay.Cache
|
2018-12-04 18:47:58 +00:00
|
|
|
statdb *statdb.StatDB
|
2018-11-27 22:50:52 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
metrics *monkit.Registry
|
|
|
|
identity *provider.FullIdentity
|
2018-11-21 17:31:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:01:09 +00:00
|
|
|
// ---------------------
|
|
|
|
// Kad/Overlay commands:
|
|
|
|
// ---------------------
|
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
// CountNodes returns the number of nodes in the cache and in kademlia
|
|
|
|
func (srv *Server) CountNodes(ctx context.Context, req *pb.CountNodesRequest) (*pb.CountNodesResponse, error) {
|
2018-11-30 17:36:05 +00:00
|
|
|
overlayKeys, err := srv.cache.DB.List(nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-04 14:02:03 +00:00
|
|
|
kadNodes, err := srv.dht.GetNodes(ctx, srv.identity.ID, 0)
|
2018-12-03 20:01:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-30 17:36:05 +00:00
|
|
|
|
2018-11-21 17:31:27 +00:00
|
|
|
return &pb.CountNodesResponse{
|
2018-11-30 17:36:05 +00:00
|
|
|
Kademlia: int64(len(kadNodes)),
|
|
|
|
Overlay: int64(len(overlayKeys)),
|
2018-11-21 17:31:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuckets returns all kademlia buckets for current kademlia instance
|
|
|
|
func (srv *Server) GetBuckets(ctx context.Context, req *pb.GetBucketsRequest) (*pb.GetBucketsResponse, error) {
|
|
|
|
rt, err := srv.dht.GetRoutingTable(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.GetBucketsResponse{}, ServerError.Wrap(err)
|
|
|
|
}
|
|
|
|
b, err := rt.GetBucketIds()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-03 20:01:54 +00:00
|
|
|
// TODO(bryanchriswhite): should use bucketID type
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs, err := storj.NodeIDsFromBytes(b.ByteSlices())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-21 17:31:27 +00:00
|
|
|
return &pb.GetBucketsResponse{
|
|
|
|
Total: int64(len(b)),
|
2018-12-03 20:01:54 +00:00
|
|
|
// TODO(bryanchriswhite): should use bucketID type
|
|
|
|
Ids: nodeIDs,
|
2018-11-21 17:31:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucket retrieves all of a given K buckets contents
|
|
|
|
func (srv *Server) GetBucket(ctx context.Context, req *pb.GetBucketRequest) (*pb.GetBucketResponse, error) {
|
|
|
|
rt, err := srv.dht.GetRoutingTable(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-03 20:01:54 +00:00
|
|
|
// TODO(bryanchriswhite): should use bucketID type
|
2018-11-21 17:31:27 +00:00
|
|
|
bucket, ok := rt.GetBucket(req.Id)
|
|
|
|
if !ok {
|
|
|
|
return &pb.GetBucketResponse{}, ServerError.New("GetBuckets returned non-OK response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.GetBucketResponse{
|
|
|
|
Id: req.Id,
|
|
|
|
Nodes: bucket.Nodes(),
|
|
|
|
}, nil
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
|
2018-11-27 22:50:52 +00:00
|
|
|
// PingNode sends a PING RPC to the provided node ID in the Kad network.
|
|
|
|
func (srv *Server) PingNode(ctx context.Context, req *pb.PingNodeRequest) (*pb.PingNodeResponse, error) {
|
|
|
|
rt, err := srv.dht.GetRoutingTable(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.PingNodeResponse{}, ServerError.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
self := rt.Local()
|
|
|
|
|
|
|
|
nc, err := node.NewNodeClient(srv.identity, self, srv.dht)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.PingNodeResponse{}, ServerError.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := nc.Ping(ctx, pb.Node{
|
2018-11-29 14:57:00 +00:00
|
|
|
Id: req.Id,
|
|
|
|
Type: self.Type,
|
2018-11-27 22:50:52 +00:00
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: req.Address,
|
|
|
|
},
|
|
|
|
})
|
2018-11-30 17:36:05 +00:00
|
|
|
res := &pb.PingNodeResponse{Ok: p}
|
2018-11-27 22:50:52 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-11-30 17:36:05 +00:00
|
|
|
return res, ServerError.Wrap(err)
|
2018-11-27 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 17:36:05 +00:00
|
|
|
return res, nil
|
2018-11-27 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:39:28 +00:00
|
|
|
// LookupNode triggers a Kademlia lookup and returns the node the network found.
|
|
|
|
func (srv *Server) LookupNode(ctx context.Context, req *pb.LookupNodeRequest) (*pb.LookupNodeResponse, error) {
|
|
|
|
id, err := storj.NodeIDFromString(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.LookupNodeResponse{}, err
|
|
|
|
}
|
|
|
|
node, err := srv.dht.FindNode(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.LookupNodeResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.LookupNodeResponse{
|
|
|
|
Node: &pb.Node{
|
|
|
|
Id: node.Id,
|
|
|
|
},
|
|
|
|
Meta: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:01:09 +00:00
|
|
|
// ---------------------
|
|
|
|
// StatDB commands:
|
|
|
|
// ---------------------
|
|
|
|
|
|
|
|
// GetStats returns the stats for a particular node ID
|
|
|
|
func (srv *Server) GetStats(ctx context.Context, req *pb.GetStatsRequest) (*pb.GetStatsResponse, error) {
|
|
|
|
getReq := &statsproto.GetRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeId: req.NodeId,
|
2018-11-21 22:01:09 +00:00
|
|
|
}
|
|
|
|
res, err := srv.statdb.Get(ctx, getReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.GetStatsResponse{
|
2018-12-04 18:47:58 +00:00
|
|
|
AuditCount: res.Stats.AuditCount,
|
2018-11-21 22:01:09 +00:00
|
|
|
AuditRatio: res.Stats.AuditSuccessRatio,
|
2018-12-04 18:47:58 +00:00
|
|
|
UptimeCount: res.Stats.UptimeCount,
|
2018-11-21 22:01:09 +00:00
|
|
|
UptimeRatio: res.Stats.UptimeRatio,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateStats creates a node with specified stats
|
|
|
|
func (srv *Server) CreateStats(ctx context.Context, req *pb.CreateStatsRequest) (*pb.CreateStatsResponse, error) {
|
|
|
|
node := &statsproto.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: req.NodeId,
|
2018-11-21 22:01:09 +00:00
|
|
|
}
|
|
|
|
stats := &statsproto.NodeStats{
|
|
|
|
AuditCount: req.AuditCount,
|
|
|
|
AuditSuccessCount: req.AuditSuccessCount,
|
|
|
|
UptimeCount: req.UptimeCount,
|
|
|
|
UptimeSuccessCount: req.UptimeSuccessCount,
|
|
|
|
}
|
|
|
|
createReq := &statsproto.CreateRequest{
|
|
|
|
Node: node,
|
|
|
|
Stats: stats,
|
|
|
|
}
|
|
|
|
_, err := srv.statdb.Create(ctx, createReq)
|
2018-12-04 18:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-21 22:01:09 +00:00
|
|
|
|
2018-12-04 18:47:58 +00:00
|
|
|
return &pb.CreateStatsResponse{}, nil
|
2018-11-21 22:01:09 +00:00
|
|
|
}
|