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(stream_family): Fix memory tracking for the STREAMs. SECOND PR #4781

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 29 additions & 12 deletions src/core/compact_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ size_t QlMAllocSize(quicklist* ql, bool slow) {
return node_size + ql->count * 16; // we account for each member 16 bytes.
}

size_t UpdateSize(size_t size, int64_t update) {
if (update >= 0) {
return size + update;
}

// update < 0, so we need to convert it to unsigned
uint64_t unsigned_update = 0;
if (update > std::numeric_limits<int64_t>::min()) {
unsigned_update = -update;
} else {
static constexpr uint64_t kMaxUpdate = std::numeric_limits<int64_t>::max();
unsigned_update = kMaxUpdate + 1;
}

if (unsigned_update > std::numeric_limits<size_t>::max()) {
LOG(DFATAL) << "Overflow. Update size is too large. Size: " << size << ", update: " << update;
return 0;
}

DCHECK(static_cast<uint64_t>(size) >= unsigned_update);
return size - unsigned_update;
}

inline void FreeObjSet(unsigned encoding, void* ptr, MemoryResource* mr) {
switch (encoding) {
case kEncodingStrMap2: {
Expand Down Expand Up @@ -925,22 +948,16 @@ void CompactObj::SetJson(JsonType&& j) {
void CompactObj::SetJsonSize(int64_t size) {
if (taglen_ == JSON_TAG && JsonEnconding() == kEncodingJsonCons) {
// JSON.SET or if mem hasn't changed from a JSON op then we just update.
if (size < 0) {
DCHECK(static_cast<int64_t>(u_.json_obj.cons.bytes_used) >= size);
}
u_.json_obj.cons.bytes_used += size;
u_.json_obj.cons.bytes_used = UpdateSize(u_.json_obj.cons.bytes_used, size);
}
}

void CompactObj::AddStreamSize(int64_t size) {
if (size < 0) {
// We might have a negative size. For example, if we remove a consumer,
// the tracker will report a negative net (since we deallocated),
// so the object now consumes less memory than it did before. This DCHECK
// is for fanity and to catch any potential issues with our tracking approach.
DCHECK(static_cast<int64_t>(u_.r_obj.Size()) >= size);
}
u_.r_obj.SetSize((u_.r_obj.Size() + size));
// We might have a negative size. For example, if we remove a consumer,
// the tracker will report a negative net (since we deallocated),
// so the object now consumes less memory than it did before. This DCHECK
// is for fanity and to catch any potential issues with our tracking approach.
u_.r_obj.SetSize(UpdateSize(u_.r_obj.Size(), size));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the only functional difference from the other PR. Consider merging them into one if this is just a single line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's merge the PR for JSON first because I see that the fakeredis tests are failing. It might take some time to understand what the bug is. And then maybe we will need to fix stream memory tracking in other places to pass the tests

Copy link
Contributor

Choose a reason for hiding this comment

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

no issue with that -- whatever is more convenient

}

void CompactObj::SetJson(const uint8_t* buf, size_t len) {
Expand Down
6 changes: 4 additions & 2 deletions src/server/json_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ OpResult<DbSlice::ItAndUpdater> SetJson(const OpArgs& op_args, string_view key,

op_args.shard->search_indices()->RemoveDoc(key, op_args.db_cntx, res.it->second);

JsonMemTracker tracker;
std::optional<JsonType> parsed_json = ShardJsonFromString(json_str);
if (!parsed_json) {
VLOG(1) << "got invalid JSON string '" << json_str << "' cannot be saved";
Expand All @@ -354,6 +355,9 @@ OpResult<DbSlice::ItAndUpdater> SetJson(const OpArgs& op_args, string_view key,
} else {
res.it->second.SetJson(std::move(*parsed_json));
}

tracker.SetJsonSize(res.it->second, res.is_new);

op_args.shard->search_indices()->AddDoc(key, op_args.db_cntx, res.it->second);
return std::move(res);
}
Expand Down Expand Up @@ -1329,10 +1333,8 @@ OpResult<bool> OpSet(const OpArgs& op_args, string_view key, string_view path,
}
}

JsonMemTracker mem_tracker;
auto st = SetJson(op_args, key, json_str);
RETURN_ON_BAD_STATUS(st);
mem_tracker.SetJsonSize(st->it->second, st->is_new);
return true;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/dragonfly/replication_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2951,3 +2951,35 @@ async def test_preempt_in_atomic_section_of_heartbeat(df_factory: DflyInstanceFa
await wait_for_replicas_state(*c_replicas)

await fill_task


async def test_bug_in_json_memory_tracking(df_factory: DflyInstanceFactory):
"""
This test reproduces a bug in the JSON memory tracking.
"""
master = df_factory.create(proactor_threads=2, serialization_max_chunk_size=1)
replicas = [df_factory.create(proactor_threads=2) for i in range(2)]

# Start instances and connect clients
df_factory.start_all([master] + replicas)
c_master = master.client()
c_replicas = [replica.client() for replica in replicas]

total = 100000
await c_master.execute_command(f"DEBUG POPULATE {total} tmp 1000 TYPE SET ELEMENTS 100")

thresehold = 25000
for i in range(thresehold):
rand = random.randint(1, 4)
await c_master.execute_command(f"EXPIRE tmp:{i} {rand} NX")

seeder = StaticSeeder(key_target=100_000)
fill_task = asyncio.create_task(seeder.run(master.client()))

for replica in c_replicas:
await replica.execute_command(f"REPLICAOF LOCALHOST {master.port}")

async with async_timeout.timeout(240):
await wait_for_replicas_state(*c_replicas)

await fill_task
Loading