-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove transaction lib on cluster code dependency (#4417)
- Loading branch information
1 parent
cb752d9
commit 933c9f0
Showing
30 changed files
with
221 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.