mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 10:25:47 +02:00
chore: Introduce ShardArgs as a distinct type (#2952)
Done in preparation to make ShardArgs a smart iterable type, but currently it's just a wrapper aroung ArgSlice. Also refactored common.{h,cc} into tx_base.{h,cc} In addition, fixed a bug in key tracking, where we wrongly created weak_ref in a shard thread instead of doing this in the coordinator thread. Finally, identified another bug (not fixed yet) where we track all the arguments instead of tracking keys only. Besides this, no functional changes around the moved code. Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
parent
2230397a12
commit
89b1d7d52a
25 changed files with 568 additions and 476 deletions
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2022, DragonflyDB authors. All rights reserved.
|
||||
// Copyright 2024, DragonflyDB authors. All rights reserved.
|
||||
// See LICENSE for licensing terms.
|
||||
//
|
||||
|
||||
|
@ -27,8 +27,6 @@ enum class ListDir : uint8_t { LEFT, RIGHT };
|
|||
constexpr int64_t kMaxExpireDeadlineSec = (1u << 28) - 1; // 8.5 years
|
||||
constexpr int64_t kMaxExpireDeadlineMs = kMaxExpireDeadlineSec * 1000;
|
||||
|
||||
using DbIndex = uint16_t;
|
||||
using ShardId = uint16_t;
|
||||
using LSN = uint64_t;
|
||||
using TxId = uint64_t;
|
||||
using TxClock = uint64_t;
|
||||
|
@ -39,17 +37,11 @@ using facade::CmdArgVec;
|
|||
using facade::MutableSlice;
|
||||
using facade::OpResult;
|
||||
|
||||
using ArgSlice = absl::Span<const std::string_view>;
|
||||
using StringVec = std::vector<std::string>;
|
||||
|
||||
// keys are RDB_TYPE_xxx constants.
|
||||
using RdbTypeFreqMap = absl::flat_hash_map<unsigned, size_t>;
|
||||
|
||||
constexpr DbIndex kInvalidDbId = DbIndex(-1);
|
||||
constexpr ShardId kInvalidSid = ShardId(-1);
|
||||
constexpr DbIndex kMaxDbId = 1024; // Reasonable starting point.
|
||||
using LockFp = uint64_t; // a key fingerprint used by the LockTable.
|
||||
|
||||
class CommandId;
|
||||
class Transaction;
|
||||
class EngineShard;
|
||||
|
@ -67,98 +59,6 @@ struct LockTagOptions {
|
|||
static const LockTagOptions& instance();
|
||||
};
|
||||
|
||||
struct KeyLockArgs {
|
||||
DbIndex db_index = 0;
|
||||
absl::Span<const LockFp> fps;
|
||||
};
|
||||
|
||||
// Describes key indices.
|
||||
struct KeyIndex {
|
||||
unsigned start;
|
||||
unsigned end; // does not include this index (open limit).
|
||||
unsigned step; // 1 for commands like mget. 2 for commands like mset.
|
||||
|
||||
// if index is non-zero then adds another key index (usually 0).
|
||||
// relevant for for commands like ZUNIONSTORE/ZINTERSTORE for destination key.
|
||||
std::optional<uint16_t> bonus{};
|
||||
bool has_reverse_mapping = false;
|
||||
|
||||
KeyIndex(unsigned s = 0, unsigned e = 0, unsigned step = 0) : start(s), end(e), step(step) {
|
||||
}
|
||||
|
||||
static KeyIndex Range(unsigned start, unsigned end, unsigned step = 1) {
|
||||
return KeyIndex{start, end, step};
|
||||
}
|
||||
|
||||
bool HasSingleKey() const {
|
||||
return !bonus && (start + step >= end);
|
||||
}
|
||||
|
||||
unsigned num_args() const {
|
||||
return end - start + bool(bonus);
|
||||
}
|
||||
};
|
||||
|
||||
struct DbContext {
|
||||
DbIndex db_index = 0;
|
||||
uint64_t time_now_ms = 0;
|
||||
};
|
||||
|
||||
struct OpArgs {
|
||||
EngineShard* shard;
|
||||
const Transaction* tx;
|
||||
DbContext db_cntx;
|
||||
|
||||
OpArgs() : shard(nullptr), tx(nullptr) {
|
||||
}
|
||||
|
||||
OpArgs(EngineShard* s, const Transaction* tx, const DbContext& cntx)
|
||||
: shard(s), tx(tx), db_cntx(cntx) {
|
||||
}
|
||||
};
|
||||
|
||||
// A strong type for a lock tag. Helps to disambiguate between keys and the parts of the
|
||||
// keys that are used for locking.
|
||||
class LockTag {
|
||||
std::string_view str_;
|
||||
|
||||
public:
|
||||
using is_stackonly = void; // marks that this object does not use heap.
|
||||
|
||||
LockTag() = default;
|
||||
explicit LockTag(std::string_view key);
|
||||
|
||||
explicit operator std::string_view() const {
|
||||
return str_;
|
||||
}
|
||||
|
||||
LockFp Fingerprint() const;
|
||||
|
||||
// To make it hashable.
|
||||
template <typename H> friend H AbslHashValue(H h, const LockTag& tag) {
|
||||
return H::combine(std::move(h), tag.str_);
|
||||
}
|
||||
|
||||
bool operator==(const LockTag& o) const {
|
||||
return str_ == o.str_;
|
||||
}
|
||||
};
|
||||
|
||||
// Record non auto journal command with own txid and dbid.
|
||||
void RecordJournal(const OpArgs& op_args, std::string_view cmd, ArgSlice args,
|
||||
uint32_t shard_cnt = 1, bool multi_commands = false);
|
||||
|
||||
// Record non auto journal command finish. Call only when command translates to multi commands.
|
||||
void RecordJournalFinish(const OpArgs& op_args, uint32_t shard_cnt);
|
||||
|
||||
// Record expiry in journal with independent transaction. Must be called from shard thread holding
|
||||
// key.
|
||||
void RecordExpiry(DbIndex dbid, std::string_view key);
|
||||
|
||||
// Trigger journal write to sink, no journal record will be added to journal.
|
||||
// Must be called from shard thread of journal to sink.
|
||||
void TriggerJournalWriteToSink();
|
||||
|
||||
struct IoMgrStats {
|
||||
uint64_t read_total = 0;
|
||||
uint64_t read_delay_usec = 0;
|
||||
|
@ -205,8 +105,6 @@ enum class GlobalState : uint8_t {
|
|||
|
||||
std::ostream& operator<<(std::ostream& os, const GlobalState& state);
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, ArgSlice list);
|
||||
|
||||
enum class TimeUnit : uint8_t { SEC, MSEC };
|
||||
|
||||
inline void ToUpper(const MutableSlice* val) {
|
||||
|
@ -414,10 +312,6 @@ inline uint32_t MemberTimeSeconds(uint64_t now_ms) {
|
|||
return (now_ms / 1000) - kMemberExpiryBase;
|
||||
}
|
||||
|
||||
// Checks whether the touched key is valid for a blocking transaction watching it
|
||||
using KeyReadyChecker =
|
||||
std::function<bool(EngineShard*, const DbContext& context, Transaction* tx, std::string_view)>;
|
||||
|
||||
struct MemoryBytesFlag {
|
||||
uint64_t value = 0;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue