Skip to content

Commit 8b81ad9

Browse files
committed
update interface based on comment v1
Signed-off-by: Miles Song <[email protected]>
1 parent 7afd285 commit 8b81ad9

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

vmsdk/src/cluster_map.h

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,45 @@
1010

1111
#include <bitset>
1212
#include <string>
13+
#include <string_view>
1314
#include <vector>
1415

1516
#include "src/query/fanout_template.h"
1617
#include "src/valkeymodule.h"
1718

1819
namespace vmsdk {
20+
namespace cluster_map {
21+
22+
const size_t NUM_SLOTS = 16384;
23+
24+
// Enumeration for fanout target modes
25+
enum class FanoutTargetMode {
26+
kRandom, // Default: randomly select one node per shard
27+
kReplicasOnly, // Select only replicas, one per shard
28+
kPrimary, // Select all primary (master) nodes
29+
kAll // Select all nodes (both primary and replica)
30+
};
31+
32+
struct NodeInfo {
33+
enum Type {
34+
kLocal,
35+
kRemote,
36+
};
37+
std::string node_id;
38+
Type type;
39+
// Empty string if type is kLocal.
40+
std::string address;
41+
42+
bool operator==(const NodeInfo& other) const {
43+
return type == other.type && address == other.address;
44+
}
45+
46+
friend std::ostream& operator<<(std::ostream& os, const NodeInfo& target) {
47+
os << "NodeInfo{type: " << target.type << ", address: " << target.address
48+
<< "}";
49+
return os;
50+
}
51+
};
1952

2053
struct ShardInfo {
2154
// shard_id is the primary node id
@@ -28,46 +61,52 @@ struct ShardInfo {
2861
};
2962

3063
class ClusterMap {
31-
// flexible to add other getter methods
3264
public:
65+
const std::vector<NodeInfo>& GetPrimaryTargets() const;
66+
const std::vector<NodeInfo>& GetReplicaTargets() const;
67+
const std::vector<NodeInfo>& GetRandomTargets() const;
68+
const std::vector<NodeInfo>& GetAllTargets() const;
69+
3370
// create a new cluster map in the background
3471
static std::shared_ptr<ClusterMap> CreateNewClusterMap(ValkeyModuleCtx* ctx);
3572

3673
// slot ownership checks
3774
bool IsSlotOwned(uint16_t slot) const;
3875

3976
// shard lookups
40-
const ShardInfo* GetShardById(const std::string& shard_id) const;
41-
const std::string& GetShardIdBySlot(uint16_t slot) const;
77+
const ShardInfo* GetShardById(std::string_view shard_id) const;
4278
const absl::flat_hash_map<std::string, ShardInfo>& GetAllShards() const;
4379

4480
// get cluster level slot fingerprint
4581
uint64_t GetClusterSlotsFingerprint() const;
4682

4783
// get fingerprint for a specific shard
48-
uint64_t GetShardSlotsFingerprint(const std::string& shard_id) const;
84+
uint64_t GetShardSlotsFingerprint(std::string_view shard_id) const;
4985

5086
private:
5187
// 1: slot is owned by this cluster, 0: slot is not owned by this cluster
52-
std::bitset<16384> owned_slots_;
88+
std::bitset<NUM_SLOTS> owned_slots_;
5389

5490
// slot-to-shard lookup
55-
std::array<std::string, 16384> slot_to_shard_id_;
91+
std::array<std::string, NUM_SLOTS> slot_to_shard_id_;
5692

5793
absl::flat_hash_map<std::string, ShardInfo> shards_;
5894

5995
// Cluster-level fingerprint (hash of all shard fingerprints)
6096
uint64_t cluster_slots_fingerprint_;
6197

6298
// Pre-computed target lists
63-
std::vector<valkey_search::query::fanout::FanoutSearchTarget>
64-
primary_targets_;
65-
std::vector<valkey_search::query::fanout::FanoutSearchTarget>
66-
replica_targets_;
67-
std::vector<valkey_search::query::fanout::FanoutSearchTarget> random_targets_;
68-
std::vector<valkey_search::query::fanout::FanoutSearchTarget> all_targets_;
99+
std::vector<NodeInfo> primary_targets_;
100+
std::vector<NodeInfo> replica_targets_;
101+
std::vector<NodeInfo> random_targets_;
102+
std::vector<NodeInfo> all_targets_;
103+
104+
// private helper function to refresh targets in CreateNewClusterMap
105+
std::vector<NodeInfo> GetTargets(ValkeyModuleCtx* ctx,
106+
FanoutTargetMode target_mode);
69107
};
70108

109+
} // namespace cluster_map
71110
} // namespace vmsdk
72111

73112
#endif // VMSDK_SRC_CLUSTER_MAP_H_

0 commit comments

Comments
 (0)