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

added support for RANDOMKEY #303

Closed
wants to merge 0 commits into from
Closed

Conversation

rkarthick15
Copy link
Contributor

#254 added support for RANDOMKEY Command

@JyotinderSingh
Copy link
Collaborator

Please rebase on latest master and resolve the conflicts. Will review once done.

core/store.go Outdated
@@ -328,3 +329,18 @@ func delByPtr(ptr unsafe.Pointer) bool {
}
return false
}

func RandomKey() string {
if len(keypool) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You need to acquire a lock when dealing with the keypool

core/store.go Outdated
}
index := rand.Intn(len(keypool))
currentIndex := 0
for key := range keypool {
Copy link
Contributor

@soumya-codes soumya-codes Aug 12, 2024

Choose a reason for hiding this comment

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

Wouldn't this operation have O(N) complexity? If so there may be a chance of higher latency when the key size is large?

Wouldn't limiting the numbers of iterations to >=1024 be enough?
The time-complexity is REDIS is mentioned as O(1). Do we know how is it implemented in Redis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JyotinderSingh & @soumya-codes I might need some help over here. To implement this RANDOMKEY command. I coundn't able to come up with any solution for O(1) complexity

Copy link
Contributor

Choose a reason for hiding this comment

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

How about taking a very simplistic approach and limiting the number of iterations to within 512/1024.

idxRange := len(keypool) / 2
if idxRange > 1024 {
  idxRange = 1024
}

rand.Seed(time.Now().UnixNano())
randomIndex := rand.Intn(idxRange)

@JyotinderSingh wdyt?

Copy link
Collaborator

@JyotinderSingh JyotinderSingh Aug 14, 2024

Choose a reason for hiding this comment

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

This approach sounds good to me. But it will introduce a very heavy sampling bias towards the start of the keyset.
But I think it is an acceptable trade off at this stage, we could revisit this if we decide to modify our store data structures in the future.

core/store.go Outdated
@@ -328,3 +329,18 @@ func delByPtr(ptr unsafe.Pointer) bool {
}
return false
}

func RandomKey() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we know if accessing a key randomly effects the expiry of the key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@soumya-codes yes RANDOMKEY should not return expired keys (with maxtries of 100, if exceeds max try it may return the expired key)

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