Skip to content

Commit

Permalink
Python: Replace instances of Redis with Valkey (valkey-io#2266)
Browse files Browse the repository at this point in the history
* Replace instances of Redis with Valkey

Signed-off-by: Jonathan Louie <[email protected]>

* Update CHANGELOG.md

Signed-off-by: Jonathan Louie <[email protected]>

* Revert doc change for lolwut

Signed-off-by: Jonathan Louie <[email protected]>

---------

Signed-off-by: Jonathan Louie <[email protected]>
Signed-off-by: jonathanl-bq <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Co-authored-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
jonathanl-bq and acarbonetto authored Sep 11, 2024
1 parent 87b5848 commit ba7af10
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Python: Replace instances of Redis with Valkey ([#2266](https://github.com/valkey-io/valkey-glide/pull/2266))
* Java: Replace instances of Redis with Valkey ([#2268](https://github.com/valkey-io/valkey-glide/pull/2268))
* Node: Replace instances of Redis with Valkey ([#2260](https://github.com/valkey-io/valkey-glide/pull/2260))
* Java: Fetch server version using info command ([#2258](https://github.com/valkey-io/valkey-glide/pull/2258))
Expand Down
4 changes: 2 additions & 2 deletions python/DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ Before starting this step, make sure you've installed all software requirments.
```
> **Note:** To build the wrapper binary with debug symbols remove the --strip flag.
8. Run tests:
1. Ensure that you have installed redis-server and redis-cli on your host. You can find the Redis installation guide at the following link: [Redis Installation Guide](https://redis.io/docs/install/install-redis/install-redis-on-linux/).
1. Ensure that you have installed redis-server or valkey-server and redis-cli or valkey-cli on your host. You can find the Redis installation guide at the following link: [Redis Installation Guide](https://redis.io/docs/install/install-redis/install-redis-on-linux/). You can get Valkey from the following link: [Valkey Download](https://valkey.io/download/).
2. Validate the activation of the virtual environment from step 4 by ensuring its name (`.env`) is displayed next to your command prompt.
3. Execute the following command from the python folder:
```bash
pytest --asyncio-mode=auto
```
> **Note:** To run redis modules tests, add -k "test_server_modules.py".
> **Note:** To run Valkey modules tests, add -k "test_server_modules.py".

- Install Python development requirements with:

Expand Down
2 changes: 1 addition & 1 deletion python/python/glide/glide_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class GlideClient(BaseClient, StandaloneCommands):
"""
Client used for connection to standalone servers.
For full documentation, see
https://github.com/valkey-io/valkey-glide/wiki/Python-wrapper#redis-standalone
https://github.com/valkey-io/valkey-glide/wiki/Python-wrapper#standalone
"""


Expand Down
26 changes: 13 additions & 13 deletions python/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
from glide.logger import Level as logLevel
from glide.logger import Logger
from tests.utils.cluster import RedisCluster
from tests.utils.cluster import ValkeyCluster

DEFAULT_HOST = "localhost"
DEFAULT_PORT = 6379
Expand Down Expand Up @@ -55,7 +55,7 @@ def pytest_addoption(parser):
parser.addoption(
"--load-module",
action="append",
help="""Load additional Redis modules (provide full path for the module's shared library).
help="""Load additional Valkey modules (provide full path for the module's shared library).
Use multiple times for multiple modules.
Example:
pytest --load-module=/path/to/module1.so --load-module=/path/to/module2.so""",
Expand Down Expand Up @@ -110,27 +110,27 @@ def parse_endpoints(endpoints_str: str) -> List[List[str]]:

def create_clusters(tls, load_module, cluster_endpoints, standalone_endpoints):
"""
Create Redis clusters based on the provided options.
Create Valkey clusters based on the provided options.
"""
if cluster_endpoints or standalone_endpoints:
# Endpoints were passed by the caller, not creating clusters internally
if cluster_endpoints:
cluster_endpoints = parse_endpoints(cluster_endpoints)
pytest.redis_cluster = RedisCluster(tls=tls, addresses=cluster_endpoints)
pytest.valkey_cluster = ValkeyCluster(tls=tls, addresses=cluster_endpoints)
if standalone_endpoints:
standalone_endpoints = parse_endpoints(standalone_endpoints)
pytest.standalone_cluster = RedisCluster(
pytest.standalone_cluster = ValkeyCluster(
tls=tls, addresses=standalone_endpoints
)
else:
# No endpoints were provided, create new clusters
pytest.redis_cluster = RedisCluster(
pytest.valkey_cluster = ValkeyCluster(
tls=tls,
cluster_mode=True,
load_module=load_module,
addresses=cluster_endpoints,
)
pytest.standalone_cluster = RedisCluster(
pytest.standalone_cluster = ValkeyCluster(
tls=tls,
cluster_mode=False,
shard_count=1,
Expand Down Expand Up @@ -160,9 +160,9 @@ def pytest_sessionfinish(session, exitstatus):
returning the exit status to the system.
"""
try:
del pytest.redis_cluster
del pytest.valkey_cluster
except AttributeError:
# redis_cluster was not set, skip deletion
# valkey_cluster was not set, skip deletion
pass

try:
Expand Down Expand Up @@ -233,10 +233,10 @@ async def create_client(
# Create async socket client
use_tls = request.config.getoption("--tls")
if cluster_mode:
assert type(pytest.redis_cluster) is RedisCluster
assert type(pytest.valkey_cluster) is ValkeyCluster
assert database_id == 0
k = min(3, len(pytest.redis_cluster.nodes_addr))
seed_nodes = random.sample(pytest.redis_cluster.nodes_addr, k=k)
k = min(3, len(pytest.valkey_cluster.nodes_addr))
seed_nodes = random.sample(pytest.valkey_cluster.nodes_addr, k=k)
cluster_config = GlideClusterClientConfiguration(
addresses=seed_nodes if addresses is None else addresses,
use_tls=use_tls,
Expand All @@ -248,7 +248,7 @@ async def create_client(
)
return await GlideClusterClient.create(cluster_config)
else:
assert type(pytest.standalone_cluster) is RedisCluster
assert type(pytest.standalone_cluster) is ValkeyCluster
config = GlideClientConfiguration(
addresses=(
pytest.standalone_cluster.nodes_addr if addresses is None else addresses
Expand Down
14 changes: 7 additions & 7 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ async def test_config_rewrite(self, glide_client: TGlideClient):
if len(info_server["config_file"]) > 0:
assert await glide_client.config_rewrite() == OK
else:
# We expect Redis to return an error since the test cluster doesn't use redis.conf file
# We expect Valkey to return an error since the test cluster doesn't use valkey.conf file
with pytest.raises(RequestError) as e:
await glide_client.config_rewrite()
assert "The server is running without a config file" in str(e)
Expand Down Expand Up @@ -6951,7 +6951,7 @@ async def test_xgroup_set_id(
)

# reset the last delivered ID for the consumer group to "1-1"
# ENTRIESREAD is only supported in Redis version 7.0.0 and above
# ENTRIESREAD is only supported in Valkey version 7.0.0 and above
if await check_if_server_version_lt(glide_client, "7.0.0"):
assert await glide_client.xgroup_set_id(key, group_name, stream_id1_1) == OK
else:
Expand Down Expand Up @@ -8206,7 +8206,7 @@ async def test_function_delete_with_routing(
async def test_function_stats(self, glide_client: TGlideClient):
min_version = "7.0.0"
if await check_if_server_version_lt(glide_client, min_version):
return pytest.mark.skip(reason=f"Redis version required >= {min_version}")
return pytest.mark.skip(reason=f"Valkey version required >= {min_version}")

lib_name = "functionStats_without_route"
func_name = lib_name
Expand Down Expand Up @@ -8252,7 +8252,7 @@ async def test_function_stats_running_script(
):
min_version = "7.0.0"
if await check_if_server_version_lt(glide_client, min_version):
return pytest.mark.skip(reason=f"Redis version required >= {min_version}")
return pytest.mark.skip(reason=f"Valkey version required >= {min_version}")

lib_name = f"mylib1C{get_random_string(5)}"
func_name = f"myfunc1c{get_random_string(5)}"
Expand Down Expand Up @@ -8309,7 +8309,7 @@ async def test_function_stats_with_routing(
):
min_version = "7.0.0"
if await check_if_server_version_lt(glide_client, min_version):
return pytest.mark.skip(reason=f"Redis version required >= {min_version}")
return pytest.mark.skip(reason=f"Valkey version required >= {min_version}")

route = (
SlotKeyRoute(SlotType.PRIMARY, get_random_string(10))
Expand Down Expand Up @@ -8376,7 +8376,7 @@ async def test_function_kill_no_write(
):
min_version = "7.0.0"
if await check_if_server_version_lt(glide_client, min_version):
return pytest.mark.skip(reason=f"Redis version required >= {min_version}")
return pytest.mark.skip(reason=f"Valkey version required >= {min_version}")

lib_name = f"mylib1C{get_random_string(5)}"
func_name = f"myfunc1c{get_random_string(5)}"
Expand Down Expand Up @@ -8435,7 +8435,7 @@ async def test_function_kill_write_is_unkillable(
):
min_version = "7.0.0"
if await check_if_server_version_lt(glide_client, min_version):
return pytest.mark.skip(reason=f"Redis version required >= {min_version}")
return pytest.mark.skip(reason=f"Valkey version required >= {min_version}")

lib_name = f"mylib1C{get_random_string(5)}"
func_name = f"myfunc1c{get_random_string(5)}"
Expand Down
10 changes: 5 additions & 5 deletions python/python/tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ async def test_sharded_pubsub_many_channels(

min_version = "7.0.0"
if await check_if_server_version_lt(publishing_client, min_version):
pytest.skip(reason=f"Redis version required >= {min_version}")
pytest.skip(reason=f"Valkey version required >= {min_version}")

# Publish messages to each channel
for channel, message in channels_and_messages.items():
Expand Down Expand Up @@ -1247,9 +1247,9 @@ async def test_pubsub_combined_exact_pattern_and_sharded_one_client(
pub_sub_exact,
)

# Setup PUBSUB for sharded channels (Redis version > 7)
# Setup PUBSUB for sharded channels (Valkey version > 7)
if await check_if_server_version_lt(publishing_client, "7.0.0"):
pytest.skip("Redis version required >= 7.0.0")
pytest.skip("Valkey version required >= 7.0.0")

# Publish messages to all channels
for channel, message in {
Expand Down Expand Up @@ -2062,9 +2062,9 @@ async def test_pubsub_sharded_max_size_message(self, request, cluster_mode: bool
timeout=10000,
)

# (Redis version > 7)
# (Valkey version > 7)
if await check_if_server_version_lt(publishing_client, "7.0.0"):
pytest.skip("Redis version required >= 7.0.0")
pytest.skip("Valkey version required >= 7.0.0")

assert (
await cast(GlideClusterClient, publishing_client).publish(
Expand Down
2 changes: 1 addition & 1 deletion python/python/tests/utils/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SCRIPT_FILE = os.path.abspath(f"{__file__}/../../../../../utils/cluster_manager.py")


class RedisCluster:
class ValkeyCluster:
def __init__(
self,
tls,
Expand Down
12 changes: 6 additions & 6 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ fn glide(_py: Python, m: &PyModule) -> PyResult<()> {
let len = iterator.len();

iterator.try_fold(Vec::with_capacity(len), |mut acc, val| {
acc.push(redis_value_to_py(py, val)?);
acc.push(resp_value_to_py(py, val)?);
Ok(acc)
})
}

fn redis_value_to_py(py: Python, val: Value) -> PyResult<PyObject> {
fn resp_value_to_py(py: Python, val: Value) -> PyResult<PyObject> {
match val {
Value::Nil => Ok(py.None()),
Value::SimpleString(str) => {
Expand All @@ -175,14 +175,14 @@ fn glide(_py: Python, m: &PyModule) -> PyResult<()> {
Value::Map(map) => {
let dict = PyDict::new(py);
for (key, value) in map {
dict.set_item(redis_value_to_py(py, key)?, redis_value_to_py(py, value)?)?;
dict.set_item(resp_value_to_py(py, key)?, resp_value_to_py(py, value)?)?;
}
Ok(dict.into_py(py))
}
Value::Attribute { data, attributes } => {
let dict = PyDict::new(py);
let value = redis_value_to_py(py, *data)?;
let attributes = redis_value_to_py(py, Value::Map(attributes))?;
let value = resp_value_to_py(py, *data)?;
let attributes = resp_value_to_py(py, Value::Map(attributes))?;
dict.set_item("value", value)?;
dict.set_item("attributes", attributes)?;
Ok(dict.into_py(py))
Expand Down Expand Up @@ -213,7 +213,7 @@ fn glide(_py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m)]
pub fn value_from_pointer(py: Python, pointer: u64) -> PyResult<PyObject> {
let value = unsafe { Box::from_raw(pointer as *mut Value) };
redis_value_to_py(py, *value)
resp_value_to_py(py, *value)
}

#[pyfn(m)]
Expand Down

0 comments on commit ba7af10

Please sign in to comment.