Skip to content

Commit

Permalink
fix(bug): server crash info memory while saving (#2637)
Browse files Browse the repository at this point in the history
* bug: crash info memory while saving

Signed-off-by: adi_holden <[email protected]>
  • Loading branch information
adiholden authored and romange committed Feb 21, 2024
1 parent 307cb1d commit 9ed5513
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/server/detail/save_stages_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ error_code RdbSnapshot::SaveBody() {
}

size_t RdbSnapshot::GetSaveBuffersSize() {
CHECK(saver_);
return saver_->GetTotalBuffersSize();
}

Expand Down Expand Up @@ -172,17 +173,24 @@ SaveInfo SaveStagesController::Save() {

size_t SaveStagesController::GetSaveBuffersSize() {
std::atomic<size_t> total_bytes{0};
if (use_dfs_format_) {
auto cb = [this, &total_bytes](ShardId sid) {
total_bytes.fetch_add(snapshots_[sid].first->GetSaveBuffersSize(), memory_order_relaxed);
};
shard_set->RunBriefInParallel([&](EngineShard* es) { cb(es->shard_id()); });

} else {
// When rdb format save is running, there is only one rdb saver instance, it is running on the
// connection thread that runs the save command.
total_bytes.store(snapshots_.front().first->GetSaveBuffersSize(), memory_order_relaxed);
auto add_snapshot_bytes = [this, &total_bytes](ShardId sid) {
if (auto& snapshot = snapshots_[sid].first; snapshot && snapshot->HasStarted()) {
total_bytes.fetch_add(snapshot->GetSaveBuffersSize(), memory_order_relaxed);
}
};

if (snapshots_.size() > 0) {
if (use_dfs_format_) {
shard_set->RunBriefInParallel([&](EngineShard* es) { add_snapshot_bytes(es->shard_id()); });

} else {
// When rdb format save is running, there is only one rdb saver instance, it is running on the
// connection thread that runs the save command.
add_snapshot_bytes(0);
}
}

return total_bytes.load(memory_order_relaxed);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/dragonfly/snapshot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,28 @@ async def test_snapshot(self, df_seeder_factory, df_server):
await a_client.connection_pool.disconnect()


@dfly_args({**BASIC_ARGS, "dbfilename": "info-while-snapshot"})
async def test_infomemory_while_snapshoting(async_client: aioredis.Redis):
await async_client.execute_command("DEBUG POPULATE 10000 key 4048 RAND")

async def save():
await async_client.execute_command("SAVE", "DF")

save_finished = False

async def info_in_loop():
while not save_finished:
await async_client.execute_command("INFO MEMORY")
await asyncio.sleep(0.1)

save_task = asyncio.create_task(save())
info_task = asyncio.create_task(info_in_loop())

await save_task
save_finished = True
await info_task


# If DRAGONFLY_S3_BUCKET is configured, AWS credentials must also be
# configured.
@pytest.mark.skipif(
Expand Down

0 comments on commit 9ed5513

Please sign in to comment.