Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: handle non-existent keys properly in Store.SMembers #111

Merged
merged 2 commits into from
Feb 6, 2025

Conversation

volmedo
Copy link
Member

@volmedo volmedo commented Feb 5, 2025

I saw some unexpected traces when debugging some custom instrumentation I'm adding. Basically, the service was not looking for claims in IPNI or legacy systems even though there were no results in the cache.

Turns out that the SMEMBERS command doesn't return (nil) when the key doesn't exist, as others commands do. Instead, it returns an empty set (see the docs for SMEMBERS and then SINTER). Therefore, the store was returning an empty slice instead of ErrKeyNotFound.

I added an explicit existence check in SMembers when the command returns an empty set, assuming that it may be interesting to differentiate between a non-existent and an empty set.

@volmedo volmedo requested a review from alanshaw February 5, 2025 16:03
@volmedo volmedo self-assigned this Feb 5, 2025
Copy link

codecov bot commented Feb 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Files with missing lines Coverage Δ
pkg/redis/redisstore.go 86.36% <100.00%> (+0.20%) ⬆️

... and 2 files with indirect coverage changes

Copy link
Member

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

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

Hmm, Exists will be another round trip to the redis instance right? In the interests of speed and cost I'd probably either:

  1. Check the number of items in the result and return not found error if 0
  2. Make a note that this function does not return a not found error and check for 0 members in the calling code

...also bearing in mind that we do not have an API to remove from the set, so 0 members is effectively not exists

@volmedo
Copy link
Member Author

volmedo commented Feb 6, 2025

According to the traces, Exists takes around 0.8ms to complete, so it's not that much of a performance penalty. Not sure about the cost, though.

My first implementation was 1. I then thought that an empty set could have a different meaning. We don't currently do it, but we could store an empty set for hashes we have already tried to find claims for in the different systems but we couldn't find any. That would speed up future queries where we know we won't find any claims. Of course, that means that no claims will be returned as long as the key doesn't expire.

I'm not sure how the upload-service queries the indexer or what behavior it expects, so please let me know whether the above makes any sense or not. If it doesn't make sense, I'll simplify and implement 1.

Copy link
Member

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

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

Wow that is fast! Still, my gut instinct is to not cache failures so I'm not sure I'd agree to the possibility of an empty set anyway. I've approved anyway, it's not worth blocking the fix.

@volmedo
Copy link
Member Author

volmedo commented Feb 6, 2025

Let's assume empty sets are not useful for now. We can iterate on this if we find a use case for them in the future.

Copy link
Member

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

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

ACK ok! Still LGTM

@volmedo volmedo merged commit 4985df2 into main Feb 6, 2025
9 checks passed
@volmedo volmedo deleted the vic/fix/smembers-nonexistent-key branch February 6, 2025 12:01
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants