Skip to content

Commit 1c00333

Browse files
yipin-chenshohamazonacarbonetto
authored
Python: add FUNCTION FLUSH command (#1700)
* Python: Added FUNCTION LOAD command * Python: adds FUNCTION FLUSH command * Updated CHANGELOG.md * Resolved merge issues related to FlushMode * Minor adjustments on command documentation * Revert one minor change in example. --------- Co-authored-by: Shoham Elias <[email protected]> Co-authored-by: Andrew Carbonetto <[email protected]>
1 parent 1df349b commit 1c00333

6 files changed

Lines changed: 145 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* Python: Added FUNCTION LOAD command ([#1699](https://github.com/aws/glide-for-redis/pull/1699))
6363
* Python: Added XPENDING command ([#1704](https://github.com/aws/glide-for-redis/pull/1704))
6464
* Python: Added RANDOMKEY command ([#1701](https://github.com/aws/glide-for-redis/pull/1701))
65+
* Python: Added FUNCTION FLUSH command ([#1700](https://github.com/aws/glide-for-redis/pull/1700))
6566

6667
### Breaking Changes
6768
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))

python/python/glide/async_commands/cluster_commands.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,37 @@ async def function_load(
353353
),
354354
)
355355

356+
async def function_flush(
357+
self, mode: Optional[FlushMode] = None, route: Optional[Route] = None
358+
) -> TOK:
359+
"""
360+
Deletes all function libraries.
361+
362+
See https://valkey.io/docs/latest/commands/function-flush/ for more details.
363+
364+
Args:
365+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
366+
route (Optional[Route]): The command will be routed to all primaries, unless `route` is provided,
367+
in which case the client will route the command to the nodes defined by `route`.
368+
369+
Returns:
370+
TOK: A simple `OK`.
371+
372+
Examples:
373+
>>> await client.function_flush(FlushMode.SYNC)
374+
"OK"
375+
376+
Since: Redis 7.0.0.
377+
"""
378+
return cast(
379+
TOK,
380+
await self._execute_command(
381+
RequestType.FunctionFlush,
382+
[mode.value] if mode else [],
383+
route,
384+
),
385+
)
386+
356387
async def time(self, route: Optional[Route] = None) -> TClusterResponse[List[str]]:
357388
"""
358389
Returns the server time.

python/python/glide/async_commands/standalone_commands.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,32 @@ async def function_load(self, library_code: str, replace: bool = False) -> str:
262262
),
263263
)
264264

265+
async def function_flush(self, mode: Optional[FlushMode] = None) -> TOK:
266+
"""
267+
Deletes all function libraries.
268+
269+
See https://valkey.io/docs/latest/commands/function-flush/ for more details.
270+
271+
Args:
272+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
273+
274+
Returns:
275+
TOK: A simple `OK`.
276+
277+
Examples:
278+
>>> await client.function_flush(FlushMode.SYNC)
279+
"OK"
280+
281+
Since: Redis 7.0.0.
282+
"""
283+
return cast(
284+
TOK,
285+
await self._execute_command(
286+
RequestType.FunctionFlush,
287+
[mode.value] if mode else [],
288+
),
289+
)
290+
265291
async def time(self) -> List[str]:
266292
"""
267293
Returns the server time.

python/python/glide/async_commands/transaction.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,27 @@ def function_load(
18011801
["REPLACE", library_code] if replace else [library_code],
18021802
)
18031803

1804+
def function_flush(
1805+
self: TTransaction, mode: Optional[FlushMode] = None
1806+
) -> TTransaction:
1807+
"""
1808+
Deletes all function libraries.
1809+
1810+
See https://valkey.io/docs/latest/commands/function-flush/ for more details.
1811+
1812+
Args:
1813+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
1814+
1815+
Commands response:
1816+
TOK: A simple `OK`.
1817+
1818+
Since: Redis 7.0.0.
1819+
"""
1820+
return self.append_command(
1821+
RequestType.FunctionFlush,
1822+
[mode.value] if mode else [],
1823+
)
1824+
18041825
def xadd(
18051826
self: TTransaction,
18061827
key: str,

python/python/tests/test_async_client.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6731,6 +6731,66 @@ async def test_function_load_cluster_with_route(
67316731

67326732
assert await redis_client.function_load(new_code, True, route) == lib_name
67336733

6734+
@pytest.mark.parametrize("cluster_mode", [True, False])
6735+
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
6736+
async def test_function_flush(self, redis_client: TGlideClient):
6737+
min_version = "7.0.0"
6738+
if await check_if_server_version_lt(redis_client, min_version):
6739+
pytest.skip(f"Redis version required >= {min_version}")
6740+
6741+
lib_name = f"mylib1C{get_random_string(5)}"
6742+
func_name = f"myfunc1c{get_random_string(5)}"
6743+
code = generate_lua_lib_code(lib_name, {func_name: "return args[1]"}, True)
6744+
6745+
# Load the function
6746+
assert await redis_client.function_load(code) == lib_name
6747+
6748+
# TODO: Ensure the function exists with FUNCTION LIST
6749+
6750+
# Flush functions
6751+
assert await redis_client.function_flush(FlushMode.SYNC) == OK
6752+
assert await redis_client.function_flush(FlushMode.ASYNC) == OK
6753+
6754+
# TODO: Ensure the function is no longer present with FUNCTION LIST
6755+
6756+
# Attempt to re-load library without overwriting to ensure FLUSH was effective
6757+
assert await redis_client.function_load(code) == lib_name
6758+
6759+
# Clean up by flushing functions again
6760+
await redis_client.function_flush()
6761+
6762+
@pytest.mark.parametrize("cluster_mode", [True])
6763+
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
6764+
@pytest.mark.parametrize("single_route", [True, False])
6765+
async def test_function_flush_with_routing(
6766+
self, redis_client: GlideClusterClient, single_route: bool
6767+
):
6768+
min_version = "7.0.0"
6769+
if await check_if_server_version_lt(redis_client, min_version):
6770+
pytest.skip(f"Redis version required >= {min_version}")
6771+
6772+
lib_name = f"mylib1C{get_random_string(5)}"
6773+
func_name = f"myfunc1c{get_random_string(5)}"
6774+
code = generate_lua_lib_code(lib_name, {func_name: "return args[1]"}, True)
6775+
route = SlotKeyRoute(SlotType.PRIMARY, "1") if single_route else AllPrimaries()
6776+
6777+
# Load the function
6778+
assert await redis_client.function_load(code, False, route) == lib_name
6779+
6780+
# TODO: Ensure the function exists with FUNCTION LIST
6781+
6782+
# Flush functions
6783+
assert await redis_client.function_flush(FlushMode.SYNC, route) == OK
6784+
assert await redis_client.function_flush(FlushMode.ASYNC, route) == OK
6785+
6786+
# TODO: Ensure the function is no longer present with FUNCTION LIST
6787+
6788+
# Attempt to re-load library without overwriting to ensure FLUSH was effective
6789+
assert await redis_client.function_load(code, False, route) == lib_name
6790+
6791+
# Clean up by flushing functions again
6792+
assert await redis_client.function_flush(route=route) == OK
6793+
67346794
@pytest.mark.parametrize("cluster_mode", [True, False])
67356795
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
67366796
async def test_srandmember(self, redis_client: TGlideClient):

python/python/tests/test_transaction.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ async def transaction_test(
102102
args.append(lib_name)
103103
transaction.function_load(code, True)
104104
args.append(lib_name)
105+
transaction.function_flush()
106+
args.append(OK)
107+
transaction.function_flush(FlushMode.ASYNC)
108+
args.append(OK)
109+
transaction.function_flush(FlushMode.SYNC)
110+
args.append(OK)
105111

106112
transaction.dbsize()
107113
args.append(0)

0 commit comments

Comments
 (0)