From 671c60d3a5c21169978805ca4ed616941a3aeabf Mon Sep 17 00:00:00 2001 From: zhangjl Date: Tue, 3 Feb 2015 18:56:47 +0800 Subject: Fix the bug redis do not delete the expired keys When using redis as zmq backend, the keys in redis maybe like follows: "service" "service.host1" "service.host1.host1" "service.host2" "service.host2.host2" "service.host3" "service.host3.host3" If we stopped the service on host1, the key named service.host1 and service.host1.host1 shoule be delete. As the result, the topicexchange message would not sent to host1. But the fact is service.host1 still exsit, and topicexchange message may still sent to host1. To resolve this problem , change the comparison of ttl function from -1 to -2 according to http://redis.io/commands/ttl Change-Id: I5c0af97019fffed6e949eb58d7d60c85f5b08ea1 Closes-Bug:#1417464 (cherry picked from commit 1958f6e549d3769202960f5af93982afea74cd80) --- oslo_messaging/_drivers/matchmaker_redis.py | 7 ++++++- oslo_messaging/tests/drivers/test_matchmaker_redis.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/oslo_messaging/_drivers/matchmaker_redis.py b/oslo_messaging/_drivers/matchmaker_redis.py index 4f6e6f0..290b603 100644 --- a/oslo_messaging/_drivers/matchmaker_redis.py +++ b/oslo_messaging/_drivers/matchmaker_redis.py @@ -108,7 +108,12 @@ class MatchMakerRedis(mm_common.HeartbeatMatchMakerBase): self.register(key, host) def is_alive(self, topic, host): - if self.redis.ttl(host) == -1: + # After redis 2.8, if the specialized key doesn't exist, + # TTL fuction would return -2. If key exists, + # but doesn't have expiration associated, + # TTL func would return -1. For more information, + # please visit http://redis.io/commands/ttl + if self.redis.ttl(host) == -2: self.expire(topic, host) return False return True diff --git a/oslo_messaging/tests/drivers/test_matchmaker_redis.py b/oslo_messaging/tests/drivers/test_matchmaker_redis.py index 097029d..7760404 100644 --- a/oslo_messaging/tests/drivers/test_matchmaker_redis.py +++ b/oslo_messaging/tests/drivers/test_matchmaker_redis.py @@ -87,3 +87,11 @@ class RedisMatchMakerTest(test_utils.BaseTestCase): self.assertEqual( sorted(self.matcher.redis.smembers('ack_alive')), ['ack_alive.controller1']) + + def test_is_alive(self): + self.assertEqual( + self.matcher.is_alive('conductor', 'conductor.controller1'), + True) + self.assertEqual( + self.matcher.is_alive('conductor', 'conductor.controller2'), + False) -- cgit v1.2.1