forked from dpnova/tornado-memcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tornadoasyncmemcache.py
647 lines (526 loc) · 20.3 KB
/
tornadoasyncmemcache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/usr/bin/env python
"""
Example using ClientPool
========
import tornado.ioloop
import tornado.web
import tornadoasyncmemcache as memcache
import time
ccs = memcache.ClientPool(['127.0.0.1:11211'], maxclients=100)
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
time_str = time.strftime('%Y-%m-%d %H:%M:%S')
ccs.set('test_data', 'Hello world @ %s' % time_str,
callback=self._get_start)
def _get_start(self, data):
ccs.get('test_data', callback=self._get_end)
def _get_end(self, data):
self.write(data)
self.finish()
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
"""
import weakref
import sys
import socket
import time
import types
from tornado import iostream, ioloop
from functools import partial
import collections
try:
import cPickle as pickle
except ImportError:
import pickle
__author__ = "Tornadoified: David Novakovic [email protected], original code: Evan Martin <[email protected]>"
__version__ = "1.0"
__copyright__ = "Copyright (C) 2003 Danga Interactive"
__license__ = "Python"
class TooManyClients(Exception):
pass
class ClientPool(object):
CMDS = ('get', 'replace', 'set', 'decr', 'incr', 'delete')
def __init__(self,
servers,
mincached = 0,
maxcached = 0,
maxclients = 0,
*args, **kwargs):
assert isinstance(mincached, int)
assert isinstance(maxcached, int)
assert isinstance(maxclients, int)
if maxclients > 0:
assert maxclients >= mincached
assert maxclients >= maxcached
if maxcached > 0:
assert maxcached >= mincached
self._servers = servers
self._args, self._kwargs = args, kwargs
self._used = collections.deque()
self._maxclients = maxclients
self._mincached = mincached
self._maxcached = maxcached
self._clients = collections.deque(self._create_clients(mincached))
def _create_clients(self, n):
assert n >= 0
return [Client(self._servers, *self._args, **self._kwargs)
for x in xrange(n)]
def _execute_command(self, cmd, *args, **kwargs):
if not self._clients:
if self._maxclients > 0 and (len(self._clients)
+ len(self._used) >= self._maxclients):
raise TooManyClients("Max of %d clients is already reached"
% self._maxclients)
self._clients.append(self._create_clients(1)[0])
c = self._clients.popleft()
kwargs['callback'] = partial(self._gen_cb, c=c, _cb=kwargs['callback'])
self._used.append(c)
getattr(c, cmd)(*args, **kwargs)
#wraps _execute_command to reinitialize clients in case of server disconnection
def _do(self, cmd, *args, **kwargs):
try:
self._execute_command(cmd, *args, **kwargs)
except IOError as ex:
if ex.message == 'Stream is closed':
self._clients = collections.deque(self._create_clients(self._mincached))
self._execute_command(cmd, *args, **kwargs)
else:
raise ex
def __getattr__(self, name):
if name in self.CMDS:
return partial(self._do, name)
raise AttributeError("'%s' object has no attribute '%s'" %
(self.__class__.__name__, name))
def _gen_cb(self, response, c, _cb, *args, **kwargs):
self._used.remove(c)
if self._maxcached == 0 or self._maxcached > len(self._clients):
self._clients.append(c)
else:
c.disconnect_all()
_cb(response, *args, **kwargs)
class _Error(Exception):
pass
class Client(object):
"""
Object representing a pool of memcache servers.
See L{memcache} for an overview.
In all cases where a key is used, the key can be either:
1. A simple hashable type (string, integer, etc.).
2. A tuple of C{(hashvalue, key)}. This is useful if you want to avoid
making this module calculate a hash value. You may prefer, for
example, to keep all of a given user's objects on the same memcache
server, so you could use the user's unique id as the hash value.
@group Setup: __init__, set_servers, forget_dead_hosts, disconnect_all, debuglog
@group Insertion: set, add, replace
@group Retrieval: get, get_multi
@group Integers: incr, decr
@group Removal: delete
@sort: __init__, set_servers, forget_dead_hosts, disconnect_all, debuglog,\
set, add, replace, get, get_multi, incr, decr, delete
"""
_FLAG_PICKLE = 1<<0
_FLAG_INTEGER = 1<<1
_FLAG_LONG = 1<<2
_SERVER_RETRIES = 10 # how many times to try finding a free server.
_ASYNC_CLIENTS = weakref.WeakKeyDictionary()
# def __new__(cls, servers, max_connections=1000, debug=0, io_loop=None):
# # There is one client per IOLoop since they share curl instances
# io_loop = io_loop or ioloop.IOLoop.instance()
# if io_loop in cls._ASYNC_CLIENTS:
# return cls._ASYNC_CLIENTS[io_loop]
# else:
# instance = super(Client, cls).__new__(cls)
# instance.io_loop = io_loop
# instance.set_servers(servers)
# instance.debug = debug
# instance.stats = {}
# instance.servers
# cls._ASYNC_CLIENTS[io_loop] = instance
# return instance
def __init__(self, servers, debug=0, io_loop=None):
io_loop = io_loop or ioloop.IOLoop.instance()
self.io_loop = io_loop
self.set_servers(servers)
self.debug = debug
self.stats = {}
self.servers
self._ASYNC_CLIENTS[io_loop] = self
# def __init__(self, servers, debug=0):
# """
# Create a new Client object with the given list of servers.
#
# @param servers: C{servers} is passed to L{set_servers}.
# @param debug: whether to display error messages when a server can't be
# contacted.
# """
# self.set_servers(servers)
# self.debug = debug
# self.stats = {}
def set_servers(self, servers):
"""
Set the pool of servers used by this client.
@param servers: an array of servers.
Servers can be passed in two forms:
1. Strings of the form C{"host:port"}, which implies a default weight of 1.
2. Tuples of the form C{("host:port", weight)}, where C{weight} is
an integer weight value.
"""
self.servers = [_Host(s, self.debuglog) for s in servers]
self._init_buckets()
def debuglog(self, str):
if self.debug:
sys.stderr.write("MemCached: %s\n" % str)
def _statlog(self, func):
if not self.stats.has_key(func):
self.stats[func] = 1
else:
self.stats[func] += 1
def forget_dead_hosts(self):
"""
Reset every host in the pool to an "alive" state.
"""
for s in self.servers:
s.dead_until = 0
def _init_buckets(self):
self.buckets = []
for server in self.servers:
for i in range(server.weight):
self.buckets.append(server)
def _get_server(self, key):
if type(key) == types.TupleType:
serverhash = key[0]
key = key[1]
else:
serverhash = hash(key)
for i in range(Client._SERVER_RETRIES):
server = self.buckets[serverhash % len(self.buckets)]
if server.connect():
# print "(using server %s)" % server
return server, key
serverhash = hash(str(serverhash) + str(i))
return None, None
def disconnect_all(self):
for s in self.servers:
s.close_socket()
def delete(self, key, time=0, callback=None):
'''Deletes a key from the memcache.
@return: Nonzero on success.
@rtype: int
'''
server, key = self._get_server(key)
if not server:
self.finish(partial(callback,0))
self._statlog('delete')
if time:
cmd = "delete %s %d" % (key, time)
else:
cmd = "delete %s" % key
server.send_cmd(cmd, callback=partial(self._delete_send_cb,server, callback))
def _delete_send_cb(self, server, callback):
server.expect("DELETED",callback=partial(self._expect_cb, callback=callback))
# except socket.error, msg:
# server.mark_dead(msg[1])
# return 0
# return 1
def incr(self, key, delta=1, callback=None):
"""
Sends a command to the server to atomically increment the value for C{key} by
C{delta}, or by 1 if C{delta} is unspecified. Returns None if C{key} doesn't
exist on server, otherwise it returns the new value after incrementing.
Note that the value for C{key} must already exist in the memcache, and it
must be the string representation of an integer.
>>> mc.set("counter", "20") # returns 1, indicating success
1
>>> mc.incr("counter")
21
>>> mc.incr("counter")
22
Overflow on server is not checked. Be aware of values approaching
2**32. See L{decr}.
@param delta: Integer amount to increment by (should be zero or greater).
@return: New value after incrementing.
@rtype: int
"""
self._incrdecr("incr", key, delta, callback=callback)
def decr(self, key, delta=1, callback=None):
"""
Like L{incr}, but decrements. Unlike L{incr}, underflow is checked and
new values are capped at 0. If server value is 1, a decrement of 2
returns 0, not -1.
@param delta: Integer amount to decrement by (should be zero or greater).
@return: New value after decrementing.
@rtype: int
"""
self._incrdecr("decr", key, delta, callback=callback)
def _incrdecr(self, cmd, key, delta, callback):
server, key = self._get_server(key)
if not server:
self.finish(partial(callback, 0))
self._statlog(cmd)
cmd = "%s %s %d" % (cmd, key, delta)
server.send_cmd(cmd, callback=partial(self._send_incrdecr_check_cb,server, callback))
def _send_incrdecr_cb(self, server, callback):
server.readline(callback=partial(self._send_incrdecr_check_cb, callback=callback))
def _send_incrdecr_check_cb(self, line, callback):
self.finish(partial(callback,int(line)))
# except socket.error, msg:
# server.mark_dead(msg[1])
# return None
def add(self, key, val, time=0, callback=None):
'''
Add new key with value.
Like L{set}, but only stores in memcache if the key doesn't already exist.
@return: Nonzero on success.
@rtype: int
'''
self._set("add", key, val, time, callback)
def replace(self, key, val, time=0, callback=None):
'''Replace existing key with value.
Like L{set}, but only stores in memcache if the key already exists.
The opposite of L{add}.
@return: Nonzero on success.
@rtype: int
'''
self._set("replace", key, val, time, callback)
def set(self, key, val, time=0, callback=None):
'''Unconditionally sets a key to a given value in the memcache.
The C{key} can optionally be an tuple, with the first element being the
hash value, if you want to avoid making this module calculate a hash value.
You may prefer, for example, to keep all of a given user's objects on the
same memcache server, so you could use the user's unique id as the hash
value.
@return: Nonzero on success.
@rtype: int
'''
self._set("set", key, val, time, callback)
def _set(self, cmd, key, val, time, callback):
server, key = self._get_server(key)
if not server:
self.finish(partial(callback,0))
self._statlog(cmd)
flags = 0
if isinstance(val, types.StringTypes):
pass
elif isinstance(val, int):
flags |= Client._FLAG_INTEGER
val = "%d" % val
elif isinstance(val, long):
flags |= Client._FLAG_LONG
val = "%d" % val
else:
flags |= Client._FLAG_PICKLE
val = pickle.dumps(val, 2)
fullcmd = "%s %s %d %d %d\r\n%s" % (cmd, key, flags, time, len(val), val)
server.send_cmd(fullcmd, callback=partial(self._set_send_cb, server=server, callback=callback))
def _set_send_cb(self, server, callback):
server.expect("STORED", callback=partial(self._expect_cb, value=None, callback=callback))
# except socket.error, msg:
# server.mark_dead(msg[1])
# return 0
# return 1
def get(self, key, callback):
'''Retrieves a key from the memcache.
@return: The value or None.
'''
server, key = self._get_server(key)
if not server:
return None
self._statlog('get')
server.send_cmd("get %s" % key, partial(self._get_send_cb, server=server, callback=callback))
def _get_send_cb(self, server, callback):
self._expectvalue(server, line=None, callback=partial(self._get_expectval_cb, server=server, callback=callback))
def _get_expectval_cb(self, rkey, flags, rlen, server, callback):
if not rkey:
self.finish(partial(callback,None))
return
self._recv_value(server, flags, rlen, partial(self._get_recv_cb, server=server, callback=callback))
def _get_recv_cb(self, value, server, callback):
server.expect("END", partial(self._expect_cb, value=value, callback=callback))
def _expect_cb(self, expected=None, value=None, callback=None):
# print "in expect cb"
self.finish(partial(callback,value))
# except (_Error, socket.error), msg:
# if type(msg) is types.TupleType:
# msg = msg[1]
# server.mark_dead(msg)
# return None
# return value
# commented out sendmulti, as it would bea easier for the caller
# to just send an appropriate callback with multipl requests.
def _expectvalue(self, server, line=None, callback=None):
if not line:
server.readline(partial(self._expectvalue_cb, callback=callback))
else:
self._expectvalue_cb(line, callback)
def _expectvalue_cb(self, line, callback):
if line[:5] == 'VALUE':
resp, rkey, flags, len = line.split()
flags = int(flags)
rlen = int(len)
callback(rkey, flags, rlen)
else:
callback(None, None, None)
def _recv_value(self, server, flags, rlen, callback):
rlen += 2 # include \r\n
server.recv(rlen, partial(self._recv_value_cb,rlen=rlen, flags=flags, callback=callback))
def _recv_value_cb(self, buf, flags, rlen, callback):
if len(buf) != rlen:
raise _Error("received %d bytes when expecting %d" % (len(buf), rlen))
if len(buf) == rlen:
buf = buf[:-2] # strip \r\n
if flags == 0:
val = buf
elif flags & Client._FLAG_INTEGER:
val = int(buf)
elif flags & Client._FLAG_LONG:
val = long(buf)
elif flags & Client._FLAG_PICKLE:
val = pickle.loads(buf)
else:
self.debuglog("unknown flags on get: %x\n" % flags)
self.finish(partial(callback,val))
def finish(self, callback):
callback()
# self.disconnect_all()
class _Host:
_DEAD_RETRY = 30 # number of seconds before retrying a dead server.
def __init__(self, host, debugfunc=None):
if isinstance(host, types.TupleType):
host = host[0]
self.weight = host[1]
else:
self.weight = 1
if host.find(":") > 0:
self.ip, self.port = host.split(":")
self.port = int(self.port)
else:
self.ip, self.port = host, 11211
if not debugfunc:
debugfunc = lambda x: x
self.debuglog = debugfunc
self.deaduntil = 0
self.socket = None
self.stream = None
def _check_dead(self):
if self.deaduntil and self.deaduntil > time.time():
return 1
self.deaduntil = 0
return 0
def connect(self):
if self._get_socket():
return 1
return 0
def mark_dead(self, reason):
print "MemCache: %s: %s. Marking dead." % (self, reason)
self.deaduntil = time.time() + _Host._DEAD_RETRY
self.close_socket()
def _get_socket(self):
if self._check_dead():
return None
if self.socket:
return self.socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Python 2.3-ism: s.settimeout(1)
try:
s.connect((self.ip, self.port))
except socket.error, msg:
self.mark_dead("connect: %s" % msg[1])
return None
self.socket = s
self.stream = iostream.IOStream(s)
self.stream.debug=True
return s
def close_socket(self):
if self.socket:
# self.socket.close()
self.stream.close()
self.stream = None
self.socket = None
def send_cmd(self, cmd, callback):
# print "in sendcmd", repr(cmd), callback
self.stream.write(cmd+"\r\n", callback)
#self.socket.sendall(cmd + "\r\n")
def readline(self, callback):
self.stream.read_until("\r\n", callback)
def expect(self, text, callback):
self.readline(partial(self._expect_cb, text=text, callback=callback))
def _expect_cb(self, data, text, callback):
if data.rstrip() != text:
self.debuglog("while expecting '%s', got unexpected response '%s'" % (text, data))
callback(data)
def recv(self, rlen, callback):
self.stream.read_bytes(rlen, callback)
def __str__(self):
d = ''
if self.deaduntil:
d = " (dead until %d)" % self.deaduntil
return "%s:%d%s" % (self.ip, self.port, d)
def _doctest():
import doctest, memcache
servers = ["127.0.0.1:11211"]
mc = Client(servers, debug=1)
globs = {"mc": mc}
return doctest.testmod(memcache, globs=globs)
if __name__ == "__main__":
print "Testing docstrings..."
_doctest()
print "Running tests:"
print
#servers = ["127.0.0.1:11211", "127.0.0.1:11212"]
servers = ["127.0.0.1:11211"]
mc = Client(servers, debug=1)
def to_s(val):
if not isinstance(val, types.StringTypes):
return "%s (%s)" % (val, type(val))
return "%s" % val
def test_setget(key, val):
print "Testing set/get {'%s': %s} ..." % (to_s(key), to_s(val)),
mc.set(key, val)
newval = mc.get(key)
if newval == val:
print "OK"
return 1
else:
print "FAIL"
return 0
class FooStruct:
def __init__(self):
self.bar = "baz"
def __str__(self):
return "A FooStruct"
def __eq__(self, other):
if isinstance(other, FooStruct):
return self.bar == other.bar
return 0
test_setget("a_string", "some random string")
test_setget("an_integer", 42)
if test_setget("long", long(1<<30)):
print "Testing delete ...",
if mc.delete("long"):
print "OK"
else:
print "FAIL"
print "Testing get_multi ...",
print mc.get_multi(["a_string", "an_integer"])
print "Testing get(unknown value) ...",
print to_s(mc.get("unknown_value"))
f = FooStruct()
test_setget("foostruct", f)
print "Testing incr ...",
x = mc.incr("an_integer", 1)
if x == 43:
print "OK"
else:
print "FAIL"
print "Testing decr ...",
x = mc.decr("an_integer", 1)
if x == 42:
print "OK"
else:
print "FAIL"
# vim: ts=4 sw=4 softtabstop=4 et :