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

Add defragment support for HFE #13229

Merged
merged 87 commits into from May 14, 2024
Merged

Conversation

sundb
Copy link
Collaborator

@sundb sundb commented Apr 19, 2024

Background

  1. All hash objects that contain HFE are referenced by db->hexpires.
  2. All fields in a dict hash object with HFE are referenced by an ebucket.

So when we defrag the hash object or the field in a dict with HFE, we also need to update the references in them.

Interface

  1. Add a new interface ebDefragItem, which can accept a defrag callback to defrag items in ebuckets, and simultaneously update their references in the ebucket.

Mainly changes

  1. The key type of dict of hash object is no longer sds, so add new activeDefragHfieldDict() to defrag the dict instead of activeDefragSdsDict().
  2. When we defrag the dict of hash object by using dictScanDefrag(), we always set the defrag callback defragKey of dictDefragFunctions to NULL, because we can't reallocate a field with out updating it's reference in ebuckets.
    Instead, we will defrag the field of the dict and update its reference in the callback dictScanDefrag of dictScanFunction().
  3. When we defrag the hash robj with HFE, we will use ebDefragItem to defrag the robj and update the reference in db->hexpires.

TODO:

Defrag ebucket structure incremently, which will be handler in a future PR.

@sundb sundb requested a review from moticless April 19, 2024 08:44
@sundb sundb marked this pull request as ready for review April 25, 2024 12:48
@sundb sundb changed the title WIP: Hash Field Expiration Defragmentation AddHash Field Expiration Defragmentation Apr 25, 2024
@sundb sundb changed the title AddHash Field Expiration Defragmentation Add defragment support for HFE Apr 25, 2024
@sundb sundb requested review from moticless and tezc May 9, 2024 06:28
src/defrag.c Outdated Show resolved Hide resolved
src/ebuckets.c Outdated
@@ -1780,6 +1780,59 @@ void ebValidate(ebuckets eb, EbucketsType *type) {
ebValidateRax(ebGetRaxPtr(eb), type);
}

eItem ebDefragItem(ebuckets *eb, EbucketsType *type, eItem item, ebDefragFunction *fn) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add assert and verify all the calls to this function that the ExpireMeta of the item is not trash.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hfieldGetExpireTime() has ensured that the trash is not 1.
the caller is responsible for ensuring the item has TTL via hfieldGetExpireTime(), otherwise, all the calls to this function will be dangerous.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added comment (include note) for this function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

after struggling, i decide to add the assertion.

src/defrag.c Outdated
Comment on lines 754 to 756
if (unlikely(ob->type == OBJ_HASH && hashTypeHasExpireField(ob))) {
/* Update its reference in the ebucket while defragging it. */
newob = ebDefragItem(&db->hexpires, &hashExpireBucketsType, ob, (ebDefragFunction *)activeDefragStringOb);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function should renamed to hashTypeHasExpireMeta or hashTypeExpireSupport . It doesn't necessarily gurantee that it has fields with expiry. Only tells that it support expiry.

We need to verify either inside ebDefragItem or before the call that ExpireMeta of the hash is not Trash.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can discard entire function hashTypeHasExpireMeta() and use instead existing hashTypeGetMinExpire() against EB_EXPIRE_TIME_INVALID.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good idea, i choose 2

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done with 5bf9ddb (#13229).

@sundb sundb requested review from tezc and moticless May 9, 2024 09:23
src/defrag.c Outdated Show resolved Hide resolved
src/ebuckets.c Show resolved Hide resolved
src/ebuckets.c Show resolved Hide resolved
sundb and others added 2 commits May 10, 2024 21:39
Co-authored-by: Ozan Tezcan <ozantezcan@gmail.com>
Co-authored-by: Moti Cohen <moti.cohen@redis.com>
src/server.h Outdated Show resolved Hide resolved
@@ -204,7 +204,7 @@ start_server {tags {"external:skip needs:debug"}} {
}

# Wait for active expire
wait_for_condition 50 20 { [r EXISTS same40] == 0 } else { fail "hash `same40` should be expired" }
wait_for_condition 500 20 { [r EXISTS same40] == 0 } else { fail "hash `same40` should be expired" }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

src/server.h Outdated Show resolved Hide resolved
src/server.h Outdated Show resolved Hide resolved
src/ebuckets.c Show resolved Hide resolved
src/ebuckets.c Outdated
@@ -1780,6 +1780,59 @@ void ebValidate(ebuckets eb, EbucketsType *type) {
ebValidateRax(ebGetRaxPtr(eb), type);
}

eItem ebDefragItem(ebuckets *eb, EbucketsType *type, eItem item, ebDefragFunction *fn) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added comment (include note) for this function.

src/defrag.c Outdated
Comment on lines 754 to 756
if (unlikely(ob->type == OBJ_HASH && hashTypeHasExpireField(ob))) {
/* Update its reference in the ebucket while defragging it. */
newob = ebDefragItem(&db->hexpires, &hashExpireBucketsType, ob, (ebDefragFunction *)activeDefragStringOb);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done with 5bf9ddb (#13229).

src/ebuckets.c Show resolved Hide resolved
src/ebuckets.c Show resolved Hide resolved
src/ebuckets.c Outdated
@@ -1780,6 +1780,59 @@ void ebValidate(ebuckets eb, EbucketsType *type) {
ebValidateRax(ebGetRaxPtr(eb), type);
}

eItem ebDefragItem(ebuckets *eb, EbucketsType *type, eItem item, ebDefragFunction *fn) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

after struggling, i decide to add the assertion.

src/ebuckets.c Show resolved Hide resolved
@tezc
Copy link
Collaborator

tezc commented May 13, 2024

I see the comment that you'll add test cases for eblist and listpack later. I didn't do deep bug hunting but overall, PR seems good to me.

tezc
tezc previously approved these changes May 13, 2024
@sundb
Copy link
Collaborator Author

sundb commented May 14, 2024

@tezc please have a look, make the test cover the listpackex and eblist.

@sundb
Copy link
Collaborator Author

sundb commented May 14, 2024

@tezc because the defragment for ebucket doesn't complete, i increase the threhold to make the test stable, now the test is just for ensuring the correctly updating of references.

tests/unit/memefficiency.tcl Outdated Show resolved Hide resolved
@sundb sundb merged commit 80be2cc into redis:hash-field-expiry-integ May 14, 2024
13 checks passed
@sundb sundb deleted the hfe-defrag branch May 14, 2024 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants