您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
memcached-一致性哈希实验课(1)
发布时间:2017-11-22 17:28:04编辑:雪饮阅读()
实验前的准备工作:
编写memcached配置文件如:
config.php:
<?php
$_memserv=array();
$_memserv['A']=array('host'=>'127.0.0.1','port'=>'11211');
$_memserv['B']=array('host'=>'127.0.0.1','port'=>'11212');
$_memserv['C']=array('host'=>'127.0.0.1','port'=>'11213');
$_memserv['D']=array('host'=>'127.0.0.1','port'=>'11214');
$_memserv['E']=array('host'=>'127.0.0.1','port'=>'11215');
编写一致性哈希分布式算法和分布式取模算法类如:
hash.php:
<?php
interface hash{
public function _hash($str);
}
interface distribution{
public function lookup($key);
}
class Moder implements hash,distribution{
public $_nodes=array();//节点数组
public $_cnt=0;
public function _hash($str){
//把字符串转成32位无符号整数
return sprintf('%u',crc32($str));
}
public function addNode($node){
if(in_array($node,$this->_nodes)){
return true;
}
$this->_nodes[]=$node;
$this->_cnt+=1;
return true;
}
public function delNode($node){
if(!in_array($node,$this->_nodes)){
return true;
}
$key=array_search($node,$this->_nodes);
unset($this->_nodes[$key]);
//节点减1
$this->_cnt -=1;
return true;
}
public function lookup($key){
$fval=floatval($this->_hash($key));
$key=fmod($fval,$this->_cnt);
return $this->_nodes[$key];
}
}
class Consistent implements hash,distribution{
protected $_nodes=array();
protected $_postion=array();
protected $_mul=64;
public function _hash($str){
//把字符串转成32位无符号整数
return sprintf('%u',crc32($str));
}
//核心功能
public function lookup($key){
$point=$this->_hash($key);
//先取圆环上最小的一个节点,当成结果
$node=current($this->_postion);
foreach($this->_postion as $k=>$v){
if($point <= $k){
$node=$v;
break;
}
}
return $node;
}
public function addNode($node){
for($i=0;$i<$this->_mul;$i++){
$this->_postion[$this->_hash($node.'_'.$i)]=$node;
}
$this->_sortPos();
}
//循环所有的虚拟节点,谁的值==指定的真实节点,就把他删掉
public function delNode($node){
foreach($this->_postion as $k=>$v){
if($v==$node){
unset($this->_postion[$k]);
}
}
}
protected function _sortPos(){
ksort($this->_postion,SORT_REGULAR);
}
//调试用的函数
public function printPos(){
print_r($this->_postion);
}
}
?>
编写为memcached填充测试数据的脚本如:
initdata.php:
<?php
require ('./config.php');
/*
为歌memcached节点,各充入1000条数据
步骤:引入配置文件,循环各节点,连接并填入数据
*/
$mem=new Memcache();
foreach ($_memserv as $k=>$s){
//连接memcached,超时时间设置为2秒
$mem->connect($s['host'],$s['port'],2);
for($i=1;$i<=1000;$i++){
//添加key,标记为0,永不失效
$mem->add('key'.$i,'value'.$i,0,0);
}
echo $k,'号服务器数据初始化完毕<br/>';
}
编写统计命中率的脚本如:
load.php:
<?php
require('./config.php');
$mem=new memcache();
$gets=0;
$hits=0;
foreach($_memserv as $k=>$s){
$mem->connect($s['host'],$s['port'],2);
$res=$mem->getstats();
$gets += $res['cmd_get'];
$hits += $res['get_hits'];
}
$rate=1;
if($gets>0){
$rate=$hits/$gets;
}
echo $rate;
编写实时统计命中率的html界面如:
index.html:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="./js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function () {
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
y=parseFloat($.ajax({url:'load.php',async:false}).responseText);
//y=$.ajax({url:'load.php',async:false}).responseText;
console.log(y);
series.addPoint([x, y], true, true);
}, 2000);
}
}
},
title: {
text: 'Memcached his rate'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
});
});
</script>
</head>
<body>
<script src="./js/highcharts.js"></script>
<script src="./js/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
</html>
接下来需要开启5台memcached做为测试的memcached服务器,为了方便,就以不同的端口来模拟多个实体memcached服务器。
用命令"memcached -m 4 -p 11211 -vvv"则生成5个memcached服务端窗口。
然后执行上面编写的initdata脚本为这5个memcached服务器填充测试数据,如果memcached无异常,则脚本执行完毕后这5个窗口中都能看到好多add命令的执行记录。
然后访问最后的index.html,如果没有成功了,其结果应该是这样的:
最后就一直时直线的状况了。
关键字词:memcached