Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(cluster): add upper bound for scan during migration #2724

Merged
merged 5 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/cluster/slot_migrate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = ComposeSlotKeyUpperBound(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));

Expand Down Expand Up @@ -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 = ComposeSlotKeyUpperBound(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);

Expand Down
5 changes: 0 additions & 5 deletions src/cluster/slot_migrate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@
#include <rocksdb/transaction_log.h>
#include <rocksdb/write_batch.h>

#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <vector>

#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"
Expand Down
5 changes: 2 additions & 3 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
#include <rocksdb/env.h>
#include <strings.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <limits>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -125,7 +124,7 @@ Status SetRocksdbCompression(Server *srv, const rocksdb::CompressionType compres
std::vector<std::string> 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++) {
Expand Down
2 changes: 2 additions & 0 deletions src/storage/redis_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ std::string ComposeSlotKeyPrefix(const Slice &ns, int slotid) {
return output;
}

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)),
expire(0),
Expand Down
1 change: 1 addition & 0 deletions src/storage/redis_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ template <typename T = Slice>
[[nodiscard]] std::tuple<T, T> 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 ComposeSlotKeyUpperBound(const Slice &ns, int slotid);

class InternalKey {
public:
Expand Down
Loading