From 8b9214606881314ac955fa2d26b0740d696b8faf Mon Sep 17 00:00:00 2001 From: Rivers Jin Date: Fri, 17 Jan 2025 14:54:11 +0800 Subject: [PATCH 1/4] perf(cluster): add upper bound for scan during migration --- src/cluster/slot_migrate.cc | 10 +++++++++- src/cluster/slot_migrate.h | 5 ----- src/config/config.cc | 5 ++--- src/storage/redis_metadata.cc | 13 +++++++++++++ src/storage/redis_metadata.h | 1 + 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/cluster/slot_migrate.cc b/src/cluster/slot_migrate.cc index 4b544cf7a72..566f2cce502 100644 --- a/src/cluster/slot_migrate.cc +++ b/src/cluster/slot_migrate.cc @@ -29,6 +29,7 @@ #include "io_util.h" #include "storage/batch_extractor.h" #include "storage/iterator.h" +#include "storage/redis_metadata.h" #include "sync_migrate_context.h" #include "thread_util.h" #include "time_util.h" @@ -343,10 +344,13 @@ Status SlotMigrator::sendSnapshotByCmd() { std::string prefix = ComposeSlotKeyPrefix(namespace_, slot_range.start); LOG(INFO) << "[migrate] Iterate keys of slot(s), key's prefix: " << prefix; + std::string upper_bound = ComposeSlotKeyUperBound(namespace_, slot_range.end); rocksdb::ReadOptions read_options = storage_->DefaultScanOptions(); read_options.snapshot = slot_snapshot_; Slice prefix_slice(prefix); + Slice upper_bound_slice(upper_bound); read_options.iterate_lower_bound = &prefix_slice; + read_options.iterate_upper_bound = &upper_bound_slice; rocksdb::ColumnFamilyHandle *cf_handle = storage_->GetCFHandle(ColumnFamilyID::Metadata); auto iter = util::UniqueIterator(storage_->GetDB()->NewIterator(read_options, cf_handle)); @@ -1267,13 +1271,17 @@ Status SlotMigrator::sendMigrationBatch(BatchSender *batch) { Status SlotMigrator::sendSnapshotByRawKV() { uint64_t start_ts = util::GetTimeStampMS(); auto slot_range = slot_range_.load(); - LOG(INFO) << "[migrate] Migrating snapshot of slot(s) " << slot_range.String() << " by raw key value"; + LOG(INFO) << fmt::format("[migrate] Migrating snapshot of slot(s) {} by raw key value", slot_range.String()); auto prefix = ComposeSlotKeyPrefix(namespace_, slot_range.start); + auto upper_bound = ComposeSlotKeyUperBound(namespace_, slot_range.end); + rocksdb::ReadOptions read_options = storage_->DefaultScanOptions(); read_options.snapshot = slot_snapshot_; rocksdb::Slice prefix_slice(prefix); + rocksdb::Slice upper_bound_slice(upper_bound); read_options.iterate_lower_bound = &prefix_slice; + read_options.iterate_upper_bound = &upper_bound_slice; auto no_txn_ctx = engine::Context::NoTransactionContext(storage_); engine::DBIterator iter(no_txn_ctx, read_options); diff --git a/src/cluster/slot_migrate.h b/src/cluster/slot_migrate.h index 6d323b6bdf2..8b5cf493fad 100644 --- a/src/cluster/slot_migrate.h +++ b/src/cluster/slot_migrate.h @@ -26,8 +26,6 @@ #include #include -#include -#include #include #include #include @@ -35,10 +33,7 @@ #include #include "batch_sender.h" -#include "encoding.h" -#include "parse_util.h" #include "server/server.h" -#include "slot_import.h" #include "status.h" #include "storage/redis_db.h" #include "unique_fd.h" diff --git a/src/config/config.cc b/src/config/config.cc index 9d8e2a79272..fe7e3fb0e2f 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -24,13 +24,12 @@ #include #include -#include +#include #include #include #include #include #include -#include #include #include #include @@ -125,7 +124,7 @@ Status SetRocksdbCompression(Server *srv, const rocksdb::CompressionType compres std::vector compression_per_level_builder; compression_per_level_builder.reserve(KVROCKS_MAX_LSM_LEVEL); - for (int i = 0; i < compression_start_level; i++) { + for (size_t i = 0; i < compression_start_level; i++) { compression_per_level_builder.emplace_back("kNoCompression"); } for (size_t i = compression_start_level; i < KVROCKS_MAX_LSM_LEVEL; i++) { diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc index 76403faaef3..116ff66f0ab 100644 --- a/src/storage/redis_metadata.cc +++ b/src/storage/redis_metadata.cc @@ -159,6 +159,19 @@ std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid) { return output; } +std::string ComposeSlotKeyUperBound(const Slice &ns, int slotid) { + std::string output; + + PutFixed8(&output, static_cast(ns.size())); + output.append(ns.data(), ns.size()); + + // Is't ok cause slotid <= HASH_SLOTS_SIZE(16384), uint16_t <= 65535 + // no overflow + PutFixed16(&output, static_cast(slotid + 1)); + + return output; +} + Metadata::Metadata(RedisType type, bool generate_version, bool use_64bit_common_field) : flags((use_64bit_common_field ? METADATA_64BIT_ENCODING_MASK : 0) | (METADATA_TYPE_MASK & type)), expire(0), diff --git a/src/storage/redis_metadata.h b/src/storage/redis_metadata.h index 5590609be37..133be301029 100644 --- a/src/storage/redis_metadata.h +++ b/src/storage/redis_metadata.h @@ -112,6 +112,7 @@ template [[nodiscard]] std::tuple ExtractNamespaceKey(Slice ns_key, bool slot_id_encoded); [[nodiscard]] std::string ComposeNamespaceKey(const Slice &ns, const Slice &key, bool slot_id_encoded); [[nodiscard]] std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid); +[[nodiscard]] std::string ComposeSlotKeyUperBound(const Slice &ns, int slotid); class InternalKey { public: From 794e2018df6a31599588dc17aa35dd977e56a493 Mon Sep 17 00:00:00 2001 From: Rivers Jin Date: Fri, 17 Jan 2025 16:09:55 +0800 Subject: [PATCH 2/4] [squash me] correct spelling of 'UpperBound' in function names --- src/cluster/slot_migrate.cc | 4 ++-- src/storage/redis_metadata.cc | 2 +- src/storage/redis_metadata.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cluster/slot_migrate.cc b/src/cluster/slot_migrate.cc index 566f2cce502..4bd7e56c575 100644 --- a/src/cluster/slot_migrate.cc +++ b/src/cluster/slot_migrate.cc @@ -344,7 +344,7 @@ Status SlotMigrator::sendSnapshotByCmd() { std::string prefix = ComposeSlotKeyPrefix(namespace_, slot_range.start); LOG(INFO) << "[migrate] Iterate keys of slot(s), key's prefix: " << prefix; - std::string upper_bound = ComposeSlotKeyUperBound(namespace_, slot_range.end); + std::string upper_bound = ComposeSlotKeyUpperBound(namespace_, slot_range.end); rocksdb::ReadOptions read_options = storage_->DefaultScanOptions(); read_options.snapshot = slot_snapshot_; Slice prefix_slice(prefix); @@ -1274,7 +1274,7 @@ Status SlotMigrator::sendSnapshotByRawKV() { LOG(INFO) << fmt::format("[migrate] Migrating snapshot of slot(s) {} by raw key value", slot_range.String()); auto prefix = ComposeSlotKeyPrefix(namespace_, slot_range.start); - auto upper_bound = ComposeSlotKeyUperBound(namespace_, slot_range.end); + auto upper_bound = ComposeSlotKeyUpperBound(namespace_, slot_range.end); rocksdb::ReadOptions read_options = storage_->DefaultScanOptions(); read_options.snapshot = slot_snapshot_; diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc index 116ff66f0ab..eff7b7a895e 100644 --- a/src/storage/redis_metadata.cc +++ b/src/storage/redis_metadata.cc @@ -159,7 +159,7 @@ std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid) { return output; } -std::string ComposeSlotKeyUperBound(const Slice &ns, int slotid) { +std::string ComposeSlotKeyUpperBound(const Slice &ns, int slotid) { std::string output; PutFixed8(&output, static_cast(ns.size())); diff --git a/src/storage/redis_metadata.h b/src/storage/redis_metadata.h index 133be301029..cba9df80cb5 100644 --- a/src/storage/redis_metadata.h +++ b/src/storage/redis_metadata.h @@ -112,7 +112,7 @@ template [[nodiscard]] std::tuple ExtractNamespaceKey(Slice ns_key, bool slot_id_encoded); [[nodiscard]] std::string ComposeNamespaceKey(const Slice &ns, const Slice &key, bool slot_id_encoded); [[nodiscard]] std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid); -[[nodiscard]] std::string ComposeSlotKeyUperBound(const Slice &ns, int slotid); +[[nodiscard]] std::string ComposeSlotKeyUpperBound(const Slice &ns, int slotid); class InternalKey { public: From fb6fcbd8e973bc3d5e0874781cc9131eaad27cf0 Mon Sep 17 00:00:00 2001 From: Rivers Date: Fri, 17 Jan 2025 17:48:20 +0800 Subject: [PATCH 3/4] Update src/storage/redis_metadata.cc Co-authored-by: Twice --- src/storage/redis_metadata.cc | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc index eff7b7a895e..2d05f3a04d6 100644 --- a/src/storage/redis_metadata.cc +++ b/src/storage/redis_metadata.cc @@ -160,16 +160,7 @@ std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid) { } std::string ComposeSlotKeyUpperBound(const Slice &ns, int slotid) { - std::string output; - - PutFixed8(&output, static_cast(ns.size())); - output.append(ns.data(), ns.size()); - - // Is't ok cause slotid <= HASH_SLOTS_SIZE(16384), uint16_t <= 65535 - // no overflow - PutFixed16(&output, static_cast(slotid + 1)); - - return output; + return ComposeSlotKeyPrefix(ns, slotid + 1); } Metadata::Metadata(RedisType type, bool generate_version, bool use_64bit_common_field) From 61a95fe82b10c403ff3e4bc8efba4782975aa24d Mon Sep 17 00:00:00 2001 From: Rivers Jin Date: Sat, 18 Jan 2025 09:54:26 +0800 Subject: [PATCH 4/4] [squash me] format --- src/storage/redis_metadata.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc index 2d05f3a04d6..9df1053262b 100644 --- a/src/storage/redis_metadata.cc +++ b/src/storage/redis_metadata.cc @@ -159,9 +159,7 @@ std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid) { return output; } -std::string ComposeSlotKeyUpperBound(const Slice &ns, int slotid) { - return ComposeSlotKeyPrefix(ns, slotid + 1); -} +std::string ComposeSlotKeyUpperBound(const Slice &ns, int slotid) { return ComposeSlotKeyPrefix(ns, slotid + 1); } Metadata::Metadata(RedisType type, bool generate_version, bool use_64bit_common_field) : flags((use_64bit_common_field ? METADATA_64BIT_ENCODING_MASK : 0) | (METADATA_TYPE_MASK & type)),