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);
}
posted @ 2016-07-21 23:19  nice_cp  阅读(466)  评论(0编辑  收藏  举报