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

[Solves #254] Add support for RANDOMKEY command #1093

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

xanish
Copy link

@xanish xanish commented Oct 14, 2024

Hey @JyotinderSingh @arpitbbhayani,

Sorry if it was not allowed to initiate a pull without getting assigned (Issue #254) (If it was feel free to discard this MR).

I had implemented this some weeks back but did not get a reply so decided to commit and push the changes.

For now I've made it so that it uses the existing KEYS implementation and works in O(n). It took approximately 1ms for 100k key entries and 40ms for 1m entries based on the go test benchmark.

If we need it in O(1) might have to create a separate key slice to keep track of the keys and extract RANDOMKEY in O(1).

Additionally, I have skipped the loop to check if the RANDOMKEY is expired, so it is possible for a non-evicted expired key to be returned, again can make changes as needed for the requirement.

Thanks.

@apoorvyadav1111
Copy link
Contributor

Hi @xanish, Thanks for the changes. Ideally, people work on the issues assigned to them, to avoid duplicate effort. Hopefully, this will answer your question, but if you would like to discuss more, drop a message on the discord community here.

@JyotinderSingh
Copy link
Collaborator

Since the existing owner of the issue did not submit any PRs for a while, let's go ahead with this one. Assigning the issue to you. I'll go through this PR in a bit

@JyotinderSingh
Copy link
Collaborator

Let's ensure we don't return expired but non-evicted keys.

@xanish
Copy link
Author

xanish commented Oct 16, 2024

Updated and rebased with new changes on upstream master

Copy link
Contributor

@SyedMa3 SyedMa3 left a comment

Choose a reason for hiding this comment

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

Also, please add integration tests to resp, http, websockets.

internal/eval/eval.go Outdated Show resolved Hide resolved
@xanish xanish force-pushed the issue-254-support-randomkey branch from bc83cfe to b84fd3f Compare October 20, 2024 15:38
@xanish
Copy link
Author

xanish commented Oct 22, 2024

@SyedMa3 looks good?

@SyedMa3
Copy link
Contributor

SyedMa3 commented Oct 23, 2024

If we have to get it done in less than O(0) time complexity, we would have to dive deep into the map implementation of Go.

I found this article which explores random key in O(1) space and O(L * (1 + k/n)) time, where n is the number of elements in the map, k is the "capacity" of the map (how many elements it can hold before being resized), and L is the length of the longest "bucket chain." Since Go maps double in size when they grow, the total time will generally not exceed 2x the normal map lookup time.

Article: https://lukechampine.com/hackmap.html
Repo: https://github.com/lukechampine/randmap

We can take inspiration and maybe do something like this if it's possible?
I have not understood it completely so not sure if it's production safe.

What do you guys think?
@JyotinderSingh @apoorvyadav1111 @arpitbbhayani @soumya-codes @AshwinKul28

internal/eval/eval.go Outdated Show resolved Hide resolved
internal/eval/eval.go Outdated Show resolved Hide resolved
@JyotinderSingh
Copy link
Collaborator

If we have to get it done in less than O(0) time complexity, we would have to dive deep into the map implementation of Go.

I found this article which explores random key in O(1) space and O(L * (1 + k/n)) time, where n is the number of elements in the map, k is the "capacity" of the map (how many elements it can hold before being resized), and L is the length of the longest "bucket chain." Since Go maps double in size when they grow, the total time will generally not exceed 2x the normal map lookup time.

Article: https://lukechampine.com/hackmap.html

Repo: https://github.com/lukechampine/randmap

We can take inspiration and maybe do something like this if it's possible?

I have not understood it completely so not sure if it's production safe.

What do you guys think?

@JyotinderSingh @apoorvyadav1111 @arpitbbhayani @soumya-codes @AshwinKul28

This is quite a big task to take up. Maybe we can put this off for later since it is not of the highest priority to have this command run in O(1) time.

Thanks for exploring and diving deep into this @SyedMa3. If someone wants to take up this task independently then that's great. However, the core team is stretched thin across a lot of the different projects which are going on right now and won't be able to provide active support on the initiative to make this run faster.

internal/eval/eval.go Outdated Show resolved Hide resolved
@xanish
Copy link
Author

xanish commented Oct 24, 2024

@JyotinderSingh @SyedMa3 lemme know if there is anything to be changed here.

@xanish xanish force-pushed the issue-254-support-randomkey branch from 2c7138b to 521798e Compare October 24, 2024 16:49
@JyotinderSingh
Copy link
Collaborator

This PR does not compile, could you please take a look @xanish?

Copy link
Collaborator

@JyotinderSingh JyotinderSingh left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the comments so far @xanish. Please address @SyedMa3's comments and the new comment added by me.

internal/eval/eval.go Outdated Show resolved Hide resolved
@xanish xanish force-pushed the issue-254-support-randomkey branch from 7748e9a to 319f450 Compare November 14, 2024 18:49
Copy link
Collaborator

@JyotinderSingh JyotinderSingh left a comment

Choose a reason for hiding this comment

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

Thanks for adding support for the RANDOMKEY command @xanish! I've left a few comments.

docs/src/content/docs/commands/RANDOMKEY.md Outdated Show resolved Hide resolved
internal/eval/commands.go Outdated Show resolved Hide resolved
internal/eval/eval.go Outdated Show resolved Hide resolved
@JyotinderSingh
Copy link
Collaborator

Some of the test cases are failing, please take a look.

@xanish xanish force-pushed the issue-254-support-randomkey branch from 136a60e to f636322 Compare November 17, 2024 20:40
@xanish
Copy link
Author

xanish commented Nov 18, 2024

@JyotinderSingh can you check now

@JyotinderSingh
Copy link
Collaborator

@xanish thanks for working on this feature.

We are going to make multi-threading the default mode of operation for DiceDB in the coming days. We will need to migrate this command as a part of this change itself.

I'm pulling in @AshwinKul28 to help lead the discussion on that and guide you about it since multithreading support for this command will not be straightforward.

Feel free to take your time on the implementation.

@xanish
Copy link
Author

xanish commented Dec 16, 2024

Hey @AshwinKul28 can you direct me on this? i will have free time from this weekend so i can wrap this up.

@xanish
Copy link
Author

xanish commented Jan 3, 2025

Hey @JyotinderSingh @AshwinKul28 does everything seem fine here? Can we get it merged?

@JyotinderSingh
Copy link
Collaborator

Hey @JyotinderSingh @AshwinKul28 does everything seem fine here? Can we get it merged?

Will review this soon, thanks for working on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants