今天对magent进行代理测试
memagent(http://code.google.com/p/memagent/)是一个memcache proxy,它提供的功能及特点有:
1、和每个memcache server保持多个长连接,效果是减少memcache server保持的连接数量及创建销毁连接的开销。不过,memcache本身就支持大并发连接,这个功能也就没什么特别的说道。
2、支持memcache的binary协议命令,实现请求的转发。
3、和memcache一样,基于libevent的事件驱动来处理IO。
4、支持ketama 的一致性hash算法。
5、支持memcache backup集群,当memcache集群有机器挂了,memagent会将get请求转向memcache backup集群。这个功能对于cache的稳定性要求高的场景下会有用武之地。
就提供的功能而言,memagent是个很简单的东西。对于较大的memcache集群,可以考虑搭一套memagent作为proxy使用。
Magent 代理的安装
安装magent到/usr/local/下
cd /usr/local
mkdir magent
解压
[root@localhost tmp]# cd /usr/local/magent
[root@localhost magent]# ls
magent-0.6.tar.gz
[root@localhost magent]# tar zxvf magent-0.6.tar.gz
ketama.c
ketama.h
magent.c
Makefile
安装
[root@localhost magent]# /sbin/ldconfig
[root@localhost magent]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost magent]# make
gcc -Wall -g -O2 -I/usr/local/include -c -o magent.o magent.c
gcc -Wall -g -O2 -I/usr/local/include -c -o ketama.o ketama.c
gcc -Wall -g -O2 -I/usr/local/include -o magent magent.o ketama.o -levent -lm -lm -L/usr/local/lib
配置memcached的服务
[root@localhost /]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11212
[root@localhost /]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11213
[root@localhost /]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11214
启动memagent服务
[root@localhost magent]# ./magent -u root -n 4096 -l 192.168.1.118 -p 12000 -s 192.168.1.118:11212 -s 192.168.1.118:11213 -b 192.168.1.118:11214
Magent 的命令参数
-h this message
-u uid
-g gid
-p port, default is 11211. (0 to disable tcp support)
-s ip:port, set memcached server ip and port
-b ip:port, set backup memcached server ip and port
-l ip, local bind ip address, default is 0.0.0.0
-n number, set max connections, default is 4096
-D don't go to background
-k use ketama key allocation algorithm
-f file, unix socket path to listen on. default is off
-i number, max keep alive connections for one memcached server, default is 20
-v verbose
1、 分别在11212、11213、11214端口启动3个Memcached进程,在12000端口开启magent代理程序;
2、 11212、11213端口为主Memcached,11214端口为备份Memcached;
3、 连接上12000的magent,set key1和set key2,根据哈希算法,key1被写入11213和11214端口的Memcached,key2被写入11213和11214端口的Memcached;
4、 当11212、11213端口的Memcached死掉,连接到12000端口的magent取数据,数据会从11214端口的Memcached取出;
5、 当11212、11213端口的Memcached重启复活,连接到12000端口,magent会从11212或11213端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11214端口的Memcached还有数据(此问题尚待改进)。
整个测试流程
[root@localhost ~]# telnet 192.168.1.118 12000
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
stats
memcached agent v0.6
matrix 1 -> 192.168.1.118:11212, pool size 0
matrix 2 -> 192.168.1.118:11213, pool size 1
END
set key1 0 0 5
hello
STORED
set key2 0 0 5
world
STORED
quit
Connection closed by foreign host.
[root@localhost ~]# telnet 192.168.1.118 11212
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 5
world
END
quit
Connection closed by foreign host.
[root@localhost ~]# telnet 192.168.1.118 11213
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
get key2
END
quit
Connection closed by foreign host.
[root@localhost ~]# telnet 192.168.1.118 11214
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
get key2
VALUE key2 0 5
world
END
quit
Connection closed by foreign host.
模拟11212、11213端口的memcache死掉
[root@localhost ~]# ps -ef | grep memcached
root 5126 1 0 19:14 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.1.118 -p 11212
root 5134 1 0 19:14 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.1.118 -p 11213
root 5142 1 0 19:14 ? 00:00:00 memcached -m 1 -u root -d -l 192.168.1.118 -p 11214
root 5976 5526 0 20:11 pts/1 00:00:00 grep memcached
[root@localhost ~]# kill -9 5126
[root@localhost ~]# kill -9 5134
[root@localhost ~]# telnet 192.168.1.118 12000
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
VALUE key1 0 5
hello
END
get key2
VALUE key2 0 5
world
END
quit
Connection closed by foreign host.
模拟11212、11213端口的memcache重启复活
[root@localhost ~]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11212
[root@localhost ~]# memcached -m 1 -u root -d -l 192.168.1.118 -p 11213
[root@localhost ~]# telnet 192.168.1.118 12000
Trying 192.168.1.118...
Connected to 192.168.1.118 (192.168.1.118).
Escape character is '^]'.
get key1
END
get key2
END
quit
Connection closed by foreign host.