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

fix: support dump for external objects #4794

Merged
merged 1 commit into from
Mar 18, 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
18 changes: 16 additions & 2 deletions src/server/generic_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ extern "C" {
#include "server/rdb_save.h"
#include "server/search/doc_index.h"
#include "server/set_family.h"
#include "server/tiered_storage.h"
#include "server/transaction.h"
#include "util/fibers/future.h"
#include "util/varz.h"

ABSL_FLAG(uint32_t, dbnum, 16, "Number of databases");
Expand Down Expand Up @@ -524,9 +526,21 @@ OpResult<std::string> OpDump(const OpArgs& op_args, string_view key) {
if (IsValid(it)) {
DVLOG(1) << "Dump: key '" << key << "' successfully found, going to dump it";
io::StringSink sink;
SerializerBase::DumpObject(it->second, &sink);
return sink.str(); // TODO: Add rvalue overload to str()
const PrimeValue& pv = it->second;

if (pv.IsExternal() && !pv.IsCool()) {
util::fb2::Future<string> future =
op_args.shard->tiered_storage()->Read(op_args.db_cntx.db_index, key, pv);

CompactObj co(future.Get());
SerializerBase::DumpObject(co, &sink);
} else {
SerializerBase::DumpObject(it->second, &sink);
Copy link
Collaborator

@adiholden adiholden Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this cool thingy I dont remember this...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is very cool, actually. When we offload an object we may still keep it in-memory so if we hit it later, we can warm it up without hitting disk by moving it from the cool storage. Cool storage is just a FIFO queue of items and if we become low on memory we start discarding the oldest ones, making the values fully external.

}

return std::move(sink).str();
}

// fallback
DVLOG(1) << "Dump: '" << key << "' Not found";
return OpStatus::KEY_NOTFOUND;
Expand Down
9 changes: 8 additions & 1 deletion src/server/rdb_save.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,14 @@ std::error_code RdbSerializer::SaveValue(const PrimeValue& pv) {
if (opt_int) {
ec = SaveLongLongAsString(*opt_int);
} else {
ec = SaveString(pv.GetSlice(&tmp_str_));
if (pv.IsExternal()) {
if (pv.IsCool()) {
return SaveValue(pv.GetCool().record->value);
}
LOG(FATAL) << "External string not supported yet";
} else {
ec = SaveString(pv.GetSlice(&tmp_str_));
}
}
} else {
ec = SaveObject(pv);
Expand Down
5 changes: 2 additions & 3 deletions src/server/snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,8 @@ void SliceSnapshot::SerializeExternal(DbIndex db_index, PrimeKey key, const Prim
time_t expire_time, uint32_t mc_flags) {
// We prefer avoid blocking, so we just schedule a tiered read and append
// it to the delayed entries.
util::fb2::Future<string> future;
auto cb = [future](const std::string& v) mutable { future.Resolve(v); };
EngineShard::tlocal()->tiered_storage()->Read(db_index, key.ToString(), pv, std::move(cb));
util::fb2::Future<string> future =
EngineShard::tlocal()->tiered_storage()->Read(db_index, key.ToString(), pv);

delayed_entries_.push_back({db_index, std::move(key), std::move(future), expire_time, mc_flags});
++type_freq_map_[RDB_TYPE_STRING];
Expand Down
20 changes: 20 additions & 0 deletions src/server/tiered_storage_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,24 @@ TEST_F(TieredStorageTest, SetExistingExpire) {
}
}

TEST_F(TieredStorageTest, Dump) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 0.0f); // offload all values

// we want to test without cooling to trigger disk I/O on reads.
SetFlag(&FLAGS_tiered_experimental_cooling, false);

const int kNum = 10;
for (size_t i = 0; i < kNum; i++) {
Run({"SET", absl::StrCat("k", i), BuildString(3000)}); // big enough to trigger offloading.
}

ExpectConditionWithinTimeout([&] { return GetMetrics().tiered_stats.total_stashes == kNum; });

auto resp = Run({"DUMP", "k0"});
EXPECT_THAT(Run({"del", "k0"}), IntArg(1));
resp = Run({"restore", "k0", "0", facade::ToSV(resp.GetBuf())});
EXPECT_EQ(resp, "OK");
}

} // namespace dfly
Loading