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 cluster support for scripting #1937

Merged
merged 5 commits into from
Feb 22, 2022

Conversation

jakebarnwell
Copy link
Contributor

@jakebarnwell jakebarnwell commented Feb 11, 2022

Pull Request check-list

Please make sure to review and check all of these items:

  • Does $ tox pass with this change (including linting)?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.

Description of change

Partly fixes #1898:

Adds scripting support, but does not yet add .lock() to RedisCluster. That will come in a following PR.

Much of this change was stolen adapted from redis-py-cluster.

Implements the following commands:

  • EVAL: the command is sent to the cluster depending on the keys (i.e., in EVAL <SCRIPT> num_keys KEY1 ... KEYN ...); the keys must all be on the same node. If the script requires 0 keys, the command is sent to a random node.
  • EVALSHA: same implementation as above
  • SCRIPT EXISTS: the command is sent to all primaries. The result is a list of booleans. Each boolean is an AND of "does the script exist on each node". In other words, each boolean is True iff the script exists on all nodes.
  • SCRIPT FLUSH: the command is sent to all primaries. The result is a bool AND over all nodes' responses.
  • SCRIPT LOAD: the command is sent to all primaries. The result is the SHA1 digest.

I have not (yet?) implemented the EVAL_RO commands -- do those need to be implemented? Maybe.

This PR does not implement, nor do I intend to implement:

  • SCRIPT KILL
  • scripting support with a cluster pipeline

I have added a couple unit tests for the newly-implemented things. I have not yet added documentation to the PR, but I will.

@codecov-commenter
Copy link

codecov-commenter commented Feb 11, 2022

Codecov Report

Merging #1937 (49f6159) into master (1983905) will decrease coverage by 0.04%.
The diff coverage is 93.68%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1937      +/-   ##
==========================================
- Coverage   92.69%   92.64%   -0.05%     
==========================================
  Files         100      100              
  Lines       20825    20909      +84     
==========================================
+ Hits        19303    19371      +68     
- Misses       1522     1538      +16     
Impacted Files Coverage Δ
redis/commands/parser.py 90.32% <ø> (ø)
redis/utils.py 88.88% <ø> (ø)
tests/test_command_parser.py 94.23% <50.00%> (-5.77%) ⬇️
redis/commands/core.py 87.22% <92.00%> (-1.08%) ⬇️
redis/cluster.py 92.50% <94.11%> (+0.08%) ⬆️
redis/commands/cluster.py 94.82% <100.00%> (ø)
tests/test_scripting.py 100.00% <100.00%> (ø)
redis/asyncio/connection.py 82.77% <0.00%> (+0.11%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1983905...49f6159. Read the comment docs.

@@ -4663,7 +4663,7 @@ def __init__(self, registered_client, script):
if isinstance(script, str):
# We need the encoding from the client in order to generate an
# accurate byte representation of the script
encoder = registered_client.connection_pool.get_encoder()
encoder = registered_client.get_encoder()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is technically a breaking change. If any consumers of redis-py use Scripts directly, injecting their own registered_client that isn't a subclass of Redis or RedisCluster, this could break for them if they haven't got client.get_encoder() defined.

Copy link
Contributor

Choose a reason for hiding this comment

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

While I believe it's very unlikely - I'd rather not break the universe. Perhaps we could wrap this with an if getattr, check. What do you think @jakebarnwell

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea! I will try something like that

r.set("a", 2)
# 2 * 3 == 6
assert r.eval(multiply_script, 1, "a", 3) == 6

# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
@pytest.mark.onlynoncluster
def test_eval_ro(self, unstable_r):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't implemented eval_ro -- I'm not sure if this PR desires it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you should have to deal with this.

@jakebarnwell
Copy link
Contributor Author

@chayim can you do another review? :)

@chayim chayim added the feature New feature label Feb 22, 2022
@chayim
Copy link
Contributor

chayim commented Feb 22, 2022

@jakebarnwell This is a beautiful thing. Thank you for the approach, and the changes. I've approved, and we'll merge this into master shortly. It'll be part of the 4.2.0rc1 release.

@chayim
Copy link
Contributor

chayim commented Feb 22, 2022

@dvora-h Can you have a look at the conflict here and resolve it for our friend @jakebarnwell prior to the 4.2.0rc1 release?

@dvora-h dvora-h merged commit e5ac39a into redis:master Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lock method does not exist in the redis client using cluster mode
4 participants