From 47c6c8ebbf92c8794ebbeaca76e3728e4a748f69 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Fri, 4 Dec 2015 14:57:03 +0100 Subject: [PATCH] Fix reconnection when heartbeat is missed When a heartbeat is missing we call ensure_connection() that runs a dummy method to trigger the reconnection code in kombu. But also the code is triggered only if the channel is None. In case of the heartbeat threads we didn't reset the channel before reconnecting, so the dummy method doesn't do anything. This change sets the channel to None to ensure the connection is reestablished before the dummy method is run. Also it replaces the dummy method by checking the kombu connection object. So we are sure the connection is reestablished. Closes-bug: #1493890 (cherry picked from commit I39f8cd23c5a5498e6f4c1aa3236ed27f3b5d7c9a) Change-Id: Id98d4054ecbc787e0d44884a9e4c48e3fae803b2 --- oslo_messaging/_drivers/impl_rabbit.py | 6 ++++-- oslo_messaging/tests/drivers/test_impl_rabbit.py | 2 ++ tests/drivers/test_impl_rabbit.py | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/oslo_messaging/_drivers/impl_rabbit.py b/oslo_messaging/_drivers/impl_rabbit.py index 9716b9cee..f6e0f7290 100644 --- a/oslo_messaging/_drivers/impl_rabbit.py +++ b/oslo_messaging/_drivers/impl_rabbit.py @@ -778,7 +778,10 @@ class Connection(object): return False def ensure_connection(self): - self.ensure(method=lambda: True) + # NOTE(sileht): we reset the channel and ensure + # the kombu underlying connection works + self._set_current_channel(None) + self.ensure(method=lambda: self.connection.connection) def ensure(self, method, retry=None, recoverable_error_callback=None, error_callback=None, @@ -919,7 +922,6 @@ class Connection(object): try: self._set_current_channel(self.connection.channel()) except recoverable_errors: - self._set_current_channel(None) self.ensure_connection() self.consumers = [] self.consumer_num = itertools.count(1) diff --git a/oslo_messaging/tests/drivers/test_impl_rabbit.py b/oslo_messaging/tests/drivers/test_impl_rabbit.py index 011224bff..e4cc1b4e6 100644 --- a/oslo_messaging/tests/drivers/test_impl_rabbit.py +++ b/oslo_messaging/tests/drivers/test_impl_rabbit.py @@ -820,6 +820,8 @@ class RpcKombuHATestCase(test_utils.BaseTestCase): self.useFixture(mockpatch.Patch( 'kombu.connection.Connection.connect', side_effect=self.kombu_connect)) + self.useFixture(mockpatch.Patch( + 'kombu.connection.Connection.connection')) self.useFixture(mockpatch.Patch( 'kombu.connection.Connection.channel')) diff --git a/tests/drivers/test_impl_rabbit.py b/tests/drivers/test_impl_rabbit.py index 7b5821218..f3c2e5629 100644 --- a/tests/drivers/test_impl_rabbit.py +++ b/tests/drivers/test_impl_rabbit.py @@ -724,6 +724,8 @@ class RpcKombuHATestCase(test_utils.BaseTestCase): self.useFixture(mockpatch.Patch( 'kombu.connection.Connection.connect', side_effect=self.kombu_connect)) + self.useFixture(mockpatch.Patch( + 'kombu.connection.Connection.connection')) self.useFixture(mockpatch.Patch( 'kombu.connection.Connection.channel'))