1
"""Copyright 2008 Orbitz WorldWide
2
Copyright 2011 Chris Davis
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License."""
16
from graphite.logger import log
19
from hashlib import md5
24
def hashRequest(request):
25
# Normalize the request parameters so ensure we're deterministic
26
queryParams = ["%s=%s" % (key, '&'.join(values))
27
for (key,values) in request.GET.lists()
28
if not key.startswith('_')]
30
normalizedParams = ','.join( sorted(queryParams) ) or 'noParam'
31
myHash = stripControlChars(normalizedParams) #memcached doesn't like unprintable characters in its keys
33
return compactHash(myHash)
36
def hashData(targets, startTime, endTime):
37
targetsString = ','.join(targets)
38
startTimeString = startTime.strftime("%Y%m%d_%H%M")
39
endTimeString = endTime.strftime("%Y%m%d_%H%M")
40
myHash = targetsString + '@' + startTimeString + ':' + endTimeString
41
myHash = stripControlChars(myHash)
43
return compactHash(myHash)
46
def stripControlChars(string):
47
return filter(lambda char: ord(char) >= 33, string)
50
def compactHash(string):
53
return hash.hexdigest()
57
class ConsistentHashRing:
58
def __init__(self, nodes, replica_count=100):
60
self.replica_count = replica_count
64
def compute_ring_position(self, key):
65
big_hash = md5( str(key) ).hexdigest()
66
small_hash = int(big_hash[:4], 16)
69
def add_node(self, key):
70
for i in range(self.replica_count):
71
replica_key = "%s:%d" % (key, i)
72
position = self.compute_ring_position(replica_key)
73
entry = (position, key)
74
bisect.insort(self.ring, entry)
76
def remove_node(self, key):
77
self.ring = [entry for entry in self.ring if entry[1] != key]
79
def get_node(self, key):
80
position = self.compute_ring_position(key)
81
search_entry = (position, None)
82
index = bisect.bisect_left(self.ring, search_entry)
83
index %= len(self.ring)
84
entry = self.ring[index]