feat(cluster): check command keys ownership (#1194)

* feat(cluster): check command keys ownership on cluster mode

Signed-off-by: adi_holden <adi@dragonflydb.io>
This commit is contained in:
adiholden 2023-05-10 12:06:52 +03:00 committed by GitHub
parent 36cd15a196
commit 577472eb22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 205 additions and 16 deletions

View file

@ -0,0 +1,53 @@
extern "C" {
#include "redis/crc16.h"
}
#include <shared_mutex>
#include <string_view>
#include "cluster_config.h"
namespace dfly {
bool ClusterConfig::cluster_enabled = false;
static constexpr SlotId kMaxSlotNum = 0x3FFF;
std::string_view ClusterConfig::KeyTag(std::string_view key) {
size_t start = key.find('{');
if (start == key.npos) {
return key;
}
size_t end = key.find('}', start + 1);
if (end == key.npos || end == start + 1) {
return key;
}
return key.substr(start + 1, end - start - 1);
}
SlotId ClusterConfig::KeySlot(std::string_view key) {
std::string_view tag = KeyTag(key);
return crc16(tag.data(), tag.length()) & kMaxSlotNum;
}
ClusterConfig::ClusterConfig() {
cluster_enabled = true;
AddSlots();
}
void ClusterConfig::AddSlots() {
// TODO update logic acording to config
// currently add all slots to owned slots
std::lock_guard lk{slots_mu_};
for (SlotId slot_id = 0; slot_id <= kMaxSlotNum; ++slot_id) {
owned_slots_.emplace(slot_id);
}
return;
}
bool ClusterConfig::IsMySlot(SlotId id) {
std::shared_lock sl(slots_mu_);
return owned_slots_.contains(id);
}
} // namespace dfly