2018-07-27 22:11:44 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package statdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-10-08 23:15:54 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-07-27 22:11:44 +01:00
|
|
|
|
2018-09-10 15:23:08 +01:00
|
|
|
"storj.io/storj/pkg/pointerdb/auth"
|
2018-07-27 22:11:44 +01:00
|
|
|
dbx "storj.io/storj/pkg/statdb/dbx"
|
|
|
|
pb "storj.io/storj/pkg/statdb/proto"
|
|
|
|
)
|
|
|
|
|
2018-10-08 23:15:54 +01:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2018-07-27 22:11:44 +01:00
|
|
|
// Server implements the statdb RPC service
|
|
|
|
type Server struct {
|
|
|
|
DB *dbx.DB
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
|
|
|
func NewServer(driver, source string, logger *zap.Logger) (*Server, error) {
|
|
|
|
db, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(db.Schema())
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "already exists") {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) validateAuth(APIKeyBytes []byte) error {
|
|
|
|
if !auth.ValidateAPIKey(string(APIKeyBytes)) {
|
2018-08-27 18:28:16 +01:00
|
|
|
s.logger.Error("unauthorized request: ", zap.Error(status.Errorf(codes.Unauthenticated, "Invalid API credential")))
|
|
|
|
return status.Errorf(codes.Unauthenticated, "Invalid API credential")
|
2018-07-27 22:11:44 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-25 02:52:58 +01:00
|
|
|
// Create a db entry for the provided storagenode
|
2018-07-27 22:11:44 +01:00
|
|
|
func (s *Server) Create(ctx context.Context, createReq *pb.CreateRequest) (resp *pb.CreateResponse, err error) {
|
2018-10-08 23:15:54 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-27 22:11:44 +01:00
|
|
|
s.logger.Debug("entering statdb Create")
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
APIKeyBytes := createReq.APIKey
|
2018-07-27 22:11:44 +01:00
|
|
|
if err := s.validateAuth(APIKeyBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node := createReq.Node
|
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditSuccessRatio := initRatioVars(node.UpdateAuditSuccess, node.AuditSuccess)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := initRatioVars(node.UpdateUptime, node.IsUp)
|
|
|
|
|
|
|
|
dbNode, err := s.DB.Create_Node(
|
|
|
|
ctx,
|
|
|
|
dbx.Node_Id(string(node.NodeId)),
|
|
|
|
dbx.Node_AuditSuccessCount(auditSuccessCount),
|
|
|
|
dbx.Node_TotalAuditCount(totalAuditCount),
|
|
|
|
dbx.Node_AuditSuccessRatio(auditSuccessRatio),
|
|
|
|
dbx.Node_UptimeSuccessCount(uptimeSuccessCount),
|
|
|
|
dbx.Node_TotalUptimeCount(totalUptimeCount),
|
|
|
|
dbx.Node_UptimeRatio(uptimeRatio),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
s.logger.Debug("created in the db: " + string(node.NodeId))
|
|
|
|
|
|
|
|
nodeStats := &pb.NodeStats{
|
|
|
|
NodeId: []byte(dbNode.Id),
|
|
|
|
AuditSuccessRatio: dbNode.AuditSuccessRatio,
|
|
|
|
UptimeRatio: dbNode.UptimeRatio,
|
|
|
|
}
|
|
|
|
return &pb.CreateResponse{
|
|
|
|
Stats: nodeStats,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-25 02:52:58 +01:00
|
|
|
// Get a storagenode's stats from the db
|
2018-07-27 22:11:44 +01:00
|
|
|
func (s *Server) Get(ctx context.Context, getReq *pb.GetRequest) (resp *pb.GetResponse, err error) {
|
2018-10-08 23:15:54 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-27 22:11:44 +01:00
|
|
|
s.logger.Debug("entering statdb Get")
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
APIKeyBytes := getReq.APIKey
|
2018-07-27 22:11:44 +01:00
|
|
|
err = s.validateAuth(APIKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbNode, err := s.DB.Get_Node_By_Id(ctx, dbx.Node_Id(string(getReq.NodeId)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeStats := &pb.NodeStats{
|
|
|
|
NodeId: []byte(dbNode.Id),
|
|
|
|
AuditSuccessRatio: dbNode.AuditSuccessRatio,
|
|
|
|
UptimeRatio: dbNode.UptimeRatio,
|
|
|
|
}
|
|
|
|
return &pb.GetResponse{
|
|
|
|
Stats: nodeStats,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-25 02:52:58 +01:00
|
|
|
// Update a single storagenode's stats in the db
|
2018-07-27 22:11:44 +01:00
|
|
|
func (s *Server) Update(ctx context.Context, updateReq *pb.UpdateRequest) (resp *pb.UpdateResponse, err error) {
|
2018-10-08 23:15:54 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-27 22:11:44 +01:00
|
|
|
s.logger.Debug("entering statdb Update")
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
APIKeyBytes := updateReq.APIKey
|
2018-07-27 22:11:44 +01:00
|
|
|
err = s.validateAuth(APIKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node := updateReq.Node
|
|
|
|
|
|
|
|
dbNode, err := s.DB.Get_Node_By_Id(ctx, dbx.Node_Id(string(node.NodeId)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
auditSuccessCount := dbNode.AuditSuccessCount
|
|
|
|
totalAuditCount := dbNode.TotalAuditCount
|
2018-08-27 18:28:16 +01:00
|
|
|
var auditSuccessRatio float64
|
2018-10-08 23:15:54 +01:00
|
|
|
uptimeSuccessCount := dbNode.UptimeSuccessCount
|
2018-07-27 22:11:44 +01:00
|
|
|
totalUptimeCount := dbNode.TotalUptimeCount
|
2018-08-27 18:28:16 +01:00
|
|
|
var uptimeRatio float64
|
2018-07-27 22:11:44 +01:00
|
|
|
|
|
|
|
updateFields := dbx.Node_Update_Fields{}
|
|
|
|
|
|
|
|
if node.UpdateAuditSuccess {
|
|
|
|
auditSuccessCount, totalAuditCount, auditSuccessRatio = updateRatioVars(
|
|
|
|
node.AuditSuccess,
|
|
|
|
auditSuccessCount,
|
|
|
|
totalAuditCount,
|
|
|
|
)
|
|
|
|
|
|
|
|
updateFields.AuditSuccessCount = dbx.Node_AuditSuccessCount(auditSuccessCount)
|
|
|
|
updateFields.TotalAuditCount = dbx.Node_TotalAuditCount(totalAuditCount)
|
|
|
|
updateFields.AuditSuccessRatio = dbx.Node_AuditSuccessRatio(auditSuccessRatio)
|
|
|
|
}
|
|
|
|
if node.UpdateUptime {
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio = updateRatioVars(
|
|
|
|
node.IsUp,
|
|
|
|
uptimeSuccessCount,
|
|
|
|
totalUptimeCount,
|
|
|
|
)
|
|
|
|
|
|
|
|
updateFields.UptimeSuccessCount = dbx.Node_UptimeSuccessCount(uptimeSuccessCount)
|
|
|
|
updateFields.TotalUptimeCount = dbx.Node_TotalUptimeCount(totalUptimeCount)
|
|
|
|
updateFields.UptimeRatio = dbx.Node_UptimeRatio(uptimeRatio)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbNode, err = s.DB.Update_Node_By_Id(ctx, dbx.Node_Id(string(node.NodeId)), updateFields)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeStats := &pb.NodeStats{
|
|
|
|
NodeId: []byte(dbNode.Id),
|
|
|
|
AuditSuccessRatio: dbNode.AuditSuccessRatio,
|
|
|
|
UptimeRatio: dbNode.UptimeRatio,
|
|
|
|
}
|
|
|
|
return &pb.UpdateResponse{
|
|
|
|
Stats: nodeStats,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateBatch for updating multiple farmers' stats in the db
|
|
|
|
func (s *Server) UpdateBatch(ctx context.Context, updateBatchReq *pb.UpdateBatchRequest) (resp *pb.UpdateBatchResponse, err error) {
|
2018-10-08 23:15:54 +01:00
|
|
|
// todo(moby) how should we handle one node failing to update but not all?
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-27 22:11:44 +01:00
|
|
|
s.logger.Debug("entering statdb UpdateBatch")
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
APIKeyBytes := updateBatchReq.APIKey
|
2018-07-27 22:11:44 +01:00
|
|
|
nodeStatsList := make([]*pb.NodeStats, len(updateBatchReq.NodeList))
|
|
|
|
for i, node := range updateBatchReq.NodeList {
|
|
|
|
updateReq := &pb.UpdateRequest{
|
|
|
|
Node: node,
|
|
|
|
APIKey: APIKeyBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRes, err := s.Update(ctx, updateReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeStatsList[i] = updateRes.Stats
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBatchRes := &pb.UpdateBatchResponse{
|
|
|
|
StatsList: nodeStatsList,
|
|
|
|
}
|
|
|
|
return updateBatchRes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initRatioVars(shouldUpdate, status bool) (int64, int64, float64) {
|
|
|
|
var (
|
|
|
|
successCount int64
|
|
|
|
totalCount int64
|
|
|
|
ratio float64
|
|
|
|
)
|
|
|
|
|
|
|
|
if shouldUpdate {
|
|
|
|
return updateRatioVars(status, successCount, totalCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return successCount, totalCount, ratio
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateRatioVars(newStatus bool, successCount, totalCount int64) (int64, int64, float64) {
|
|
|
|
totalCount++
|
|
|
|
if newStatus {
|
|
|
|
successCount++
|
|
|
|
}
|
|
|
|
newRatio := float64(successCount) / float64(totalCount)
|
|
|
|
return successCount, totalCount, newRatio
|
|
|
|
}
|