From 3c4ace32666c52b35173f22ea9953242fca143ff Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 15:10:58 +0200 Subject: [PATCH 1/7] feat(ddc-schemas): numeric 'nodeID' is replaced with stringified 'nodeKey' --- ddc-schemas | 2 +- model/pb/state.pb.go | 147 +++++++++++++++++++++---------------------- 2 files changed, 74 insertions(+), 75 deletions(-) diff --git a/ddc-schemas b/ddc-schemas index a1bd223..23cd2e6 160000 --- a/ddc-schemas +++ b/ddc-schemas @@ -1 +1 @@ -Subproject commit a1bd22370f112d88c9d5f730a0d6124bc700abb2 +Subproject commit 23cd2e62d7ab55d21856b03df4644d35e2119ceb diff --git a/model/pb/state.pb.go b/model/pb/state.pb.go index 29ce6cb..7ac98b5 100644 --- a/model/pb/state.pb.go +++ b/model/pb/state.pb.go @@ -160,8 +160,8 @@ type ShortState struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // ID of the node from smart contract - NodeID uint32 `protobuf:"varint,1,opt,name=nodeID,proto3" json:"nodeID,omitempty"` + // PubKey of the node from smart contract + NodeKey string `protobuf:"bytes,1,opt,name=nodeKey,proto3" json:"nodeKey,omitempty"` // ID of the cluster from smart contract ClusterID uint32 `protobuf:"varint,2,opt,name=clusterID,proto3" json:"clusterID,omitempty"` // URL of the node @@ -208,11 +208,11 @@ func (*ShortState) Descriptor() ([]byte, []int) { return file_state_proto_rawDescGZIP(), []int{1} } -func (x *ShortState) GetNodeID() uint32 { +func (x *ShortState) GetNodeKey() string { if x != nil { - return x.NodeID + return x.NodeKey } - return 0 + return "" } func (x *ShortState) GetClusterID() uint32 { @@ -261,7 +261,7 @@ func (x *ShortState) GetUpdated() uint64 { // State bring short state (last updated state) and list of checks (who checks the state and what is the result) // each check is signed by CDN node, that make them // statistic is a last statistic state of CDN node, that needed for diagnostic only -// sign(CID(NodeID+ClusterID+URL+State+Location+Size+Updated_at)) +// sign(CID(nodeKey+ClusterID+URL+State+Location+Size+Updated_at)) type GossipState struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -415,8 +415,7 @@ func (x *Check) GetSignature() *StateSignature { // // - It is normal, if time of signing is bigger from state timestamp. // - The State hash is: -// - Short state: `Hash = CID(NodeID+ClusterID+URL+State+Location+Size+Updated_at)` -// +// - Short state: `Hash = CID(nodeKey+ClusterID+URL+State+Location+Size+Updated_at)` // - The signing scheme should be sr25519, and `scheme` should be empty. // - If this not supported by a signer, then `scheme` should be "ed25519". // @@ -695,72 +694,72 @@ var file_state_proto_rawDesc = []byte{ 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x25, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x47, 0x6f, 0x73, 0x73, 0x69, 0x70, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, - 0x3a, 0x02, 0x38, 0x01, 0x22, 0xbf, 0x01, 0x0a, 0x0a, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x53, 0x74, - 0x61, 0x74, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x6f, 0x64, 0x65, 0x49, 0x44, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x0d, 0x52, 0x06, 0x6e, 0x6f, 0x64, 0x65, 0x49, 0x44, 0x12, 0x1c, 0x0a, 0x09, 0x63, - 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x49, 0x44, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, - 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x49, 0x44, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x1f, 0x0a, 0x05, 0x73, - 0x74, 0x61, 0x74, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x2e, - 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1a, 0x0a, 0x08, - 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, - 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a, 0x65, - 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x18, 0x0a, 0x07, - 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x04, 0x52, 0x07, 0x75, - 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x22, 0x96, 0x02, 0x0a, 0x0b, 0x47, 0x6f, 0x73, 0x73, 0x69, - 0x70, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x2d, 0x0a, 0x0a, 0x6c, 0x61, 0x73, 0x74, 0x5f, 0x73, - 0x74, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x2e, - 0x53, 0x68, 0x6f, 0x72, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x09, 0x6c, 0x61, 0x73, 0x74, - 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x30, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, - 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, - 0x61, 0x74, 0x65, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x52, 0x09, 0x73, 0x69, - 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x12, 0x2b, 0x0a, 0x09, 0x73, 0x74, 0x61, 0x74, 0x69, - 0x73, 0x74, 0x69, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x2e, - 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, 0x69, - 0x73, 0x74, 0x69, 0x63, 0x12, 0x33, 0x0a, 0x06, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x18, 0x04, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x70, 0x62, 0x2e, 0x47, 0x6f, 0x73, 0x73, 0x69, 0x70, - 0x53, 0x74, 0x61, 0x74, 0x65, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x45, 0x6e, 0x74, 0x72, - 0x79, 0x52, 0x06, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x1a, 0x44, 0x0a, 0x0b, 0x43, 0x68, 0x65, - 0x63, 0x6b, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, - 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x2e, 0x43, - 0x68, 0x65, 0x63, 0x6b, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, - 0x5f, 0x0a, 0x05, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x24, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, - 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x68, 0x6f, - 0x72, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x12, 0x30, - 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x53, 0x69, 0x67, 0x6e, - 0x61, 0x74, 0x75, 0x72, 0x65, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, - 0x22, 0x9a, 0x01, 0x0a, 0x0e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x74, - 0x75, 0x72, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x69, 0x67, - 0x6e, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x73, 0x69, 0x67, 0x6e, 0x65, - 0x72, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x6d, 0x75, 0x6c, - 0x74, 0x69, 0x48, 0x61, 0x73, 0x68, 0x54, 0x79, 0x70, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, - 0x52, 0x0d, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x48, 0x61, 0x73, 0x68, 0x54, 0x79, 0x70, 0x65, 0x12, - 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x05, 0x20, 0x01, - 0x28, 0x04, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22, 0xbf, 0x01, - 0x0a, 0x09, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x12, 0x1a, 0x0a, 0x08, 0x73, - 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x73, - 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x2b, 0x0a, 0x09, 0x72, 0x65, 0x64, 0x69, 0x72, - 0x65, 0x63, 0x74, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, 0x62, 0x2e, - 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x73, 0x52, 0x09, 0x72, 0x65, 0x64, 0x69, 0x72, - 0x65, 0x63, 0x74, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x73, 0x69, - 0x7a, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x63, 0x61, 0x63, 0x68, 0x65, 0x53, - 0x69, 0x7a, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x63, 0x70, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, - 0x52, 0x03, 0x63, 0x70, 0x75, 0x12, 0x10, 0x0a, 0x03, 0x72, 0x61, 0x6d, 0x18, 0x05, 0x20, 0x01, - 0x28, 0x0d, 0x52, 0x03, 0x72, 0x61, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x64, 0x18, 0x06, 0x20, - 0x01, 0x28, 0x0d, 0x52, 0x02, 0x68, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x70, 0x74, 0x69, 0x6d, - 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x70, 0x74, 0x69, 0x6d, 0x65, 0x22, - 0x33, 0x0a, 0x09, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x73, 0x12, 0x12, 0x0a, 0x04, - 0x73, 0x6f, 0x66, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, 0x6f, 0x66, 0x74, - 0x12, 0x12, 0x0a, 0x04, 0x68, 0x61, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, - 0x68, 0x61, 0x72, 0x64, 0x2a, 0x37, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x06, 0x0a, - 0x02, 0x4e, 0x41, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x47, 0x52, 0x41, 0x59, 0x10, 0x01, 0x12, - 0x09, 0x0a, 0x05, 0x47, 0x52, 0x45, 0x45, 0x4e, 0x10, 0x02, 0x12, 0x08, 0x0a, 0x04, 0x42, 0x4c, - 0x55, 0x45, 0x10, 0x03, 0x12, 0x07, 0x0a, 0x03, 0x52, 0x45, 0x44, 0x10, 0x04, 0x42, 0x05, 0x5a, - 0x03, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x3a, 0x02, 0x38, 0x01, 0x22, 0xc1, 0x01, 0x0a, 0x0a, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x53, 0x74, + 0x61, 0x74, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6e, 0x6f, 0x64, 0x65, 0x4b, 0x65, 0x79, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6e, 0x6f, 0x64, 0x65, 0x4b, 0x65, 0x79, 0x12, 0x1c, 0x0a, + 0x09, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x49, 0x44, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, + 0x52, 0x09, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x49, 0x44, 0x12, 0x10, 0x0a, 0x03, 0x75, + 0x72, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x1f, 0x0a, + 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, + 0x62, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1a, + 0x0a, 0x08, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x08, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, + 0x7a, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x12, 0x18, + 0x0a, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x04, 0x52, + 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x22, 0x96, 0x02, 0x0a, 0x0b, 0x47, 0x6f, 0x73, + 0x73, 0x69, 0x70, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x2d, 0x0a, 0x0a, 0x6c, 0x61, 0x73, 0x74, + 0x5f, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, + 0x62, 0x2e, 0x53, 0x68, 0x6f, 0x72, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x09, 0x6c, 0x61, + 0x73, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x30, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, + 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x62, 0x2e, + 0x53, 0x74, 0x61, 0x74, 0x65, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x52, 0x09, + 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x12, 0x2b, 0x0a, 0x09, 0x73, 0x74, 0x61, + 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, + 0x62, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x52, 0x09, 0x73, 0x74, 0x61, + 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x12, 0x33, 0x0a, 0x06, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x73, + 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x70, 0x62, 0x2e, 0x47, 0x6f, 0x73, 0x73, + 0x69, 0x70, 0x53, 0x74, 0x61, 0x74, 0x65, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x45, 0x6e, + 0x74, 0x72, 0x79, 0x52, 0x06, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x1a, 0x44, 0x0a, 0x0b, 0x43, + 0x68, 0x65, 0x63, 0x6b, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, + 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x1f, 0x0a, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x70, 0x62, + 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, + 0x01, 0x22, 0x5f, 0x0a, 0x05, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x24, 0x0a, 0x05, 0x73, 0x74, + 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x62, 0x2e, 0x53, + 0x68, 0x6f, 0x72, 0x74, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, + 0x12, 0x30, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x53, 0x69, + 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, + 0x72, 0x65, 0x22, 0x9a, 0x01, 0x0a, 0x0e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x53, 0x69, 0x67, 0x6e, + 0x61, 0x74, 0x75, 0x72, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, + 0x69, 0x67, 0x6e, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x73, 0x69, 0x67, + 0x6e, 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x6d, + 0x75, 0x6c, 0x74, 0x69, 0x48, 0x61, 0x73, 0x68, 0x54, 0x79, 0x70, 0x65, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x04, 0x52, 0x0d, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x48, 0x61, 0x73, 0x68, 0x54, 0x79, 0x70, + 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x04, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22, + 0xbf, 0x01, 0x0a, 0x09, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x12, 0x1a, 0x0a, + 0x08, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, + 0x08, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x2b, 0x0a, 0x09, 0x72, 0x65, 0x64, + 0x69, 0x72, 0x65, 0x63, 0x74, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x70, + 0x62, 0x2e, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x73, 0x52, 0x09, 0x72, 0x65, 0x64, + 0x69, 0x72, 0x65, 0x63, 0x74, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x61, 0x63, 0x68, 0x65, 0x5f, + 0x73, 0x69, 0x7a, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x63, 0x61, 0x63, 0x68, + 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x63, 0x70, 0x75, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x03, 0x63, 0x70, 0x75, 0x12, 0x10, 0x0a, 0x03, 0x72, 0x61, 0x6d, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x72, 0x61, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x64, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x02, 0x68, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x70, 0x74, + 0x69, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x70, 0x74, 0x69, 0x6d, + 0x65, 0x22, 0x33, 0x0a, 0x09, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x73, 0x12, 0x12, + 0x0a, 0x04, 0x73, 0x6f, 0x66, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, 0x6f, + 0x66, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x61, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, + 0x52, 0x04, 0x68, 0x61, 0x72, 0x64, 0x2a, 0x37, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, + 0x06, 0x0a, 0x02, 0x4e, 0x41, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x47, 0x52, 0x41, 0x59, 0x10, + 0x01, 0x12, 0x09, 0x0a, 0x05, 0x47, 0x52, 0x45, 0x45, 0x4e, 0x10, 0x02, 0x12, 0x08, 0x0a, 0x04, + 0x42, 0x4c, 0x55, 0x45, 0x10, 0x03, 0x12, 0x07, 0x0a, 0x03, 0x52, 0x45, 0x44, 0x10, 0x04, 0x42, + 0x05, 0x5a, 0x03, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( From 2e586c8e58a7e770cbb86aa2d2582f17596ac9f2 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 16:02:59 +0200 Subject: [PATCH 2/7] feat(topology): numeric 'nodeID' is replaced with stringified 'nodeKey' --- Makefile | 2 +- contract/pkg/bucket/messages.go | 14 +- .../pkg/cache/ddc_bucket_contract_cache.go | 6 +- contract/pkg/mock/ddc_bucket_contract_mock.go | 4 +- core/pkg/topology/sync/topology.go | 19 +- core/pkg/topology/topology.go | 33 ++-- core/pkg/topology/topology_test.go | 166 +++++++++--------- core/pkg/topology/vNode.go | 10 +- 8 files changed, 131 insertions(+), 123 deletions(-) diff --git a/Makefile b/Makefile index 7fe2c69..1ef3bd4 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ packages = ./model/... ./core/... ./contract/... ./contentadrstorage/... -test: +tests: go test ${packages} lint: diff --git a/contract/pkg/bucket/messages.go b/contract/pkg/bucket/messages.go index 6040ce2..76492a9 100644 --- a/contract/pkg/bucket/messages.go +++ b/contract/pkg/bucket/messages.go @@ -12,7 +12,7 @@ type ( Balance = types.U128 Cash = Balance Resource = uint32 - NodeId = uint32 + NodeKey = string Token = uint64 ClusterId = uint32 AccountId = types.AccountID @@ -45,7 +45,7 @@ type Cluster struct { ResourcePerVNode Resource ResourceUsed Resource Revenues Cash - Nodes []NodeId + Nodes []NodeKey VNodes [][]Token TotalRent Balance CdnNodesKeys []string @@ -66,7 +66,7 @@ type ClusterStatus struct { type CDNCluster struct { ManagerId AccountId - CDNNodes []NodeId + CDNNodes []NodeKey ResourceUsed Resource Revenues Cash UsdPerGb Balance @@ -161,12 +161,12 @@ type ClusterCreatedEvent struct { type ClusterNodeReplacedEvent struct { ClusterId ClusterId - NodeId NodeId + NodeKey NodeKey } type ClusterReserveResourceEvent struct { ClusterId ClusterId - NodeId NodeId + NodeKey NodeKey } type ClusterDistributeRevenuesEvent struct { @@ -185,13 +185,13 @@ type CdnClusterDistributeRevenuesEvent struct { } type CdnNodeCreatedEvent struct { - NodeId NodeId + NodeKey NodeKey AccountId AccountId Payment Balance } type NodeCreatedEvent struct { - NodeId NodeId + NodeKey NodeKey ProviderId AccountId RentPerMonth Balance NodeParams Params diff --git a/contract/pkg/cache/ddc_bucket_contract_cache.go b/contract/pkg/cache/ddc_bucket_contract_cache.go index 8e7107a..da2f5aa 100644 --- a/contract/pkg/cache/ddc_bucket_contract_cache.go +++ b/contract/pkg/cache/ddc_bucket_contract_cache.go @@ -25,7 +25,7 @@ type ( ClearNodes() ClearBuckets() ClearAccounts() - ClearNodeById(id bucket.NodeId) + ClearNodeById(id bucket.NodeKey) ClearBucketById(id bucket.BucketId) ClearAccountById(id bucket.AccountId) bucket.DdcBucketContract @@ -200,8 +200,8 @@ func (d *ddcBucketContractCached) ClearAccounts() { d.accountCache.Flush() } -func (d *ddcBucketContractCached) ClearNodeById(id bucket.NodeId) { //nolint:golint,unused - d.nodeCache.Delete(toString(id)) +func (d *ddcBucketContractCached) ClearNodeById(key bucket.NodeKey) { //nolint:golint,unused + d.nodeCache.Delete(key) } func (d *ddcBucketContractCached) ClearNodeByKey(nodeKey string) { diff --git a/contract/pkg/mock/ddc_bucket_contract_mock.go b/contract/pkg/mock/ddc_bucket_contract_mock.go index 5012b0e..24146e0 100644 --- a/contract/pkg/mock/ddc_bucket_contract_mock.go +++ b/contract/pkg/mock/ddc_bucket_contract_mock.go @@ -37,7 +37,7 @@ type ( Cluster struct { Id uint32 VNodes [][]uint64 - Nodes []uint32 + Nodes []string Params string } @@ -55,7 +55,7 @@ type ( CDNCluster struct { Id uint32 - Nodes []uint32 + Nodes []string } ddcBucketContractMock struct { diff --git a/core/pkg/topology/sync/topology.go b/core/pkg/topology/sync/topology.go index 1f19fe5..d82ca58 100644 --- a/core/pkg/topology/sync/topology.go +++ b/core/pkg/topology/sync/topology.go @@ -1,8 +1,9 @@ package sync import ( - "github.com/cerebellum-network/cere-ddc-sdk-go/core/pkg/topology" "sync" + + "github.com/cerebellum-network/cere-ddc-sdk-go/core/pkg/topology" ) type ring struct { @@ -10,15 +11,15 @@ type ring struct { mutex sync.RWMutex } -func NewTopology(nodeIds []uint32, vNodes [][]uint64, replicaFactor uint) topology.Ring { +func NewTopology(nodeKeys []string, vNodes [][]uint64, replicaFactor uint) topology.Ring { return &ring{ - ring: topology.NewTopology(nodeIds, vNodes, replicaFactor), + ring: topology.NewTopology(nodeKeys, vNodes, replicaFactor), } } -func (r *ring) Tokens(nodeId uint32) []uint64 { +func (r *ring) Tokens(nodeKey string) []uint64 { r.mutex.RLock() - result := r.ring.Tokens(nodeId) + result := r.ring.Tokens(nodeKey) r.mutex.RUnlock() return result @@ -40,17 +41,17 @@ func (r *ring) Replicas(token uint64) []topology.VNode { return result } -func (r *ring) Partitions(nodeId uint32) []topology.Partition { +func (r *ring) Partitions(nodeKey string) []topology.Partition { r.mutex.RLock() - result := r.ring.Partitions(nodeId) + result := r.ring.Partitions(nodeKey) r.mutex.RUnlock() return result } -func (r *ring) ExcessPartitions(nodeId uint32) []topology.Partition { +func (r *ring) ExcessPartitions(nodeKey string) []topology.Partition { r.mutex.RLock() - result := r.ring.ExcessPartitions(nodeId) + result := r.ring.ExcessPartitions(nodeKey) r.mutex.RUnlock() return result diff --git a/core/pkg/topology/topology.go b/core/pkg/topology/topology.go index 9c97bb3..0f8b019 100644 --- a/core/pkg/topology/topology.go +++ b/core/pkg/topology/topology.go @@ -1,18 +1,19 @@ package topology import ( - "github.com/cerebellum-network/cere-ddc-sdk-go/core/pkg/utils" "sort" + + "github.com/cerebellum-network/cere-ddc-sdk-go/core/pkg/utils" ) type ( Ring interface { - Tokens(nodeId uint32) []uint64 + Tokens(nodeKey string) []uint64 Neighbours(token uint64) (VNode, VNode) Replicas(token uint64) []VNode - Partitions(nodeId uint32) []Partition - ExcessPartitions(nodeId uint32) []Partition + Partitions(nodeKey string) []Partition + ExcessPartitions(nodeKey string) []Partition RemoveVNode(token uint64) bool @@ -26,17 +27,17 @@ type ( } ) -func NewTopology(nodeIds []uint32, vNodes [][]uint64, replicaFactor uint) Ring { +func NewTopology(NodesKeys []string, vNodes [][]uint64, replicaFactor uint) Ring { if replicaFactor == 0 { replicaFactor = 1 } topologyVNodes := make([]VNode, 0) - for i, nodeId := range nodeIds { + for i, nodeKey := range NodesKeys { for _, token := range vNodes[i] { topologyVNode := VNode{ - nodeId: nodeId, - token: token, + nodeKey: nodeKey, + token: token, } topologyVNodes = append(topologyVNodes, topologyVNode) @@ -53,10 +54,10 @@ func NewTopology(nodeIds []uint32, vNodes [][]uint64, replicaFactor uint) Ring { } } -func (r *ring) Tokens(nodeId uint32) []uint64 { +func (r *ring) Tokens(nodeKey string) []uint64 { result := make([]uint64, 0) - r.vNodeDo(nodeId, func(i int, vNode VNode) { + r.vNodeDo(nodeKey, func(i int, vNode VNode) { result = append(result, vNode.token) }) @@ -90,9 +91,9 @@ func (r *ring) Neighbours(token uint64) (prev VNode, next VNode) { return } -func (r *ring) Partitions(nodeId uint32) []Partition { +func (r *ring) Partitions(nodeKey string) []Partition { result := make([]Partition, 0) - r.vNodeDo(nodeId, func(i int, vNode VNode) { + r.vNodeDo(nodeKey, func(i int, vNode VNode) { for j := uint(1); j < r.replicationFactor; j++ { i = r.prevIndex(i) } @@ -108,8 +109,8 @@ func (r *ring) Partitions(nodeId uint32) []Partition { return result } -func (r *ring) ExcessPartitions(nodeId uint32) []Partition { - partitions := r.Partitions(nodeId) +func (r *ring) ExcessPartitions(nodeKey string) []Partition { + partitions := r.Partitions(nodeKey) result := make([]Partition, 0) for i := 0; i < len(partitions); i++ { @@ -155,9 +156,9 @@ func (r *ring) nextIndex(i int) int { return nextIndex(i, len(r.vNodes)) } -func (r *ring) vNodeDo(nodeId uint32, do func(int, VNode)) { +func (r *ring) vNodeDo(nodeKey string, do func(int, VNode)) { for i, vNode := range r.vNodes { - if nodeId == vNode.nodeId { + if nodeKey == vNode.nodeKey { do(i, vNode) } } diff --git a/core/pkg/topology/topology_test.go b/core/pkg/topology/topology_test.go index 35850b2..c9d4ee5 100644 --- a/core/pkg/topology/topology_test.go +++ b/core/pkg/topology/topology_test.go @@ -1,33 +1,39 @@ package topology import ( - "github.com/stretchr/testify/assert" "sort" "testing" + + "github.com/stretchr/testify/assert" ) +var nodeKey1 = "e5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a" +var nodeKey2 = "8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" +var nodeKey3 = "90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22" +var nodeKey4 = "306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20" + var clusters = []struct { name string - nodeIds []uint32 + nodeKeys []string vNodes [][]uint64 replicaFactor uint }{ - {"size 2 replication 3", []uint32{1, 2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 2}, - {"size 3 replication 3", []uint32{1, 2, 3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 3}, - {"size 4 replication 3", []uint32{1, 2, 3, 4}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}, {4611686018427387903}}, 3}, - {"size 2 replication 1", []uint32{1, 2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 1}, - {"size 3 replication 1", []uint32{1, 2, 3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 1}, + {"size 2 replication 3", []string{nodeKey1, nodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 2}, + {"size 3 replication 3", []string{nodeKey1, nodeKey2, nodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 3}, + {"size 4 replication 3", []string{nodeKey1, nodeKey2, nodeKey3, nodeKey4}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}, {4611686018427387903}}, 3}, + {"size 2 replication 1", []string{nodeKey1, nodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 1}, + {"size 3 replication 1", []string{nodeKey1, nodeKey2, nodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 1}, } func TestTokens(t *testing.T) { for _, test := range clusters { t.Run(test.name, func(t *testing.T) { //given - testSubject := NewTopology(test.nodeIds, test.vNodes, test.replicaFactor) + testSubject := NewTopology(test.nodeKeys, test.vNodes, test.replicaFactor) - for i, nodeId := range test.nodeIds { + for i, nodeKey := range test.nodeKeys { //when - tokens := testSubject.Tokens(nodeId) + tokens := testSubject.Tokens(nodeKey) //then assert.True(t, sort.SliceIsSorted(tokens, func(i, j int) bool { return tokens[i] < tokens[j] })) @@ -49,19 +55,19 @@ func TestNeighbours(t *testing.T) { prev VNode next VNode }{ - {0, 3074457345618258602, VNode{2, 18446744073709551612}, VNode{2, 6148914691236517204}}, - {0, 3074457345618258602 + 100, VNode{1, 3074457345618258602}, VNode{2, 6148914691236517204}}, - {0, 18446744073709551612, VNode{1, 15372286728091293010}, VNode{1, 3074457345618258602}}, - {0, 18446744073709551612 + 1, VNode{2, 18446744073709551612}, VNode{1, 3074457345618258602}}, - {1, 6148914691236517204, VNode{1, 3074457345618258602}, VNode{3, 9223372036854775806}}, - {1, 6148914691236517204 + 100, VNode{2, 6148914691236517204}, VNode{3, 9223372036854775806}}, + {0, 3074457345618258602, VNode{nodeKey2, 18446744073709551612}, VNode{nodeKey2, 6148914691236517204}}, + {0, 3074457345618258602 + 100, VNode{nodeKey1, 3074457345618258602}, VNode{nodeKey2, 6148914691236517204}}, + {0, 18446744073709551612, VNode{nodeKey1, 15372286728091293010}, VNode{nodeKey1, 3074457345618258602}}, + {0, 18446744073709551612 + 1, VNode{nodeKey2, 18446744073709551612}, VNode{nodeKey1, 3074457345618258602}}, + {1, 6148914691236517204, VNode{nodeKey1, 3074457345618258602}, VNode{nodeKey3, 9223372036854775806}}, + {1, 6148914691236517204 + 100, VNode{nodeKey2, 6148914691236517204}, VNode{nodeKey3, 9223372036854775806}}, } for _, test := range tests { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeIds, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) //when prev, next := testSubject.Neighbours(test.token) @@ -80,21 +86,21 @@ func TestReplicas(t *testing.T) { token uint64 replicas []VNode }{ - {0, 3074457345618258602, []VNode{{1, 3074457345618258602}, {2, 6148914691236517204}}}, - {0, 3074457345618258602 + 100, []VNode{{1, 3074457345618258602}, {2, 6148914691236517204}}}, - {0, 18446744073709551612, []VNode{{2, 18446744073709551612}, {1, 3074457345618258602}}}, - {0, 18446744073709551612 + 1, []VNode{{2, 18446744073709551612}, {1, 3074457345618258602}}}, - {1, 6148914691236517204, []VNode{{2, 6148914691236517204}, {3, 9223372036854775806}, {1, 12297829382473034408}}}, - {1, 6148914691236517204 + 10, []VNode{{2, 6148914691236517204}, {3, 9223372036854775806}, {1, 12297829382473034408}}}, - {2, 6980919141067302587, []VNode{{2, 6148914691236517204}, {3, 9223372036854775806}, {1, 12297829382473034408}}}, - {2, 7577601381952616217, []VNode{{2, 6148914691236517204}, {3, 9223372036854775806}, {1, 12297829382473034408}}}, + {0, 3074457345618258602, []VNode{{nodeKey1, 3074457345618258602}, {nodeKey2, 6148914691236517204}}}, + {0, 3074457345618258602 + 100, []VNode{{nodeKey1, 3074457345618258602}, {nodeKey2, 6148914691236517204}}}, + {0, 18446744073709551612, []VNode{{nodeKey2, 18446744073709551612}, {nodeKey1, 3074457345618258602}}}, + {0, 18446744073709551612 + 1, []VNode{{nodeKey2, 18446744073709551612}, {nodeKey1, 3074457345618258602}}}, + {1, 6148914691236517204, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, + {1, 6148914691236517204 + 10, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, + {2, 6980919141067302587, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, + {2, 7577601381952616217, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, } for _, test := range tests { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeIds, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) //when replicas := testSubject.Replicas(test.token) @@ -111,66 +117,66 @@ func TestReplicas(t *testing.T) { func TestPartitions(t *testing.T) { tests := []struct { clusterId int - nodeId uint32 + nodeKey string partitions []Partition }{ - {3, 1, + {3, nodeKey1, []Partition{ - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeId: 1, token: 3074457345618258602}}}, - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeId: 1, token: 9223372036854775806}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeId: 1, token: 15372286728091293010}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 9223372036854775806}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 15372286728091293010}}}, }, }, - {3, 2, + {3, nodeKey2, []Partition{ - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeId: 2, token: 6148914691236517204}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeId: 2, token: 12297829382473034408}}}, - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeId: 2, token: 18446744073709551612}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 12297829382473034408}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 18446744073709551612}}}, }, }, - {4, 1, + {4, nodeKey1, []Partition{ - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeId: 1, token: 3074457345618258602}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeId: 1, token: 12297829382473034408}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 12297829382473034408}}}, }, }, - {4, 2, + {4, nodeKey2, []Partition{ - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeId: 2, token: 6148914691236517204}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeId: 2, token: 15372286728091293010}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 15372286728091293010}}}, }, }, - {4, 3, + {4, nodeKey3, []Partition{ - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeId: 3, token: 9223372036854775806}}}, - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeId: 3, token: 18446744073709551612}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey3, token: 9223372036854775806}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey3, token: 18446744073709551612}}}, }, }, - {0, 1, + {0, nodeKey1, []Partition{ - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeId: 2, token: 18446744073709551612}, {nodeId: 1, token: 3074457345618258602}}}, - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeId: 1, token: 3074457345618258602}, {nodeId: 2, token: 6148914691236517204}}}, - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeId: 2, token: 6148914691236517204}, {nodeId: 1, token: 9223372036854775806}}}, - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeId: 1, token: 9223372036854775806}, {nodeId: 2, token: 12297829382473034408}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeId: 2, token: 12297829382473034408}, {nodeId: 1, token: 15372286728091293010}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeId: 1, token: 15372286728091293010}, {nodeId: 2, token: 18446744073709551612}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 18446744073709551612}, {nodeKey: nodeKey1, token: 3074457345618258602}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey2, token: 6148914691236517204}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}, {nodeKey: nodeKey1, token: 9223372036854775806}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 9223372036854775806}, {nodeKey: nodeKey2, token: 12297829382473034408}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 12297829382473034408}, {nodeKey: nodeKey1, token: 15372286728091293010}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 15372286728091293010}, {nodeKey: nodeKey2, token: 18446744073709551612}}}, }, }, - {0, 2, + {0, nodeKey2, []Partition{ - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeId: 1, token: 3074457345618258602}, {nodeId: 2, token: 6148914691236517204}}}, - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeId: 2, token: 6148914691236517204}, {nodeId: 1, token: 9223372036854775806}}}, - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeId: 1, token: 9223372036854775806}, {nodeId: 2, token: 12297829382473034408}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeId: 2, token: 12297829382473034408}, {nodeId: 1, token: 15372286728091293010}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeId: 1, token: 15372286728091293010}, {nodeId: 2, token: 18446744073709551612}}}, - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeId: 2, token: 18446744073709551612}, {nodeId: 1, token: 3074457345618258602}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey2, token: 6148914691236517204}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}, {nodeKey: nodeKey1, token: 9223372036854775806}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 9223372036854775806}, {nodeKey: nodeKey2, token: 12297829382473034408}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 12297829382473034408}, {nodeKey: nodeKey1, token: 15372286728091293010}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 15372286728091293010}, {nodeKey: nodeKey2, token: 18446744073709551612}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 18446744073709551612}, {nodeKey: nodeKey1, token: 3074457345618258602}}}, }, }, - {2, 4, + {2, nodeKey4, []Partition{ - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeId: 3, token: 18446744073709551612}, {nodeId: 1, token: 3074457345618258602}, {nodeId: 4, token: 4611686018427387903}}}, - {From: 3074457345618258602, To: 4611686018427387903 - 1, VNodes: []VNode{{nodeId: 1, token: 3074457345618258602}, {nodeId: 4, token: 4611686018427387903}, {nodeId: 2, token: 6148914691236517204}}}, - {From: 4611686018427387903, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeId: 4, token: 4611686018427387903}, {nodeId: 2, token: 6148914691236517204}, {nodeId: 3, token: 9223372036854775806}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey3, token: 18446744073709551612}, {nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey4, token: 4611686018427387903}}}, + {From: 3074457345618258602, To: 4611686018427387903 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey4, token: 4611686018427387903}, {nodeKey: nodeKey2, token: 6148914691236517204}}}, + {From: 4611686018427387903, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey4, token: 4611686018427387903}, {nodeKey: nodeKey2, token: 6148914691236517204}, {nodeKey: nodeKey3, token: 9223372036854775806}}}, }, }, } @@ -179,10 +185,10 @@ func TestPartitions(t *testing.T) { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeIds, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) //when - partitions := testSubject.Partitions(test.nodeId) + partitions := testSubject.Partitions(test.nodeKey) //then assert.Len(t, partitions, len(test.partitions)) @@ -196,28 +202,28 @@ func TestPartitions(t *testing.T) { func TestExcessPartitions(t *testing.T) { tests := []struct { clusterId int - nodeId uint32 + nodeKey string excessPartitions []Partition }{ - {3, 1, []Partition{{From: 6148914691236517204, To: 9223372036854775806 - 1}, {From: 12297829382473034408, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 3074457345618258602 - 1}}}, - {3, 2, []Partition{{From: 9223372036854775806, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 6148914691236517204 - 1}}}, - {4, 1, []Partition{{From: 6148914691236517204, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 3074457345618258602 - 1}}}, - {4, 2, []Partition{{From: 9223372036854775806, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 6148914691236517204 - 1}}}, - {4, 3, []Partition{{From: 12297829382473034408, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 9223372036854775806 - 1}}}, - {0, 1, []Partition{}}, - {0, 2, []Partition{}}, - {0, 3, []Partition{}}, - {2, 4, []Partition{{From: 6148914691236517204, To: 18446744073709551612 - 1}}}, + {3, nodeKey1, []Partition{{From: 6148914691236517204, To: 9223372036854775806 - 1}, {From: 12297829382473034408, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 3074457345618258602 - 1}}}, + {3, nodeKey2, []Partition{{From: 9223372036854775806, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 6148914691236517204 - 1}}}, + {4, nodeKey1, []Partition{{From: 6148914691236517204, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 3074457345618258602 - 1}}}, + {4, nodeKey2, []Partition{{From: 9223372036854775806, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 6148914691236517204 - 1}}}, + {4, nodeKey3, []Partition{{From: 12297829382473034408, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 9223372036854775806 - 1}}}, + {0, nodeKey1, []Partition{}}, + {0, nodeKey2, []Partition{}}, + {0, nodeKey3, []Partition{}}, + {2, nodeKey4, []Partition{{From: 6148914691236517204, To: 18446744073709551612 - 1}}}, } for _, test := range tests { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeIds, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) //when - partitions := testSubject.ExcessPartitions(test.nodeId) + partitions := testSubject.ExcessPartitions(test.nodeKey) //then assert.Len(t, partitions, len(test.excessPartitions)) @@ -236,15 +242,15 @@ func TestRemoveVNode(t *testing.T) { expectIsRemoved bool expected []VNode }{ - {0, 100, false, []VNode{{token: 3074457345618258602, nodeId: 1}, {token: 6148914691236517204, nodeId: 2}, {token: 9223372036854775806, nodeId: 1}, {token: 12297829382473034408, nodeId: 2}, {token: 15372286728091293010, nodeId: 1}, {token: 18446744073709551612, nodeId: 2}}}, - {0, 9223372036854775806, true, []VNode{{token: 3074457345618258602, nodeId: 1}, {token: 6148914691236517204, nodeId: 2}, {token: 12297829382473034408, nodeId: 2}, {token: 15372286728091293010, nodeId: 1}, {token: 18446744073709551612, nodeId: 2}}}, - {0, 12297829382473034408, true, []VNode{{token: 3074457345618258602, nodeId: 1}, {token: 6148914691236517204, nodeId: 2}, {token: 9223372036854775806, nodeId: 1}, {token: 15372286728091293010, nodeId: 1}, {token: 18446744073709551612, nodeId: 2}}}, + {0, 100, false, []VNode{{token: 3074457345618258602, nodeKey: nodeKey1}, {token: 6148914691236517204, nodeKey: nodeKey2}, {token: 9223372036854775806, nodeKey: nodeKey1}, {token: 12297829382473034408, nodeKey: nodeKey2}, {token: 15372286728091293010, nodeKey: nodeKey1}, {token: 18446744073709551612, nodeKey: nodeKey2}}}, + {0, 9223372036854775806, true, []VNode{{token: 3074457345618258602, nodeKey: nodeKey1}, {token: 6148914691236517204, nodeKey: nodeKey2}, {token: 12297829382473034408, nodeKey: nodeKey2}, {token: 15372286728091293010, nodeKey: nodeKey1}, {token: 18446744073709551612, nodeKey: nodeKey2}}}, + {0, 12297829382473034408, true, []VNode{{token: 3074457345618258602, nodeKey: nodeKey1}, {token: 6148914691236517204, nodeKey: nodeKey2}, {token: 9223372036854775806, nodeKey: nodeKey1}, {token: 15372286728091293010, nodeKey: nodeKey1}, {token: 18446744073709551612, nodeKey: nodeKey2}}}, } for _, test := range tests { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeIds, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) //when ok := testSubject.RemoveVNode(test.token) diff --git a/core/pkg/topology/vNode.go b/core/pkg/topology/vNode.go index 15af732..db8c2b7 100644 --- a/core/pkg/topology/vNode.go +++ b/core/pkg/topology/vNode.go @@ -3,12 +3,12 @@ package topology import "fmt" type VNode struct { - nodeId uint32 - token uint64 + nodeKey string + token uint64 } -func (v VNode) NodeId() uint32 { - return v.nodeId +func (v VNode) NodeKey() string { + return v.nodeKey } func (v VNode) Token() uint64 { @@ -16,5 +16,5 @@ func (v VNode) Token() uint64 { } func (v VNode) String() string { - return fmt.Sprintf("{\"nodeId\":%d,\"token\":%d}", v.nodeId, v.token) + return fmt.Sprintf("{\"nodeKey\":%v,\"token\":%d}", v.nodeKey, v.token) } From 4bc94f8d93928811096611c35322c4a4f31f1eb1 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 19:11:37 +0200 Subject: [PATCH 3/7] feat(cluster): cluster response struct is updated --- contract/pkg/bucket/messages.go | 28 ++++++++++--------- contract/pkg/bucket/messages_test.go | 22 +++++++++------ contract/pkg/mock/ddc_bucket_contract_mock.go | 25 ++++++++++++++--- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/contract/pkg/bucket/messages.go b/contract/pkg/bucket/messages.go index 76492a9..70b5f4f 100644 --- a/contract/pkg/bucket/messages.go +++ b/contract/pkg/bucket/messages.go @@ -24,15 +24,13 @@ type ( ) const ( - UNKNOWN NodeState = iota - ADDING + ADDING = iota ACTIVE DELETING OFFLINE ) var NodeTags = map[string]byte{ - "UNKNOWN": UNKNOWN, "ADDING": ADDING, "ACTIVE": ACTIVE, "DELETING": DELETING, @@ -41,27 +39,31 @@ var NodeTags = map[string]byte{ type Cluster struct { ManagerId AccountId - NodesKeys []string + Params Params + NodesKeys []NodeKey ResourcePerVNode Resource ResourceUsed Resource Revenues Cash - Nodes []NodeKey - VNodes [][]Token TotalRent Balance - CdnNodesKeys []string - CdnUsdPerGb Balance + CdnNodesKeys []NodeKey CdnRevenues Cash + CdnUsdPerGb Balance } -type NewCluster struct { - Params Params - ResourcePerVNode Resource +type NodeVNodesInfo struct { + NodeKey NodeKey + VNodes []Token } type ClusterStatus struct { ClusterId ClusterId Cluster Cluster - Params Params + VNodes []NodeVNodesInfo +} + +type NewCluster struct { + Params Params + ResourcePerVNode Resource } type CDNCluster struct { @@ -222,7 +224,7 @@ type ClusterParams struct { func (c *ClusterStatus) ReplicationFactor() uint { params := &ClusterParams{} - err := json.Unmarshal([]byte(c.Params), params) + err := json.Unmarshal([]byte(c.Cluster.Params), params) if err != nil || params.ReplicationFactor <= 0 { return 0 } diff --git a/contract/pkg/bucket/messages_test.go b/contract/pkg/bucket/messages_test.go index df9dcc1..ab3e172 100644 --- a/contract/pkg/bucket/messages_test.go +++ b/contract/pkg/bucket/messages_test.go @@ -2,10 +2,11 @@ package bucket import ( "encoding/hex" - "github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg" "strings" "testing" + "github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg" + "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/stretchr/testify/assert" ) @@ -30,7 +31,7 @@ func TestClusterStatus_ReplicationFactor(t *testing.T) { type fields struct { ClusterId ClusterId Cluster Cluster - Params Params + VNodes []NodeVNodesInfo } tests := []struct { name string @@ -41,8 +42,9 @@ func TestClusterStatus_ReplicationFactor(t *testing.T) { name: "ReplicationFactor as integer", fields: fields{ ClusterId: 1, - Params: `{"ReplicationFactor": 3}`, - Cluster: Cluster{}, + Cluster: Cluster{ + Params: `{"ReplicationFactor": 3}`, + }, }, want: 3, }, @@ -50,8 +52,9 @@ func TestClusterStatus_ReplicationFactor(t *testing.T) { name: "ReplicationFactor as string", fields: fields{ ClusterId: 1, - Params: `{"ReplicationFactor": "3"}`, - Cluster: Cluster{}, + Cluster: Cluster{ + Params: `{"ReplicationFactor": "3"}`, + }, }, want: 3, }, @@ -59,8 +62,9 @@ func TestClusterStatus_ReplicationFactor(t *testing.T) { name: "ReplicationFactor not set", fields: fields{ ClusterId: 1, - Params: `{}`, - Cluster: Cluster{}, + Cluster: Cluster{ + Params: `{}`, + }, }, want: 0, }, @@ -70,7 +74,7 @@ func TestClusterStatus_ReplicationFactor(t *testing.T) { c := &ClusterStatus{ ClusterId: tt.fields.ClusterId, Cluster: tt.fields.Cluster, - Params: tt.fields.Params, + VNodes: tt.fields.VNodes, } assert.Equalf(t, tt.want, c.ReplicationFactor(), "ReplicationFactor()") }) diff --git a/contract/pkg/mock/ddc_bucket_contract_mock.go b/contract/pkg/mock/ddc_bucket_contract_mock.go index 24146e0..037186a 100644 --- a/contract/pkg/mock/ddc_bucket_contract_mock.go +++ b/contract/pkg/mock/ddc_bucket_contract_mock.go @@ -36,11 +36,16 @@ type ( Cluster struct { Id uint32 - VNodes [][]uint64 + VNodes []NodeVNodes Nodes []string Params string } + NodeVNodes struct { + NodeKey string + VNodes []uint64 + } + CDNNode struct { Key string `json:"key"` Params CDNNodeParams `json:"params"` @@ -69,6 +74,18 @@ type ( } ) +func mapSourceToDestination(vNodes []NodeVNodes) []bucket.NodeVNodesInfo { + var nodesVNodes []bucket.NodeVNodesInfo + for _, item := range vNodes { + nodeVNodes := bucket.NodeVNodesInfo{ + NodeKey: item.NodeKey, + VNodes: item.VNodes, + } + nodesVNodes = append(nodesVNodes, nodeVNodes) + } + return nodesVNodes +} + func CreateDdcBucketContractMock(apiUrl string, accountId string, nodes []Node, clusters []Cluster, cdnNodes []CDNNode, cdnClusters []CDNCluster) bucket.DdcBucketContract { log.Info("DDC Bucket contract configured [MOCK]") return &ddcBucketContractMock{ @@ -102,14 +119,14 @@ func (d *ddcBucketContractMock) ClusterGet(clusterId uint32) (*bucket.ClusterSta ClusterId: clusterId, Cluster: bucket.Cluster{ ManagerId: types.AccountID{}, - Nodes: cluster.Nodes, - VNodes: cluster.VNodes, + Params: cluster.Params, + NodesKeys: cluster.Nodes, ResourcePerVNode: 32, ResourceUsed: 0, Revenues: types.NewU128(*big.NewInt(1)), TotalRent: types.NewU128(*big.NewInt(1)), }, - Params: cluster.Params, + VNodes: mapSourceToDestination(cluster.VNodes), }, nil } } From e391485fabe5ea783257a257db6a417cc590ef5a Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 20:07:02 +0200 Subject: [PATCH 4/7] test(scheduler): making testable node keys public --- core/pkg/topology/topology_test.go | 143 +++++++++++++++-------------- 1 file changed, 73 insertions(+), 70 deletions(-) diff --git a/core/pkg/topology/topology_test.go b/core/pkg/topology/topology_test.go index c9d4ee5..7a40206 100644 --- a/core/pkg/topology/topology_test.go +++ b/core/pkg/topology/topology_test.go @@ -7,10 +7,13 @@ import ( "github.com/stretchr/testify/assert" ) -var nodeKey1 = "e5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a" -var nodeKey2 = "8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" -var nodeKey3 = "90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22" -var nodeKey4 = "306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20" +var NodeKey1 = "e5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a" +var NodeKey2 = "8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" +var NodeKey3 = "90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22" +var NodeKey4 = "306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20" +var NodeKey5 = "e659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e" +var NodeKey6 = "1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c" +var NodeKey7 = "be5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f" var clusters = []struct { name string @@ -18,11 +21,11 @@ var clusters = []struct { vNodes [][]uint64 replicaFactor uint }{ - {"size 2 replication 3", []string{nodeKey1, nodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 2}, - {"size 3 replication 3", []string{nodeKey1, nodeKey2, nodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 3}, - {"size 4 replication 3", []string{nodeKey1, nodeKey2, nodeKey3, nodeKey4}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}, {4611686018427387903}}, 3}, - {"size 2 replication 1", []string{nodeKey1, nodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 1}, - {"size 3 replication 1", []string{nodeKey1, nodeKey2, nodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 1}, + {"size 2 replication 3", []string{NodeKey1, NodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 2}, + {"size 3 replication 3", []string{NodeKey1, NodeKey2, NodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 3}, + {"size 4 replication 3", []string{NodeKey1, NodeKey2, NodeKey3, NodeKey4}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}, {4611686018427387903}}, 3}, + {"size 2 replication 1", []string{NodeKey1, NodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 1}, + {"size 3 replication 1", []string{NodeKey1, NodeKey2, NodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 1}, } func TestTokens(t *testing.T) { @@ -55,12 +58,12 @@ func TestNeighbours(t *testing.T) { prev VNode next VNode }{ - {0, 3074457345618258602, VNode{nodeKey2, 18446744073709551612}, VNode{nodeKey2, 6148914691236517204}}, - {0, 3074457345618258602 + 100, VNode{nodeKey1, 3074457345618258602}, VNode{nodeKey2, 6148914691236517204}}, - {0, 18446744073709551612, VNode{nodeKey1, 15372286728091293010}, VNode{nodeKey1, 3074457345618258602}}, - {0, 18446744073709551612 + 1, VNode{nodeKey2, 18446744073709551612}, VNode{nodeKey1, 3074457345618258602}}, - {1, 6148914691236517204, VNode{nodeKey1, 3074457345618258602}, VNode{nodeKey3, 9223372036854775806}}, - {1, 6148914691236517204 + 100, VNode{nodeKey2, 6148914691236517204}, VNode{nodeKey3, 9223372036854775806}}, + {0, 3074457345618258602, VNode{NodeKey2, 18446744073709551612}, VNode{NodeKey2, 6148914691236517204}}, + {0, 3074457345618258602 + 100, VNode{NodeKey1, 3074457345618258602}, VNode{NodeKey2, 6148914691236517204}}, + {0, 18446744073709551612, VNode{NodeKey1, 15372286728091293010}, VNode{NodeKey1, 3074457345618258602}}, + {0, 18446744073709551612 + 1, VNode{NodeKey2, 18446744073709551612}, VNode{NodeKey1, 3074457345618258602}}, + {1, 6148914691236517204, VNode{NodeKey1, 3074457345618258602}, VNode{NodeKey3, 9223372036854775806}}, + {1, 6148914691236517204 + 100, VNode{NodeKey2, 6148914691236517204}, VNode{NodeKey3, 9223372036854775806}}, } for _, test := range tests { @@ -86,14 +89,14 @@ func TestReplicas(t *testing.T) { token uint64 replicas []VNode }{ - {0, 3074457345618258602, []VNode{{nodeKey1, 3074457345618258602}, {nodeKey2, 6148914691236517204}}}, - {0, 3074457345618258602 + 100, []VNode{{nodeKey1, 3074457345618258602}, {nodeKey2, 6148914691236517204}}}, - {0, 18446744073709551612, []VNode{{nodeKey2, 18446744073709551612}, {nodeKey1, 3074457345618258602}}}, - {0, 18446744073709551612 + 1, []VNode{{nodeKey2, 18446744073709551612}, {nodeKey1, 3074457345618258602}}}, - {1, 6148914691236517204, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, - {1, 6148914691236517204 + 10, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, - {2, 6980919141067302587, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, - {2, 7577601381952616217, []VNode{{nodeKey2, 6148914691236517204}, {nodeKey3, 9223372036854775806}, {nodeKey1, 12297829382473034408}}}, + {0, 3074457345618258602, []VNode{{NodeKey1, 3074457345618258602}, {NodeKey2, 6148914691236517204}}}, + {0, 3074457345618258602 + 100, []VNode{{NodeKey1, 3074457345618258602}, {NodeKey2, 6148914691236517204}}}, + {0, 18446744073709551612, []VNode{{NodeKey2, 18446744073709551612}, {NodeKey1, 3074457345618258602}}}, + {0, 18446744073709551612 + 1, []VNode{{NodeKey2, 18446744073709551612}, {NodeKey1, 3074457345618258602}}}, + {1, 6148914691236517204, []VNode{{NodeKey2, 6148914691236517204}, {NodeKey3, 9223372036854775806}, {NodeKey1, 12297829382473034408}}}, + {1, 6148914691236517204 + 10, []VNode{{NodeKey2, 6148914691236517204}, {NodeKey3, 9223372036854775806}, {NodeKey1, 12297829382473034408}}}, + {2, 6980919141067302587, []VNode{{NodeKey2, 6148914691236517204}, {NodeKey3, 9223372036854775806}, {NodeKey1, 12297829382473034408}}}, + {2, 7577601381952616217, []VNode{{NodeKey2, 6148914691236517204}, {NodeKey3, 9223372036854775806}, {NodeKey1, 12297829382473034408}}}, } for _, test := range tests { @@ -120,63 +123,63 @@ func TestPartitions(t *testing.T) { nodeKey string partitions []Partition }{ - {3, nodeKey1, + {3, NodeKey1, []Partition{ - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}}}, - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 9223372036854775806}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 15372286728091293010}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 3074457345618258602}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 9223372036854775806}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 15372286728091293010}}}, }, }, - {3, nodeKey2, + {3, NodeKey2, []Partition{ - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 12297829382473034408}}}, - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 18446744073709551612}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 6148914691236517204}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 12297829382473034408}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 18446744073709551612}}}, }, }, - {4, nodeKey1, + {4, NodeKey1, []Partition{ - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 12297829382473034408}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 3074457345618258602}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 12297829382473034408}}}, }, }, - {4, nodeKey2, + {4, NodeKey2, []Partition{ - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 15372286728091293010}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 6148914691236517204}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 15372286728091293010}}}, }, }, - {4, nodeKey3, + {4, NodeKey3, []Partition{ - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey3, token: 9223372036854775806}}}, - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey3, token: 18446744073709551612}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: NodeKey3, token: 9223372036854775806}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: NodeKey3, token: 18446744073709551612}}}, }, }, - {0, nodeKey1, + {0, NodeKey1, []Partition{ - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 18446744073709551612}, {nodeKey: nodeKey1, token: 3074457345618258602}}}, - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey2, token: 6148914691236517204}}}, - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}, {nodeKey: nodeKey1, token: 9223372036854775806}}}, - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 9223372036854775806}, {nodeKey: nodeKey2, token: 12297829382473034408}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 12297829382473034408}, {nodeKey: nodeKey1, token: 15372286728091293010}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 15372286728091293010}, {nodeKey: nodeKey2, token: 18446744073709551612}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 18446744073709551612}, {nodeKey: NodeKey1, token: 3074457345618258602}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 3074457345618258602}, {nodeKey: NodeKey2, token: 6148914691236517204}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 6148914691236517204}, {nodeKey: NodeKey1, token: 9223372036854775806}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 9223372036854775806}, {nodeKey: NodeKey2, token: 12297829382473034408}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 12297829382473034408}, {nodeKey: NodeKey1, token: 15372286728091293010}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 15372286728091293010}, {nodeKey: NodeKey2, token: 18446744073709551612}}}, }, }, - {0, nodeKey2, + {0, NodeKey2, []Partition{ - {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey2, token: 6148914691236517204}}}, - {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 6148914691236517204}, {nodeKey: nodeKey1, token: 9223372036854775806}}}, - {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 9223372036854775806}, {nodeKey: nodeKey2, token: 12297829382473034408}}}, - {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 12297829382473034408}, {nodeKey: nodeKey1, token: 15372286728091293010}}}, - {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 15372286728091293010}, {nodeKey: nodeKey2, token: 18446744073709551612}}}, - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey2, token: 18446744073709551612}, {nodeKey: nodeKey1, token: 3074457345618258602}}}, + {From: 3074457345618258602, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 3074457345618258602}, {nodeKey: NodeKey2, token: 6148914691236517204}}}, + {From: 6148914691236517204, To: 9223372036854775806 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 6148914691236517204}, {nodeKey: NodeKey1, token: 9223372036854775806}}}, + {From: 9223372036854775806, To: 12297829382473034408 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 9223372036854775806}, {nodeKey: NodeKey2, token: 12297829382473034408}}}, + {From: 12297829382473034408, To: 15372286728091293010 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 12297829382473034408}, {nodeKey: NodeKey1, token: 15372286728091293010}}}, + {From: 15372286728091293010, To: 18446744073709551612 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 15372286728091293010}, {nodeKey: NodeKey2, token: 18446744073709551612}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: NodeKey2, token: 18446744073709551612}, {nodeKey: NodeKey1, token: 3074457345618258602}}}, }, }, - {2, nodeKey4, + {2, NodeKey4, []Partition{ - {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: nodeKey3, token: 18446744073709551612}, {nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey4, token: 4611686018427387903}}}, - {From: 3074457345618258602, To: 4611686018427387903 - 1, VNodes: []VNode{{nodeKey: nodeKey1, token: 3074457345618258602}, {nodeKey: nodeKey4, token: 4611686018427387903}, {nodeKey: nodeKey2, token: 6148914691236517204}}}, - {From: 4611686018427387903, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: nodeKey4, token: 4611686018427387903}, {nodeKey: nodeKey2, token: 6148914691236517204}, {nodeKey: nodeKey3, token: 9223372036854775806}}}, + {From: 18446744073709551612, To: 3074457345618258602 - 1, VNodes: []VNode{{nodeKey: NodeKey3, token: 18446744073709551612}, {nodeKey: NodeKey1, token: 3074457345618258602}, {nodeKey: NodeKey4, token: 4611686018427387903}}}, + {From: 3074457345618258602, To: 4611686018427387903 - 1, VNodes: []VNode{{nodeKey: NodeKey1, token: 3074457345618258602}, {nodeKey: NodeKey4, token: 4611686018427387903}, {nodeKey: NodeKey2, token: 6148914691236517204}}}, + {From: 4611686018427387903, To: 6148914691236517204 - 1, VNodes: []VNode{{nodeKey: NodeKey4, token: 4611686018427387903}, {nodeKey: NodeKey2, token: 6148914691236517204}, {nodeKey: NodeKey3, token: 9223372036854775806}}}, }, }, } @@ -205,15 +208,15 @@ func TestExcessPartitions(t *testing.T) { nodeKey string excessPartitions []Partition }{ - {3, nodeKey1, []Partition{{From: 6148914691236517204, To: 9223372036854775806 - 1}, {From: 12297829382473034408, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 3074457345618258602 - 1}}}, - {3, nodeKey2, []Partition{{From: 9223372036854775806, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 6148914691236517204 - 1}}}, - {4, nodeKey1, []Partition{{From: 6148914691236517204, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 3074457345618258602 - 1}}}, - {4, nodeKey2, []Partition{{From: 9223372036854775806, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 6148914691236517204 - 1}}}, - {4, nodeKey3, []Partition{{From: 12297829382473034408, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 9223372036854775806 - 1}}}, - {0, nodeKey1, []Partition{}}, - {0, nodeKey2, []Partition{}}, - {0, nodeKey3, []Partition{}}, - {2, nodeKey4, []Partition{{From: 6148914691236517204, To: 18446744073709551612 - 1}}}, + {3, NodeKey1, []Partition{{From: 6148914691236517204, To: 9223372036854775806 - 1}, {From: 12297829382473034408, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 3074457345618258602 - 1}}}, + {3, NodeKey2, []Partition{{From: 9223372036854775806, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 6148914691236517204 - 1}}}, + {4, NodeKey1, []Partition{{From: 6148914691236517204, To: 12297829382473034408 - 1}, {From: 15372286728091293010, To: 3074457345618258602 - 1}}}, + {4, NodeKey2, []Partition{{From: 9223372036854775806, To: 15372286728091293010 - 1}, {From: 18446744073709551612, To: 6148914691236517204 - 1}}}, + {4, NodeKey3, []Partition{{From: 12297829382473034408, To: 18446744073709551612 - 1}, {From: 3074457345618258602, To: 9223372036854775806 - 1}}}, + {0, NodeKey1, []Partition{}}, + {0, NodeKey2, []Partition{}}, + {0, NodeKey3, []Partition{}}, + {2, NodeKey4, []Partition{{From: 6148914691236517204, To: 18446744073709551612 - 1}}}, } for _, test := range tests { @@ -242,9 +245,9 @@ func TestRemoveVNode(t *testing.T) { expectIsRemoved bool expected []VNode }{ - {0, 100, false, []VNode{{token: 3074457345618258602, nodeKey: nodeKey1}, {token: 6148914691236517204, nodeKey: nodeKey2}, {token: 9223372036854775806, nodeKey: nodeKey1}, {token: 12297829382473034408, nodeKey: nodeKey2}, {token: 15372286728091293010, nodeKey: nodeKey1}, {token: 18446744073709551612, nodeKey: nodeKey2}}}, - {0, 9223372036854775806, true, []VNode{{token: 3074457345618258602, nodeKey: nodeKey1}, {token: 6148914691236517204, nodeKey: nodeKey2}, {token: 12297829382473034408, nodeKey: nodeKey2}, {token: 15372286728091293010, nodeKey: nodeKey1}, {token: 18446744073709551612, nodeKey: nodeKey2}}}, - {0, 12297829382473034408, true, []VNode{{token: 3074457345618258602, nodeKey: nodeKey1}, {token: 6148914691236517204, nodeKey: nodeKey2}, {token: 9223372036854775806, nodeKey: nodeKey1}, {token: 15372286728091293010, nodeKey: nodeKey1}, {token: 18446744073709551612, nodeKey: nodeKey2}}}, + {0, 100, false, []VNode{{token: 3074457345618258602, nodeKey: NodeKey1}, {token: 6148914691236517204, nodeKey: NodeKey2}, {token: 9223372036854775806, nodeKey: NodeKey1}, {token: 12297829382473034408, nodeKey: NodeKey2}, {token: 15372286728091293010, nodeKey: NodeKey1}, {token: 18446744073709551612, nodeKey: NodeKey2}}}, + {0, 9223372036854775806, true, []VNode{{token: 3074457345618258602, nodeKey: NodeKey1}, {token: 6148914691236517204, nodeKey: NodeKey2}, {token: 12297829382473034408, nodeKey: NodeKey2}, {token: 15372286728091293010, nodeKey: NodeKey1}, {token: 18446744073709551612, nodeKey: NodeKey2}}}, + {0, 12297829382473034408, true, []VNode{{token: 3074457345618258602, nodeKey: NodeKey1}, {token: 6148914691236517204, nodeKey: NodeKey2}, {token: 9223372036854775806, nodeKey: NodeKey1}, {token: 15372286728091293010, nodeKey: NodeKey1}, {token: 18446744073709551612, nodeKey: NodeKey2}}}, } for _, test := range tests { cluster := clusters[test.clusterId] From 2dc24e51826c87f32e365416c6b8b594eaf45853 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 21:10:04 +0200 Subject: [PATCH 5/7] feat(topology): creating Topology object based on grouped vNodes --- contract/pkg/mock/ddc_bucket_contract_mock.go | 10 +- core/pkg/topology/sync/topology.go | 4 +- core/pkg/topology/topology.go | 17 ++- core/pkg/topology/topology_test.go | 112 +++++++++++++++--- 4 files changed, 116 insertions(+), 27 deletions(-) diff --git a/contract/pkg/mock/ddc_bucket_contract_mock.go b/contract/pkg/mock/ddc_bucket_contract_mock.go index 037186a..98357ed 100644 --- a/contract/pkg/mock/ddc_bucket_contract_mock.go +++ b/contract/pkg/mock/ddc_bucket_contract_mock.go @@ -74,12 +74,12 @@ type ( } ) -func mapSourceToDestination(vNodes []NodeVNodes) []bucket.NodeVNodesInfo { +func mapNodesVNodes(nodes []NodeVNodes) []bucket.NodeVNodesInfo { var nodesVNodes []bucket.NodeVNodesInfo - for _, item := range vNodes { + for _, node := range nodes { nodeVNodes := bucket.NodeVNodesInfo{ - NodeKey: item.NodeKey, - VNodes: item.VNodes, + NodeKey: node.NodeKey, + VNodes: node.VNodes, } nodesVNodes = append(nodesVNodes, nodeVNodes) } @@ -126,7 +126,7 @@ func (d *ddcBucketContractMock) ClusterGet(clusterId uint32) (*bucket.ClusterSta Revenues: types.NewU128(*big.NewInt(1)), TotalRent: types.NewU128(*big.NewInt(1)), }, - VNodes: mapSourceToDestination(cluster.VNodes), + VNodes: mapNodesVNodes(cluster.VNodes), }, nil } } diff --git a/core/pkg/topology/sync/topology.go b/core/pkg/topology/sync/topology.go index d82ca58..8ca7406 100644 --- a/core/pkg/topology/sync/topology.go +++ b/core/pkg/topology/sync/topology.go @@ -11,9 +11,9 @@ type ring struct { mutex sync.RWMutex } -func NewTopology(nodeKeys []string, vNodes [][]uint64, replicaFactor uint) topology.Ring { +func NewTopology(nodes topology.NodesVNodes, replicaFactor uint) topology.Ring { return &ring{ - ring: topology.NewTopology(nodeKeys, vNodes, replicaFactor), + ring: topology.NewTopology(nodes, replicaFactor), } } diff --git a/core/pkg/topology/topology.go b/core/pkg/topology/topology.go index 0f8b019..06d6432 100644 --- a/core/pkg/topology/topology.go +++ b/core/pkg/topology/topology.go @@ -27,16 +27,25 @@ type ( } ) -func NewTopology(NodesKeys []string, vNodes [][]uint64, replicaFactor uint) Ring { +type ( + NodeVNodes struct { + nodeKey string + vNodes []uint64 + } + + NodesVNodes = []NodeVNodes +) + +func NewTopology(nodes NodesVNodes, replicaFactor uint) Ring { if replicaFactor == 0 { replicaFactor = 1 } topologyVNodes := make([]VNode, 0) - for i, nodeKey := range NodesKeys { - for _, token := range vNodes[i] { + for _, node := range nodes { + for _, token := range node.vNodes { topologyVNode := VNode{ - nodeKey: nodeKey, + nodeKey: node.nodeKey, token: token, } diff --git a/core/pkg/topology/topology_test.go b/core/pkg/topology/topology_test.go index 7a40206..3ee6f68 100644 --- a/core/pkg/topology/topology_test.go +++ b/core/pkg/topology/topology_test.go @@ -17,31 +17,111 @@ var NodeKey7 = "be5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f var clusters = []struct { name string - nodeKeys []string - vNodes [][]uint64 + nodesVNodes NodesVNodes replicaFactor uint }{ - {"size 2 replication 3", []string{NodeKey1, NodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 2}, - {"size 3 replication 3", []string{NodeKey1, NodeKey2, NodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 3}, - {"size 4 replication 3", []string{NodeKey1, NodeKey2, NodeKey3, NodeKey4}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}, {4611686018427387903}}, 3}, - {"size 2 replication 1", []string{NodeKey1, NodeKey2}, [][]uint64{{9223372036854775806, 3074457345618258602, 15372286728091293010}, {12297829382473034408, 6148914691236517204, 18446744073709551612}}, 1}, - {"size 3 replication 1", []string{NodeKey1, NodeKey2, NodeKey3}, [][]uint64{{12297829382473034408, 3074457345618258602}, {6148914691236517204, 15372286728091293010}, {18446744073709551612, 9223372036854775806}}, 1}, + { + "size 2 replication 3", + NodesVNodes{ + NodeVNodes{ + nodeKey: NodeKey1, + vNodes: []uint64{9223372036854775806, 3074457345618258602, 15372286728091293010}, + }, + NodeVNodes{ + nodeKey: NodeKey2, + vNodes: []uint64{12297829382473034408, 6148914691236517204, 18446744073709551612}, + }, + }, + 2, + }, + { + "size 3 replication 3", + NodesVNodes{ + NodeVNodes{ + nodeKey: NodeKey1, + vNodes: []uint64{12297829382473034408, 3074457345618258602}, + }, + NodeVNodes{ + nodeKey: NodeKey2, + vNodes: []uint64{6148914691236517204, 15372286728091293010}, + }, + NodeVNodes{ + nodeKey: NodeKey3, + vNodes: []uint64{18446744073709551612, 9223372036854775806}, + }, + }, + 3, + }, + { + "size 4 replication 3", + NodesVNodes{ + NodeVNodes{ + nodeKey: NodeKey1, + vNodes: []uint64{12297829382473034408, 3074457345618258602}, + }, + NodeVNodes{ + nodeKey: NodeKey2, + vNodes: []uint64{6148914691236517204, 15372286728091293010}, + }, + NodeVNodes{ + nodeKey: NodeKey3, + vNodes: []uint64{18446744073709551612, 9223372036854775806}, + }, + NodeVNodes{ + nodeKey: NodeKey4, + vNodes: []uint64{4611686018427387903}, + }, + }, + 3, + }, + { + "size 2 replication 1", + NodesVNodes{ + NodeVNodes{ + nodeKey: NodeKey1, + vNodes: []uint64{9223372036854775806, 3074457345618258602, 15372286728091293010}, + }, + NodeVNodes{ + nodeKey: NodeKey2, + vNodes: []uint64{12297829382473034408, 6148914691236517204, 18446744073709551612}, + }, + }, + 1, + }, + { + "size 3 replication 1", + NodesVNodes{ + NodeVNodes{ + nodeKey: NodeKey1, + vNodes: []uint64{12297829382473034408, 3074457345618258602}, + }, + NodeVNodes{ + nodeKey: NodeKey2, + vNodes: []uint64{6148914691236517204, 15372286728091293010}, + }, + NodeVNodes{ + nodeKey: NodeKey3, + vNodes: []uint64{18446744073709551612, 9223372036854775806}, + }, + }, + 1, + }, } func TestTokens(t *testing.T) { for _, test := range clusters { t.Run(test.name, func(t *testing.T) { //given - testSubject := NewTopology(test.nodeKeys, test.vNodes, test.replicaFactor) + testSubject := NewTopology(test.nodesVNodes, test.replicaFactor) - for i, nodeKey := range test.nodeKeys { + for _, node := range test.nodesVNodes { //when - tokens := testSubject.Tokens(nodeKey) + tokens := testSubject.Tokens(node.nodeKey) //then assert.True(t, sort.SliceIsSorted(tokens, func(i, j int) bool { return tokens[i] < tokens[j] })) - expected := test.vNodes[i] + expected := node.vNodes assert.Len(t, tokens, len(expected)) for _, value := range expected { assert.Contains(t, tokens, value) @@ -70,7 +150,7 @@ func TestNeighbours(t *testing.T) { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodesVNodes, cluster.replicaFactor) //when prev, next := testSubject.Neighbours(test.token) @@ -103,7 +183,7 @@ func TestReplicas(t *testing.T) { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodesVNodes, cluster.replicaFactor) //when replicas := testSubject.Replicas(test.token) @@ -188,7 +268,7 @@ func TestPartitions(t *testing.T) { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodesVNodes, cluster.replicaFactor) //when partitions := testSubject.Partitions(test.nodeKey) @@ -223,7 +303,7 @@ func TestExcessPartitions(t *testing.T) { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodesVNodes, cluster.replicaFactor) //when partitions := testSubject.ExcessPartitions(test.nodeKey) @@ -253,7 +333,7 @@ func TestRemoveVNode(t *testing.T) { cluster := clusters[test.clusterId] t.Run(cluster.name, func(t *testing.T) { //given - testSubject := NewTopology(cluster.nodeKeys, cluster.vNodes, cluster.replicaFactor) + testSubject := NewTopology(cluster.nodesVNodes, cluster.replicaFactor) //when ok := testSubject.RemoveVNode(test.token) From 925cbba2369e576316f19574a138e40e71e7a0bf Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 21:20:36 +0200 Subject: [PATCH 6/7] refactor(cluster): field name for grouped vNodes is changed --- contract/pkg/bucket/messages.go | 6 +++--- contract/pkg/bucket/messages_test.go | 12 ++++++------ contract/pkg/mock/ddc_bucket_contract_mock.go | 10 ++++------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/contract/pkg/bucket/messages.go b/contract/pkg/bucket/messages.go index 70b5f4f..1386ff3 100644 --- a/contract/pkg/bucket/messages.go +++ b/contract/pkg/bucket/messages.go @@ -56,9 +56,9 @@ type NodeVNodesInfo struct { } type ClusterStatus struct { - ClusterId ClusterId - Cluster Cluster - VNodes []NodeVNodesInfo + ClusterId ClusterId + Cluster Cluster + NodesVNodes []NodeVNodesInfo } type NewCluster struct { diff --git a/contract/pkg/bucket/messages_test.go b/contract/pkg/bucket/messages_test.go index ab3e172..9630991 100644 --- a/contract/pkg/bucket/messages_test.go +++ b/contract/pkg/bucket/messages_test.go @@ -29,9 +29,9 @@ func TestBucketWriteAccess(t *testing.T) { func TestClusterStatus_ReplicationFactor(t *testing.T) { type fields struct { - ClusterId ClusterId - Cluster Cluster - VNodes []NodeVNodesInfo + ClusterId ClusterId + Cluster Cluster + NodesVNodes []NodeVNodesInfo } tests := []struct { name string @@ -72,9 +72,9 @@ func TestClusterStatus_ReplicationFactor(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := &ClusterStatus{ - ClusterId: tt.fields.ClusterId, - Cluster: tt.fields.Cluster, - VNodes: tt.fields.VNodes, + ClusterId: tt.fields.ClusterId, + Cluster: tt.fields.Cluster, + NodesVNodes: tt.fields.NodesVNodes, } assert.Equalf(t, tt.want, c.ReplicationFactor(), "ReplicationFactor()") }) diff --git a/contract/pkg/mock/ddc_bucket_contract_mock.go b/contract/pkg/mock/ddc_bucket_contract_mock.go index 98357ed..0edb115 100644 --- a/contract/pkg/mock/ddc_bucket_contract_mock.go +++ b/contract/pkg/mock/ddc_bucket_contract_mock.go @@ -35,10 +35,9 @@ type ( } Cluster struct { - Id uint32 - VNodes []NodeVNodes - Nodes []string - Params string + Id uint32 + NodesVNodes []NodeVNodes + Params string } NodeVNodes struct { @@ -120,13 +119,12 @@ func (d *ddcBucketContractMock) ClusterGet(clusterId uint32) (*bucket.ClusterSta Cluster: bucket.Cluster{ ManagerId: types.AccountID{}, Params: cluster.Params, - NodesKeys: cluster.Nodes, ResourcePerVNode: 32, ResourceUsed: 0, Revenues: types.NewU128(*big.NewInt(1)), TotalRent: types.NewU128(*big.NewInt(1)), }, - VNodes: mapNodesVNodes(cluster.VNodes), + NodesVNodes: mapNodesVNodes(cluster.NodesVNodes), }, nil } } From 89ee068218673a072209b54361a5abe6528b51ff Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Tue, 15 Aug 2023 21:33:15 +0200 Subject: [PATCH 7/7] fix(topology): param object fields made public --- core/pkg/topology/topology.go | 8 ++-- core/pkg/topology/topology_test.go | 60 +++++++++++++++--------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/core/pkg/topology/topology.go b/core/pkg/topology/topology.go index 06d6432..c05e49a 100644 --- a/core/pkg/topology/topology.go +++ b/core/pkg/topology/topology.go @@ -29,8 +29,8 @@ type ( type ( NodeVNodes struct { - nodeKey string - vNodes []uint64 + NodeKey string + VNodes []uint64 } NodesVNodes = []NodeVNodes @@ -43,9 +43,9 @@ func NewTopology(nodes NodesVNodes, replicaFactor uint) Ring { topologyVNodes := make([]VNode, 0) for _, node := range nodes { - for _, token := range node.vNodes { + for _, token := range node.VNodes { topologyVNode := VNode{ - nodeKey: node.nodeKey, + nodeKey: node.NodeKey, token: token, } diff --git a/core/pkg/topology/topology_test.go b/core/pkg/topology/topology_test.go index 3ee6f68..a75fef4 100644 --- a/core/pkg/topology/topology_test.go +++ b/core/pkg/topology/topology_test.go @@ -24,12 +24,12 @@ var clusters = []struct { "size 2 replication 3", NodesVNodes{ NodeVNodes{ - nodeKey: NodeKey1, - vNodes: []uint64{9223372036854775806, 3074457345618258602, 15372286728091293010}, + NodeKey: NodeKey1, + VNodes: []uint64{9223372036854775806, 3074457345618258602, 15372286728091293010}, }, NodeVNodes{ - nodeKey: NodeKey2, - vNodes: []uint64{12297829382473034408, 6148914691236517204, 18446744073709551612}, + NodeKey: NodeKey2, + VNodes: []uint64{12297829382473034408, 6148914691236517204, 18446744073709551612}, }, }, 2, @@ -38,16 +38,16 @@ var clusters = []struct { "size 3 replication 3", NodesVNodes{ NodeVNodes{ - nodeKey: NodeKey1, - vNodes: []uint64{12297829382473034408, 3074457345618258602}, + NodeKey: NodeKey1, + VNodes: []uint64{12297829382473034408, 3074457345618258602}, }, NodeVNodes{ - nodeKey: NodeKey2, - vNodes: []uint64{6148914691236517204, 15372286728091293010}, + NodeKey: NodeKey2, + VNodes: []uint64{6148914691236517204, 15372286728091293010}, }, NodeVNodes{ - nodeKey: NodeKey3, - vNodes: []uint64{18446744073709551612, 9223372036854775806}, + NodeKey: NodeKey3, + VNodes: []uint64{18446744073709551612, 9223372036854775806}, }, }, 3, @@ -56,20 +56,20 @@ var clusters = []struct { "size 4 replication 3", NodesVNodes{ NodeVNodes{ - nodeKey: NodeKey1, - vNodes: []uint64{12297829382473034408, 3074457345618258602}, + NodeKey: NodeKey1, + VNodes: []uint64{12297829382473034408, 3074457345618258602}, }, NodeVNodes{ - nodeKey: NodeKey2, - vNodes: []uint64{6148914691236517204, 15372286728091293010}, + NodeKey: NodeKey2, + VNodes: []uint64{6148914691236517204, 15372286728091293010}, }, NodeVNodes{ - nodeKey: NodeKey3, - vNodes: []uint64{18446744073709551612, 9223372036854775806}, + NodeKey: NodeKey3, + VNodes: []uint64{18446744073709551612, 9223372036854775806}, }, NodeVNodes{ - nodeKey: NodeKey4, - vNodes: []uint64{4611686018427387903}, + NodeKey: NodeKey4, + VNodes: []uint64{4611686018427387903}, }, }, 3, @@ -78,12 +78,12 @@ var clusters = []struct { "size 2 replication 1", NodesVNodes{ NodeVNodes{ - nodeKey: NodeKey1, - vNodes: []uint64{9223372036854775806, 3074457345618258602, 15372286728091293010}, + NodeKey: NodeKey1, + VNodes: []uint64{9223372036854775806, 3074457345618258602, 15372286728091293010}, }, NodeVNodes{ - nodeKey: NodeKey2, - vNodes: []uint64{12297829382473034408, 6148914691236517204, 18446744073709551612}, + NodeKey: NodeKey2, + VNodes: []uint64{12297829382473034408, 6148914691236517204, 18446744073709551612}, }, }, 1, @@ -92,16 +92,16 @@ var clusters = []struct { "size 3 replication 1", NodesVNodes{ NodeVNodes{ - nodeKey: NodeKey1, - vNodes: []uint64{12297829382473034408, 3074457345618258602}, + NodeKey: NodeKey1, + VNodes: []uint64{12297829382473034408, 3074457345618258602}, }, NodeVNodes{ - nodeKey: NodeKey2, - vNodes: []uint64{6148914691236517204, 15372286728091293010}, + NodeKey: NodeKey2, + VNodes: []uint64{6148914691236517204, 15372286728091293010}, }, NodeVNodes{ - nodeKey: NodeKey3, - vNodes: []uint64{18446744073709551612, 9223372036854775806}, + NodeKey: NodeKey3, + VNodes: []uint64{18446744073709551612, 9223372036854775806}, }, }, 1, @@ -116,12 +116,12 @@ func TestTokens(t *testing.T) { for _, node := range test.nodesVNodes { //when - tokens := testSubject.Tokens(node.nodeKey) + tokens := testSubject.Tokens(node.NodeKey) //then assert.True(t, sort.SliceIsSorted(tokens, func(i, j int) bool { return tokens[i] < tokens[j] })) - expected := node.vNodes + expected := node.VNodes assert.Len(t, tokens, len(expected)) for _, value := range expected { assert.Contains(t, tokens, value)