2013-08-31 22:36:58 -04:00
|
|
|
# -*- coding:utf-8 -*-
|
2013-09-20 01:00:54 +08:00
|
|
|
# Copyright (c) 2010-2012 OpenStack Foundation
|
2010-07-12 17:03:45 -05:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2013-08-31 22:36:58 -04:00
|
|
|
"""Tests for swift.common.utils"""
|
2010-07-12 17:03:45 -05:00
|
|
|
|
2013-10-07 21:28:48 -07:00
|
|
|
from collections import defaultdict
|
2010-07-12 17:03:45 -05:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import unittest
|
2012-01-12 22:30:32 +00:00
|
|
|
from uuid import uuid4
|
2010-07-12 17:03:45 -05:00
|
|
|
|
2013-09-04 22:20:44 +00:00
|
|
|
from eventlet import GreenPool, sleep, Queue
|
|
|
|
from eventlet.pools import Pool
|
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
from swift.common import memcached
|
2013-10-07 21:28:48 -07:00
|
|
|
from mock import patch, MagicMock
|
Adding StatsD logging to Swift.
Documentation, including a list of metrics reported and their semantics,
is in the Admin Guide in a new section, "Reporting Metrics to StatsD".
An optional "metric prefix" may be configured which will be prepended to
every metric name sent to StatsD.
Here is the rationale for doing a deep integration like this versus only
sending metrics to StatsD in middleware. It's the only way to report
some internal activities of Swift in a real-time manner. So to have one
way of reporting to StatsD and one place/style of configuration, even
some things (like, say, timing of PUT requests into the proxy-server)
which could be logged via middleware are consistently logged the same
way (deep integration via the logger delegate methods).
When log_statsd_host is configured, get_logger() injects a
swift.common.utils.StatsdClient object into the logger as
logger.statsd_client. Then a set of delegate methods on LogAdapter
either pass through to the StatsdClient object or become no-ops. This
allows StatsD logging to look like:
self.logger.increment('some.metric.here')
and do the right thing in all cases and with no messy conditional logic.
I wanted to use the pystatsd module for the StatsD client, but the
version on PyPi is lagging the git repo (and is missing both the prefix
functionality and timing_since() method). So I wrote my
swift.common.utils.StatsdClient. The interface is the same as
pystatsd.Client, but the code was written from scratch. It's pretty
simple, and the tests I added cover it. This also frees Swift from an
optional dependency on the pystatsd module, making this feature easier
to enable.
There's test coverage for the new code and all existing tests continue
to pass.
Refactored out _one_audit_pass() method in swift/account/auditor.py and
swift/container/auditor.py.
Fixed some misc. PEP8 violations.
Misc test cleanups and refactorings (particularly the way "fake logging"
is handled).
Change-Id: Ie968a9ae8771f59ee7591e2ae11999c44bfe33b2
2012-04-01 16:47:08 -07:00
|
|
|
from test.unit import NullLoggingHandler
|
2010-07-12 17:03:45 -05:00
|
|
|
|
|
|
|
|
2013-09-04 22:20:44 +00:00
|
|
|
class MockedMemcachePool(memcached.MemcacheConnPool):
|
|
|
|
def __init__(self, mocks):
|
|
|
|
Pool.__init__(self, max_size=2)
|
|
|
|
self.mocks = mocks
|
2013-11-14 15:58:52 -08:00
|
|
|
# setting this for the eventlet workaround in the MemcacheConnPool
|
|
|
|
self._parent_class_getter = super(memcached.MemcacheConnPool, self).get
|
2013-09-04 22:20:44 +00:00
|
|
|
|
|
|
|
def create(self):
|
|
|
|
return self.mocks.pop(0)
|
|
|
|
|
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
class ExplodingMockMemcached(object):
|
|
|
|
exploded = False
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def sendall(self, string):
|
|
|
|
self.exploded = True
|
|
|
|
raise socket.error()
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def readline(self):
|
|
|
|
self.exploded = True
|
|
|
|
raise socket.error()
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def read(self, size):
|
|
|
|
self.exploded = True
|
|
|
|
raise socket.error()
|
|
|
|
|
2013-05-07 09:40:53 -07:00
|
|
|
def close(self):
|
|
|
|
pass
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2013-08-31 22:36:58 -04:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
class MockMemcached(object):
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def __init__(self):
|
|
|
|
self.inbuf = ''
|
|
|
|
self.outbuf = ''
|
|
|
|
self.cache = {}
|
|
|
|
self.down = False
|
|
|
|
self.exc_on_delete = False
|
2011-02-23 11:44:36 -08:00
|
|
|
self.read_return_none = False
|
2013-05-07 09:40:53 -07:00
|
|
|
self.close_called = False
|
2010-07-12 17:03:45 -05:00
|
|
|
|
|
|
|
def sendall(self, string):
|
|
|
|
if self.down:
|
|
|
|
raise Exception('mock is down')
|
|
|
|
self.inbuf += string
|
|
|
|
while '\n' in self.inbuf:
|
|
|
|
cmd, self.inbuf = self.inbuf.split('\n', 1)
|
|
|
|
parts = cmd.split()
|
|
|
|
if parts[0].lower() == 'set':
|
|
|
|
self.cache[parts[1]] = parts[2], parts[3], \
|
2012-09-04 23:01:02 +08:00
|
|
|
self.inbuf[:int(parts[4])]
|
|
|
|
self.inbuf = self.inbuf[int(parts[4]) + 2:]
|
2010-07-12 17:03:45 -05:00
|
|
|
if len(parts) < 6 or parts[5] != 'noreply':
|
|
|
|
self.outbuf += 'STORED\r\n'
|
|
|
|
elif parts[0].lower() == 'add':
|
|
|
|
value = self.inbuf[:int(parts[4])]
|
2012-09-04 23:01:02 +08:00
|
|
|
self.inbuf = self.inbuf[int(parts[4]) + 2:]
|
2010-07-12 17:03:45 -05:00
|
|
|
if parts[1] in self.cache:
|
|
|
|
if len(parts) < 6 or parts[5] != 'noreply':
|
|
|
|
self.outbuf += 'NOT_STORED\r\n'
|
|
|
|
else:
|
|
|
|
self.cache[parts[1]] = parts[2], parts[3], value
|
|
|
|
if len(parts) < 6 or parts[5] != 'noreply':
|
|
|
|
self.outbuf += 'STORED\r\n'
|
|
|
|
elif parts[0].lower() == 'delete':
|
|
|
|
if self.exc_on_delete:
|
|
|
|
raise Exception('mock is has exc_on_delete set')
|
|
|
|
if parts[1] in self.cache:
|
|
|
|
del self.cache[parts[1]]
|
|
|
|
if 'noreply' not in parts:
|
|
|
|
self.outbuf += 'DELETED\r\n'
|
|
|
|
elif 'noreply' not in parts:
|
|
|
|
self.outbuf += 'NOT_FOUND\r\n'
|
|
|
|
elif parts[0].lower() == 'get':
|
|
|
|
for key in parts[1:]:
|
|
|
|
if key in self.cache:
|
|
|
|
val = self.cache[key]
|
2012-09-04 23:01:02 +08:00
|
|
|
self.outbuf += 'VALUE %s %s %s\r\n' % (
|
|
|
|
key, val[0], len(val[2]))
|
2010-07-12 17:03:45 -05:00
|
|
|
self.outbuf += val[2] + '\r\n'
|
|
|
|
self.outbuf += 'END\r\n'
|
|
|
|
elif parts[0].lower() == 'incr':
|
|
|
|
if parts[1] in self.cache:
|
|
|
|
val = list(self.cache[parts[1]])
|
|
|
|
val[2] = str(int(val[2]) + int(parts[2]))
|
|
|
|
self.cache[parts[1]] = val
|
|
|
|
self.outbuf += str(val[2]) + '\r\n'
|
|
|
|
else:
|
|
|
|
self.outbuf += 'NOT_FOUND\r\n'
|
2010-10-22 15:25:22 -07:00
|
|
|
elif parts[0].lower() == 'decr':
|
|
|
|
if parts[1] in self.cache:
|
|
|
|
val = list(self.cache[parts[1]])
|
|
|
|
if int(val[2]) - int(parts[2]) > 0:
|
|
|
|
val[2] = str(int(val[2]) - int(parts[2]))
|
|
|
|
else:
|
|
|
|
val[2] = '0'
|
|
|
|
self.cache[parts[1]] = val
|
|
|
|
self.outbuf += str(val[2]) + '\r\n'
|
|
|
|
else:
|
|
|
|
self.outbuf += 'NOT_FOUND\r\n'
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def readline(self):
|
2011-02-23 11:44:36 -08:00
|
|
|
if self.read_return_none:
|
|
|
|
return None
|
2010-07-12 17:03:45 -05:00
|
|
|
if self.down:
|
|
|
|
raise Exception('mock is down')
|
|
|
|
if '\n' in self.outbuf:
|
|
|
|
response, self.outbuf = self.outbuf.split('\n', 1)
|
2012-09-04 23:01:02 +08:00
|
|
|
return response + '\n'
|
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def read(self, size):
|
|
|
|
if self.down:
|
|
|
|
raise Exception('mock is down')
|
|
|
|
if len(self.outbuf) >= size:
|
|
|
|
response = self.outbuf[:size]
|
|
|
|
self.outbuf = self.outbuf[size:]
|
|
|
|
return response
|
|
|
|
|
2013-05-07 09:40:53 -07:00
|
|
|
def close(self):
|
|
|
|
self.close_called = True
|
|
|
|
pass
|
|
|
|
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
class TestMemcached(unittest.TestCase):
|
2013-08-31 22:36:58 -04:00
|
|
|
"""Tests for swift.common.memcached"""
|
2010-07-12 17:03:45 -05:00
|
|
|
|
|
|
|
def test_get_conns(self):
|
|
|
|
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock1.bind(('127.0.0.1', 0))
|
|
|
|
sock1.listen(1)
|
|
|
|
sock1ipport = '%s:%s' % sock1.getsockname()
|
|
|
|
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock2.bind(('127.0.0.1', 0))
|
|
|
|
sock2.listen(1)
|
2012-01-12 22:30:32 +00:00
|
|
|
orig_port = memcached.DEFAULT_MEMCACHED_PORT
|
|
|
|
try:
|
|
|
|
sock2ip, memcached.DEFAULT_MEMCACHED_PORT = sock2.getsockname()
|
|
|
|
sock2ipport = '%s:%s' % (sock2ip, memcached.DEFAULT_MEMCACHED_PORT)
|
|
|
|
# We're deliberately using sock2ip (no port) here to test that the
|
|
|
|
# default port is used.
|
|
|
|
memcache_client = memcached.MemcacheRing([sock1ipport, sock2ip])
|
|
|
|
one = two = True
|
|
|
|
while one or two: # Run until we match hosts one and two
|
|
|
|
key = uuid4().hex
|
|
|
|
for conn in memcache_client._get_conns(key):
|
|
|
|
peeripport = '%s:%s' % conn[2].getpeername()
|
|
|
|
self.assert_(peeripport in (sock1ipport, sock2ipport))
|
|
|
|
if peeripport == sock1ipport:
|
|
|
|
one = False
|
|
|
|
if peeripport == sock2ipport:
|
|
|
|
two = False
|
|
|
|
finally:
|
|
|
|
memcached.DEFAULT_MEMCACHED_PORT = orig_port
|
2010-07-12 17:03:45 -05:00
|
|
|
|
|
|
|
def test_set_get(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
2010-07-12 17:03:45 -05:00
|
|
|
memcache_client.set('some_key', [1, 2, 3])
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(mock.cache.values()[0][1], '0')
|
2010-07-12 17:03:45 -05:00
|
|
|
memcache_client.set('some_key', [4, 5, 6])
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [4, 5, 6])
|
Do not use pickle for serialization in memcache, but JSON
We don't want to use pickle as it can execute arbitrary code. JSON is
safer. However, note that it supports serialization for only some
specific subset of object types; this should be enough for what we need,
though.
To avoid issues on upgrades (unability to read pickled values, and cache
poisoning for old servers not understanding JSON), we add a
memcache_serialization_support configuration option, with the following
values:
0 = older, insecure pickle serialization
1 = json serialization but pickles can still be read (still insecure)
2 = json serialization only (secure and the default)
To avoid an instant full cache flush, existing installations should
upgrade with 0, then set to 1 and reload, then after some time (24
hours) set to 2 and reload. Support for 0 and 1 will be removed in
future versions.
Part of bug 1006414.
Change-Id: Id7d6d547b103b4f23ebf5be98b88f09ec6027ce4
2012-06-21 14:37:41 +02:00
|
|
|
memcache_client.set('some_key', ['simple str', 'utf8 str éà'])
|
2012-09-04 23:01:02 +08:00
|
|
|
# As per http://wiki.openstack.org/encoding,
|
|
|
|
# we should expect to have unicode
|
|
|
|
self.assertEquals(
|
|
|
|
memcache_client.get('some_key'), ['simple str', u'utf8 str éà'])
|
2010-07-12 17:03:45 -05:00
|
|
|
self.assert_(float(mock.cache.values()[0][1]) == 0)
|
|
|
|
memcache_client.set('some_key', [1, 2, 3], timeout=10)
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(mock.cache.values()[0][1], '10')
|
Swift MemcacheRing (set) interface is incompatible fixes
This patch fixes the Swift MemcacheRing set and set_multi
interface incompatible problem with python memcache. The fix
added two extra named parameters to both set and set_multi
method. When only time or timeout parameter is present, then one
of the value will be used. When both time and timeout are present,
the time parameter will be used.
Named parameter min_compress_len is added for pure compatibility
purposes. The current implementation ignores this parameter.
To make swift memcached methods all consistent cross the board,
method incr and decr have also been changed to include a new
named parameter time.
In future OpenStack releases, the named parameter timeout will be
removed, keep the named parameter timeout around for now is
to make sure that mismatched releases between client and server
will still work.
From now on, when a call is made to set, set_multi, decr, incr
by using timeout parametner, a warning message will be logged to
indicate the deprecation of the parameter.
Fixes: bug #1095730
Change-Id: I07af784a54d7d79395fc3265e74145f92f38a893
2013-02-13 13:54:51 -05:00
|
|
|
memcache_client.set('some_key', [1, 2, 3], time=20)
|
|
|
|
self.assertEquals(mock.cache.values()[0][1], '20')
|
|
|
|
|
2012-11-16 00:09:14 -05:00
|
|
|
sixtydays = 60 * 24 * 60 * 60
|
|
|
|
esttimeout = time.time() + sixtydays
|
|
|
|
memcache_client.set('some_key', [1, 2, 3], timeout=sixtydays)
|
2010-07-12 17:03:45 -05:00
|
|
|
self.assert_(-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1)
|
Swift MemcacheRing (set) interface is incompatible fixes
This patch fixes the Swift MemcacheRing set and set_multi
interface incompatible problem with python memcache. The fix
added two extra named parameters to both set and set_multi
method. When only time or timeout parameter is present, then one
of the value will be used. When both time and timeout are present,
the time parameter will be used.
Named parameter min_compress_len is added for pure compatibility
purposes. The current implementation ignores this parameter.
To make swift memcached methods all consistent cross the board,
method incr and decr have also been changed to include a new
named parameter time.
In future OpenStack releases, the named parameter timeout will be
removed, keep the named parameter timeout around for now is
to make sure that mismatched releases between client and server
will still work.
From now on, when a call is made to set, set_multi, decr, incr
by using timeout parametner, a warning message will be logged to
indicate the deprecation of the parameter.
Fixes: bug #1095730
Change-Id: I07af784a54d7d79395fc3265e74145f92f38a893
2013-02-13 13:54:51 -05:00
|
|
|
memcache_client.set('some_key', [1, 2, 3], time=sixtydays)
|
|
|
|
self.assert_(-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1)
|
2010-07-12 17:03:45 -05:00
|
|
|
|
|
|
|
def test_incr(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.incr('some_key', delta=5), 5)
|
2010-07-12 17:03:45 -05:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '5')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.incr('some_key', delta=5), 10)
|
2010-07-12 17:03:45 -05:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '10')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.incr('some_key', delta=1), 11)
|
2010-07-12 17:03:45 -05:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '11')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.incr('some_key', delta=-5), 6)
|
2010-10-22 15:25:22 -07:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '6')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.incr('some_key', delta=-15), 0)
|
2010-10-25 12:52:25 -07:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '0')
|
2011-02-23 11:44:36 -08:00
|
|
|
mock.read_return_none = True
|
|
|
|
self.assertRaises(memcached.MemcacheConnectionError,
|
|
|
|
memcache_client.incr, 'some_key', delta=-15)
|
2013-05-07 09:40:53 -07:00
|
|
|
self.assertTrue(mock.close_called)
|
2010-10-25 12:52:25 -07:00
|
|
|
|
2012-11-16 00:09:14 -05:00
|
|
|
def test_incr_w_timeout(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
Swift MemcacheRing (set) interface is incompatible fixes
This patch fixes the Swift MemcacheRing set and set_multi
interface incompatible problem with python memcache. The fix
added two extra named parameters to both set and set_multi
method. When only time or timeout parameter is present, then one
of the value will be used. When both time and timeout are present,
the time parameter will be used.
Named parameter min_compress_len is added for pure compatibility
purposes. The current implementation ignores this parameter.
To make swift memcached methods all consistent cross the board,
method incr and decr have also been changed to include a new
named parameter time.
In future OpenStack releases, the named parameter timeout will be
removed, keep the named parameter timeout around for now is
to make sure that mismatched releases between client and server
will still work.
From now on, when a call is made to set, set_multi, decr, incr
by using timeout parametner, a warning message will be logged to
indicate the deprecation of the parameter.
Fixes: bug #1095730
Change-Id: I07af784a54d7d79395fc3265e74145f92f38a893
2013-02-13 13:54:51 -05:00
|
|
|
memcache_client.incr('some_key', delta=5, time=55)
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '5')
|
|
|
|
self.assertEquals(mock.cache.values()[0][1], '55')
|
|
|
|
memcache_client.delete('some_key')
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), None)
|
|
|
|
fiftydays = 50 * 24 * 60 * 60
|
|
|
|
esttimeout = time.time() + fiftydays
|
Swift MemcacheRing (set) interface is incompatible fixes
This patch fixes the Swift MemcacheRing set and set_multi
interface incompatible problem with python memcache. The fix
added two extra named parameters to both set and set_multi
method. When only time or timeout parameter is present, then one
of the value will be used. When both time and timeout are present,
the time parameter will be used.
Named parameter min_compress_len is added for pure compatibility
purposes. The current implementation ignores this parameter.
To make swift memcached methods all consistent cross the board,
method incr and decr have also been changed to include a new
named parameter time.
In future OpenStack releases, the named parameter timeout will be
removed, keep the named parameter timeout around for now is
to make sure that mismatched releases between client and server
will still work.
From now on, when a call is made to set, set_multi, decr, incr
by using timeout parametner, a warning message will be logged to
indicate the deprecation of the parameter.
Fixes: bug #1095730
Change-Id: I07af784a54d7d79395fc3265e74145f92f38a893
2013-02-13 13:54:51 -05:00
|
|
|
memcache_client.incr('some_key', delta=5, time=fiftydays)
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '5')
|
|
|
|
self.assert_(-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1)
|
|
|
|
memcache_client.delete('some_key')
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), None)
|
|
|
|
memcache_client.incr('some_key', delta=5)
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), '5')
|
|
|
|
self.assertEquals(mock.cache.values()[0][1], '0')
|
Swift MemcacheRing (set) interface is incompatible fixes
This patch fixes the Swift MemcacheRing set and set_multi
interface incompatible problem with python memcache. The fix
added two extra named parameters to both set and set_multi
method. When only time or timeout parameter is present, then one
of the value will be used. When both time and timeout are present,
the time parameter will be used.
Named parameter min_compress_len is added for pure compatibility
purposes. The current implementation ignores this parameter.
To make swift memcached methods all consistent cross the board,
method incr and decr have also been changed to include a new
named parameter time.
In future OpenStack releases, the named parameter timeout will be
removed, keep the named parameter timeout around for now is
to make sure that mismatched releases between client and server
will still work.
From now on, when a call is made to set, set_multi, decr, incr
by using timeout parametner, a warning message will be logged to
indicate the deprecation of the parameter.
Fixes: bug #1095730
Change-Id: I07af784a54d7d79395fc3265e74145f92f38a893
2013-02-13 13:54:51 -05:00
|
|
|
memcache_client.incr('some_key', delta=5, time=55)
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '10')
|
|
|
|
self.assertEquals(mock.cache.values()[0][1], '0')
|
|
|
|
|
2010-10-25 12:52:25 -07:00
|
|
|
def test_decr(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.decr('some_key', delta=5), 0)
|
2010-10-25 12:52:25 -07:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '0')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.incr('some_key', delta=15), 15)
|
2010-10-25 12:52:25 -07:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '15')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.decr('some_key', delta=4), 11)
|
2010-10-25 12:52:25 -07:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '11')
|
2013-10-15 15:38:02 +00:00
|
|
|
self.assertEquals(memcache_client.decr('some_key', delta=15), 0)
|
2010-10-22 15:25:22 -07:00
|
|
|
self.assertEquals(memcache_client.get('some_key'), '0')
|
2011-02-23 11:44:36 -08:00
|
|
|
mock.read_return_none = True
|
|
|
|
self.assertRaises(memcached.MemcacheConnectionError,
|
|
|
|
memcache_client.decr, 'some_key', delta=15)
|
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
def test_retry(self):
|
|
|
|
logging.getLogger().addHandler(NullLoggingHandler())
|
2012-09-04 23:01:02 +08:00
|
|
|
memcache_client = memcached.MemcacheRing(
|
|
|
|
['1.2.3.4:11211', '1.2.3.5:11211'])
|
2010-07-12 17:03:45 -05:00
|
|
|
mock1 = ExplodingMockMemcached()
|
|
|
|
mock2 = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock2, mock2)])
|
|
|
|
memcache_client._client_cache['1.2.3.5:11211'] = MockedMemcachePool(
|
|
|
|
[(mock1, mock1)])
|
2010-07-12 17:03:45 -05:00
|
|
|
memcache_client.set('some_key', [1, 2, 3])
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
|
|
|
self.assertEquals(mock1.exploded, True)
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
2010-07-12 17:03:45 -05:00
|
|
|
memcache_client.set('some_key', [1, 2, 3])
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
|
|
|
memcache_client.delete('some_key')
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), None)
|
|
|
|
|
|
|
|
def test_multi(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
2010-07-12 17:03:45 -05:00
|
|
|
memcache_client.set_multi(
|
|
|
|
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key')
|
|
|
|
self.assertEquals(
|
|
|
|
memcache_client.get_multi(('some_key2', 'some_key1'), 'multi_key'),
|
|
|
|
[[4, 5, 6], [1, 2, 3]])
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(mock.cache.values()[0][1], '0')
|
|
|
|
self.assertEquals(mock.cache.values()[1][1], '0')
|
2010-07-12 17:03:45 -05:00
|
|
|
memcache_client.set_multi(
|
|
|
|
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key',
|
|
|
|
timeout=10)
|
2012-11-16 00:09:14 -05:00
|
|
|
self.assertEquals(mock.cache.values()[0][1], '10')
|
|
|
|
self.assertEquals(mock.cache.values()[1][1], '10')
|
Swift MemcacheRing (set) interface is incompatible fixes
This patch fixes the Swift MemcacheRing set and set_multi
interface incompatible problem with python memcache. The fix
added two extra named parameters to both set and set_multi
method. When only time or timeout parameter is present, then one
of the value will be used. When both time and timeout are present,
the time parameter will be used.
Named parameter min_compress_len is added for pure compatibility
purposes. The current implementation ignores this parameter.
To make swift memcached methods all consistent cross the board,
method incr and decr have also been changed to include a new
named parameter time.
In future OpenStack releases, the named parameter timeout will be
removed, keep the named parameter timeout around for now is
to make sure that mismatched releases between client and server
will still work.
From now on, when a call is made to set, set_multi, decr, incr
by using timeout parametner, a warning message will be logged to
indicate the deprecation of the parameter.
Fixes: bug #1095730
Change-Id: I07af784a54d7d79395fc3265e74145f92f38a893
2013-02-13 13:54:51 -05:00
|
|
|
memcache_client.set_multi(
|
|
|
|
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key',
|
|
|
|
time=20)
|
|
|
|
self.assertEquals(mock.cache.values()[0][1], '20')
|
|
|
|
self.assertEquals(mock.cache.values()[1][1], '20')
|
|
|
|
|
2012-11-16 00:09:14 -05:00
|
|
|
fortydays = 50 * 24 * 60 * 60
|
|
|
|
esttimeout = time.time() + fortydays
|
|
|
|
memcache_client.set_multi(
|
|
|
|
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key',
|
|
|
|
timeout=fortydays)
|
2010-07-12 17:03:45 -05:00
|
|
|
self.assert_(-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1)
|
|
|
|
self.assert_(-1 <= float(mock.cache.values()[1][1]) - esttimeout <= 1)
|
2012-09-04 23:01:02 +08:00
|
|
|
self.assertEquals(memcache_client.get_multi(
|
|
|
|
('some_key2', 'some_key1', 'not_exists'), 'multi_key'),
|
|
|
|
[[4, 5, 6], [1, 2, 3], None])
|
2010-07-12 17:03:45 -05:00
|
|
|
|
Do not use pickle for serialization in memcache, but JSON
We don't want to use pickle as it can execute arbitrary code. JSON is
safer. However, note that it supports serialization for only some
specific subset of object types; this should be enough for what we need,
though.
To avoid issues on upgrades (unability to read pickled values, and cache
poisoning for old servers not understanding JSON), we add a
memcache_serialization_support configuration option, with the following
values:
0 = older, insecure pickle serialization
1 = json serialization but pickles can still be read (still insecure)
2 = json serialization only (secure and the default)
To avoid an instant full cache flush, existing installations should
upgrade with 0, then set to 1 and reload, then after some time (24
hours) set to 2 and reload. Support for 0 and 1 will be removed in
future versions.
Part of bug 1006414.
Change-Id: Id7d6d547b103b4f23ebf5be98b88f09ec6027ce4
2012-06-21 14:37:41 +02:00
|
|
|
def test_serialization(self):
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'],
|
|
|
|
allow_pickle=True)
|
|
|
|
mock = MockMemcached()
|
2013-09-04 22:20:44 +00:00
|
|
|
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
|
|
|
|
[(mock, mock)] * 2)
|
Do not use pickle for serialization in memcache, but JSON
We don't want to use pickle as it can execute arbitrary code. JSON is
safer. However, note that it supports serialization for only some
specific subset of object types; this should be enough for what we need,
though.
To avoid issues on upgrades (unability to read pickled values, and cache
poisoning for old servers not understanding JSON), we add a
memcache_serialization_support configuration option, with the following
values:
0 = older, insecure pickle serialization
1 = json serialization but pickles can still be read (still insecure)
2 = json serialization only (secure and the default)
To avoid an instant full cache flush, existing installations should
upgrade with 0, then set to 1 and reload, then after some time (24
hours) set to 2 and reload. Support for 0 and 1 will be removed in
future versions.
Part of bug 1006414.
Change-Id: Id7d6d547b103b4f23ebf5be98b88f09ec6027ce4
2012-06-21 14:37:41 +02:00
|
|
|
memcache_client.set('some_key', [1, 2, 3])
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
|
|
|
memcache_client._allow_pickle = False
|
|
|
|
memcache_client._allow_unpickle = True
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
|
|
|
memcache_client._allow_unpickle = False
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), None)
|
|
|
|
memcache_client.set('some_key', [1, 2, 3])
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
|
|
|
memcache_client._allow_unpickle = True
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
|
|
|
memcache_client._allow_pickle = True
|
|
|
|
self.assertEquals(memcache_client.get('some_key'), [1, 2, 3])
|
2010-07-12 17:03:45 -05:00
|
|
|
|
2013-09-04 22:20:44 +00:00
|
|
|
def test_connection_pooling(self):
|
|
|
|
with patch('swift.common.memcached.socket') as mock_module:
|
|
|
|
# patch socket, stub socket.socket, mock sock
|
|
|
|
mock_sock = mock_module.socket.return_value
|
|
|
|
|
|
|
|
# track clients waiting for connections
|
|
|
|
connected = []
|
|
|
|
connections = Queue()
|
2013-10-04 08:04:42 -04:00
|
|
|
errors = []
|
2013-09-04 22:20:44 +00:00
|
|
|
|
|
|
|
def wait_connect(addr):
|
|
|
|
connected.append(addr)
|
2013-11-14 15:58:52 -08:00
|
|
|
sleep(0.1) # yield
|
2013-10-04 08:04:42 -04:00
|
|
|
val = connections.get()
|
|
|
|
if val is not None:
|
|
|
|
errors.append(val)
|
|
|
|
|
2013-09-04 22:20:44 +00:00
|
|
|
mock_sock.connect = wait_connect
|
|
|
|
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'],
|
|
|
|
connect_timeout=10)
|
|
|
|
# sanity
|
|
|
|
self.assertEquals(1, len(memcache_client._client_cache))
|
|
|
|
for server, pool in memcache_client._client_cache.items():
|
2013-10-04 08:04:42 -04:00
|
|
|
self.assertEqual(2, pool.max_size)
|
2013-09-04 22:20:44 +00:00
|
|
|
|
|
|
|
# make 10 requests "at the same time"
|
|
|
|
p = GreenPool()
|
|
|
|
for i in range(10):
|
|
|
|
p.spawn(memcache_client.set, 'key', 'value')
|
|
|
|
for i in range(3):
|
|
|
|
sleep(0.1)
|
2013-10-04 08:04:42 -04:00
|
|
|
self.assertEqual(2, len(connected))
|
|
|
|
|
2013-09-04 22:20:44 +00:00
|
|
|
# give out a connection
|
|
|
|
connections.put(None)
|
2013-10-04 08:04:42 -04:00
|
|
|
|
|
|
|
# at this point, only one connection should have actually been
|
|
|
|
# created, the other is in the creation step, and the rest of the
|
|
|
|
# clients are not attempting to connect. we let this play out a
|
|
|
|
# bit to verify.
|
2013-09-04 22:20:44 +00:00
|
|
|
for i in range(3):
|
|
|
|
sleep(0.1)
|
2013-10-04 08:04:42 -04:00
|
|
|
self.assertEqual(2, len(connected))
|
|
|
|
|
|
|
|
# finish up, this allows the final connection to be created, so
|
|
|
|
# that all the other clients can use the two existing connections
|
|
|
|
# and no others will be created.
|
|
|
|
connections.put(None)
|
|
|
|
connections.put('nono')
|
|
|
|
self.assertEqual(2, len(connected))
|
2013-09-04 22:20:44 +00:00
|
|
|
p.waitall()
|
2013-10-04 08:04:42 -04:00
|
|
|
self.assertEqual(2, len(connected))
|
|
|
|
self.assertEqual(0, len(errors),
|
|
|
|
"A client was allowed a third connection")
|
|
|
|
connections.get_nowait()
|
|
|
|
self.assertTrue(connections.empty())
|
2013-09-04 22:20:44 +00:00
|
|
|
|
2013-11-14 15:58:52 -08:00
|
|
|
# Ensure we exercise the backported-for-pre-eventlet-version-0.9.17 get()
|
|
|
|
# code, even if the executing eventlet's version is already newer.
|
|
|
|
@patch.object(memcached, 'eventlet_version', '0.9.16')
|
|
|
|
def test_connection_pooling_pre_0_9_17(self):
|
|
|
|
with patch('swift.common.memcached.socket') as mock_module:
|
|
|
|
connected = []
|
|
|
|
count = [0]
|
|
|
|
|
|
|
|
def _slow_yielding_connector(addr):
|
|
|
|
count[0] += 1
|
|
|
|
if count[0] % 3 == 0:
|
|
|
|
raise ValueError('whoops!')
|
|
|
|
sleep(0.1)
|
|
|
|
connected.append(addr)
|
|
|
|
|
|
|
|
mock_module.socket.return_value.connect.side_effect = \
|
|
|
|
_slow_yielding_connector
|
|
|
|
|
|
|
|
# If POOL_SIZE is not small enough relative to USER_COUNT, the
|
|
|
|
# "free_items" business in the eventlet.pools.Pool will cause
|
|
|
|
# spurious failures below. I found these values to work well on a
|
|
|
|
# VM running in VirtualBox on a late 2013 Retina MacbookPro:
|
|
|
|
POOL_SIZE = 5
|
|
|
|
USER_COUNT = 50
|
|
|
|
|
|
|
|
pool = memcached.MemcacheConnPool('1.2.3.4:11211', size=POOL_SIZE,
|
|
|
|
connect_timeout=10)
|
|
|
|
self.assertEqual(POOL_SIZE, pool.max_size)
|
|
|
|
|
|
|
|
def _user():
|
|
|
|
got = None
|
|
|
|
while not got:
|
|
|
|
try:
|
|
|
|
got = pool.get()
|
2013-11-26 14:39:30 -08:00
|
|
|
except: # noqa
|
2013-11-14 15:58:52 -08:00
|
|
|
pass
|
|
|
|
pool.put(got)
|
|
|
|
|
|
|
|
# make a bunch of requests "at the same time"
|
|
|
|
p = GreenPool()
|
|
|
|
for i in range(USER_COUNT):
|
|
|
|
p.spawn(_user)
|
|
|
|
p.waitall()
|
|
|
|
|
|
|
|
# If the except block after the "created = self.create()" call
|
|
|
|
# doesn't correctly decrement self.current_size, this test will
|
|
|
|
# fail by having some number less than POOL_SIZE connections (in my
|
|
|
|
# testing, anyway).
|
|
|
|
self.assertEqual(POOL_SIZE, len(connected))
|
|
|
|
|
|
|
|
# Subsequent requests should get and use the existing
|
|
|
|
# connections, not creating any more.
|
|
|
|
for i in range(USER_COUNT):
|
|
|
|
p.spawn(_user)
|
|
|
|
p.waitall()
|
|
|
|
|
|
|
|
self.assertEqual(POOL_SIZE, len(connected))
|
|
|
|
|
2013-10-07 21:28:48 -07:00
|
|
|
def test_connection_pool_timeout(self):
|
|
|
|
orig_conn_pool = memcached.MemcacheConnPool
|
|
|
|
try:
|
|
|
|
connections = defaultdict(Queue)
|
|
|
|
pending = defaultdict(int)
|
|
|
|
served = defaultdict(int)
|
|
|
|
|
|
|
|
class MockConnectionPool(orig_conn_pool):
|
|
|
|
def get(self):
|
|
|
|
pending[self.server] += 1
|
|
|
|
conn = connections[self.server].get()
|
|
|
|
pending[self.server] -= 1
|
|
|
|
return conn
|
|
|
|
|
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
connections[self.server].put(*args, **kwargs)
|
|
|
|
served[self.server] += 1
|
|
|
|
|
|
|
|
memcached.MemcacheConnPool = MockConnectionPool
|
|
|
|
|
|
|
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211',
|
|
|
|
'1.2.3.5:11211'],
|
|
|
|
io_timeout=0.5,
|
|
|
|
pool_timeout=0.1)
|
|
|
|
|
|
|
|
p = GreenPool()
|
|
|
|
for i in range(10):
|
|
|
|
p.spawn(memcache_client.set, 'key', 'value')
|
|
|
|
|
|
|
|
# let everyone block
|
|
|
|
sleep(0)
|
|
|
|
self.assertEqual(pending['1.2.3.5:11211'], 10)
|
|
|
|
|
|
|
|
# hand out a couple slow connection
|
|
|
|
mock_conn = MagicMock(), MagicMock()
|
|
|
|
mock_conn[1].sendall = lambda x: sleep(0.2)
|
|
|
|
connections['1.2.3.5:11211'].put(mock_conn)
|
|
|
|
connections['1.2.3.5:11211'].put(mock_conn)
|
|
|
|
|
|
|
|
# so far so good, everyone is still waiting
|
|
|
|
sleep(0)
|
|
|
|
self.assertEqual(pending['1.2.3.5:11211'], 8)
|
|
|
|
self.assertEqual(len(memcache_client._errors['1.2.3.5:11211']), 0)
|
|
|
|
|
|
|
|
# but they won't wait longer than pool_timeout
|
|
|
|
mock_conn = MagicMock(), MagicMock()
|
|
|
|
connections['1.2.3.4:11211'].put(mock_conn)
|
|
|
|
connections['1.2.3.4:11211'].put(mock_conn)
|
|
|
|
p.waitall()
|
|
|
|
self.assertEqual(len(memcache_client._errors['1.2.3.5:11211']), 8)
|
|
|
|
self.assertEqual(served['1.2.3.5:11211'], 2)
|
|
|
|
self.assertEqual(len(memcache_client._errors['1.2.3.4:11211']), 0)
|
|
|
|
self.assertEqual(served['1.2.3.4:11211'], 8)
|
|
|
|
|
|
|
|
# and we never got more put in that we gave out
|
|
|
|
self.assertEqual(connections['1.2.3.5:11211'].qsize(), 2)
|
|
|
|
self.assertEqual(connections['1.2.3.4:11211'].qsize(), 2)
|
|
|
|
finally:
|
|
|
|
memcached.MemcacheConnPool = orig_conn_pool
|
2012-09-04 23:01:02 +08:00
|
|
|
|
2010-07-12 17:03:45 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|