Skip to content

Support redis-py v2 and v3#948

Merged
thedrow merged 1 commit into
celery:masterfrom
ashb:patch-1
Nov 19, 2018
Merged

Support redis-py v2 and v3#948
thedrow merged 1 commit into
celery:masterfrom
ashb:patch-1

Conversation

@ashb

@ashb ashb commented Nov 16, 2018

Copy link
Copy Markdown
Contributor

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

ashb commented Nov 16, 2018

Copy link
Copy Markdown
Contributor Author

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

@auvipy

auvipy commented Nov 16, 2018

Copy link
Copy Markdown
Member

is this the only incompatible change?

@ashb

ashb commented Nov 17, 2018

Copy link
Copy Markdown
Contributor Author

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

Comment thread kombu/transport/redis.py Outdated

@thedrow thedrow left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread kombu/transport/redis.py Outdated
Comment thread kombu/transport/redis.py
@ashb

ashb commented Nov 18, 2018

Copy link
Copy Markdown
Contributor Author

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

@thedrow

thedrow commented Nov 19, 2018

Copy link
Copy Markdown
Contributor

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

andymccurdy commented Nov 19, 2018

Copy link
Copy Markdown
Contributor

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

thedrow commented Nov 19, 2018

Copy link
Copy Markdown
Contributor

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

@ashb

ashb commented Nov 19, 2018

Copy link
Copy Markdown
Contributor Author

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

@ashb

ashb commented Nov 19, 2018

Copy link
Copy Markdown
Contributor Author

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

@ashb

ashb commented Nov 19, 2018

Copy link
Copy Markdown
Contributor Author

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

@codecov

codecov Bot commented Nov 19, 2018

Copy link
Copy Markdown

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.

@thedrow thedrow left a comment

Copy link
Copy Markdown
Contributor

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

ashb commented Nov 19, 2018

Copy link
Copy Markdown
Contributor Author

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

ashb commented Nov 19, 2018

Copy link
Copy Markdown
Contributor Author

@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
Comment thread kombu/transport/redis.py
# 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}]

@Tenzer Tenzer Nov 20, 2018

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

5 participants