Update statdb (#650)
* add sdbclient.UpdateUptime; update args for sdbclient.CreateEntryIfNotExists * add auditcount to node stats; restructure statdb.CreateEntryIfNotExists * add noop mock sdbclient * add the ability to create a node in statdb without "default" stats * update statdb.CreateEntryIfNotExists * take fewer args for sdbclient.CreateWithStats/FindValidNodes * add sdbclient.UpdateAuditSuccess * update sdbclient.Update so that all fields are updated when called (reduce args) * update error checking in statdb.Create
This commit is contained in:
parent
d3c347a0ac
commit
2c36dcc2c8
@ -94,8 +94,7 @@ func main() {
|
||||
farmer1.UpdateAuditSuccess = true
|
||||
farmer1.UpdateUptime = true
|
||||
|
||||
nodeStats, err := client.Update(ctx, farmer1.NodeId, farmer1.AuditSuccess, farmer1.IsUp, nil,
|
||||
farmer1.UpdateAuditSuccess, farmer1.UpdateUptime, false)
|
||||
nodeStats, err := client.Update(ctx, farmer1.NodeId, farmer1.AuditSuccess, farmer1.IsUp, nil)
|
||||
if err != nil || status.Code(err) == codes.Internal {
|
||||
logger.Error("failed to update", zap.Error(err))
|
||||
os.Exit(1)
|
||||
|
@ -41,7 +41,7 @@ func (m *Node) Reset() { *m = Node{} }
|
||||
func (m *Node) String() string { return proto.CompactTextString(m) }
|
||||
func (*Node) ProtoMessage() {}
|
||||
func (*Node) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{0}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{0}
|
||||
}
|
||||
func (m *Node) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Node.Unmarshal(m, b)
|
||||
@ -117,6 +117,9 @@ type NodeStats struct {
|
||||
AuditSuccessRatio float64 `protobuf:"fixed64,3,opt,name=audit_success_ratio,json=auditSuccessRatio,proto3" json:"audit_success_ratio,omitempty"`
|
||||
UptimeRatio float64 `protobuf:"fixed64,4,opt,name=uptime_ratio,json=uptimeRatio,proto3" json:"uptime_ratio,omitempty"`
|
||||
AuditCount int64 `protobuf:"varint,5,opt,name=audit_count,json=auditCount,proto3" json:"audit_count,omitempty"`
|
||||
AuditSuccessCount int64 `protobuf:"varint,6,opt,name=audit_success_count,json=auditSuccessCount,proto3" json:"audit_success_count,omitempty"`
|
||||
UptimeCount int64 `protobuf:"varint,7,opt,name=uptime_count,json=uptimeCount,proto3" json:"uptime_count,omitempty"`
|
||||
UptimeSuccessCount int64 `protobuf:"varint,8,opt,name=uptime_success_count,json=uptimeSuccessCount,proto3" json:"uptime_success_count,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
@ -126,7 +129,7 @@ func (m *NodeStats) Reset() { *m = NodeStats{} }
|
||||
func (m *NodeStats) String() string { return proto.CompactTextString(m) }
|
||||
func (*NodeStats) ProtoMessage() {}
|
||||
func (*NodeStats) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{1}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{1}
|
||||
}
|
||||
func (m *NodeStats) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_NodeStats.Unmarshal(m, b)
|
||||
@ -181,20 +184,42 @@ func (m *NodeStats) GetAuditCount() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *NodeStats) GetAuditSuccessCount() int64 {
|
||||
if m != nil {
|
||||
return m.AuditSuccessCount
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *NodeStats) GetUptimeCount() int64 {
|
||||
if m != nil {
|
||||
return m.UptimeCount
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *NodeStats) GetUptimeSuccessCount() int64 {
|
||||
if m != nil {
|
||||
return m.UptimeSuccessCount
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// CreateRequest is a request message for the Create rpc call
|
||||
type CreateRequest struct {
|
||||
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
|
||||
APIKey []byte `protobuf:"bytes,2,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
Node *Node `protobuf:"bytes,1,opt,name=node,proto3" json:"node,omitempty"`
|
||||
Stats *NodeStats `protobuf:"bytes,2,opt,name=stats,proto3" json:"stats,omitempty"`
|
||||
APIKey []byte `protobuf:"bytes,3,opt,name=APIKey,proto3" json:"APIKey,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *CreateRequest) Reset() { *m = CreateRequest{} }
|
||||
func (m *CreateRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateRequest) ProtoMessage() {}
|
||||
func (*CreateRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{2}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{2}
|
||||
}
|
||||
func (m *CreateRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_CreateRequest.Unmarshal(m, b)
|
||||
@ -221,6 +246,13 @@ func (m *CreateRequest) GetNode() *Node {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *CreateRequest) GetStats() *NodeStats {
|
||||
if m != nil {
|
||||
return m.Stats
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *CreateRequest) GetAPIKey() []byte {
|
||||
if m != nil {
|
||||
return m.APIKey
|
||||
@ -240,7 +272,7 @@ func (m *CreateResponse) Reset() { *m = CreateResponse{} }
|
||||
func (m *CreateResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateResponse) ProtoMessage() {}
|
||||
func (*CreateResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{3}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{3}
|
||||
}
|
||||
func (m *CreateResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_CreateResponse.Unmarshal(m, b)
|
||||
@ -280,7 +312,7 @@ func (m *GetRequest) Reset() { *m = GetRequest{} }
|
||||
func (m *GetRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetRequest) ProtoMessage() {}
|
||||
func (*GetRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{4}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{4}
|
||||
}
|
||||
func (m *GetRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_GetRequest.Unmarshal(m, b)
|
||||
@ -326,7 +358,7 @@ func (m *GetResponse) Reset() { *m = GetResponse{} }
|
||||
func (m *GetResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetResponse) ProtoMessage() {}
|
||||
func (*GetResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{5}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{5}
|
||||
}
|
||||
func (m *GetResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_GetResponse.Unmarshal(m, b)
|
||||
@ -367,7 +399,7 @@ func (m *FindValidNodesRequest) Reset() { *m = FindValidNodesRequest{} }
|
||||
func (m *FindValidNodesRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*FindValidNodesRequest) ProtoMessage() {}
|
||||
func (*FindValidNodesRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{6}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{6}
|
||||
}
|
||||
func (m *FindValidNodesRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_FindValidNodesRequest.Unmarshal(m, b)
|
||||
@ -421,7 +453,7 @@ func (m *FindValidNodesResponse) Reset() { *m = FindValidNodesResponse{}
|
||||
func (m *FindValidNodesResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*FindValidNodesResponse) ProtoMessage() {}
|
||||
func (*FindValidNodesResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{7}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{7}
|
||||
}
|
||||
func (m *FindValidNodesResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_FindValidNodesResponse.Unmarshal(m, b)
|
||||
@ -468,7 +500,7 @@ func (m *UpdateRequest) Reset() { *m = UpdateRequest{} }
|
||||
func (m *UpdateRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*UpdateRequest) ProtoMessage() {}
|
||||
func (*UpdateRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{8}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{8}
|
||||
}
|
||||
func (m *UpdateRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_UpdateRequest.Unmarshal(m, b)
|
||||
@ -514,7 +546,7 @@ func (m *UpdateResponse) Reset() { *m = UpdateResponse{} }
|
||||
func (m *UpdateResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*UpdateResponse) ProtoMessage() {}
|
||||
func (*UpdateResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{9}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{9}
|
||||
}
|
||||
func (m *UpdateResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_UpdateResponse.Unmarshal(m, b)
|
||||
@ -554,7 +586,7 @@ func (m *UpdateBatchRequest) Reset() { *m = UpdateBatchRequest{} }
|
||||
func (m *UpdateBatchRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*UpdateBatchRequest) ProtoMessage() {}
|
||||
func (*UpdateBatchRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{10}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{10}
|
||||
}
|
||||
func (m *UpdateBatchRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_UpdateBatchRequest.Unmarshal(m, b)
|
||||
@ -601,7 +633,7 @@ func (m *UpdateBatchResponse) Reset() { *m = UpdateBatchResponse{} }
|
||||
func (m *UpdateBatchResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*UpdateBatchResponse) ProtoMessage() {}
|
||||
func (*UpdateBatchResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{11}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{11}
|
||||
}
|
||||
func (m *UpdateBatchResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_UpdateBatchResponse.Unmarshal(m, b)
|
||||
@ -648,7 +680,7 @@ func (m *CreateEntryIfNotExistsRequest) Reset() { *m = CreateEntryIfNotE
|
||||
func (m *CreateEntryIfNotExistsRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateEntryIfNotExistsRequest) ProtoMessage() {}
|
||||
func (*CreateEntryIfNotExistsRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{12}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{12}
|
||||
}
|
||||
func (m *CreateEntryIfNotExistsRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_CreateEntryIfNotExistsRequest.Unmarshal(m, b)
|
||||
@ -694,7 +726,7 @@ func (m *CreateEntryIfNotExistsResponse) Reset() { *m = CreateEntryIfNot
|
||||
func (m *CreateEntryIfNotExistsResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateEntryIfNotExistsResponse) ProtoMessage() {}
|
||||
func (*CreateEntryIfNotExistsResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_statdb_5a3c71d6f7d0333f, []int{13}
|
||||
return fileDescriptor_statdb_b838e72ff4f67ff3, []int{13}
|
||||
}
|
||||
func (m *CreateEntryIfNotExistsResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_CreateEntryIfNotExistsResponse.Unmarshal(m, b)
|
||||
@ -987,51 +1019,53 @@ var _StatDB_serviceDesc = grpc.ServiceDesc{
|
||||
Metadata: "statdb.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("statdb.proto", fileDescriptor_statdb_5a3c71d6f7d0333f) }
|
||||
func init() { proto.RegisterFile("statdb.proto", fileDescriptor_statdb_b838e72ff4f67ff3) }
|
||||
|
||||
var fileDescriptor_statdb_5a3c71d6f7d0333f = []byte{
|
||||
// 675 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x55, 0xc1, 0x6e, 0xd3, 0x40,
|
||||
0x10, 0x95, 0xe3, 0xd4, 0x6d, 0xc6, 0x6e, 0xa5, 0x6e, 0x68, 0x30, 0x41, 0x29, 0x21, 0x55, 0x21,
|
||||
0x5c, 0x42, 0x54, 0x24, 0xaa, 0x1e, 0x38, 0xb4, 0xa5, 0xad, 0x22, 0xaa, 0x82, 0xb6, 0x6a, 0x11,
|
||||
0x27, 0xcb, 0x8d, 0xb7, 0xb0, 0x52, 0x6a, 0x9b, 0xec, 0x5a, 0x6a, 0xf9, 0x26, 0xee, 0xfc, 0x1b,
|
||||
0x27, 0xb4, 0x3b, 0x6b, 0xc5, 0x8e, 0x62, 0xa1, 0x08, 0x8e, 0x7e, 0x33, 0xfb, 0xde, 0xdb, 0x37,
|
||||
0xb3, 0x09, 0x78, 0x42, 0x86, 0x32, 0xba, 0x19, 0xa4, 0xd3, 0x44, 0x26, 0xc4, 0xc1, 0xaf, 0xde,
|
||||
0x6f, 0x0b, 0xea, 0x17, 0x49, 0xc4, 0xc8, 0x63, 0x58, 0x8d, 0x93, 0x88, 0x05, 0x3c, 0xf2, 0xad,
|
||||
0xae, 0xd5, 0xf7, 0xa8, 0xa3, 0x3e, 0x47, 0x11, 0x79, 0x0e, 0xde, 0x24, 0x94, 0x2c, 0x1e, 0x3f,
|
||||
0x04, 0x13, 0x2e, 0xa4, 0x5f, 0xeb, 0xda, 0x7d, 0x9b, 0xba, 0x06, 0x3b, 0xe7, 0x42, 0x92, 0x1d,
|
||||
0x58, 0x0f, 0xb3, 0x88, 0xcb, 0x40, 0x64, 0xe3, 0x31, 0x13, 0xc2, 0xb7, 0xbb, 0x56, 0x7f, 0x8d,
|
||||
0x7a, 0x1a, 0xbc, 0x44, 0x8c, 0x34, 0x61, 0x85, 0x8b, 0x20, 0x4b, 0xfd, 0xba, 0x2e, 0xd6, 0xb9,
|
||||
0xb8, 0x4a, 0xc9, 0x2e, 0x6c, 0x64, 0x69, 0x14, 0x4a, 0x16, 0x18, 0x3e, 0x7f, 0x45, 0x57, 0xd7,
|
||||
0x11, 0x3d, 0x47, 0x90, 0x0c, 0xe1, 0x91, 0x69, 0x2b, 0xeb, 0x38, 0xba, 0x99, 0x60, 0xed, 0xb0,
|
||||
0xa8, 0xb6, 0x03, 0x86, 0x22, 0xc8, 0x52, 0xc9, 0xef, 0x98, 0xbf, 0x8a, 0x96, 0x10, 0xbc, 0xd2,
|
||||
0x58, 0xef, 0x97, 0x05, 0x0d, 0x75, 0xf9, 0x4b, 0x19, 0x4a, 0x51, 0x9d, 0x40, 0x07, 0x20, 0x4f,
|
||||
0xe0, 0x60, 0xe8, 0xd7, 0xba, 0x56, 0xdf, 0xa6, 0x0d, 0x83, 0x1c, 0x0c, 0xc9, 0x00, 0x9a, 0x25,
|
||||
0x57, 0xc1, 0x34, 0x94, 0x3c, 0xd1, 0x19, 0x58, 0x74, 0xb3, 0x98, 0x01, 0x55, 0x05, 0x15, 0x28,
|
||||
0x7a, 0x32, 0x8d, 0x75, 0xdd, 0xe8, 0x22, 0x86, 0x2d, 0xcf, 0xc0, 0x45, 0xca, 0x71, 0x92, 0xc5,
|
||||
0x52, 0x67, 0x62, 0x53, 0xd0, 0xd0, 0xb1, 0x42, 0x7a, 0x23, 0x58, 0x3f, 0x9e, 0xb2, 0x50, 0x32,
|
||||
0xca, 0xbe, 0x67, 0x4c, 0x48, 0xd2, 0x85, 0xba, 0x72, 0xab, 0x9d, 0xbb, 0x7b, 0xde, 0xc0, 0x0c,
|
||||
0x5b, 0xdd, 0x8e, 0xea, 0x0a, 0x69, 0x81, 0x73, 0xf8, 0x69, 0xf4, 0x81, 0x3d, 0xe8, 0x1b, 0x78,
|
||||
0xd4, 0x7c, 0xf5, 0x0e, 0x60, 0x23, 0xa7, 0x12, 0x69, 0x12, 0x0b, 0x46, 0x5e, 0xc2, 0x8a, 0x3a,
|
||||
0x2e, 0x0c, 0xd9, 0x66, 0x91, 0x4c, 0x47, 0x45, 0xb1, 0xde, 0x7b, 0x07, 0x70, 0xc6, 0x64, 0x6e,
|
||||
0xa1, 0x32, 0xbf, 0x2a, 0xe5, 0xb7, 0xe0, 0xea, 0xe3, 0xcb, 0xca, 0xfe, 0x80, 0xad, 0x53, 0x1e,
|
||||
0x47, 0xd7, 0xe1, 0x84, 0x47, 0xaa, 0x28, 0x72, 0x07, 0x4f, 0x60, 0xcd, 0x38, 0x50, 0x24, 0x76,
|
||||
0xdf, 0xa3, 0xab, 0x68, 0x41, 0x90, 0x01, 0x34, 0xee, 0x78, 0x1c, 0xa0, 0x40, 0xad, 0x4a, 0x60,
|
||||
0xed, 0x8e, 0xc7, 0xb8, 0x0c, 0x33, 0xcf, 0x76, 0xc9, 0xf3, 0x35, 0xb4, 0xe6, 0xb5, 0x8d, 0xfd,
|
||||
0x0e, 0x40, 0x1a, 0x0a, 0xc1, 0xa2, 0x82, 0x7c, 0x03, 0x11, 0x65, 0xa0, 0x03, 0x70, 0x1b, 0xf2,
|
||||
0x89, 0x29, 0xd7, 0xb0, 0x8c, 0xc8, 0x28, 0x12, 0x6a, 0xa0, 0x57, 0x7a, 0x35, 0xff, 0xcb, 0x40,
|
||||
0x73, 0xaa, 0x65, 0x93, 0xfd, 0x0c, 0x04, 0x8f, 0x1e, 0x85, 0x72, 0xfc, 0x2d, 0xb7, 0xf2, 0x0a,
|
||||
0x1a, 0x3a, 0x56, 0xfd, 0xfc, 0xd5, 0xc5, 0xe6, 0xfd, 0xe8, 0xd4, 0xf5, 0x2f, 0x41, 0x95, 0xa7,
|
||||
0x7b, 0x68, 0x96, 0x88, 0x8d, 0xb1, 0x21, 0x80, 0x16, 0x2e, 0x52, 0x2f, 0x70, 0xd7, 0xd0, 0x4d,
|
||||
0x5a, 0xe0, 0x35, 0x78, 0x26, 0x46, 0xa5, 0x89, 0x41, 0xce, 0xdb, 0x71, 0xb1, 0x43, 0x8f, 0xa7,
|
||||
0xf7, 0x05, 0x3a, 0xb8, 0xde, 0x27, 0xb1, 0x9c, 0x3e, 0x8c, 0x6e, 0x2f, 0x12, 0x79, 0x72, 0xcf,
|
||||
0x85, 0x14, 0xff, 0x1e, 0xf4, 0x08, 0xb6, 0xab, 0xa8, 0x97, 0x0c, 0x7e, 0xef, 0xa7, 0x0d, 0x8e,
|
||||
0x02, 0xde, 0x1f, 0x91, 0x7d, 0x70, 0x90, 0x95, 0x6c, 0xe5, 0xed, 0xa5, 0xa7, 0xde, 0x6e, 0xcd,
|
||||
0xc3, 0x46, 0x6c, 0x00, 0xf6, 0x19, 0x93, 0x84, 0xe4, 0xe5, 0xd9, 0xd3, 0x6c, 0x37, 0x4b, 0x98,
|
||||
0xe9, 0xff, 0x08, 0x1b, 0xe5, 0x55, 0x26, 0x9d, 0xbc, 0x6d, 0xe1, 0xf3, 0x6a, 0x6f, 0x57, 0x95,
|
||||
0x0d, 0xe1, 0x3e, 0x38, 0x38, 0xe4, 0x99, 0xf3, 0xd2, 0x4e, 0xcf, 0x9c, 0xcf, 0xed, 0xe7, 0x29,
|
||||
0xb8, 0x85, 0xed, 0x20, 0xed, 0x72, 0x5b, 0x71, 0x17, 0xdb, 0x4f, 0x17, 0xd6, 0x0c, 0xcf, 0x57,
|
||||
0x68, 0x2d, 0x1e, 0x08, 0xd9, 0x2d, 0x67, 0x56, 0xb1, 0x0b, 0xed, 0x17, 0x7f, 0x6b, 0x43, 0xa1,
|
||||
0x1b, 0x47, 0xff, 0x89, 0xbe, 0xf9, 0x13, 0x00, 0x00, 0xff, 0xff, 0x63, 0x10, 0xde, 0x1a, 0x54,
|
||||
0x07, 0x00, 0x00,
|
||||
var fileDescriptor_statdb_b838e72ff4f67ff3 = []byte{
|
||||
// 714 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x55, 0xd1, 0x6e, 0x12, 0x41,
|
||||
0x14, 0x0d, 0x2c, 0x5d, 0xe0, 0xee, 0x42, 0xd2, 0xc1, 0x22, 0x62, 0xa8, 0x48, 0x53, 0xc5, 0x17,
|
||||
0x24, 0x35, 0xb1, 0xe1, 0xc1, 0x87, 0xb6, 0xb6, 0x0d, 0xb1, 0xa9, 0x66, 0x9b, 0xd6, 0xf8, 0xb4,
|
||||
0xd9, 0xb2, 0x53, 0x9d, 0x84, 0xee, 0x22, 0x33, 0x24, 0xad, 0xdf, 0xe4, 0x27, 0xf8, 0x65, 0x3e,
|
||||
0x99, 0xb9, 0x77, 0x36, 0xec, 0x22, 0xb4, 0x36, 0x3e, 0xee, 0xb9, 0x87, 0x73, 0xce, 0x9e, 0xb9,
|
||||
0x3b, 0x80, 0x2b, 0x55, 0xa0, 0xc2, 0xcb, 0xde, 0x64, 0x1a, 0xab, 0x98, 0xd9, 0xf4, 0xd4, 0xf9,
|
||||
0x9d, 0x83, 0xc2, 0x69, 0x1c, 0x72, 0xf6, 0x18, 0x8a, 0x51, 0x1c, 0x72, 0x5f, 0x84, 0x8d, 0x5c,
|
||||
0x3b, 0xd7, 0x75, 0x3d, 0x5b, 0x3f, 0x0e, 0x43, 0xf6, 0x1c, 0xdc, 0x71, 0xa0, 0x78, 0x34, 0xba,
|
||||
0xf5, 0xc7, 0x42, 0xaa, 0x46, 0xbe, 0x6d, 0x75, 0x2d, 0xcf, 0x31, 0xd8, 0x89, 0x90, 0x8a, 0x6d,
|
||||
0x41, 0x25, 0x98, 0x85, 0x42, 0xf9, 0x72, 0x36, 0x1a, 0x71, 0x29, 0x1b, 0x56, 0x3b, 0xd7, 0x2d,
|
||||
0x79, 0x2e, 0x82, 0x67, 0x84, 0xb1, 0x1a, 0xac, 0x09, 0xe9, 0xcf, 0x26, 0x8d, 0x02, 0x0e, 0x0b,
|
||||
0x42, 0x9e, 0x4f, 0xd8, 0x36, 0x54, 0x67, 0x93, 0x30, 0x50, 0xdc, 0x37, 0x7a, 0x8d, 0x35, 0x9c,
|
||||
0x56, 0x08, 0x3d, 0x21, 0x90, 0xf5, 0xe1, 0x91, 0xa1, 0x65, 0x7d, 0x6c, 0x24, 0x33, 0x9a, 0xed,
|
||||
0xa5, 0xdd, 0xb6, 0xc0, 0x48, 0xf8, 0xb3, 0x89, 0x12, 0xd7, 0xbc, 0x51, 0xa4, 0x48, 0x04, 0x9e,
|
||||
0x23, 0xd6, 0xf9, 0x95, 0x87, 0xb2, 0x7e, 0xf9, 0x33, 0x15, 0x28, 0xb9, 0xba, 0x81, 0x16, 0x40,
|
||||
0xd2, 0xc0, 0xa0, 0xdf, 0xc8, 0xb7, 0x73, 0x5d, 0xcb, 0x2b, 0x1b, 0x64, 0xd0, 0x67, 0x3d, 0xa8,
|
||||
0x65, 0x52, 0xf9, 0xd3, 0x40, 0x89, 0x18, 0x3b, 0xc8, 0x79, 0xeb, 0xe9, 0x0e, 0x3c, 0x3d, 0xd0,
|
||||
0x85, 0x52, 0x26, 0x43, 0x2c, 0x20, 0xd1, 0x21, 0x8c, 0x28, 0xcf, 0xc0, 0x21, 0xc9, 0x51, 0x3c,
|
||||
0x8b, 0x14, 0x76, 0x62, 0x79, 0x80, 0xd0, 0x81, 0x46, 0xfe, 0xf6, 0x24, 0xa2, 0x8d, 0xc4, 0x8c,
|
||||
0x27, 0xf1, 0xe7, 0x9e, 0x44, 0x2c, 0x22, 0xd1, 0x78, 0x12, 0x05, 0x3b, 0x46, 0x4a, 0x56, 0xb3,
|
||||
0x84, 0x54, 0x46, 0xb3, 0xb4, 0x68, 0x67, 0x0a, 0x95, 0x83, 0x29, 0x0f, 0x14, 0xf7, 0xf8, 0xf7,
|
||||
0x19, 0x97, 0x8a, 0xb5, 0xa1, 0xa0, 0x2b, 0xc3, 0xfa, 0x9c, 0x1d, 0xb7, 0x67, 0x36, 0x4e, 0x57,
|
||||
0xec, 0xe1, 0x84, 0xbd, 0x84, 0x35, 0x0d, 0x4a, 0x6c, 0xd1, 0xd9, 0x59, 0x4f, 0x53, 0xf0, 0x14,
|
||||
0x3c, 0x9a, 0xb3, 0x3a, 0xd8, 0x7b, 0x9f, 0x86, 0x1f, 0xf8, 0x2d, 0xf6, 0xe8, 0x7a, 0xe6, 0xa9,
|
||||
0x33, 0x80, 0x6a, 0xe2, 0x29, 0x27, 0x71, 0x24, 0x53, 0x92, 0xb9, 0xbb, 0x25, 0x3b, 0xef, 0x00,
|
||||
0x8e, 0xb9, 0x4a, 0xb2, 0xae, 0x3c, 0xed, 0xb9, 0x73, 0x3e, 0xe3, 0xfc, 0x16, 0x1c, 0xfc, 0xf9,
|
||||
0x43, 0x6d, 0x7f, 0xc0, 0xc6, 0x91, 0x88, 0xc2, 0x8b, 0x60, 0x2c, 0x42, 0x3d, 0x94, 0x49, 0x82,
|
||||
0x27, 0x50, 0x32, 0x09, 0xb4, 0x88, 0xd5, 0x75, 0xbd, 0x22, 0x45, 0x90, 0xac, 0x07, 0xe5, 0x6b,
|
||||
0x11, 0xf9, 0xf7, 0x54, 0x55, 0xba, 0x16, 0xd1, 0xd9, 0x9d, 0x6d, 0x5d, 0x40, 0x7d, 0xd1, 0xdb,
|
||||
0xc4, 0x6f, 0x01, 0x4c, 0x02, 0x29, 0x79, 0x98, 0xb2, 0x2f, 0x13, 0xa2, 0x03, 0xb4, 0x00, 0xae,
|
||||
0x02, 0x31, 0x36, 0xe3, 0x3c, 0x8d, 0x09, 0x19, 0x86, 0xb2, 0x33, 0x84, 0xca, 0x39, 0x7e, 0x48,
|
||||
0xff, 0x7e, 0xf2, 0xab, 0x6a, 0x1d, 0x40, 0x35, 0x91, 0x7a, 0x68, 0xb3, 0x9f, 0x81, 0xd1, 0x4f,
|
||||
0xf7, 0x03, 0x35, 0xfa, 0x96, 0x44, 0x79, 0x05, 0x65, 0xac, 0x15, 0x2f, 0x2b, 0xfd, 0x62, 0x8b,
|
||||
0x79, 0xb0, 0x75, 0xbc, 0xb7, 0x56, 0x65, 0xba, 0x81, 0x5a, 0x46, 0xd8, 0x04, 0xeb, 0x03, 0xa0,
|
||||
0x71, 0x5a, 0x7a, 0x49, 0xba, 0x32, 0x92, 0xd0, 0xe0, 0x35, 0xb8, 0xa6, 0x46, 0xed, 0x49, 0x45,
|
||||
0x2e, 0xc6, 0x71, 0x88, 0x81, 0xc7, 0xd3, 0xf9, 0x02, 0x2d, 0x5a, 0xef, 0xc3, 0x48, 0x4d, 0x6f,
|
||||
0x87, 0x57, 0xa7, 0xb1, 0x3a, 0xbc, 0x11, 0x52, 0xc9, 0xff, 0x2f, 0x7a, 0x08, 0x9b, 0xab, 0xa4,
|
||||
0x1f, 0x58, 0xfc, 0xce, 0x4f, 0x0b, 0x6c, 0x0d, 0xbc, 0xdf, 0x67, 0xbb, 0x60, 0x93, 0x2a, 0xdb,
|
||||
0x48, 0xe8, 0x99, 0x3b, 0xa1, 0x59, 0x5f, 0x84, 0x8d, 0x59, 0x0f, 0xac, 0x63, 0xae, 0x18, 0x4b,
|
||||
0xc6, 0xf3, 0x4f, 0xb3, 0x59, 0xcb, 0x60, 0x86, 0xff, 0x11, 0xaa, 0xd9, 0x55, 0x66, 0xad, 0x84,
|
||||
0xb6, 0xf4, 0xf3, 0x6a, 0x6e, 0xae, 0x1a, 0x1b, 0xc1, 0x5d, 0xb0, 0xe9, 0x90, 0xe7, 0xc9, 0x33,
|
||||
0x3b, 0x3d, 0x4f, 0xbe, 0xb0, 0x9f, 0x47, 0xe0, 0xa4, 0xb6, 0x83, 0x35, 0xb3, 0xb4, 0xf4, 0x2e,
|
||||
0x36, 0x9f, 0x2e, 0x9d, 0x19, 0x9d, 0xaf, 0x50, 0x5f, 0x7e, 0x20, 0x6c, 0x3b, 0xdb, 0xd9, 0x8a,
|
||||
0x5d, 0x68, 0xbe, 0xb8, 0x8f, 0x46, 0x46, 0x97, 0x36, 0xfe, 0xe5, 0xbf, 0xf9, 0x13, 0x00, 0x00,
|
||||
0xff, 0xff, 0x9c, 0x3e, 0x97, 0xa1, 0x02, 0x08, 0x00, 0x00,
|
||||
}
|
||||
|
@ -37,13 +37,17 @@ message NodeStats {
|
||||
int64 latency_90 = 2; // 90th percentile measure of storagenode latency
|
||||
double audit_success_ratio = 3; // (auditSuccessCount / totalAuditCount)
|
||||
double uptime_ratio = 4; // (uptimeCount / totalUptimeCheckCount)
|
||||
int64 audit_count = 5; // used for FindValidNodes
|
||||
int64 audit_count = 5;
|
||||
int64 audit_success_count = 6;
|
||||
int64 uptime_count = 7;
|
||||
int64 uptime_success_count = 8;
|
||||
}
|
||||
|
||||
// CreateRequest is a request message for the Create rpc call
|
||||
message CreateRequest {
|
||||
Node node = 1;
|
||||
bytes APIKey = 2;
|
||||
NodeStats stats = 2;
|
||||
bytes APIKey = 3;
|
||||
}
|
||||
|
||||
// CreateResponse is a response message for the Create rpc call
|
||||
|
@ -26,13 +26,15 @@ type StatDB struct {
|
||||
// Client services offerred for the interface
|
||||
type Client interface {
|
||||
Create(ctx context.Context, nodeID []byte) error
|
||||
CreateWithStats(ctx context.Context, nodeID []byte, stats *pb.NodeStats) error
|
||||
Get(ctx context.Context, nodeID []byte) (*pb.NodeStats, error)
|
||||
FindValidNodes(ctx context.Context, nodeIDs [][]byte, minAuditCount int64,
|
||||
minAuditSuccess, minUptime float64) (passedIDs [][]byte, err error)
|
||||
Update(ctx context.Context, nodeID []byte, auditSuccess, isUp bool, latencyList []int64,
|
||||
updateAuditSuccess, updateUptime, updateLatency bool) (*pb.NodeStats, error)
|
||||
FindValidNodes(ctx context.Context, nodeIDs [][]byte, minStats *pb.NodeStats) (passedIDs [][]byte, err error)
|
||||
Update(ctx context.Context, nodeID []byte, auditSuccess, isUp bool,
|
||||
latencyList []int64) (stats *pb.NodeStats, err error)
|
||||
UpdateUptime(ctx context.Context, nodeID []byte, isUp bool) (*pb.NodeStats, error)
|
||||
UpdateAuditSuccess(ctx context.Context, nodeID []byte, passed bool) (*pb.NodeStats, error)
|
||||
UpdateBatch(ctx context.Context, nodes []*pb.Node) ([]*pb.NodeStats, []*pb.Node, error)
|
||||
CreateEntryIfNotExists(ctx context.Context, node *pb.Node) (stats *pb.NodeStats, err error)
|
||||
CreateEntryIfNotExists(ctx context.Context, nodeID []byte) (stats *pb.NodeStats, err error)
|
||||
}
|
||||
|
||||
// NewClient initializes a new statdb client
|
||||
@ -52,14 +54,12 @@ func NewClient(identity *provider.FullIdentity, address string, APIKey []byte) (
|
||||
// a compiler trick to make sure *StatDB implements Client
|
||||
var _ Client = (*StatDB)(nil)
|
||||
|
||||
// Create is used for creating a new entry in the stats db
|
||||
// Create is used for creating a new entry in the stats db with default reputation
|
||||
func (sdb *StatDB) Create(ctx context.Context, nodeID []byte) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
node := pb.Node{
|
||||
NodeId: nodeID,
|
||||
UpdateAuditSuccess: false,
|
||||
UpdateUptime: false,
|
||||
NodeId: nodeID,
|
||||
}
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: &node,
|
||||
@ -70,6 +70,24 @@ func (sdb *StatDB) Create(ctx context.Context, nodeID []byte) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateWithStats is used for creating a new entry in the stats db with a specific reputation
|
||||
// stats must have AuditCount, AuditSuccessCount, UptimeCount, UptimeSuccessCount
|
||||
func (sdb *StatDB) CreateWithStats(ctx context.Context, nodeID []byte, stats *pb.NodeStats) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
node := &pb.Node{
|
||||
NodeId: nodeID,
|
||||
}
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: node,
|
||||
Stats: stats,
|
||||
APIKey: sdb.APIKey,
|
||||
}
|
||||
_, err = sdb.client.Create(ctx, createReq)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Get is used for retrieving a new entry from the stats db
|
||||
func (sdb *StatDB) Get(ctx context.Context, nodeID []byte) (stats *pb.NodeStats, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
@ -87,18 +105,15 @@ func (sdb *StatDB) Get(ctx context.Context, nodeID []byte) (stats *pb.NodeStats,
|
||||
}
|
||||
|
||||
// FindValidNodes is used for retrieving a subset of nodes that meet a minimum reputation requirement
|
||||
func (sdb *StatDB) FindValidNodes(ctx context.Context, nodeIDs [][]byte, minAuditCount int64,
|
||||
minAuditSuccess, minUptime float64) (passedIDs [][]byte, err error) {
|
||||
// minStats must have AuditSuccessRatio, UptimeRatio, AuditCount
|
||||
func (sdb *StatDB) FindValidNodes(ctx context.Context, nodeIDs [][]byte,
|
||||
minStats *pb.NodeStats) (passedIDs [][]byte, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
findValidNodesReq := &pb.FindValidNodesRequest{
|
||||
NodeIds: nodeIDs,
|
||||
MinStats: &pb.NodeStats{
|
||||
AuditSuccessRatio: minAuditSuccess,
|
||||
UptimeRatio: minUptime,
|
||||
AuditCount: minAuditCount,
|
||||
},
|
||||
APIKey: sdb.APIKey,
|
||||
NodeIds: nodeIDs,
|
||||
MinStats: minStats,
|
||||
APIKey: sdb.APIKey,
|
||||
}
|
||||
|
||||
res, err := sdb.client.FindValidNodes(ctx, findValidNodesReq)
|
||||
@ -110,8 +125,8 @@ func (sdb *StatDB) FindValidNodes(ctx context.Context, nodeIDs [][]byte, minAudi
|
||||
}
|
||||
|
||||
// Update is used for updating a node's stats in the stats db
|
||||
func (sdb *StatDB) Update(ctx context.Context, nodeID []byte, auditSuccess, isUp bool, latencyList []int64,
|
||||
updateAuditSuccess, updateUptime, updateLatency bool) (stats *pb.NodeStats, err error) {
|
||||
func (sdb *StatDB) Update(ctx context.Context, nodeID []byte,
|
||||
auditSuccess, isUp bool, latencyList []int64) (stats *pb.NodeStats, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
node := pb.Node{
|
||||
@ -119,9 +134,9 @@ func (sdb *StatDB) Update(ctx context.Context, nodeID []byte, auditSuccess, isUp
|
||||
AuditSuccess: auditSuccess,
|
||||
IsUp: isUp,
|
||||
LatencyList: latencyList,
|
||||
UpdateAuditSuccess: updateAuditSuccess,
|
||||
UpdateUptime: updateUptime,
|
||||
UpdateLatency: updateLatency,
|
||||
UpdateAuditSuccess: true,
|
||||
UpdateUptime: true,
|
||||
UpdateLatency: true,
|
||||
}
|
||||
updateReq := &pb.UpdateRequest{
|
||||
Node: &node,
|
||||
@ -136,6 +151,50 @@ func (sdb *StatDB) Update(ctx context.Context, nodeID []byte, auditSuccess, isUp
|
||||
return res.Stats, err
|
||||
}
|
||||
|
||||
// UpdateUptime is used for updating a node's uptime in statdb
|
||||
func (sdb *StatDB) UpdateUptime(ctx context.Context, nodeID []byte,
|
||||
isUp bool) (stats *pb.NodeStats, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
node := pb.Node{
|
||||
NodeId: nodeID,
|
||||
IsUp: isUp,
|
||||
UpdateAuditSuccess: false,
|
||||
UpdateUptime: true,
|
||||
UpdateLatency: false,
|
||||
}
|
||||
updateReq := &pb.UpdateRequest{
|
||||
Node: &node,
|
||||
APIKey: sdb.APIKey,
|
||||
}
|
||||
|
||||
res, err := sdb.client.Update(ctx, updateReq)
|
||||
|
||||
return res.Stats, err
|
||||
}
|
||||
|
||||
// UpdateAuditSuccess is used for updating a node's audit success in statdb
|
||||
func (sdb *StatDB) UpdateAuditSuccess(ctx context.Context, nodeID []byte,
|
||||
passed bool) (stats *pb.NodeStats, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
node := pb.Node{
|
||||
NodeId: nodeID,
|
||||
AuditSuccess: passed,
|
||||
UpdateAuditSuccess: true,
|
||||
UpdateUptime: false,
|
||||
UpdateLatency: false,
|
||||
}
|
||||
updateReq := &pb.UpdateRequest{
|
||||
Node: &node,
|
||||
APIKey: sdb.APIKey,
|
||||
}
|
||||
|
||||
res, err := sdb.client.Update(ctx, updateReq)
|
||||
|
||||
return res.Stats, err
|
||||
}
|
||||
|
||||
// UpdateBatch is used for updating multiple nodes' stats in the stats db
|
||||
func (sdb *StatDB) UpdateBatch(ctx context.Context, nodes []*pb.Node) (statsList []*pb.NodeStats, failedNodes []*pb.Node, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
@ -154,9 +213,10 @@ func (sdb *StatDB) UpdateBatch(ctx context.Context, nodes []*pb.Node) (statsList
|
||||
}
|
||||
|
||||
// CreateEntryIfNotExists creates a db entry for a node if entry doesn't already exist
|
||||
func (sdb *StatDB) CreateEntryIfNotExists(ctx context.Context, node *pb.Node) (stats *pb.NodeStats, err error) {
|
||||
func (sdb *StatDB) CreateEntryIfNotExists(ctx context.Context, nodeID []byte) (stats *pb.NodeStats, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
node := &pb.Node{NodeId: nodeID}
|
||||
createReq := &pb.CreateEntryIfNotExistsRequest{
|
||||
Node: node,
|
||||
APIKey: sdb.APIKey,
|
||||
|
94
pkg/statdb/sdbclient/mock_client.go
Normal file
94
pkg/statdb/sdbclient/mock_client.go
Normal file
@ -0,0 +1,94 @@
|
||||
// Copyright (C) 2018 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package sdbclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "storj.io/storj/pkg/statdb/proto"
|
||||
)
|
||||
|
||||
// MockStatDB creates a noop Mock Statdb Client
|
||||
type MockStatDB struct{}
|
||||
|
||||
// NewMockClient initializes a new mock statdb client
|
||||
func NewMockClient() Client {
|
||||
return &MockStatDB{}
|
||||
}
|
||||
|
||||
// a compiler trick to make sure *MockStatDB implements Client
|
||||
var _ Client = (*MockStatDB)(nil)
|
||||
|
||||
// Create is used for creating a new entry in the stats db with default reputation
|
||||
func (sdb *MockStatDB) Create(ctx context.Context, nodeID []byte) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateWithStats is used for creating a new entry in the stats db with a specific reputation
|
||||
func (sdb *MockStatDB) CreateWithStats(ctx context.Context, nodeID []byte, stats *pb.NodeStats) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get is used for retrieving an entry from statdb or creating a new one if one does not exist
|
||||
func (sdb *MockStatDB) Get(ctx context.Context, nodeID []byte) (stats *pb.NodeStats, err error) {
|
||||
stats = &pb.NodeStats{
|
||||
AuditSuccessRatio: 0,
|
||||
UptimeRatio: 0,
|
||||
AuditCount: 0,
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// FindValidNodes is used for retrieving a subset of nodes that meet a minimum reputation requirement
|
||||
func (sdb *MockStatDB) FindValidNodes(ctx context.Context, nodeIDs [][]byte, minStats *pb.NodeStats) (passedIDs [][]byte, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Update is used for updating a node's stats in the stats db
|
||||
func (sdb *MockStatDB) Update(ctx context.Context, nodeID []byte, auditSuccess,
|
||||
isUp bool, latencyList []int64) (stats *pb.NodeStats, err error) {
|
||||
stats = &pb.NodeStats{
|
||||
AuditSuccessRatio: 0,
|
||||
UptimeRatio: 0,
|
||||
AuditCount: 0,
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// UpdateUptime is used for updating a node's uptime in statdb
|
||||
func (sdb *MockStatDB) UpdateUptime(ctx context.Context, nodeID []byte,
|
||||
isUp bool) (stats *pb.NodeStats, err error) {
|
||||
stats = &pb.NodeStats{
|
||||
AuditSuccessRatio: 0,
|
||||
UptimeRatio: 0,
|
||||
AuditCount: 0,
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// UpdateAuditSuccess is used for updating a node's audit success in statdb
|
||||
func (sdb *MockStatDB) UpdateAuditSuccess(ctx context.Context, nodeID []byte,
|
||||
passed bool) (stats *pb.NodeStats, err error) {
|
||||
stats = &pb.NodeStats{
|
||||
AuditSuccessRatio: 0,
|
||||
UptimeRatio: 0,
|
||||
AuditCount: 0,
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// UpdateBatch is used for updating multiple nodes' stats in the stats db
|
||||
func (sdb *MockStatDB) UpdateBatch(ctx context.Context, nodes []*pb.Node) (statsList []*pb.NodeStats, failedNodes []*pb.Node, err error) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
// CreateEntryIfNotExists creates a db entry for a node if entry doesn't already exist
|
||||
func (sdb *MockStatDB) CreateEntryIfNotExists(ctx context.Context, nodeID []byte) (stats *pb.NodeStats, err error) {
|
||||
stats = &pb.NodeStats{
|
||||
AuditSuccessRatio: 0,
|
||||
UptimeRatio: 0,
|
||||
AuditCount: 0,
|
||||
}
|
||||
return stats, nil
|
||||
}
|
@ -8,6 +8,7 @@ import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@ -20,7 +21,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
mon = monkit.Package()
|
||||
mon = monkit.Package()
|
||||
errAuditSuccess = errs.Class("statdb audit success error")
|
||||
errUptime = errs.Class("statdb uptime error")
|
||||
)
|
||||
|
||||
// Server implements the statdb RPC service
|
||||
@ -65,10 +68,33 @@ func (s *Server) Create(ctx context.Context, createReq *pb.CreateRequest) (resp
|
||||
return nil, err
|
||||
}
|
||||
|
||||
node := createReq.Node
|
||||
var (
|
||||
totalAuditCount int64
|
||||
auditSuccessCount int64
|
||||
auditSuccessRatio float64
|
||||
totalUptimeCount int64
|
||||
uptimeSuccessCount int64
|
||||
uptimeRatio float64
|
||||
)
|
||||
|
||||
auditSuccessCount, totalAuditCount, auditSuccessRatio := initRatioVars(node.UpdateAuditSuccess, node.AuditSuccess)
|
||||
uptimeSuccessCount, totalUptimeCount, uptimeRatio := initRatioVars(node.UpdateUptime, node.IsUp)
|
||||
stats := createReq.Stats
|
||||
if stats != nil {
|
||||
totalAuditCount = stats.AuditCount
|
||||
auditSuccessCount = stats.AuditSuccessCount
|
||||
auditSuccessRatio, err = checkRatioVars(auditSuccessCount, totalAuditCount)
|
||||
if err != nil {
|
||||
return nil, errAuditSuccess.Wrap(err)
|
||||
}
|
||||
|
||||
totalUptimeCount = stats.UptimeCount
|
||||
uptimeSuccessCount = stats.UptimeSuccessCount
|
||||
uptimeRatio, err = checkRatioVars(uptimeSuccessCount, totalUptimeCount)
|
||||
if err != nil {
|
||||
return nil, errUptime.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
node := createReq.Node
|
||||
|
||||
dbNode, err := s.DB.Create_Node(
|
||||
ctx,
|
||||
@ -87,6 +113,7 @@ func (s *Server) Create(ctx context.Context, createReq *pb.CreateRequest) (resp
|
||||
|
||||
nodeStats := &pb.NodeStats{
|
||||
NodeId: dbNode.Id,
|
||||
AuditCount: dbNode.TotalAuditCount,
|
||||
AuditSuccessRatio: dbNode.AuditSuccessRatio,
|
||||
UptimeRatio: dbNode.UptimeRatio,
|
||||
}
|
||||
@ -113,6 +140,7 @@ func (s *Server) Get(ctx context.Context, getReq *pb.GetRequest) (resp *pb.GetRe
|
||||
|
||||
nodeStats := &pb.NodeStats{
|
||||
NodeId: dbNode.Id,
|
||||
AuditCount: dbNode.TotalAuditCount,
|
||||
AuditSuccessRatio: dbNode.AuditSuccessRatio,
|
||||
UptimeRatio: dbNode.UptimeRatio,
|
||||
}
|
||||
@ -285,27 +313,28 @@ func (s *Server) UpdateBatch(ctx context.Context, updateBatchReq *pb.UpdateBatch
|
||||
// CreateEntryIfNotExists creates a statdb node entry and saves to statdb if it didn't already exist
|
||||
func (s *Server) CreateEntryIfNotExists(ctx context.Context, createIfReq *pb.CreateEntryIfNotExistsRequest) (resp *pb.CreateEntryIfNotExistsResponse, err error) {
|
||||
APIKeyBytes := createIfReq.APIKey
|
||||
|
||||
getReq := &pb.GetRequest{
|
||||
NodeId: createIfReq.Node.NodeId,
|
||||
APIKey: APIKeyBytes,
|
||||
}
|
||||
getRes, err := s.Get(ctx, getReq)
|
||||
if err != nil {
|
||||
// TODO: figure out how to confirm error is type dbx.ErrorCode_NoRows
|
||||
if strings.Contains(err.Error(), "no rows in result set") {
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: createIfReq.Node,
|
||||
APIKey: APIKeyBytes,
|
||||
}
|
||||
res, err := s.Create(ctx, createReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
createEntryIfNotExistsRes := &pb.CreateEntryIfNotExistsResponse{
|
||||
Stats: res.Stats,
|
||||
}
|
||||
return createEntryIfNotExistsRes, nil
|
||||
// TODO: figure out better way to confirm error is type dbx.ErrorCode_NoRows
|
||||
if err != nil && strings.Contains(err.Error(), "no rows in result set") {
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: createIfReq.Node,
|
||||
APIKey: APIKeyBytes,
|
||||
}
|
||||
res, err := s.Create(ctx, createReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
createEntryIfNotExistsRes := &pb.CreateEntryIfNotExistsResponse{
|
||||
Stats: res.Stats,
|
||||
}
|
||||
return createEntryIfNotExistsRes, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
createEntryIfNotExistsRes := &pb.CreateEntryIfNotExistsResponse{
|
||||
@ -314,20 +343,6 @@ func (s *Server) CreateEntryIfNotExists(ctx context.Context, createIfReq *pb.Cre
|
||||
return createEntryIfNotExistsRes, 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 {
|
||||
@ -336,3 +351,18 @@ func updateRatioVars(newStatus bool, successCount, totalCount int64) (int64, int
|
||||
newRatio := float64(successCount) / float64(totalCount)
|
||||
return successCount, totalCount, newRatio
|
||||
}
|
||||
|
||||
func checkRatioVars(successCount, totalCount int64) (ratio float64, err error) {
|
||||
if successCount < 0 {
|
||||
return 0, errs.New("success count less than 0")
|
||||
}
|
||||
if totalCount < 0 {
|
||||
return 0, errs.New("total count less than 0")
|
||||
}
|
||||
if successCount > totalCount {
|
||||
return 0, errs.New("success count greater than total count")
|
||||
}
|
||||
|
||||
ratio = float64(successCount) / float64(totalCount)
|
||||
return ratio, nil
|
||||
}
|
||||
|
@ -20,18 +20,14 @@ var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
||||
func TestCreateExists(t *testing.T) {
|
||||
func TestCreateDoesNotExist(t *testing.T) {
|
||||
dbPath := getDBPath()
|
||||
statdb, db, err := getServerAndDB(dbPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
apiKey := []byte("")
|
||||
nodeID := []byte("testnodeid")
|
||||
node := &pb.Node{
|
||||
NodeId: nodeID,
|
||||
UpdateAuditSuccess: false,
|
||||
UpdateUptime: false,
|
||||
}
|
||||
node := &pb.Node{NodeId: nodeID}
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: node,
|
||||
APIKey: apiKey,
|
||||
@ -45,12 +41,12 @@ func TestCreateExists(t *testing.T) {
|
||||
nodeInfo, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, nodeID, nodeInfo.Id, nodeID)
|
||||
assert.EqualValues(t, nodeID, nodeInfo.Id)
|
||||
assert.EqualValues(t, 0, nodeInfo.AuditSuccessRatio)
|
||||
assert.EqualValues(t, 0, nodeInfo.UptimeRatio)
|
||||
}
|
||||
|
||||
func TestCreateDuplicate(t *testing.T) {
|
||||
func TestCreateExists(t *testing.T) {
|
||||
dbPath := getDBPath()
|
||||
statdb, db, err := getServerAndDB(dbPath)
|
||||
assert.NoError(t, err)
|
||||
@ -64,11 +60,7 @@ func TestCreateDuplicate(t *testing.T) {
|
||||
uptimeSuccessCount, totalUptimeCount, uptimeRatio)
|
||||
assert.NoError(t, err)
|
||||
|
||||
node := &pb.Node{
|
||||
NodeId: nodeID,
|
||||
UpdateAuditSuccess: false,
|
||||
UpdateUptime: false,
|
||||
}
|
||||
node := &pb.Node{NodeId: nodeID}
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: node,
|
||||
APIKey: apiKey,
|
||||
@ -76,6 +68,40 @@ func TestCreateDuplicate(t *testing.T) {
|
||||
_, err = statdb.Create(ctx, createReq)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
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)
|
||||
apiKey := []byte("")
|
||||
nodeID := []byte("testnodeid")
|
||||
node := &pb.Node{NodeId: nodeID}
|
||||
stats := &pb.NodeStats{
|
||||
AuditCount: totalAuditCount,
|
||||
AuditSuccessCount: auditSuccessCount,
|
||||
UptimeCount: totalUptimeCount,
|
||||
UptimeSuccessCount: uptimeSuccessCount,
|
||||
}
|
||||
createReq := &pb.CreateRequest{
|
||||
Node: node,
|
||||
Stats: stats,
|
||||
APIKey: apiKey,
|
||||
}
|
||||
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)
|
||||
|
||||
nodeInfo, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, nodeID, nodeInfo.Id, nodeID)
|
||||
assert.EqualValues(t, auditRatio, nodeInfo.AuditSuccessRatio)
|
||||
assert.EqualValues(t, uptimeRatio, nodeInfo.UptimeRatio)
|
||||
}
|
||||
|
||||
func TestGetExists(t *testing.T) {
|
||||
dbPath := getDBPath()
|
||||
@ -333,21 +359,7 @@ func TestCreateEntryIfNotExists(t *testing.T) {
|
||||
uptimeSuccessCount1, totalUptimeCount1, uptimeRatio1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
node1 := &pb.Node{
|
||||
NodeId: nodeID1,
|
||||
UpdateAuditSuccess: true,
|
||||
AuditSuccess: true,
|
||||
UpdateUptime: true,
|
||||
IsUp: false,
|
||||
}
|
||||
node2 := &pb.Node{
|
||||
NodeId: nodeID2,
|
||||
UpdateAuditSuccess: true,
|
||||
AuditSuccess: true,
|
||||
UpdateUptime: true,
|
||||
IsUp: false,
|
||||
}
|
||||
|
||||
node1 := &pb.Node{NodeId: nodeID1}
|
||||
createIfNotExistsReq1 := &pb.CreateEntryIfNotExistsRequest{
|
||||
Node: node1,
|
||||
APIKey: apiKey,
|
||||
@ -355,12 +367,25 @@ func TestCreateEntryIfNotExists(t *testing.T) {
|
||||
_, err = statdb.CreateEntryIfNotExists(ctx, createIfNotExistsReq1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
nodeInfo1, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID1))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, nodeID1, nodeInfo1.Id)
|
||||
assert.EqualValues(t, auditRatio1, nodeInfo1.AuditSuccessRatio)
|
||||
assert.EqualValues(t, uptimeRatio1, nodeInfo1.UptimeRatio)
|
||||
|
||||
node2 := &pb.Node{NodeId: nodeID2}
|
||||
createIfNotExistsReq2 := &pb.CreateEntryIfNotExistsRequest{
|
||||
Node: node2,
|
||||
APIKey: apiKey,
|
||||
}
|
||||
_, err = statdb.CreateEntryIfNotExists(ctx, createIfNotExistsReq2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
nodeInfo2, err := db.Get_Node_By_Id(ctx, dbx.Node_Id(nodeID2))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, nodeID2, nodeInfo2.Id)
|
||||
assert.EqualValues(t, 0, nodeInfo2.AuditSuccessRatio)
|
||||
assert.EqualValues(t, 0, nodeInfo2.UptimeRatio)
|
||||
}
|
||||
|
||||
func getDBPath() string {
|
||||
|
Loading…
Reference in New Issue
Block a user