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 functions #2016

Merged
merged 10 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,21 @@ class RedisCluster(RedisClusterCommands):
[
"FLUSHALL",
"FLUSHDB",
"FUNCTION DELETE",
"FUNCTION FLUSH",
"FUNCTION LIST",
"FUNCTION LOAD",
"FUNCTION RESTORE",
"SCRIPT EXISTS",
"SCRIPT FLUSH",
"SCRIPT LOAD",
],
PRIMARIES,
),
list_keys_to_dict(
["FUNCTION DUMP"],
RANDOM,
),
list_keys_to_dict(
[
"CLUSTER COUNTKEYSINSLOT",
Expand Down Expand Up @@ -843,6 +852,10 @@ def determine_slot(self, *args):
else:
keys = self._get_command_keys(*args)
if keys is None or len(keys) == 0:
# FCALL can call a function with 0 keys, that means the function
# can be run on any node so we can just return a random slot
if command in ("FCALL", "FCALL_RO"):
return random.randrange(0, REDIS_CLUSTER_HASH_SLOTS)
raise RedisClusterException(
"No way to dispatch this command to Redis Cluster. "
"Missing key.\nYou can execute the command by specifying "
Expand Down
2 changes: 2 additions & 0 deletions redis/commands/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .core import (
ACLCommands,
DataAccessCommands,
FunctionCommands,
ManagementCommands,
PubSubCommands,
ScriptCommands,
Expand Down Expand Up @@ -212,6 +213,7 @@ class RedisClusterCommands(
PubSubCommands,
ClusterDataAccessCommands,
ScriptCommands,
FunctionCommands,
):
"""
A class for all Redis Cluster commands
Expand Down
26 changes: 25 additions & 1 deletion tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
return redis.call('GET', keys[1]) end)"


@pytest.mark.onlynoncluster
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
class TestFunction:
@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -44,6 +43,7 @@ def test_function_flush(self, unstable_r):
with pytest.raises(ResponseError):
unstable_r.function_flush("ABC")

@pytest.mark.onlynoncluster
def test_function_list(self, unstable_r):
unstable_r.function_load("Lua", "mylib", function)
res = [
Expand All @@ -62,6 +62,30 @@ def test_function_list(self, unstable_r):
assert unstable_r.function_list(library="*lib") == res
assert unstable_r.function_list(withcode=True)[0][9] == function

@pytest.mark.onlycluster
def test_function_list_on_cluster(self, unstable_r):
unstable_r.function_load("Lua", "mylib", function)
function_list = [
[
"library_name",
"mylib",
"engine",
"LUA",
"description",
None,
"functions",
[["name", "myfunc", "description", None]],
],
]
primaries = unstable_r.get_primaries()
res = {}
for node in primaries:
res[node.name] = function_list
assert unstable_r.function_list() == res
assert unstable_r.function_list(library="*lib") == res
node = primaries[0].name
assert unstable_r.function_list(withcode=True)[node][0][9] == function

def test_fcall(self, unstable_r):
unstable_r.function_load("Lua", "mylib", set_function)
unstable_r.function_load("Lua", "mylib2", get_function)
Expand Down