Skip to content

Commit

Permalink
refactor: remove transaction lib on cluster code dependency (#4417)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorysTheDev authored Jan 8, 2025
1 parent cb752d9 commit 933c9f0
Show file tree
Hide file tree
Showing 30 changed files with 221 additions and 198 deletions.
4 changes: 2 additions & 2 deletions src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif()


add_library(dfly_transaction db_slice.cc blocking_controller.cc
command_registry.cc cluster/cluster_utility.cc
command_registry.cc cluster_support.cc
journal/cmd_serializer.cc journal/tx_executor.cc namespaces.cc
common.cc journal/journal.cc journal/types.cc journal/journal_slice.cc
server_state.cc table.cc top_keys.cc transaction.cc tx_base.cc
Expand Down Expand Up @@ -60,7 +60,7 @@ add_library(dragonfly_lib bloom_family.cc
${DF_SEARCH_SRCS}
${DF_LINUX_SRCS}
cluster/cluster_config.cc cluster/cluster_family.cc cluster/incoming_slot_migration.cc
cluster/outgoing_slot_migration.cc cluster/cluster_defs.cc
cluster/outgoing_slot_migration.cc cluster/cluster_defs.cc cluster/cluster_utility.cc
acl/user.cc acl/user_registry.cc acl/acl_family.cc
acl/validator.cc)

Expand Down
10 changes: 7 additions & 3 deletions src/server/cluster/cluster_config.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#include "cluster_config.h"

#include <absl/container/flat_hash_set.h>
Expand Down Expand Up @@ -39,7 +43,7 @@ bool HasValidNodeIds(const ClusterShardInfos& new_config) {

bool IsConfigValid(const ClusterShardInfos& new_config) {
// Make sure that all slots are set exactly once.
vector<bool> slots_found(cluster::kMaxSlotNum + 1);
vector<bool> slots_found(kMaxSlotNum + 1);

if (!HasValidNodeIds(new_config)) {
return false;
Expand Down Expand Up @@ -309,7 +313,7 @@ std::shared_ptr<ClusterConfig> ClusterConfig::CloneWithoutMigrations() const {
}

bool ClusterConfig::IsMySlot(SlotId id) const {
if (id > cluster::kMaxSlotNum) {
if (id > kMaxSlotNum) {
DCHECK(false) << "Requesting a non-existing slot id " << id;
return false;
}
Expand All @@ -322,7 +326,7 @@ bool ClusterConfig::IsMySlot(std::string_view key) const {
}

ClusterNodeInfo ClusterConfig::GetMasterNodeForSlot(SlotId id) const {
CHECK_LE(id, cluster::kMaxSlotNum) << "Requesting a non-existing slot id " << id;
CHECK_LE(id, kMaxSlotNum) << "Requesting a non-existing slot id " << id;

for (const auto& shard : config_) {
if (shard.slot_ranges.Contains(id)) {
Expand Down
62 changes: 4 additions & 58 deletions src/server/cluster/cluster_defs.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
// Copyright 2024, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

extern "C" {
#include "redis/crc16.h"
}
#include "cluster_defs.h"

#include <absl/strings/str_cat.h>
#include <absl/strings/str_join.h>

#include "base/flags.h"
#include "base/logging.h"
#include "cluster_defs.h"
#include "facade/error.h"
#include "slot_set.h"
#include "src/server/common.h"

// TODO remove when tl_cluster_config will be moved out from it
#include "server/cluster/cluster_family.h"

using namespace std;

ABSL_FLAG(string, cluster_mode, "",
"Cluster mode supported. Possible values are "
"'emulated', 'yes' or ''");

namespace dfly::cluster {
std::string SlotRange::ToString() const {
return absl::StrCat("[", start, ", ", end, "]");
Expand Down Expand Up @@ -69,53 +62,6 @@ ClusterShardInfos::ClusterShardInfos(std::vector<ClusterShardInfo> infos)
std::sort(infos_.begin(), infos_.end());
}

namespace {
enum class ClusterMode {
kUninitialized,
kNoCluster,
kEmulatedCluster,
kRealCluster,
};

ClusterMode cluster_mode = ClusterMode::kUninitialized;
} // namespace

void InitializeCluster() {
string cluster_mode_str = absl::GetFlag(FLAGS_cluster_mode);

if (cluster_mode_str == "emulated") {
cluster_mode = ClusterMode::kEmulatedCluster;
} else if (cluster_mode_str == "yes") {
cluster_mode = ClusterMode::kRealCluster;
} else if (cluster_mode_str.empty()) {
cluster_mode = ClusterMode::kNoCluster;
} else {
LOG(ERROR) << "Invalid value for flag --cluster_mode. Exiting...";
exit(1);
}
}

bool IsClusterEnabled() {
return cluster_mode == ClusterMode::kRealCluster;
}

bool IsClusterEmulated() {
return cluster_mode == ClusterMode::kEmulatedCluster;
}

SlotId KeySlot(std::string_view key) {
string_view tag = LockTagOptions::instance().Tag(key);
return crc16(tag.data(), tag.length()) & kMaxSlotNum;
}

bool IsClusterEnabledOrEmulated() {
return IsClusterEnabled() || IsClusterEmulated();
}

bool IsClusterShardedByTag() {
return IsClusterEnabledOrEmulated() || LockTagOptions::instance().enabled;
}

facade::ErrorReply SlotOwnershipError(SlotId slot_id) {
const cluster::ClusterConfig* cluster_config = ClusterFamily::cluster_config();
if (!cluster_config)
Expand Down
14 changes: 1 addition & 13 deletions src/server/cluster/cluster_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
#include <vector>

#include "facade/facade_types.h"
#include "server/cluster_support.h"

namespace dfly::cluster {

using SlotId = uint16_t;

constexpr SlotId kMaxSlotNum = 0x3FFF;
constexpr SlotId kInvalidSlotId = kMaxSlotNum + 1;

struct SlotRange {
static constexpr SlotId kMaxSlotId = 0x3FFF;
SlotId start = 0;
Expand Down Expand Up @@ -169,15 +165,7 @@ enum class MigrationState : uint8_t {
C_FINISHED,
};

SlotId KeySlot(std::string_view key);

// return error message if slot doesn't belong to this node
facade::ErrorReply SlotOwnershipError(SlotId slot_id);

void InitializeCluster();
bool IsClusterEnabled();
bool IsClusterEmulated();
bool IsClusterEnabledOrEmulated();
bool IsClusterShardedByTag();

} // namespace dfly::cluster
2 changes: 1 addition & 1 deletion src/server/cluster/cluster_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ void ClusterFamily::KeySlot(CmdArgList args, SinkReplyBuilder* builder) {
return builder->SendError(WrongNumArgsError("CLUSTER KEYSLOT"));
}

SlotId id = cluster::KeySlot(ArgS(args, 1));
SlotId id = dfly::KeySlot(ArgS(args, 1));
return builder->SendLong(id);
}

Expand Down
35 changes: 4 additions & 31 deletions src/server/cluster/cluster_utility.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#include "server/cluster/cluster_utility.h"

#include "server/cluster/cluster_defs.h"
Expand All @@ -8,37 +12,6 @@ using namespace std;

namespace dfly::cluster {

void UniqueSlotChecker::Add(std::string_view key) {
if (!IsClusterEnabled()) {
return;
}

Add(KeySlot(key));
}

void UniqueSlotChecker::Add(SlotId slot_id) {
if (!IsClusterEnabled()) {
return;
}

if (!slot_id_.has_value()) {
slot_id_ = slot_id;
return;
}

if (*slot_id_ != slot_id) {
slot_id_ = kInvalidSlotId;
}
}

optional<SlotId> UniqueSlotChecker::GetUniqueSlotId() const {
if (slot_id_.has_value() && *slot_id_ == kInvalidSlotId) {
return nullopt;
}

return slot_id_;
}

uint64_t GetKeyCount(const SlotRanges& slots) {
std::atomic_uint64_t keys = 0;

Expand Down
16 changes: 0 additions & 16 deletions src/server/cluster/cluster_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,10 @@

#pragma once

#include <optional>
#include <string_view>

#include "server/cluster/cluster_defs.h"

namespace dfly::cluster {

// A simple utility class that "aggregates" SlotId-s and can tell whether all inputs were the same.
// Only works when cluster is enabled.
class UniqueSlotChecker {
public:
void Add(std::string_view key);
void Add(SlotId slot_id);

std::optional<SlotId> GetUniqueSlotId() const;

private:
std::optional<SlotId> slot_id_;
};

uint64_t GetKeyCount(const SlotRanges& slots);

} // namespace dfly::cluster
99 changes: 99 additions & 0 deletions src/server/cluster_support.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2024, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

extern "C" {
#include "redis/crc16.h"
}

#include "base/flags.h"
#include "base/logging.h"
#include "cluster_support.h"

using namespace std;

ABSL_FLAG(string, cluster_mode, "",
"Cluster mode supported. Possible values are "
"'emulated', 'yes' or ''");

namespace dfly {

void UniqueSlotChecker::Add(std::string_view key) {
if (!IsClusterEnabled()) {
return;
}

Add(KeySlot(key));
}

void UniqueSlotChecker::Add(SlotId slot_id) {
if (!IsClusterEnabled()) {
return;
}

if (!slot_id_.has_value()) {
slot_id_ = slot_id;
return;
}

if (*slot_id_ != slot_id) {
slot_id_ = kInvalidSlotId;
}
}

optional<SlotId> UniqueSlotChecker::GetUniqueSlotId() const {
if (slot_id_.has_value() && *slot_id_ == kInvalidSlotId) {
return nullopt;
}

return slot_id_;
}

namespace {
enum class ClusterMode {
kUninitialized,
kNoCluster,
kEmulatedCluster,
kRealCluster,
};

ClusterMode cluster_mode = ClusterMode::kUninitialized;
} // namespace

void InitializeCluster() {
string cluster_mode_str = absl::GetFlag(FLAGS_cluster_mode);

if (cluster_mode_str == "emulated") {
cluster_mode = ClusterMode::kEmulatedCluster;
} else if (cluster_mode_str == "yes") {
cluster_mode = ClusterMode::kRealCluster;
} else if (cluster_mode_str.empty()) {
cluster_mode = ClusterMode::kNoCluster;
} else {
LOG(ERROR) << "Invalid value for flag --cluster_mode. Exiting...";
exit(1);
}
}

bool IsClusterEnabled() {
return cluster_mode == ClusterMode::kRealCluster;
}

bool IsClusterEmulated() {
return cluster_mode == ClusterMode::kEmulatedCluster;
}

SlotId KeySlot(std::string_view key) {
string_view tag = LockTagOptions::instance().Tag(key);
return crc16(tag.data(), tag.length()) & kMaxSlotNum;
}

bool IsClusterEnabledOrEmulated() {
return IsClusterEnabled() || IsClusterEmulated();
}

bool IsClusterShardedByTag() {
return IsClusterEnabledOrEmulated() || LockTagOptions::instance().enabled;
}

} // namespace dfly
41 changes: 41 additions & 0 deletions src/server/cluster_support.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#pragma once

#include <cstdint>
#include <optional>
#include <string_view>

#include "common.h"

namespace dfly {

using SlotId = std::uint16_t;

constexpr SlotId kMaxSlotNum = 0x3FFF;
constexpr SlotId kInvalidSlotId = kMaxSlotNum + 1;

// A simple utility class that "aggregates" SlotId-s and can tell whether all inputs were the same.
// Only works when cluster is enabled.
class UniqueSlotChecker {
public:
void Add(std::string_view key);
void Add(SlotId slot_id);

std::optional<SlotId> GetUniqueSlotId() const;

private:
std::optional<SlotId> slot_id_;
};

SlotId KeySlot(std::string_view key);

void InitializeCluster();
bool IsClusterEnabled();
bool IsClusterEmulated();
bool IsClusterEnabledOrEmulated();
bool IsClusterShardedByTag();

} // namespace dfly
Loading

0 comments on commit 933c9f0

Please sign in to comment.