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

Support redis-py v2 and v3 #948

Merged
merged 1 commit into from
Nov 19, 2018
Merged

Support redis-py v2 and v3 #948

merged 1 commit into from
Nov 19, 2018

Conversation

ashb
Copy link
Contributor

@ashb ashb commented Nov 16, 2018

Further to #946 this fixes the underlying issue in a easy-to-upgrade way for end users, many of whom will have redis installed via other means. By having this check here and supporting both versions concurrently it makes it easier for end users, and to use celery/kombu in projects that use redis elsewhere.

With this change it is possibly worth reverting #946

@ashb
Copy link
Contributor Author

ashb commented Nov 16, 2018

@thedrow PTAL - this might be a better fix than my previous one.

@auvipy
Copy link
Member

auvipy commented Nov 16, 2018

is this the only incompatible change?

@ashb
Copy link
Contributor Author

ashb commented Nov 17, 2018

It seemed to be the only one that we used in this file. Unfortunately I’ve just moved house so may not have much time for a week or so to make change/fix these tests

kombu/transport/redis.py Outdated Show resolved Hide resolved
Copy link
Member

@thedrow thedrow left a comment

Choose a reason for hiding this comment

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

Minor changes and syntax fix required.
Also please include the revert to #946 in your PR.

kombu/transport/redis.py Outdated Show resolved Hide resolved
kombu/transport/redis.py Show resolved Hide resolved
@ashb
Copy link
Contributor Author

ashb commented Nov 18, 2018

@thedrow Fixed up the stupid mistakes, and added the TODO-comment. PTAL?

@thedrow
Copy link
Member

thedrow commented Nov 19, 2018

This looks good but the build is still failing!
https://travis-ci.org/celery/kombu/jobs/456711798#L1465

@thedrow thedrow added this to the 4.3 milestone Nov 19, 2018
@andymccurdy
Copy link
Contributor

andymccurdy commented Nov 19, 2018

This looks good but the build is still failing!
https://travis-ci.org/celery/kombu/jobs/456711798#L1465

Looks like there's some kind of mock interface in the tests here: https://github.com/celery/kombu/blob/master/t/unit/transport/test_redis.py#L78

Something like this would probably work.

    def zadd(self, key, mapping, **kwargs):
        for item in mapping
            self.sets[key].add(item)

@thedrow
Copy link
Member

thedrow commented Nov 19, 2018

When this tests were written fakeredis wasn't available or too premature :)

@ashb
Copy link
Contributor Author

ashb commented Nov 19, 2018

Should I update the mock, or is it only a small amount of work to change to use fakeredis?

@ashb
Copy link
Contributor Author

ashb commented Nov 19, 2018

Ah, fakeredis doesn't yet support Redis-py3 either: jamesls/fakeredis#225

@ashb
Copy link
Contributor Author

ashb commented Nov 19, 2018

Ran that test locally and it's passing now with the change Andy suggested

@codecov
Copy link

codecov bot commented Nov 19, 2018

Codecov Report

Merging #948 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #948      +/-   ##
==========================================
+ Coverage   88.66%   88.66%   +<.01%     
==========================================
  Files          63       63              
  Lines        6509     6512       +3     
  Branches      776      777       +1     
==========================================
+ Hits         5771     5774       +3     
  Misses        656      656              
  Partials       82       82
Impacted Files Coverage Δ
kombu/transport/redis.py 90.78% <100%> (+0.04%) ⬆️

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 d9de66b...3b206c8. Read the comment docs.

Copy link
Member

@thedrow thedrow left a comment

Choose a reason for hiding this comment

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

According to codecov, the case where redis-py 2.x is used is no longer covered.
I'd rather have a test for both cases to ensure that nothing breaks.

Further to celery#946 this fixes the underlying issue in a easy-to-upgrade way
for end users, many of whom will have Redis installed via other means.
By having this check here and supporting both versions concurrently it
makes it easier for end users, and to use celery/kombu in projects that
use Redis elsewhere.

With this change it is possibly worth reverting celery#946
@ashb
Copy link
Contributor Author

ashb commented Nov 19, 2018

Added tests (via case.mock.replace_module_value to switch the version back and forth. I think that should bring the coverage back up.

@ashb
Copy link
Contributor Author

ashb commented Nov 19, 2018

@thedrow ptal. Coverage for both versions added

@thedrow thedrow merged commit 05152da into celery:master Nov 19, 2018
@ashb ashb deleted the patch-1 branch November 19, 2018 17:53
# TODO: Remove this once we soley on Redis-py 3.0.0+
if redis.VERSION[0] >= 3:
# Redis-py changed the format of zadd args in v3.0.0
zadd_args = [{time(): delivery_tag}]
Copy link
Contributor

@Tenzer Tenzer Nov 20, 2018

Choose a reason for hiding this comment

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

Is this not the wrong way round? The documentation says:

For ZADD, the dict is a mapping of element-names -> score.

So the key in the dict should be the element-name - in this case delivery_tag - and the value should be the score, which here is time()?

I did a quick test locally to check:

>>> from redis import Redis
>>> r = Redis()
>>> from time import time
>>> zadd_args = [{time(): 'test_tag'}]
>>> r.zadd('kombu', *zadd_args)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jeppe/.virtualenvs/tmp-82c2c5e4c227252/lib/python3.6/site-packages/redis/client.py", line 2266, in zadd
    return self.execute_command('ZADD', name, *pieces, **options)
  File "/Users/jeppe/.virtualenvs/tmp-82c2c5e4c227252/lib/python3.6/site-packages/redis/client.py", line 755, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/Users/jeppe/.virtualenvs/tmp-82c2c5e4c227252/lib/python3.6/site-packages/redis/client.py", line 768, in parse_response
    response = connection.read_response()
  File "/Users/jeppe/.virtualenvs/tmp-82c2c5e4c227252/lib/python3.6/site-packages/redis/connection.py", line 638, in read_response
    raise response
redis.exceptions.ResponseError: value is not a valid float
>>> zadd_args2 = [{'test_tag': time()}]
>>> r.zadd('kombu', *zadd_args2)
1

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 don't know - even on redis 2.10.6 I get this:

>>> import redis
>>> redis.VERSION
(2, 10, 6)
>>> from time import time
>>> r = redis.Redis()
>>> r.zadd('kombu', time(), 'test_tag')
... 
redis.exceptions.ResponseError: value is not a valid float

and this is definately the same order as the code was previously.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, that's due to the difference between the Redis client and StrictRedis. In redis-py 3 StrictRedis has been renamed to Redis and StrictRedis has become an alias to Redis.

In redis-py 2 there's a difference in the input order, it's mentioned slightly in the README for the 2.10.6 release: https://github.com/andymccurdy/redis-py/tree/2.10.6#api-reference, but it might be easier to compare the code used. This is the StrictRedis.zadd method: https://github.com/andymccurdy/redis-py/blob/2.10.6/redis/client.py#L1677-L1697 and here's the Redis.zadd method: https://github.com/andymccurdy/redis-py/blob/2.10.6/redis/client.py#L2292-L2321.

>>> import redis
>>> redis.VERSION
(2, 10, 6)
>>> r = redis.StrictRedis()
>>> from time import time
>>> r.zadd('kombu', time(), 'test_tag')
1

Copy link
Contributor

Choose a reason for hiding this comment

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

The order was flipped around in #953.

thedrow pushed a commit that referenced this pull request Dec 6, 2018
Further to #946 this fixes the underlying issue in a easy-to-upgrade way
for end users, many of whom will have Redis installed via other means.
By having this check here and supporting both versions concurrently it
makes it easier for end users, and to use celery/kombu in projects that
use Redis elsewhere.

With this change it is possibly worth reverting #946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants