99#define  VMSDK_SRC_CLUSTER_MAP_H_ 
1010
1111#include  < bitset> 
12+ #include  < optional> 
13+ #include  < set> 
1214#include  < string> 
1315#include  < string_view> 
1416#include  < vector> 
1921namespace  vmsdk  {
2022namespace  cluster_map  {
2123
22- const  size_t  NUM_SLOTS  = 16384 ;
24+ const  size_t  k_num_slots  = 16384 ;
2325
2426//  Enumeration for fanout target modes
2527enum  class  FanoutTargetMode  {
@@ -30,65 +32,66 @@ enum class FanoutTargetMode {
3032};
3133
3234struct  NodeInfo  {
33-   enum  Type {
35+   enum  NodeRole { kPrimary , kReplica  };
36+   enum  NodeLocation {
3437    kLocal ,
3538    kRemote ,
3639  };
3740  std::string node_id;
38-   Type type;
39-   //  Empty string if type is kLocal.
41+   NodeRole role;
42+   NodeLocation location;
43+   //  Empty string if location is kLocal.
4044  std::string address;
4145
4246  bool  operator ==(const  NodeInfo& other) const  {
43-     return  type == other.type  && address == other.address ;
47+     return  role == other.role  && location == other.location  &&
48+            address == other.address ;
4449  }
4550
4651  friend  std::ostream& operator <<(std::ostream& os, const  NodeInfo& target) {
47-     os << " NodeInfo{type : "   << target.type  << " , address : "   << target.address 
48-        << " }"  ;
52+     os << " NodeInfo{role : "   << target.role  << " , location : "   << target.location 
53+        << " , address:  "  << target. address  <<  " }"  ;
4954    return  os;
5055  }
5156};
5257
5358struct  ShardInfo  {
5459  //  shard_id is the primary node id
5560  std::string shard_id;
56-   std::string primary_address;
57-   std::vector<std::string> replica_addresses;
58-   std::vector<uint16_t > owned_slots;
61+   //  primary node can be empty
62+   std::optional<NodeInfo> primary;
63+   std::vector<NodeInfo> replicas;
64+   std::set<uint16_t > owned_slots;
5965  //  Hash of owned_slots vector
6066  uint64_t  slots_fingerprint;
6167};
6268
6369class  ClusterMap  {
6470 public: 
71+   //  return pre-generated target vectors
6572  const  std::vector<NodeInfo>& GetPrimaryTargets () const ;
6673  const  std::vector<NodeInfo>& GetReplicaTargets () const ;
67-   const  std::vector<NodeInfo>& GetRandomTargets () const ;
6874  const  std::vector<NodeInfo>& GetAllTargets () const ;
6975
76+   //  generate a random targets vector from cluster bus
77+   std::vector<NodeInfo> GetRandomTargets (ValkeyModuleCtx* ctx);
78+ 
7079  //  create a new cluster map in the background
7180  static  std::shared_ptr<ClusterMap> CreateNewClusterMap (ValkeyModuleCtx* ctx);
7281
7382  //  slot ownership checks
7483  bool  IsSlotOwned (uint16_t  slot) const ;
7584
76-   //  shard lookups
85+   //  shard lookups, will return nullptr if shard does not exist 
7786  const  ShardInfo* GetShardById (std::string_view shard_id) const ;
7887  const  absl::flat_hash_map<std::string, ShardInfo>& GetAllShards () const ;
7988
8089  //  get cluster level slot fingerprint
8190  uint64_t  GetClusterSlotsFingerprint () const ;
8291
83-   //  get fingerprint for a specific shard
84-   uint64_t  GetShardSlotsFingerprint (std::string_view shard_id) const ;
85- 
8692 private: 
8793  //  1: slot is owned by this cluster, 0: slot is not owned by this cluster
88-   std::bitset<NUM_SLOTS> owned_slots_;
89- 
90-   //  slot-to-shard lookup
91-   std::array<std::string, NUM_SLOTS> slot_to_shard_id_;
94+   std::bitset<k_num_slots> owned_slots_;
9295
9396  absl::flat_hash_map<std::string, ShardInfo> shards_;
9497
@@ -98,7 +101,6 @@ class ClusterMap {
98101  //  Pre-computed target lists
99102  std::vector<NodeInfo> primary_targets_;
100103  std::vector<NodeInfo> replica_targets_;
101-   std::vector<NodeInfo> random_targets_;
102104  std::vector<NodeInfo> all_targets_;
103105
104106  //  private helper function to refresh targets in CreateNewClusterMap
0 commit comments