|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, valkey-search contributors |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD 3-Clause |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef VMSDK_SRC_CLUSTER_MAP_H_ |
| 9 | +#define VMSDK_SRC_CLUSTER_MAP_H_ |
| 10 | + |
| 11 | +#include <bitset> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "src/query/fanout_template.h" |
| 16 | +#include "src/valkeymodule.h" |
| 17 | + |
| 18 | +namespace vmsdk { |
| 19 | + |
| 20 | +struct ShardInfo { |
| 21 | + // shard_id is the primary node id |
| 22 | + std::string shard_id; |
| 23 | + std::string primary_address; |
| 24 | + std::vector<std::string> replica_addresses; |
| 25 | + std::vector<uint16_t> owned_slots; |
| 26 | + // Hash of owned_slots vector |
| 27 | + uint64_t slots_fingerprint; |
| 28 | +}; |
| 29 | + |
| 30 | +class ClusterMap { |
| 31 | + // flexible to add other getter methods |
| 32 | + public: |
| 33 | + // create a new cluster map in the background |
| 34 | + static std::shared_ptr<ClusterMap> CreateNewClusterMap(ValkeyModuleCtx* ctx); |
| 35 | + |
| 36 | + // slot ownership checks |
| 37 | + bool IsSlotOwned(uint16_t slot) const; |
| 38 | + |
| 39 | + // shard lookups |
| 40 | + const ShardInfo* GetShardById(const std::string& shard_id) const; |
| 41 | + const std::string& GetShardIdBySlot(uint16_t slot) const; |
| 42 | + const absl::flat_hash_map<std::string, ShardInfo>& GetAllShards() const; |
| 43 | + |
| 44 | + // get cluster level slot fingerprint |
| 45 | + uint64_t GetClusterSlotsFingerprint() const; |
| 46 | + |
| 47 | + // get fingerprint for a specific shard |
| 48 | + uint64_t GetShardSlotsFingerprint(const std::string& shard_id) const; |
| 49 | + |
| 50 | + private: |
| 51 | + // 1: slot is owned by this cluster, 0: slot is not owned by this cluster |
| 52 | + std::bitset<16384> owned_slots_; |
| 53 | + |
| 54 | + // slot-to-shard lookup |
| 55 | + std::array<std::string, 16384> slot_to_shard_id_; |
| 56 | + |
| 57 | + absl::flat_hash_map<std::string, ShardInfo> shards_; |
| 58 | + |
| 59 | + // Cluster-level fingerprint (hash of all shard fingerprints) |
| 60 | + uint64_t cluster_slots_fingerprint_; |
| 61 | + |
| 62 | + // 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_; |
| 69 | +}; |
| 70 | + |
| 71 | +} // namespace vmsdk |
| 72 | + |
| 73 | +#endif // VMSDK_SRC_CLUSTER_MAP_H_ |
0 commit comments