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算法原理之前,我们先来了解一下这几 ...
随机推荐
- netty 入门
先啰嗦两句,使用 netty 来搭建服务器程序,可以发现相比于传统的 nio 程序, netty 的代码更加简洁,开发难度更低,扩展性也很好,非常适合作为基础通信框架. 下面上代码: Server p ...
- Matlab动态数组实现
clear all; clc; a = []; %不是null,也不能什么都不是 for i=1:10 a = [a i]; end
- [geeksforgeeks] Count the number of occurrences in a sorted array
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...
- Base64编解码Android和ios的例子,补充JNI中的例子
1.在Android中java层提供了工具类:android.util.Base64; 里面都是静态方法,方便直接使用: 使用方法如下: // Base64 编码: byte [] encode = ...
- Sqli-labs less 57
Less-57 从代码中看到对id进行了 " " 的处理,所以此处我们构造的payload要进行 "" 的处理,示例payload: http://127.0. ...
- Sqli-labs less 59
Less-59 与less58一致,直接给出一个示例payload: http://127.0.0.1/sqli-labs/Less-59/?id=-1 union select extractval ...
- Docker初识
<Docker--从入门到实践>是Docker技术的入门教程,学习时长两天,现整理关键点如下: 1. 什么是Docker? 轻量级操作系统虚拟化解决方案:Go语言实现:下图很好地说明了Do ...
- hadoop配置错误
经过上一周的郁闷期(拖延症引发的郁闷),今天终于开始步入正轨了.今天主要是解决hadoop配置的错误以及网络时断时续的问题. 首先说明一下之前按照这篇文章的方法配置完全没有问题,但是等我配置好了发现h ...
- UVA 11795
B Mega Man’s Missions Input Standard Input Output Standard Output Mega Man is off to save the world ...
- 【hadoop】有参考价值的博客整理
好文章的网址: hadoop shuffle机制中针对中间数据的排序过程详解(源代码级) Hadoop mapreduce原理学习 与 Hadoop 对比,如何看待 Spark 技术? 深入理解Had ...