Skip to content

Commit 1096ee2

Browse files
4383zzzeek
authored andcommitted
Moves dead_retry and socket_timeout into the MemcachedBackend class
Moved the :paramref:`.MemcacheArgs.dead_retry` argument and the :paramref:`.MemcacheArgs.socket_timeout` argument which were erroneously added to the "set_parameters", where they have no effect, to be part of the Memcached connection arguments :paramref:`.MemcachedBackend.dead_retry`, :paramref:`.MemcachedBackend.socket_timeout`. Indeed, In my previous patch [1] I proposed to add the ``dead_retry`` and ``socket_timeout`` params to the ``MemcacheArgs`` class. I was wrong. My goal was to pass these parameters to the client during its initialization to set the memcached client dead_retry and socket_timeout arguments [2]. By using the MemcacheArgs they are passed to the method calls which is not what it was requested in the feature request [3]. I misunderstood the goal of this class (MemcacheArgs). The ``MemcacheArgs`` class is only inherited by the ``MemcachedBackend`` class and the ``PylibmcBackend`` class. Both libraries doesn't support ``dead_retry`` and ``socket_timeout`` in their methods related to the memcache API commands (set, get, set_multi, etc), so for this reason I think we can move those parameters safely. My previous patch led to issues [4][5] that I'm able to reproduce locally by using oslo.cache's functional test. These changes fix these issues. [1] 1de93aa [2] https://github.com/linsomniac/python-memcached/blob/7942465eba2009927e5d14b4b6dbd48b75780d80/memcache.py#L165 [3] #223 [4] https://bugzilla.redhat.com/show_bug.cgi?id=2103117 [5] https://review.opendev.org/c/openstack/requirements/+/848827 Closes: #228 Pull-request: #228 Pull-request-sha: dcef04b Change-Id: Ic77c1d657b81449a34114cf9f61c350ffc7e2ba1
1 parent d60ed49 commit 1096ee2

3 files changed

Lines changed: 60 additions & 36 deletions

File tree

docs/build/unreleased/224.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. change::
2+
:tags: bug, memcached
3+
:tickets: 224
4+
5+
Moved the :paramref:`.MemcacheArgs.dead_retry` argument and the
6+
:paramref:`.MemcacheArgs.socket_timeout` argument which were
7+
erroneously added to the "set_parameters",
8+
where they have no effect, to be part of the Memcached connection
9+
arguments :paramref:`.MemcachedBackend.dead_retry`,
10+
:paramref:`.MemcachedBackend.socket_timeout`.
11+

dogpile/cache/backends/memcached.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,6 @@ def delete_multi(self, keys):
220220
class MemcacheArgs(GenericMemcachedBackend):
221221
"""Mixin which provides support for the 'time' argument to set(),
222222
'min_compress_len' to other methods.
223-
224-
:param dead_retry: Number of seconds memcached server is considered dead
225-
before it is tried again..
226-
227-
.. versionadded:: 1.1.7
228-
229-
:param socket_timeout: Timeout in seconds for every call to a server.
230-
231-
.. versionadded:: 1.1.7
232-
233223
"""
234224

235225
def __init__(self, arguments):
@@ -242,10 +232,6 @@ def __init__(self, arguments):
242232
self.set_arguments["min_compress_len"] = arguments[
243233
"min_compress_len"
244234
]
245-
if "dead_retry" in arguments:
246-
self.set_arguments["dead_retry"] = arguments["dead_retry"]
247-
if "socket_timeout" in arguments:
248-
self.set_arguments["socket_timeout"] = arguments["socket_timeout"]
249235
super(MemcacheArgs, self).__init__(arguments)
250236

251237

@@ -316,14 +302,39 @@ class MemcachedBackend(MemcacheArgs, GenericMemcachedBackend):
316302
}
317303
)
318304
305+
:param dead_retry: Number of seconds memcached server is considered dead
306+
before it is tried again. Will be passed to ``memcache.Client``
307+
as the ``dead_retry`` parameter.
308+
309+
.. versionchanged:: 1.1.8 Moved the ``dead_retry`` argument which was
310+
erroneously added to "set_parameters" to
311+
be part of the Memcached connection arguments.
312+
313+
:param socket_timeout: Timeout in seconds for every call to a server.
314+
Will be passed to ``memcache.Client`` as the ``socket_timeout``
315+
parameter.
316+
317+
.. versionchanged:: 1.1.8 Moved the ``socket_timeout`` argument which
318+
was erroneously added to "set_parameters"
319+
to be part of the Memcached connection arguments.
320+
319321
"""
320322

323+
def __init__(self, arguments):
324+
self.dead_retry = arguments.get("dead_retry", 30)
325+
self.socket_timeout = arguments.get("socket_timeout", 3)
326+
super(MemcachedBackend, self).__init__(arguments)
327+
321328
def _imports(self):
322329
global memcache
323330
import memcache # noqa
324331

325332
def _create_client(self):
326-
return memcache.Client(self.url)
333+
return memcache.Client(
334+
self.url,
335+
dead_retry=self.dead_retry,
336+
socket_timeout=self.socket_timeout,
337+
)
327338

328339

329340
class BMemcachedBackend(GenericMemcachedBackend):

tests/cache/test_memcached_backend.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ def _imports(self):
380380
pass
381381

382382
def _create_client(self):
383-
return MockClient(self.url)
383+
return MockClient(
384+
self.url,
385+
dead_retry=self.dead_retry,
386+
socket_timeout=self.socket_timeout,
387+
)
384388

385389

386390
class MockPylibmcBackend(PylibmcBackend):
@@ -422,6 +426,24 @@ def delete(self, key):
422426
self._cache.pop(key, None)
423427

424428

429+
class MemcachedBackendTest(TestCase):
430+
def test_memcached_dead_retry(self):
431+
config_args = {
432+
"url": "127.0.0.1:11211",
433+
"dead_retry": 4,
434+
}
435+
backend = MockMemcacheBackend(arguments=config_args)
436+
eq_(backend._create_client().kw["dead_retry"], 4)
437+
438+
def test_memcached_socket_timeout(self):
439+
config_args = {
440+
"url": "127.0.0.1:11211",
441+
"socket_timeout": 6,
442+
}
443+
backend = MockMemcacheBackend(arguments=config_args)
444+
eq_(backend._create_client().kw["socket_timeout"], 6)
445+
446+
425447
class PylibmcArgsTest(TestCase):
426448
def test_binary_flag(self):
427449
backend = MockPylibmcBackend(arguments={"url": "foo", "binary": True})
@@ -476,26 +498,6 @@ def test_set_min_compress_len(self):
476498
backend.set("foo", "bar")
477499
eq_(backend._clients.memcached.canary, [{"min_compress_len": 20}])
478500

479-
def test_set_dead_retry(self):
480-
config_args = {
481-
"url": "127.0.0.1:11211",
482-
"dead_retry": 4,
483-
}
484-
485-
backend = MockMemcacheBackend(arguments=config_args)
486-
backend.set("foo", "bar")
487-
eq_(backend._clients.memcached.canary, [{"dead_retry": 4}])
488-
489-
def test_set_socket_timeout(self):
490-
config_args = {
491-
"url": "127.0.0.1:11211",
492-
"socket_timeout": 4,
493-
}
494-
495-
backend = MockMemcacheBackend(arguments=config_args)
496-
backend.set("foo", "bar")
497-
eq_(backend._clients.memcached.canary, [{"socket_timeout": 4}])
498-
499501

500502
class LocalThreadTest(TestCase):
501503
def setUp(self):

0 commit comments

Comments
 (0)