2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-19 23:57:22 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-23 12:07:22 +00:00
|
|
|
"sync/atomic"
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-07-19 15:48:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the grpc Node Server
|
|
|
|
type Server struct {
|
2018-12-12 15:40:33 +00:00
|
|
|
dht dht.DHT
|
|
|
|
log *zap.Logger
|
2019-01-23 12:07:22 +00:00
|
|
|
|
|
|
|
connected int32
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a newly instantiated Node Server
|
2018-12-12 15:40:33 +00:00
|
|
|
func NewServer(log *zap.Logger, dht dht.DHT) *Server {
|
2018-10-08 16:09:37 +01:00
|
|
|
return &Server{
|
2018-12-12 15:40:33 +00:00
|
|
|
dht: dht,
|
|
|
|
log: log,
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query is a node to node communication query
|
2018-12-12 15:40:33 +00:00
|
|
|
func (server *Server) Query(ctx context.Context, req *pb.QueryRequest) (*pb.QueryResponse, error) {
|
|
|
|
rt, err := server.dht.GetRoutingTable(ctx)
|
2018-08-23 16:20:11 +01:00
|
|
|
if err != nil {
|
2018-12-12 15:40:33 +00:00
|
|
|
return &pb.QueryResponse{}, NodeClientErr.New("could not get routing table %server", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
|
|
|
if req.GetPingback() {
|
2018-12-12 15:40:33 +00:00
|
|
|
_, err = server.dht.Ping(ctx, *req.Sender)
|
2018-08-23 16:20:11 +01:00
|
|
|
if err != nil {
|
2019-01-18 15:03:19 +00:00
|
|
|
server.log.Debug("connection to node failed", zap.Error(err), zap.String("nodeID", req.Sender.Id.String()))
|
2018-10-08 16:09:37 +01:00
|
|
|
err = rt.ConnectionFailed(req.Sender)
|
|
|
|
if err != nil {
|
2018-12-12 15:40:33 +00:00
|
|
|
server.log.Error("could not respond to connection failed", zap.Error(err))
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
2019-01-18 15:03:19 +00:00
|
|
|
} else {
|
|
|
|
err = rt.ConnectionSuccess(req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
server.log.Error("could not respond to connection success", zap.Error(err))
|
|
|
|
} else {
|
2019-01-23 12:07:22 +00:00
|
|
|
count := atomic.AddInt32(&server.connected, 1)
|
|
|
|
if count == 1 {
|
|
|
|
server.log.Sugar().Debugf("Successfully connected with %s", req.Sender.Address.Address)
|
|
|
|
} else if count%100 == 0 {
|
|
|
|
server.log.Sugar().Debugf("Successfully connected with %s %dx times", req.Sender.Address.Address, count)
|
|
|
|
}
|
2019-01-18 15:03:19 +00:00
|
|
|
}
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err := rt.FindNear(req.Target.Id, int(req.Limit))
|
2018-08-23 16:20:11 +01:00
|
|
|
if err != nil {
|
2018-12-12 15:40:33 +00:00
|
|
|
return &pb.QueryResponse{}, NodeClientErr.New("could not find near %server", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
|
|
|
return &pb.QueryResponse{Sender: req.Sender, Response: nodes}, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-10-30 18:04:29 +00:00
|
|
|
|
|
|
|
// Ping provides an easy way to verify a node is online and accepting requests
|
2018-12-12 15:40:33 +00:00
|
|
|
func (server *Server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) {
|
2018-11-08 16:18:28 +00:00
|
|
|
//TODO
|
2018-10-30 18:04:29 +00:00
|
|
|
return &pb.PingResponse{}, nil
|
|
|
|
}
|