Skip to content

cleanup: move list pop logic to single function#7997

Merged
oranagra merged 1 commit intoredis:unstablefrom
yashLadha:fix_blpop_dup_code
Nov 12, 2020
Merged

cleanup: move list pop logic to single function#7997
oranagra merged 1 commit intoredis:unstablefrom
yashLadha:fix_blpop_dup_code

Conversation

@yashLadha
Copy link
Contributor

BLPOP when there are elements in the list works in the same way as LPOP
does. Due to this they also does the same repeatitive action and logic
for the same is written at two different places. This is a bad code
practice as the one needs the context to change the BLPOP list pop code
as well when the LPOP code gets changed.

Separated the generic logic from LPOP to a function that is being used
by the BLPOP code as well.

Copy link
Member

@oranagra oranagra left a comment

Choose a reason for hiding this comment

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

i'm not sure how i feel about moving the addReply into that helper function. i think we may want to keep it part of the actual command processing function, and not the helper function.

the other parts are all about modifying the source list, so i guess that's ok. so maybe remove the value argument and rename the helper function to listElementsRemoved?

other notes:

  1. redis uses 4 spaces indentation.
  2. if you change the order of popElementFromList and popGenericCommand (and add a forward declaration at the top), maybe the diff will be easier to read (and less changes to the blame log).

this is in some way a very small part of #7551. i.e. once we do the change it suggests, the one in this PR isn't needed.
@felipou will this conflict with any work you already started on #766

@yashLadha
Copy link
Contributor Author

@oranagra I moved the logic into a single function because at that point both LPOP and BLPOP are performing the same thing. If they are performing the same thing then we do not require to use repetition of logic at two different places.

redis uses 4 spaces indentation.

Sure will take note of it. Actually having a .editorconfig or some lint rule makes this work easier.

Having major refactoring in one PR will take a lot of iteration, having smaller changes into smaller makes landing easier as well as review cycles. Again these are my own opinions 😅

@oranagra
Copy link
Member

oranagra commented Nov 1, 2020

@yashLadha the intention behind that other refactoring project is that when redis unblocks the client (when BLPOP gets unblocked), it'll just re-run BLPOP, so it'll eliminate code duplication by simply re-running the same code again.
but i now realize that that's actually solves the duplicate code between blockingPopGenericCommand and serveClientBlockedOnList, and that that's not the duplicate code you're eliminating (this logic is in some way duplicated 3 times).

regarding the addReply, IMHO the fact that it's used in the duplicate code is not necessarily a good reason to share it IMHO. i think the handling of the reply and value are more intimately related to the calling function and the command and how it wants to handle that.
if we give that function and clear name and role, i think most of it is about taking the value out of the list, and not about how we respond to the client.
If we do that, maybe we can use it in other places (like lremCommand, and serveClientBlockedOnList), since it's a building block that makes sense (every time you remove the last element from a list you need to delete it and send keyspace notification).

@felipou
Copy link
Contributor

felipou commented Nov 1, 2020

@felipou will this conflict with any work you already started on #766

I haven't done anything relevant there yet, but I definitely must sync with this for that work.

Also, I think there is a very similar code in the LMOVE/BLMOVE commands, that could also be replaced with popElementFromList. I think this reflects well the importance of what @oranagra was saying about not having the addReply inside this function.

Anyway, when thinking about #766 I thought that it would be interesting to also add the COUNT argument to LMOVE, since it could probably use the same code. If we unify all the pop logic, it probably would make that quite easier.

@felipou
Copy link
Contributor

felipou commented Nov 1, 2020

I haven't looked into it, but maybe the push functions could be refactored in the same way?

@yashLadha
Copy link
Contributor Author

I haven't looked into it, but maybe the push functions could be refactored in the same way?

Yes, it can be done but I thought instead of pushing large changes, one can iteratively improve on it. Let me know your thoughts.

regarding the addReply, IMHO the fact that it's used in the duplicate code is not necessarily a good reason to share it IMHO. I think the handling of the reply and value are more intimately related to the calling function and the command and how it wants to handle that.

Cool, I will read more about the code and see how it can be improved, and work on your suggestions.

@felipou Should I proceed with the review changes or you are also working on the same thing? This is to avoid duplication of work.

@felipou
Copy link
Contributor

felipou commented Nov 3, 2020

@felipou Should I proceed with the review changes or you are also working on the same thing? This is to avoid duplication of work.

Yes, please proceed, I'll monitor this issue closely as I work on the other issue, so if you just keep this pull requests up to date, I believe it will be easy to sync. I'll comment here if needed.

@yashLadha yashLadha force-pushed the fix_blpop_dup_code branch 2 times, most recently from 1c37138 to 24eb977 Compare November 11, 2020 01:41
@yashLadha
Copy link
Contributor Author

@oranagra @felipou Made suggested changes. Can you please re-review?

@yashLadha yashLadha requested a review from oranagra November 11, 2020 01:53
src/t_list.c Outdated
Copy link
Member

Choose a reason for hiding this comment

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

in my eyes, the decrRefcount is not part of the responsibility of this function.
it shouldn't deal with the value.
it didn't removed it from the list, and it didn't put it in the reply buffer, so it doesn't suppose to know if the refcount needs decrement or not, that's part of the responsibilities of the caller.
this function is just about what needs to be done the key, keyspace and general server tasks after the list was modified.

src/t_list.c Outdated
Comment on lines 370 to 375
Copy link
Member

Choose a reason for hiding this comment

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

this function must not assume that argv[1] is the key name, it should get it as an argument from the caller.

BLPOP when there are elements in the list workes in the same way as LPOP
does. Due to this they also does the same repeatitive action and logic
for the same is written at two different places. This is a bad code
practice as the one needs the context to change the BLPOP list pop code
as well when the LPOP code gets changed.

Separated the generic logic from LPOP to a function that is being used
by the BLPOP code as well.
@oranagra oranagra changed the title chore: move list pop logic to single function cleanup: move list pop logic to single function Nov 12, 2020
@oranagra oranagra merged commit c170365 into redis:unstable Nov 12, 2020
@oranagra
Copy link
Member

merged. thank you.

JackieXie168 pushed a commit to JackieXie168/redis that referenced this pull request Nov 19, 2020
BLPOP when there are elements in the list works in the same way as LPOP
does. Due to this they also does the same repetitive action and logic
for the same is written at two different places. This is a bad code
practice as the one needs the context to change the BLPOP list pop code
as well when the LPOP code gets changed.

Separated the generic logic from LPOP to a function that is being used
by the BLPOP code as well.
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.

3 participants