python -- 一致性Hash
python有一个python模块--hash_ring,即python中的一致性hash,使用起来也挺简单。
可以参考下官方例子:https://pypi.python.org/pypi/hash_ring/
Basic example of usage (for managing memcached instances): memcache_servers = ['192.168.0.246:11212',
'192.168.0.247:11212',
'192.168.0.249:11212'] ring = HashRing(memcache_servers)
server = ring.get_node('my_key')
Example using weights: memcache_servers = ['192.168.0.246:11212',
'192.168.0.247:11212',
'192.168.0.249:11212']
weights = {
'192.168.0.246:11212': 1,
'192.168.0.247:11212': 2,
'192.168.0.249:11212': 1
} ring = HashRing(memcache_servers, weights)
server = ring.get_node('my_key')
由于项目中其他代码使用的是C编写的Conhash.lib,测试后发现,hash_ring和conhash.lib hash出来的值不一样;
据说可以与C语言很好交互,Google后,果然可以,于是参考:官网ctypes介绍,花了点时间将通过python成功调用
了conhash接口,测试成功,代码如下(比较混乱,暂未整理):
from ctypes import *
import os
from ctypes.util import find_library class _Util_Rbtree_Node_S(Structure):
pass _Util_Rbtree_Node_S._fields_ = [('key',c_long),
('parent',POINTER(_Util_Rbtree_Node_S)),
('right',POINTER(_Util_Rbtree_Node_S)),
('left',POINTER(_Util_Rbtree_Node_S)),
('color',c_int),
('data',c_void_p)] class _Util_Rbtree_S(Structure):
_fields_ = [('root',POINTER(_Util_Rbtree_Node_S)),
('null',_Util_Rbtree_Node_S),
('size',c_uint)] class _ConHash_S(Structure):
_fields_ = [('vnode_tree',_Util_Rbtree_S),
('ivnodes',c_uint),
('cb_hashfunc',CFUNCTYPE(c_long,c_char_p))] class _Node_S(Structure):
_fields_ = [('iden',c_char * 64),
('replicas',c_uint),
('flag',c_uint),] class ConHash:
def __init__(self):
try:
conhash_lib_path = find_library('conhash')
if not conhash_lib_path:
print 'cannot find libconhash.so'
return self.__conhash_lib_path = conhash_lib_path
self.__conhash_lib = cdll.LoadLibrary(self.__conhash_lib_path) self.__conhash_lib.conhash_init.restype = POINTER(_ConHash_S)
self.__conhash_lib.conhash_lookup.restype = POINTER(_Node_S) self.conhash_init = self.__conhash_lib.conhash_init
self.conhash_fini = self.__conhash_lib.conhash_fini
self.conhash_set_node = self.__conhash_lib.conhash_set_node
self.conhash_add_node = self.__conhash_lib.conhash_add_node
self.conhash_del_node = self.__conhash_lib.conhash_del_node
#self.conhash_update_node = self.__conhash_lib.conhash_update_node
self.conhash_lookup = self.__conhash_lib.conhash_lookup
self.conhash_md5_digest = self.__conhash_lib.conhash_md5_digest
self.conhash_get_vnodes_num = self.__conhash_lib.conhash_get_vnodes_num
self.conhash_get_vnodes = self.__conhash_lib.conhash_get_vnodes
except Exception, ex:
print ex '''try:
conhash_lib_path = find_library('conhash')
ConHash = cdll.LoadLibrary(conhash_lib_path)
ConHash.conhash_init.restype = POINTER(_ConHash_S)
ConHash.conhash_lookup.restype = POINTER(_Node_S)
except Exception, ex:
print ex
''' node1 = _Node_S()
node2 = _Node_S()
node3 = _Node_S()
node4 = _Node_S()
node5 = _Node_S()
node6 = _Node_S()
node7 = _Node_S()
node8 = _Node_S()
node9 = _Node_S()
node10 = _Node_S() #ConHash.conhash_init.restype = POINTER(_ConHash_S)
conhash_inst = ConHash()
conhash = conhash_inst.conhash_init(None)
conhash_inst.conhash_set_node(byref(node2), "192.168.79.101:6380\0", 100)
conhash_inst.conhash_set_node(byref(node3), "192.168.79.101:6381\0", 100)
conhash_inst.conhash_set_node(byref(node4), "192.168.79.101:6382\0", 100)
conhash_inst.conhash_set_node(byref(node5), "192.168.79.101:6383\0", 100)
conhash_inst.conhash_set_node(byref(node1), "192.168.79.101:6384\0", 100)
conhash_inst.conhash_set_node(byref(node6), "192.168.79.101:6385\0", 100)
conhash_inst.conhash_set_node(byref(node7), "192.168.79.101:6386\0", 100)
conhash_inst.conhash_set_node(byref(node8), "192.168.79.101:6387\0", 100)
conhash_inst.conhash_set_node(byref(node9), "192.168.79.101:6388\0", 100)
conhash_inst.conhash_set_node(byref(node10), "192.168.79.101:6389\0", 100)
conhash_inst.conhash_add_node(conhash, byref(node1))
conhash_inst.conhash_add_node(conhash, byref(node2))
conhash_inst.conhash_add_node(conhash, byref(node3))
conhash_inst.conhash_add_node(conhash, byref(node4))
conhash_inst.conhash_add_node(conhash, byref(node5))
conhash_inst.conhash_add_node(conhash, byref(node6))
conhash_inst.conhash_add_node(conhash, byref(node7))
conhash_inst.conhash_add_node(conhash, byref(node8))
conhash_inst.conhash_add_node(conhash, byref(node9))
conhash_inst.conhash_add_node(conhash, byref(node10)) while True:
print 'input key:',
key = raw_input()
node11 = conhash_inst.conhash_lookup(conhash, key)
print (key, node11.contents.iden)
参考:
http://www.codeproject.com/Articles/56138/Consistent-hashing
http://docs.python.org/2/library/ctypes.html
python -- 一致性Hash的更多相关文章
- tornado--SESSION框架,一致性hash,分布式存储
预备知识 tornado框架session要自己写 cookie存储在客户端浏览器上,session数据放在服务器上 session依赖cookie 扩展tornado,返回请求前自定义session ...
- [py]一致性hash原理
1,可变,不可变 python中值得是引用地址是否变化. 2.可hash 生命周期里不可变得值都可hash 3.python中内置数据结构特点 有序不可变 有序可变 无序可变 无序不可变 5.一致性h ...
- 01--是时候让我们谈谈一致性hash了
--------------------- 假如你有图中三个盒子,我们有代号为 1,4,5,12 这四样东西 那根据代号作为主键,将东西放到盒子了,该如何放置? 我们可以对代号取模 1 mod 3 = ...
- 对一致性Hash算法,Java代码实现的深入研究
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...
- 转载自lanceyan: 一致性hash和solr千万级数据分布式搜索引擎中的应用
一致性hash和solr千万级数据分布式搜索引擎中的应用 互联网创业中大部分人都是草根创业,这个时候没有强劲的服务器,也没有钱去买很昂贵的海量数据库.在这样严峻的条件下,一批又一批的创业者从创业中获得 ...
- 一致性hash算法详解
转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...
- 探索c#之一致性Hash详解
阅读目录: 使用场景 算法原理 虚拟节点 代码示例 使用场景 以Redis为例,当系统需要缓存的内容超过单机内存大小时,例如要缓存100G数据,单机内存仅有16G时.这时候就需要考虑进行缓存数据分片, ...
- 一致性hash算法简介
一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简单哈希 ...
- 分布式缓存技术memcached学习(四)—— 一致性hash算法原理
分布式一致性hash算法简介 当你看到“分布式一致性hash算法”这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前,我们先来了解一下这几 ...
随机推荐
- 【Search a 2D Matrix】cpp
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- 【Largest Rectangle in Histogram】cpp
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- Windows下配置使用MemCached
工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manager win下的Mem ...
- 操作集合的工具类Collections
1 操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...
- 【mysql5.6】连接vs2010
参考这篇博客:http://www.tuicool.com/articles/mUZNne 配置:vs2010项目属性里面配置包含目录和库目录. 包含目录:C:\Program Files\MySQL ...
- iOS正则匹配手机号
#pragma 正则匹配手机号 + (BOOL)validateMobile:(NSString *)mobileNum { /** * 手机号码 * 移动:134[0-8 ...
- POJ 2013
#include <iostream> #include <string> #define MAXN 20 using namespace std; string _m[MAX ...
- java 关于mysql
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after co ...
- CodeIgniter API
http://apigen.juzna.cz/doc/EllisLab/CodeIgniter/tree.html Classes CI_Benchmark CI_Calendar CI_Cart C ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...