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”. ...
- 【u016】无序字母对
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个 ...
- [CSS] Re-order the appearance of grid items using the order property
As with flex items, we can set an order value on grid items. Let’s see how this affects the DOM and ...
- Windows下启动ActiveMq端口被占用的解决办法
cd /D E:\RuntimeSoft\apache-activemq-5.11.0\binactivemq.bat start结果提示:端口号被占用. Windows下查看端口号被占用开始--运行 ...
- [HTML5] Focus management using CSS, HTML, and JavaScript
Something important to consider when coding a web application is managing the user's focus. For keyb ...
- 通过getElementById来取得Form里的表单元素
<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>湖南易通 ...
- 窗体背景的绘制(Windows窗体每次都会重绘其窗体背景,所以我们可以通过拦截窗体重绘背景的消息(WM_ERASEBKGND),并自定义方法来实现重绘窗体背景)
核心思想:由于Windows窗体每次都会重绘其窗体背景,所以我们可以通过拦截窗体重绘背景的消息(WM_ERASEBKGND),并自定义方法来实现重绘窗体背景.通过TImage组件也可以实现,但是重写W ...
- 【codeforces 787C】Berzerk
[题目链接]:http://codeforces.com/contest/787/problem/C [题意] 给你怪物一开始所在的位置; 然后两人轮流操作; 可以选择让这个怪物前进自己的集合里面所拥 ...
- Lua转让C功能
在上一篇文章中(C调用lua函数)中.讲述了怎样用c语言调用lua函数,通常,A语言能调用B语言,反过来也是成立的.正如Java 与c语言之间使用JNI来互调.Lua与C也能够互调. 当lua调用c函 ...
- TensorFlow 学习(八)—— 梯度计算(gradient computation)
maxpooling 的 max 函数关于某变量的偏导也是分段的,关于它就是 1,不关于它就是 0: BP 是反向传播求关于参数的偏导,SGD 则是梯度更新,是优化算法: 1. 一个实例 relu = ...