Professional Documents
Culture Documents
Malinowski Rafa
KIM JESTEM ?
Rafa after.design Malinowski
programista php/js/python
project manager
?
poniedziaek, 11 lipca 2011
rafal@malinowski.be
http://about.me/after.design
MEMCACHED ?
dane trzymane w RAMie
wicej ni 1 frontend - wspomaganie trzymania sesji w bazie
witryny z du iloci odson
RAM jest tani i ma niski czas dostpu (szybko)
mechanizmy rotowania danych
brak miejsca - starsze dane s usuwane
ALE
ZEMSTA PROGRAMISTY
WYBR SERWERA
reszta z dzielenia
consistent hashing
ketama - consistent hashing troch inaczej
RESZTA Z DZIELENIA
1000 kluczy, dokadanie 1 serwera
2 -> 3
3->4
4->5
5->6
6->7
7->8
666
748
800
830
856
874
3->5
4->6
5->7
6->8
500
799
664
855
748
CONSISTENT HASHING
points = {}
for singlePoint in xrange(160):
point = crc32(str(singlePoint), crc32(serverData))
points[point] = "127.0.0.1:11211"
sortedPoints = sorted(point.keys())
CONSISTENT HASHING
1000 kluczy, dokadanie 1 serwera
2 -> 3
3->4
4->5
5->6
6->7
7->8
368
271
246
177
150
102
3->5
4->6
5->7
6->8
526
432
353
313
238
KETAMA
W 2007 roku wydana libketama wydana przez twrcw
audiscrobbler (obecne last.fm),
Prba ujednolicenia algorytmw wyboru serwera,
Mniej danych musi by przeniesione przy dodaniu kolejnego
serwera memcached,
Modyfikacja consistent hashing
KETAMA - ALGORYTM
buckets={}
pointSearch = 0xffffffff / 1024 // 4194303
for i in xrange(1024):
buckets[i] = consistentFind(pointSearch * bucketNumber)
consistentFind() - znajd serwer do ktrego naley dany punkt
3->4
4->5
5->6
6->7
7->8
359
209
164
185
136
115
3->5
4->6
5->7
6->8
511
337
314
295
235
PROBLEMY ?
Problemy/bdy/rnice w implementacji:
point = crc32(str(singlePoint), crc32(serverData+'-'))
SERIALIZOWANIE
PHP - serialize
a:1:{s:1:"a";i:123;}
Python - pickle/cpickle
JavaScript - JSON !
{"a":123}
SERIALIZOWANIE
Dlaczego unserialize w pythonie (2.6/2.7) nie jest fajne ?
>>> len(u"")
4
>>> len("")
8
Rozwizanie:
for match in re.finditer('s:(\d+):"(.*?)";', data, re.DOTALL):
FLAGI
Byte/
0
|
1
|
2
|
3
|
/
|
|
|
|
|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
+---------------+---------------+---------------+---------------+
0/ HEADER
/
/
/
/
/
/
/
+---------------+---------------+---------------+---------------+
24/ COMMAND-SPECIFIC EXTRAS (as needed)
/
+/ (note length in the extras length header field)
/
+---------------+---------------+---------------+---------------+
m/ Key (as needed)
/
+/ (note length in key length header field)
/
+---------------+---------------+---------------+---------------+
n/ Value (as needed)
/
+/ (note length is total body length header field, minus
/
+/
sum of the extras and key length body fields)
/
+---------------+---------------+---------------+---------------+
Total 24 bytes
FLAGI
Byte/
0
|
1
|
2
|
3
|
/
|
|
|
|
|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
+---------------+---------------+---------------+---------------+
0/ HEADER
/
/
/
/
/
/
/
+---------------+---------------+---------------+---------------+
24/ COMMAND-SPECIFIC EXTRAS (as needed)
/
+/ (note length in the extras length header field)
/
+---------------+---------------+---------------+---------------+
m/ Key (as needed)
/
+/ (note length in key length header field)
/
+---------------+---------------+---------------+---------------+
n/ Value (as needed)
/
+/ (note length is total body length header field, minus
/
+/
sum of the extras and key length body fields)
/
+---------------+---------------+---------------+---------------+
Total 24 bytes
FLAGI
/* use lowest byte for flags */
#define MMC_SERIALIZED!0x0001
#define MMC_COMPRESSED!0x0002
/* use second lowest byte to indicate data
type */
#define MMC_TYPE_STRING! 0x0000
#define MMC_TYPE_BOOL! 0x0100
/*#define MMC_TYPE_INT!0x0200*/
#define MMC_TYPE_LONG! 0x0300
/*#define MMC_TYPE_DATE! 0x0400*/
/*#define MMC_TYPE_BYTE! 0x0500*/
/*#define MMC_TYPE_FLOAT!0x0600*/
#define MMC_TYPE_DOUBLE! 0x0700
/*#define MMC_TYPE_BLOB! 0x0800*/
JavaScript
will rule the world