PHP 一致性Hash
一致性HASH
好久没有写文章了,最近忙着公司的事情,也一拖再拖。这篇一致性hash是很久之前就有的一篇算法,记录一下,这周写个基于该算法的Redis中间件。
HASH算法的精髓就在于打散原本杂乱无序的一堆节点,并排序,同时使之首尾相连,形成闭环,总有一个节点是目标节点,最坏情况下是又回到第一个节点
- 这个算法有性能问题,因为PHP是解释性语言,每次查找一个key都初始化这个环,最好的办法的把这个方法用在daemon进程里,使之缓存起来,就不必每次都执行一次
<?php
namespace WMRedis;
use RedisException;
/**
* 一致性hash
*
* 网上摘抄的一段代码
*/
class ConsistenHash
{
/**
* 虚拟节点数
*
* @var int
*/
private $_replicas = 64;
/**
* HASH算法对象
* @var object hasher
*/
private $_hasher;
/**
* 当前物理节点数
* @var int
*/
private $_targetCount = 0;
/**
* 虚拟节点到物理节点映射
* @var array { position => target, ... }
*/
private $_positionToTarget = array();
/**
* 物理节点到虚拟节点映射
* @var array { target => [ position, position, ... ], ... }
*/
private $_targetToPositions = array();
/**
* 虚拟节点排序标志位
* @var boolean
*/
private $_positionToTargetSorted = false;
/**
* 构造函数
* @param object $hasher hasher
* @param int $replicas Amount of positions to hash each target to.
*/
public function __construct($hash = 'md5',$replicas = 0)
{
$this->_hasher = strcmp(strtolower($hash),'crc32') === 0 ? new Crc32Hasher() : new Md5Hasher();
$this->_replicas = !$replicas ? $replicas : $this->_replicas;
}
/**
* 添加物理节点
* @param string $target
* @chainable
*/
public function addTarget($target)
{
if (isset($this->_targetToPositions[$target]))
{
throw new RedisException("Target '$target' already exists.");
}
$this->_targetToPositions[$target] = array();
// 打散
for ($i = 0; $i < $this->_replicas; $i++)
{
$position = $this->_hasher->hash($target . $i);
$this->_positionToTarget[$position] = $target;
$this->_targetToPositions[$target][]= $position;
}
$this->_positionToTargetSorted = false;
$this->_targetCount++;
return $this;
}
/**
* 批量添加节点
* @param array $targets
* @chainable
*/
public function addTargets($targets)
{
foreach ($targets as $target)
{
$this->addTarget($target);
}
return $this;
}
/**
* 删除节点
* @param string $target
* @chainable
*/
public function removeTarget($target)
{
if (!isset($this->_targetToPositions[$target]))
{
throw new RedisException("Target '$target' does not exist.");
}
foreach ($this->_targetToPositions[$target] as $position)
{
unset($this->_positionToTarget[$position]);
}
unset($this->_targetToPositions[$target]);
$this->_targetCount--;
return $this;
}
/**
* 返回若有物理节点
* @return array
*/
public function getAllTargets()
{
return array_keys($this->_targetToPositions);
}
/**
* 找到资源所在物理节点
* @param string $resource
* @return string
*/
public function lookup($resource)
{
$targets = $this->lookupList($resource, 1);
if (empty($targets)) throw new RedisException('No targets exist');
return $targets[0];
}
/**
* 需要多个物理节点
*
* @param string $resource
* @param int $requestedCount 节点个数
* @return array
*/
protected function lookupList($resource, $requestedCount)
{
if (!$requestedCount) {
throw new RedisException('Invalid count requested');
}
// 没有节点资源
if (empty($this->_positionToTarget)) {
return array();
}
if ($this->_targetCount == 1) {
return array(array_pop($this->_positionToTarget));
}
$resourcePosition = $this->_hasher->hash($resource);
$results = array();
$collect = false;
$this->_sortPositionTargets();
// 开始搜索
foreach ($this->_positionToTarget as $key => $value)
{
if (!$collect && (string)$key > (string)$resourcePosition)
{
// 找到第一个匹配节点
$collect = true;
}
if ($collect && !in_array($value, $results))
{
$results []= $value;
}
if (count($results) == $requestedCount || count($results) == $this->_targetCount)
{
return $results;
}
}
// 还没有找到足够的节点,则重新开始
foreach ($this->_positionToTarget as $key => $value)
{
if (!in_array($value, $results))
{
$results []= $value;
}
// 已找到足够节点,或所有节点已全部遍历
if (count($results) == $requestedCount || count($results) == $this->_targetCount)
{
return $results;
}
}
// 此时的结果是已遍历完仍然没有足够的节点数
return $results;
}
/**
* 降序排列虚拟节点
*/
private function _sortPositionTargets()
{
// sort by key (position) if not already
if (!$this->_positionToTargetSorted)
{
ksort($this->_positionToTarget, SORT_REGULAR);
$this->_positionToTargetSorted = true;
}
}
}
/**
* Crc32
*/
class Crc32Hasher implements hasher
{
public function hash($string)
{
return (string)hash('crc32', $string);
}
}
/**
* Md5
*/
class Md5Hasher implements hasher
{
public function hash($string)
{
return (string)substr(md5($string), 0, 8);
}
}
/**
* HASH因子接口
*/
interface hasher
{
public function hash($string);
}
PHP 一致性Hash的更多相关文章
- 对一致性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算法原理之前,我们先来了解一下这几 ...
- 关于Memcached一致性hash的探究
参考文章 http://blog.chinaunix.net/uid-20498361-id-4303232.html http://blog.csdn.net/kongqz/article/deta ...
- 一致性 hash 算法( consistent hashing )a
一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...
- Ceph剖析:数据分布之CRUSH算法与一致性Hash
作者:吴香伟 发表于 2014/09/05 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 数据分布是分布式存储系统的一个重要部分,数据分布算法至少要考虑以下三个 ...
- 一致性Hash算法
from wikipedia 一致哈希 历史 1997年由MIT的Karger等在一篇学术论文中提出如何将“一致性Hash”应用于用户易变的分布式Web服务中.也可用于实现健壮缓存来减少大型Web应用 ...
随机推荐
- 华为上机试题(java)
一.题目描述:通过键盘输入一串小写字母(a~z)组成的字符串.请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉.比如字符串“abacacde”过滤结果为“abcde”. ...
- js进阶 11-2 jquery属性如何操作
js进阶 11-2 jquery属性如何操作 一.总结 一句话总结:jquery中的属性用attr方法表示.jquery中都是方法. 1.jquery中的属性的增删改查操作? 只需要两个方法, at ...
- 【u213&&t037】修剪花卉
Time Limit: 1 second Memory Limit: 128 MB [问题描述] ZZ对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题. 一天他早晨骑车去上 ...
- Tools:downloading and Building EDK II工具篇:安装/使用EDKII源代码获取/编译工具[2.3]
Tools:Installing and using the Required Tools for downloading and Building EDK II工具篇:安装/使用EDKII源代码获取 ...
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- WPF中自动增加行(动画)的TextBox
原文:WPF中自动增加行(动画)的TextBox WPF中自动增加行(动画)的TextBox WPF中的Textbox控件是可以自动换行的,只要设置TextWrapping属性为"Wrap& ...
- 关系型数据库工作原理-快速缓存(翻译自Coding-Geek文章)
本文翻译自Coding-Geek文章:< How does a relational database work>. 原文链接:http://coding-geek.com/how-dat ...
- webtool小问题
webtool小问题 erlang的观察工具如crashdump,appmon,cover等工具有二种不同的界面:gs(wx)和web.这些tool都遵循一定的接口,用户可以自行扩展这些功能,使的能够 ...
- HDU 1502 - dp + 压位
传送门 题目大意: 3*n的字符串,A.B.C分别有n个,w(X)代表X字母出现的次数,要求该字符串的所有前缀中w(A) >= w(B) >= w(C),问合法方案数有多少. 题目分析: ...
- 【BZOJ 1014】 [JSOI2008]火星人prefix
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 让你在线查询最长公共前缀. 支持单节点修改; 插入操作; [题解] / ...