Skip to content

Commit d584381

Browse files
authored
refactor: Add DelMutable() helper to unify post_updater.Run() + Del() pattern (#5890)
* refactor: Add DelMutable() helper to unify post_updater.Run() + Del() pattern * fix: review comments
1 parent fbc47ff commit d584381

File tree

8 files changed

+22
-24
lines changed

8 files changed

+22
-24
lines changed

src/server/db_slice.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ void DbSlice::Del(Context cntx, Iterator it) {
816816
PerformDeletion(it, db.get());
817817
}
818818

819+
void DbSlice::DelMutable(Context cntx, ItAndUpdater it_updater) {
820+
it_updater.post_updater.Run();
821+
Del(cntx, it_updater.it);
822+
}
823+
819824
void DbSlice::FlushSlotsFb(const cluster::SlotSet& slot_ids) {
820825
VLOG(1) << "Start FlushSlotsFb";
821826
// Slot deletion can take time as it traverses all the database, hence it runs in fiber.

src/server/db_slice.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ class DbSlice {
328328
// Deletes the iterator. The iterator must be valid.
329329
void Del(Context cntx, Iterator it);
330330

331+
// Deletes a key after FindMutable(). Runs post_updater before deletion
332+
// to update memory accounting while the key is still valid.
333+
// Takes ownership of it_updater (pass by value with move semantics).
334+
void DelMutable(Context cntx, ItAndUpdater it_updater);
335+
331336
constexpr static DbIndex kDbAll = 0xFFFF;
332337

333338
// Flushes db_ind or all databases if kDbAll is passed

src/server/generic_family.cc

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ OpStatus Renamer::DelSrc(Transaction* t, EngineShard* shard) {
458458

459459
DVLOG(1) << "Rename: removing the key '" << src_key_;
460460

461-
res.post_updater.Run();
462-
db_slice.Del(t->GetDbContext(), it);
461+
db_slice.DelMutable(t->GetDbContext(), std::move(res));
463462
if (shard->journal()) {
464463
RecordJournal(t->GetOpArgs(shard), "DEL"sv, ArgSlice{src_key_}, 2);
465464
}
@@ -480,8 +479,7 @@ OpStatus Renamer::DeserializeDest(Transaction* t, EngineShard* shard) {
480479

481480
if (dest_found_) {
482481
DVLOG(1) << "Rename: deleting the destiny key '" << dest_key_;
483-
dest_res.post_updater.Run();
484-
db_slice.Del(op_args.db_cntx, dest_res.it);
482+
db_slice.DelMutable(op_args.db_cntx, std::move(dest_res));
485483
}
486484

487485
if (restore_args.Expired()) {
@@ -588,8 +586,7 @@ OpStatus OpRestore(const OpArgs& op_args, std::string_view key, std::string_view
588586
if (restore_args.Replace()) {
589587
VLOG(1) << "restore command is running with replace, found old key '" << key
590588
<< "' and removing it";
591-
res.post_updater.Run();
592-
db_slice.Del(op_args.db_cntx, res.it);
589+
db_slice.DelMutable(op_args.db_cntx, std::move(res));
593590
} else {
594591
// we are not allowed to replace it.
595592
return OpStatus::KEY_EXISTS;
@@ -938,14 +935,12 @@ OpResult<void> OpRen(const OpArgs& op_args, string_view from_key, string_view to
938935
to_res.it->first.SetSticky(sticky);
939936
to_res.post_updater.Run();
940937

941-
from_res.post_updater.Run();
942-
db_slice.Del(op_args.db_cntx, from_res.it);
938+
db_slice.DelMutable(op_args.db_cntx, std::move(from_res));
943939
} else {
944940
// Here we first delete from_it because AddNew below could invalidate from_it.
945941
// On the other hand, AddNew does not rely on the iterators - this is why we keep
946942
// the value in `from_obj`.
947-
from_res.post_updater.Run();
948-
db_slice.Del(op_args.db_cntx, from_res.it);
943+
db_slice.DelMutable(op_args.db_cntx, std::move(from_res));
949944
auto op_result = db_slice.AddNew(op_args.db_cntx, to_key, std::move(from_obj), exp_ts);
950945
RETURN_ON_BAD_STATUS(op_result);
951946
to_res = std::move(*op_result);

src/server/hset_family.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ struct KeyCleanup {
281281

282282
void DeleteKey(DbSlice& db_slice, const OpArgs& op_args, std::string_view key) {
283283
if (auto del_it = db_slice.FindMutable(op_args.db_cntx, key, OBJ_HASH); del_it) {
284-
del_it->post_updater.Run();
285-
db_slice.Del(op_args.db_cntx, del_it->it);
284+
db_slice.DelMutable(op_args.db_cntx, std::move(*del_it));
286285
if (op_args.shard->journal()) {
287286
RecordJournal(op_args, "DEL"sv, {key});
288287
}
@@ -1166,8 +1165,7 @@ void HSetFamily::HRandField(CmdArgList args, const CommandContext& cmd_cntx) {
11661165
if (string_map->Empty()) { // Can happen if we use a TTL on hash members.
11671166
auto res_it = db_slice.FindMutable(db_context, key, OBJ_HASH);
11681167
if (res_it) {
1169-
res_it->post_updater.Run();
1170-
db_slice.Del(db_context, res_it->it);
1168+
db_slice.DelMutable(db_context, std::move(*res_it));
11711169
}
11721170
return facade::OpStatus::KEY_NOTFOUND;
11731171
}

src/server/json_family.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,7 @@ OpResult<long> OpDel(const OpArgs& op_args, string_view key, string_view path,
11171117
RETURN_ON_BAD_STATUS(res_it);
11181118

11191119
if (IsValid(res_it->it)) {
1120-
res_it->post_updater.Run();
1121-
db_slice.Del(op_args.db_cntx, res_it->it);
1120+
db_slice.DelMutable(op_args.db_cntx, std::move(*res_it));
11221121
return 1;
11231122
}
11241123
return 0;

src/server/set_family.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ OpResult<uint32_t> OpAdd(const OpArgs& op_args, std::string_view key, const NewE
483483
if (overwrite && (vals_it.begin() == vals_it.end())) {
484484
auto res_it = db_slice.FindMutable(op_args.db_cntx, key, OBJ_SET);
485485
if (res_it) {
486-
res_it->post_updater.Run();
487-
db_slice.Del(op_args.db_cntx, res_it->it);
486+
db_slice.DelMutable(op_args.db_cntx, std::move(*res_it));
488487
if (journal_update && op_args.shard->journal()) {
489488
RecordJournal(op_args, "DEL"sv, ArgSlice{key});
490489
}
@@ -927,8 +926,7 @@ OpResult<StringVec> OpPop(const OpArgs& op_args, string_view key, unsigned count
927926
});
928927

929928
// Delete the set as it is now empty
930-
find_res->post_updater.Run();
931-
db_slice.Del(op_args.db_cntx, find_res->it);
929+
db_slice.DelMutable(op_args.db_cntx, std::move(*find_res));
932930

933931
// Replicate as DEL.
934932
if (op_args.shard->journal()) {

src/server/string_family.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,7 @@ void StringFamily::GetDel(CmdArgList args, const CommandContext& cmnd_cntx) {
11491149
return it_res.status();
11501150

11511151
auto value = ReadString(tx->GetDbIndex(), key, it_res->it->second, es);
1152-
it_res->post_updater.Run(); // Run manually before delete
1153-
db_slice.Del(tx->GetDbContext(), it_res->it);
1152+
db_slice.DelMutable(tx->GetDbContext(), std::move(*it_res));
11541153
return value;
11551154
};
11561155

src/server/zset_family.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,8 +1969,7 @@ OpResult<ZSetFamily::AddResult> ZSetFamily::OpAdd(const OpArgs& op_args,
19691969
if (zparams.override && members.empty()) {
19701970
auto res_it = db_slice.FindMutable(op_args.db_cntx, key, OBJ_ZSET);
19711971
if (res_it && IsValid(res_it->it)) {
1972-
res_it->post_updater.Run();
1973-
db_slice.Del(op_args.db_cntx, res_it->it);
1972+
db_slice.DelMutable(op_args.db_cntx, std::move(*res_it));
19741973
if (zparams.journal_update && op_args.shard->journal()) {
19751974
RecordJournal(op_args, "DEL"sv, ArgSlice{key});
19761975
}

0 commit comments

Comments
 (0)