一致性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的更多相关文章

  1. 对一致性Hash算法,Java代码实现的深入研究

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

  2. 转载自lanceyan: 一致性hash和solr千万级数据分布式搜索引擎中的应用

    一致性hash和solr千万级数据分布式搜索引擎中的应用 互联网创业中大部分人都是草根创业,这个时候没有强劲的服务器,也没有钱去买很昂贵的海量数据库.在这样严峻的条件下,一批又一批的创业者从创业中获得 ...

  3. 一致性hash算法详解

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  4. 探索c#之一致性Hash详解

    阅读目录: 使用场景 算法原理 虚拟节点 代码示例 使用场景 以Redis为例,当系统需要缓存的内容超过单机内存大小时,例如要缓存100G数据,单机内存仅有16G时.这时候就需要考虑进行缓存数据分片, ...

  5. 一致性hash算法简介

    一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简单哈希 ...

  6. 分布式缓存技术memcached学习(四)—— 一致性hash算法原理

    分布式一致性hash算法简介 当你看到“分布式一致性hash算法”这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前,我们先来了解一下这几 ...

  7. 关于Memcached一致性hash的探究

    参考文章 http://blog.chinaunix.net/uid-20498361-id-4303232.html http://blog.csdn.net/kongqz/article/deta ...

  8. 一致性 hash 算法( consistent hashing )a

    一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...

  9. Ceph剖析:数据分布之CRUSH算法与一致性Hash

    作者:吴香伟 发表于 2014/09/05 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 数据分布是分布式存储系统的一个重要部分,数据分布算法至少要考虑以下三个 ...

  10. 一致性Hash算法

    from wikipedia 一致哈希 历史 1997年由MIT的Karger等在一篇学术论文中提出如何将“一致性Hash”应用于用户易变的分布式Web服务中.也可用于实现健壮缓存来减少大型Web应用 ...

随机推荐

  1. 新版Sublime text3注册码被移除的解决办法

    Sublime Text是风靡世界的文本编辑器,支持多种编程语言,启动时间短,打开文件速度快,插件丰富,让很多程序员爱不释手.但是,对于未注册的Sublime Text, 经常在保存的时候会弹出一个烦 ...

  2. 我的IT成长路——为梦想扬帆起航

    在持续了一个多月的雾霾之后,西安这座城市又看到了久违的阳光,好的天气预兆新梦想的开始.我的IT路从开始接触编程开始已经有5个年头了,从一个没有摸过计算机的农村男孩到现在学会几门编程语言的IT人,这段路 ...

  3. Redis Service

    https://raw.githubusercontent.com/MSOpenTech/redis/3.0/Windows%20Service%20Documentation.md

  4. 从Handler+Message+Looper源代码带你分析Android系统的消息处理机制

    PS一句:不得不说CSDN同步做的非常烂.还得我花了近1个小时恢复这篇博客. 引言 [转载请注明出处:http://blog.csdn.net/feiduclear_up CSDN 废墟的树] 作为A ...

  5. spring boot jar 部署linux服务器

    用命令启动spring boot 项目,一旦终端命令窗口关闭,项目也就关闭了,所以我们采用脚本的方式来运行jar 正常命令 java -jar xxx.jar 脚本启动,vim 创建 start.sh ...

  6. android之照相、相冊裁剪功能的实现过程

    今天无聊做了一些照相.相冊裁剪功能,希望能够帮到大家! 不多说了,贴代码实际一点: 首先是XML: <ImageButton android:id="@+id/imageButton1 ...

  7. react中对象扩展运算符使用问题

    看别人代码看到 render() { return <Modal {...this.props}> {this.props.children} </Modal> } 对其中的{ ...

  8. 【87.65%】【codeforces 731A】Night at the Museum

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 把搜狗输入法词库导入Google拼音输入法

    为PC端Google拼音输入法增加词库 为什么折腾词库 都在说百度.讯飞等输入法上传用户词库,为了安全建议大家使用google输入法之类,话说回来,要想使用智能联想功能是不是就得把你输入习惯放在他的里 ...

  10. 如何移除解决方案中的TFS版本控制

    1.清除(删除)项目下的所有版本控制文件,这些文件有:*.vssscc,*.vspscc删除这些版本控制文件比较简单,搜索这些后缀的文件,删除即可. 2.修改项目的解决方案文件:*.sln (1).先 ...