2018-10-08 23:15:54 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-26 17:08:29 +00:00
|
|
|
package statdb_test
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-16 12:43:44 +01:00
|
|
|
"go.uber.org/zap"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-11-26 17:08:29 +00:00
|
|
|
"storj.io/storj/pkg/auth"
|
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-10-08 23:15:54 +01:00
|
|
|
dbx "storj.io/storj/pkg/statdb/dbx"
|
|
|
|
pb "storj.io/storj/pkg/statdb/proto"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-08 23:15:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-11-26 17:08:29 +00:00
|
|
|
apiKey = []byte("")
|
|
|
|
ctx = auth.WithAPIKey(context.Background(), apiKey)
|
2018-10-08 23:15:54 +01:00
|
|
|
)
|
|
|
|
|
2018-11-15 00:03:19 +00:00
|
|
|
func TestCreateDoesNotExist(t *testing.T) {
|
2018-10-08 23:15:54 +01:00
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
|
|
|
node := &pb.Node{Id: nodeID}
|
2018-10-08 23:15:54 +01:00
|
|
|
createReq := &pb.CreateRequest{
|
2018-11-26 17:08:29 +00:00
|
|
|
Node: node,
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
|
|
|
resp, err := statdb.Create(ctx, createReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
stats := resp.Stats
|
|
|
|
assert.EqualValues(t, 0, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, 0, stats.UptimeRatio)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeInfo, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID.Bytes()))
|
2018-10-08 23:15:54 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.EqualValues(t, nodeID.Bytes(), nodeInfo.Id)
|
2018-10-08 23:15:54 +01:00
|
|
|
assert.EqualValues(t, 0, nodeInfo.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, 0, nodeInfo.UptimeRatio)
|
|
|
|
}
|
|
|
|
|
2018-11-15 00:03:19 +00:00
|
|
|
func TestCreateExists(t *testing.T) {
|
2018-10-08 23:15:54 +01:00
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
node := &pb.Node{Id: nodeID}
|
2018-10-08 23:15:54 +01:00
|
|
|
createReq := &pb.CreateRequest{
|
2018-11-26 17:08:29 +00:00
|
|
|
Node: node,
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
|
|
|
_, err = statdb.Create(ctx, createReq)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2018-11-15 00:03:19 +00:00
|
|
|
func TestCreateWithStats(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
|
|
|
node := &pb.Node{Id: nodeID}
|
2018-11-15 00:03:19 +00:00
|
|
|
stats := &pb.NodeStats{
|
|
|
|
AuditCount: totalAuditCount,
|
|
|
|
AuditSuccessCount: auditSuccessCount,
|
|
|
|
UptimeCount: totalUptimeCount,
|
|
|
|
UptimeSuccessCount: uptimeSuccessCount,
|
|
|
|
}
|
|
|
|
createReq := &pb.CreateRequest{
|
2018-11-26 17:08:29 +00:00
|
|
|
Node: node,
|
|
|
|
Stats: stats,
|
2018-11-15 00:03:19 +00:00
|
|
|
}
|
|
|
|
resp, err := statdb.Create(ctx, createReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
s := resp.Stats
|
|
|
|
assert.EqualValues(t, auditRatio, s.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, uptimeRatio, s.UptimeRatio)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeInfo, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID.Bytes()))
|
2018-11-15 00:03:19 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.EqualValues(t, nodeID.Bytes(), nodeInfo.Id)
|
2018-11-15 00:03:19 +00:00
|
|
|
assert.EqualValues(t, auditRatio, nodeInfo.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, uptimeRatio, nodeInfo.UptimeRatio)
|
|
|
|
}
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
func TestGetExists(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
|
|
|
|
|
|
|
|
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
getReq := &pb.GetRequest{
|
|
|
|
NodeId: nodeID,
|
|
|
|
}
|
|
|
|
resp, err := statdb.Get(ctx, getReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
stats := resp.Stats
|
|
|
|
assert.EqualValues(t, auditRatio, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, uptimeRatio, stats.UptimeRatio)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDoesNotExist(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, _, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
getReq := &pb.GetRequest{
|
|
|
|
NodeId: nodeID,
|
|
|
|
}
|
|
|
|
_, err = statdb.Get(ctx, getReq)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:11:22 +00:00
|
|
|
func TestFindValidNodes(t *testing.T) {
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeIDs := teststorj.NodeIDsFromStrings("id1", "id2", "id3", "id4", "id5", "id6", "id7")
|
2018-10-30 17:11:22 +00:00
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tt := range []struct {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID storj.NodeID
|
2018-10-30 17:11:22 +00:00
|
|
|
auditSuccessCount int64
|
|
|
|
totalAuditCount int64
|
|
|
|
auditRatio float64
|
|
|
|
uptimeSuccessCount int64
|
|
|
|
totalUptimeCount int64
|
|
|
|
uptimeRatio float64
|
|
|
|
}{
|
2018-11-29 18:39:27 +00:00
|
|
|
{NodeIDs[0], 10, 20, 0.5, 10, 20, 0.5}, // bad ratios
|
|
|
|
{NodeIDs[1], 20, 20, 1, 20, 20, 1}, // good ratios
|
|
|
|
{NodeIDs[2], 20, 20, 1, 10, 20, 0.5}, // good audit success bad uptime
|
|
|
|
{NodeIDs[3], 10, 20, 0.5, 20, 20, 1}, // good uptime bad audit success
|
|
|
|
{NodeIDs[4], 5, 5, 1, 5, 5, 1}, // good ratios not enough audits
|
|
|
|
{NodeIDs[5], 20, 20, 1, 20, 20, 1}, // good ratios, excluded from query
|
|
|
|
{NodeIDs[6], 19, 20, 0.95, 19, 20, 0.95}, // borderline ratios
|
2018-10-30 17:11:22 +00:00
|
|
|
} {
|
|
|
|
err = createNode(ctx, db, tt.nodeID, tt.auditSuccessCount, tt.totalAuditCount, tt.auditRatio,
|
|
|
|
tt.uptimeSuccessCount, tt.totalUptimeCount, tt.uptimeRatio)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
findValidNodesReq := &pb.FindValidNodesRequest{
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeIds: storj.NodeIDList{
|
|
|
|
NodeIDs[0], NodeIDs[1],
|
|
|
|
NodeIDs[2], NodeIDs[3],
|
|
|
|
NodeIDs[4], NodeIDs[6],
|
2018-10-30 17:11:22 +00:00
|
|
|
},
|
|
|
|
MinStats: &pb.NodeStats{
|
|
|
|
AuditSuccessRatio: 0.95,
|
|
|
|
UptimeRatio: 0.95,
|
|
|
|
AuditCount: 15,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := statdb.FindValidNodes(ctx, findValidNodesReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
passed := resp.PassedIds
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.Contains(t, passed, NodeIDs[1])
|
|
|
|
assert.Contains(t, passed, NodeIDs[6])
|
2018-10-30 17:11:22 +00:00
|
|
|
assert.Len(t, passed, 2)
|
|
|
|
}
|
|
|
|
|
2018-10-08 23:15:54 +01:00
|
|
|
func TestUpdateExists(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID,
|
2018-10-08 23:15:54 +01:00
|
|
|
UpdateAuditSuccess: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
UpdateUptime: true,
|
|
|
|
IsUp: false,
|
|
|
|
}
|
|
|
|
updateReq := &pb.UpdateRequest{
|
2018-11-26 17:08:29 +00:00
|
|
|
Node: node,
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
|
|
|
resp, err := statdb.Update(ctx, updateReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, newAuditRatio := getRatio(int(auditSuccessCount+1), int(totalAuditCount+1))
|
|
|
|
_, _, newUptimeRatio := getRatio(int(uptimeSuccessCount), int(totalUptimeCount+1))
|
|
|
|
stats := resp.Stats
|
|
|
|
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:08:29 +00:00
|
|
|
func TestUpdateUptimeExists(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
2018-11-26 17:08:29 +00:00
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID,
|
|
|
|
IsUp: false,
|
2018-11-26 17:08:29 +00:00
|
|
|
}
|
|
|
|
updateReq := &pb.UpdateUptimeRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
resp, err := statdb.UpdateUptime(ctx, updateReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, newUptimeRatio := getRatio(int(uptimeSuccessCount), int(totalUptimeCount+1))
|
|
|
|
stats := resp.Stats
|
|
|
|
assert.EqualValues(t, auditRatio, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, totalAuditCount, stats.AuditCount)
|
|
|
|
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateAuditSuccessExists(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString("testnodeid")
|
2018-11-26 17:08:29 +00:00
|
|
|
|
|
|
|
auditSuccessCount, totalAuditCount, auditRatio := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID, auditSuccessCount, totalAuditCount, auditRatio,
|
|
|
|
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID,
|
2018-11-26 17:08:29 +00:00
|
|
|
AuditSuccess: false,
|
|
|
|
}
|
|
|
|
updateReq := &pb.UpdateAuditSuccessRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
resp, err := statdb.UpdateAuditSuccess(ctx, updateReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, newAuditRatio := getRatio(int(auditSuccessCount), int(totalAuditCount+1))
|
|
|
|
stats := resp.Stats
|
|
|
|
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, totalAuditCount+1, stats.AuditCount)
|
|
|
|
assert.EqualValues(t, uptimeRatio, stats.UptimeRatio)
|
|
|
|
}
|
|
|
|
|
2018-10-08 23:15:54 +01:00
|
|
|
func TestUpdateBatchExists(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID1 := teststorj.NodeIDFromString("testnodeid1")
|
|
|
|
nodeID2 := teststorj.NodeIDFromString("testnodeid2")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
auditSuccessCount1, totalAuditCount1, auditRatio1 := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1 := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID1, auditSuccessCount1, totalAuditCount1, auditRatio1,
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
auditSuccessCount2, totalAuditCount2, auditRatio2 := getRatio(7, 10)
|
|
|
|
uptimeSuccessCount2, totalUptimeCount2, uptimeRatio2 := getRatio(8, 20)
|
|
|
|
err = createNode(ctx, db, nodeID2, auditSuccessCount2, totalAuditCount2, auditRatio2,
|
|
|
|
uptimeSuccessCount2, totalUptimeCount2, uptimeRatio2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node1 := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID1,
|
2018-10-08 23:15:54 +01:00
|
|
|
UpdateAuditSuccess: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
UpdateUptime: true,
|
|
|
|
IsUp: false,
|
|
|
|
}
|
|
|
|
node2 := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID2,
|
2018-10-08 23:15:54 +01:00
|
|
|
UpdateAuditSuccess: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
UpdateUptime: false,
|
|
|
|
}
|
|
|
|
updateBatchReq := &pb.UpdateBatchRequest{
|
|
|
|
NodeList: []*pb.Node{node1, node2},
|
|
|
|
}
|
|
|
|
resp, err := statdb.UpdateBatch(ctx, updateBatchReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, newAuditRatio1 := getRatio(int(auditSuccessCount1+1), int(totalAuditCount1+1))
|
|
|
|
_, _, newUptimeRatio1 := getRatio(int(uptimeSuccessCount1), int(totalUptimeCount1+1))
|
|
|
|
_, _, newAuditRatio2 := getRatio(int(auditSuccessCount2+1), int(totalAuditCount2+1))
|
|
|
|
stats1 := resp.StatsList[0]
|
|
|
|
stats2 := resp.StatsList[1]
|
|
|
|
assert.EqualValues(t, newAuditRatio1, stats1.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, newUptimeRatio1, stats1.UptimeRatio)
|
|
|
|
assert.EqualValues(t, newAuditRatio2, stats2.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, uptimeRatio2, stats2.UptimeRatio)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateBatchDoesNotExist(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID1 := teststorj.NodeIDFromString("testnodeid1")
|
|
|
|
nodeID2 := teststorj.NodeIDFromString("testnodeid2")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
auditSuccessCount1, totalAuditCount1, auditRatio1 := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1 := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID1, auditSuccessCount1, totalAuditCount1, auditRatio1,
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
node1 := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID1,
|
2018-10-08 23:15:54 +01:00
|
|
|
UpdateAuditSuccess: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
UpdateUptime: true,
|
|
|
|
IsUp: false,
|
|
|
|
}
|
|
|
|
node2 := &pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: nodeID2,
|
2018-10-08 23:15:54 +01:00
|
|
|
UpdateAuditSuccess: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
UpdateUptime: false,
|
|
|
|
}
|
|
|
|
updateBatchReq := &pb.UpdateBatchRequest{
|
|
|
|
NodeList: []*pb.Node{node1, node2},
|
|
|
|
}
|
|
|
|
_, err = statdb.UpdateBatch(ctx, updateBatchReq)
|
2018-10-16 18:40:34 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateBatchEmpty(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID1 := teststorj.NodeIDFromString("testnodeid1")
|
2018-10-08 23:15:54 +01:00
|
|
|
|
|
|
|
auditSuccessCount1, totalAuditCount1, auditRatio1 := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1 := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID1, auditSuccessCount1, totalAuditCount1, auditRatio1,
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
updateBatchReq := &pb.UpdateBatchRequest{
|
|
|
|
NodeList: []*pb.Node{},
|
|
|
|
}
|
|
|
|
resp, err := statdb.UpdateBatch(ctx, updateBatchReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(resp.StatsList), 0)
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:40:34 +01:00
|
|
|
func TestCreateEntryIfNotExists(t *testing.T) {
|
|
|
|
dbPath := getDBPath()
|
|
|
|
statdb, db, err := getServerAndDB(dbPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID1 := teststorj.NodeIDFromString("testnodeid1")
|
|
|
|
nodeID2 := teststorj.NodeIDFromString("testnodeid2")
|
2018-10-16 18:40:34 +01:00
|
|
|
|
|
|
|
auditSuccessCount1, totalAuditCount1, auditRatio1 := getRatio(4, 10)
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1 := getRatio(8, 25)
|
|
|
|
err = createNode(ctx, db, nodeID1, auditSuccessCount1, totalAuditCount1, auditRatio1,
|
|
|
|
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
node1 := &pb.Node{Id: nodeID1}
|
2018-10-16 18:40:34 +01:00
|
|
|
createIfNotExistsReq1 := &pb.CreateEntryIfNotExistsRequest{
|
2018-11-26 17:08:29 +00:00
|
|
|
Node: node1,
|
2018-10-16 18:40:34 +01:00
|
|
|
}
|
|
|
|
_, err = statdb.CreateEntryIfNotExists(ctx, createIfNotExistsReq1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeInfo1, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID1.Bytes()))
|
2018-11-15 00:03:19 +00:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.EqualValues(t, nodeID1.Bytes(), nodeInfo1.Id)
|
2018-11-15 00:03:19 +00:00
|
|
|
assert.EqualValues(t, auditRatio1, nodeInfo1.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, uptimeRatio1, nodeInfo1.UptimeRatio)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
node2 := &pb.Node{Id: nodeID2}
|
2018-10-16 18:40:34 +01:00
|
|
|
createIfNotExistsReq2 := &pb.CreateEntryIfNotExistsRequest{
|
2018-11-26 17:08:29 +00:00
|
|
|
Node: node2,
|
2018-10-16 18:40:34 +01:00
|
|
|
}
|
|
|
|
_, err = statdb.CreateEntryIfNotExists(ctx, createIfNotExistsReq2)
|
|
|
|
assert.NoError(t, err)
|
2018-11-15 00:03:19 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeInfo2, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID2.Bytes()))
|
2018-11-15 00:03:19 +00:00
|
|
|
assert.NoError(t, err)
|
2018-11-29 18:39:27 +00:00
|
|
|
assert.EqualValues(t, nodeID2.Bytes(), nodeInfo2.Id)
|
2018-11-15 00:03:19 +00:00
|
|
|
assert.EqualValues(t, 0, nodeInfo2.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, 0, nodeInfo2.UptimeRatio)
|
2018-10-16 18:40:34 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 23:15:54 +01:00
|
|
|
func getDBPath() string {
|
|
|
|
return fmt.Sprintf("file:memdb%d?mode=memory&cache=shared", rand.Int63())
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:08:29 +00:00
|
|
|
func getServerAndDB(path string) (sdb *statdb.Server, db *dbx.DB, err error) {
|
|
|
|
sdb, err = statdb.NewServer("sqlite3", path, string(apiKey), zap.NewNop())
|
2018-10-08 23:15:54 +01:00
|
|
|
if err != nil {
|
2018-11-26 17:08:29 +00:00
|
|
|
return &statdb.Server{}, &dbx.DB{}, err
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
|
|
|
db, err = dbx.Open("sqlite3", path)
|
|
|
|
if err != nil {
|
2018-11-26 17:08:29 +00:00
|
|
|
return &statdb.Server{}, &dbx.DB{}, err
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
2018-11-26 17:08:29 +00:00
|
|
|
return sdb, db, err
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
func createNode(ctx context.Context, db *dbx.DB, nodeID storj.NodeID,
|
2018-10-08 23:15:54 +01:00
|
|
|
auditSuccessCount, totalAuditCount int64, auditRatio float64,
|
|
|
|
uptimeSuccessCount, totalUptimeCount int64, uptimeRatio float64) error {
|
|
|
|
_, err := db.Create_Node(
|
|
|
|
ctx,
|
2018-11-29 18:39:27 +00:00
|
|
|
dbx.Node_Id(nodeID.Bytes()),
|
2018-10-08 23:15:54 +01:00
|
|
|
dbx.Node_AuditSuccessCount(auditSuccessCount),
|
|
|
|
dbx.Node_TotalAuditCount(totalAuditCount),
|
|
|
|
dbx.Node_AuditSuccessRatio(auditRatio),
|
|
|
|
dbx.Node_UptimeSuccessCount(uptimeSuccessCount),
|
|
|
|
dbx.Node_TotalUptimeCount(totalUptimeCount),
|
|
|
|
dbx.Node_UptimeRatio(uptimeRatio),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRatio(s, t int) (success, total int64, ratio float64) {
|
|
|
|
ratio = float64(s) / float64(t)
|
|
|
|
return int64(s), int64(t), ratio
|
|
|
|
}
|