架构师训练营第 5 周
作业一:
用你熟悉的编程语言实现一致性 hash 算法。
编写测试用例测试这个算法,测试 100 万 KV 数据,10 个服务器节点的情况下,计算这些 KV 数据在服务器上分布数量的标准差,以评估算法的存储负载不均衡性。
import md5
class HashRing(object):
def __init__(self, nodes=None, replicas=3):
"""
Manages a hash ring
:nodes
a list of object that have a proper __str__ representation
:replicas
indicates how many virtual points should be used per node,
replicas are required to improve the distribution
"""
self.replicas = replicas
self.ring = {}
self._sorted_keys = []
if nodes:
for node in nodes:
self.add_node(node)
def add_node(self, node):
"""
Adds a node to the hash ring(including a number of replicas)
"""
for i in xrange(0, self.replicas):
key = self.gen_key("%s:%s" % (node, i))
self.ring[key] = node
self._sorted_keys.append(key)
self._sorted_keys.sort()
def remove_node(self, node):
"""
Removes node from the hash ring and its replicas
"""
for i in xrange(0, self.replicas):
key = self.gen_key("%s:%s" % (node, i))
del self.ring[key]
self._sorted_keys.remove(key)
def get_node_keys(self, node):
"""
Given a node return all its virturl node keys
"""
keys = []
for i in xrange(0, self.replicas):
key = self.gen_key("%s:%s" % (node, i))
keys.append(key)
return key
def get_node(self, string_key):
"""
Given a string key corresponding node in the hash ring is returned.
If the hash ring is empty, None is returned.
"""
return self.get_node_pos(string_key)[0]
def get_node_pos(self, string_key):
"""
Given a string key corresponding node in the hash ring is returned
along with it's position in the ring.
If the hash ring is empty, (None, None) is returned
"""
if not self.ring:
return None, None
key = self.gen_key(string_key)
nodes = self._sorted_keys
for i in xrange(0, len(nodes)):
node = nodes[i]
if key <= node:
return self.ring[node], i
return self.ring[nodes[0]], 0
def get_nodes(self, string_key):
"""
Given a string key it returns the nodes as a generator that can hold the key.
The generator is never ending and iterates through the ring starting at the correct position.
"""
if not self.ring:
yield None
node, pos = self.get_node_pos(string_key)
for key in self._sorted_keys:
yield self.ring[key]
def gen_key(self, key):
"""
Given a string key it return a long value,
this long value represent a place on the hash ring.
md5 is currently used because it mixes well.
"""
m = md5.new()
m.update(key)
return long(m.hexdigest(), 16)
原文链接:https://blog.csdn.net/tanghaiyu777/article/details/55270946
作业二:
根据当周学习情况,完成一篇学习总结
1 关于缓存:存储在计算机上的一个原始数据复制集,以便于访问,数据需要多次读取的时候,用于加快读取的速度,与缓冲的区别
2 缓存键集大小
缓存中 每个对象使用缓存键进行识别,定位一个对象的唯一方式就是对缓存键执行精确匹配。
3 缓存雪崩,导致存储系统压力增大,最后影响整个系统,解决措施由后台更新缓存,缓存失效的时候通知后台程序去更新
4 缓存穿透,是指大量请求的缓存数据不存在,直接访问的存储系统,解决措施主要是把不存在的数据也缓存起来
5 消息队列与异步架构 同步调用与异步调用
6 点对点和发布订阅模型 实现异步处理,提升处理性能
7 负载均衡架构 HTTP 重定向负载均衡 DNS 负载均衡 IP 负载均衡
8 Session服务器
9 MySQL 复制 主从复制、读写分离
评论