---
title: "Kyoto Cabinet（DBM） + Kyoto Tycoon（网络层）"
author: "wayne173"
date: "2012-04-12T19:49:49Z"
tags: []
categories: ["数据库"]
slug: "kyoto-cabinet-dbm-kyoto-tycoon"
draft: false
lastmod: "2012-04-17T13:49:20Z"
id: 1484723
url: "https://wayne173.iteye.com/blog/1484723"
---

一. 安装

前注：没使用最新版本，因为最新版本似乎存在环境依赖的bug，编译N多错误，很难通过。

(1)安装kyotocabinet
mkdir -p /data0/software/
cd /data0/software/
wget http://fallabs.com/kyotocabinet/pkg/kyotocabinet-1.2.43.tar.gz
tar xvzf kyotocabinet-1.2.43.tar.gz
cd kyotocabinet-1.2.43
./configure --prefix=/usr/local/webserver/kyotocabinet
make
make install
cd ../
(2)安装kyototycoon
cd /data0/software/
wget http://fallabs.com/kyototycoon/pkg/kyototycoon-0.9.33.tar.gz
tar xvzf kyototycoon-0.9.33.tar.gz
cd kyototycoon-0.9.33
./configure --prefix=/usr/local/webserver/kyototycoon
--with-kc=/usr/local/webserver/kyotocabinet/
make
make install
cd ../


二.使用kchashtest生成一亿条测试数据
(生成的测试数据key为8位数字，value为3位数字)

kchashtest order   -bnum 150000000 -msiz 2g -set /data0/tycoon/casket1.kch   100000000

kchashtest较影响性能的几个参数：
  -bnum 指定哈希表的桶数量。官方推荐是记录数的两倍或者更高。
  -msize 指定内存映射区域大小。
  -dfunit 设定一个值，当碎片数超过这个值系统就进行碎片整理。
  -dmn   以daemon方式启动。
  -th num : 指定线程数
生成测试数据后以ktserver模式启动(ktserver参数意义见文章尾)
ktserver -host 192.168.2.70 -port 1978 -tout 10 -log /data0/tycoon/ca.log -ls -dmn -pid /data0/tycoon/ktserver.pid /data0/tycoon/casket1.kch#opts=1#bnum=150000000#msiz=2g#dfunit=8

ktsever较影响性能的几个参数：
  -bnum 指定哈希表的桶数量。官方推荐是记录数的两倍或者更高。
  -msize 指定内存映射区域大小。
  -dfunit 设定一个值，当碎片数超过这个值系统就进行碎片整理。
  -dmn   以daemon方式启动。
  -th num : 指定线程数


三. 日常维护及使用
关闭ktserver
kill -TERM `cat /data0/tycoon/ktserver.pid `
Ktserver切割日志
mv -f /data0/tycoon/ktserver.log /data0/tycoon/ktserver.log.`date '+%Y%M%d%H%M%S'`
kill -HUP `cat /data0/tycoon/ktserver.pid`
应用
使用http 客户端
由于每个数据库操作都经由http 调用，你可以使用任何http 客户端，诸如curl 命令来操作
数据库。
例：
添加数据
curl "http://192.168.8.46:1978/rpc/set?key=japan&value=tokyo"
读取数据
curl "http://192.168.8.46:1978/rpc/get?key=japan"
删除数据
curl "http://192.168.8.46:1978/rpc/remove?key=japan"
除了上述RPC风格外还支持RESTfull风格
例:
添加数据
echo -n tokyo | curl -X PUT -T - "http://192.168.8.46:1978/japan"
读取数据
curl "http://192.168.8.46:1978/japan"
tokyo
删除数据
curl -X DELETE "http://192.168.8.46:1978/japan"


四. 主从模式的配置及功能性测试
注意事项
1.主库必须记录更新日志。
2.主库必须指定唯一的id 号。
-sid num : 指定服务器server id 号（当使用主辅模式时，每台ktserver 需要不同
的ID 号）
3.从库也必须记录更新日志，当主库宕机时，从库就变成主库。
4.从库必须指定唯一的server id 号
5.从库必须指定主库的端口号和地址。
6.从库必须指定复制时间戳文件。
下面的一个主从实例，主库端口192.168.2.70:1978,从库端口192.168.2.80:1978
首先创建一个主库实例

ktserver -dmn -host 192.168.2.70 -port 1978 -ulog /data0/tycoon/0001-ulog -sid 1  -rts /data0/tycoon/001.rts /data0/tycoon/casket1.kch#opts=1#bnum=150000000#msiz=2g#dfunit=8
(ulog 和数据库文件casket1.kch 都需要指定目录，否则将在当前目录生成,目录需要事先创建好。)

再创建一个从库实例
ktserver -dmn -host 192.1682.80 -port 1978 -ulog /data0/tycoon/0001-ulog -sid 2 -mhost 192.168.2.70 -mport 1978 -rts /data0/tycoon/001.rts 
/data0/tycoon/casket1.kch#opts=1#bnum=150000000#msiz=1g#dfunit=8

测试
向主库中添加数据
curl "http://192.168.2.70:1978/rpc/set?key=japan&value=tokyo"
读取主库
curl "http://192.168.2.70:1978/japan"
Tokyo
读取从库
curl "http://192.168.2.80:1978/japan" 
tokyo
删除主库数据
curl -X DELETE "http://192.168.2.70:1978/japan"
再次读取从库
curl "http://192.168.2.80:1978/japan" 
没有数据了。成功。


五. 双主模式的配置
Kyoto Tycoon 支持双主模式以提高高可用。
例:创建两个主库实例，分别叫A 和B。
首先创建A
ktserver -dmn -host 192.168.2.70 -port 1978 -ulog /data0/tycoon/0001-ulog -sid 1  -mhost 192.168.2.80 -mport 1978 -rts /data0/tycoon/001.rts  /data0/tycoon/casket1.kch#opts=1#bnum=150000000#msiz=11g#dfunit=8

然后创建B
ktserver -dmn -host 192.168.2.80 -port 1978 -ulog /data0/tycoon/0001-ulog -sid 2  -mhost 192.168.2.70 -mport 1978 -rts /data0/tycoon/001.rts  /data0/tycoon/casket1.kch#opts=1#bnum=150000000#msiz=11g#dfunit=8


添加数据
向A 中添加数据
curl "http://192.168.2.70:1978/rpc/set?key=one&value=first"
curl "http://192.168.2.70:1978/rpc/set?key=two&value=second"
向B 中添加数据
curl "http://192.168.2.80:1978/rpc/set?key=three&value=third"
curl "http://192.168.2.80:1978/rpc/set?key=four&value=fourth"
添加数据也可以使用ktremotemgr 实用程序
ktremotemgr set -host 192.168.2.70 -port 1978 one first
ktremotemgr set -host 192.168.2.70 -port 1978 two second
ktremotemgr set -host 192.168.2.80 -port 1978 three third
ktremotemgr set -host 192.168.2.80 -port 1978 four fourth
查看数据
$ ktremotemgr list -host 192.168.2.70 -port 1978 -pv
one first
two second
three third
four fourth
$ ktremotemgr list -host 192.168.2.80 -port 1978 -pv
one first
two second
three third
four fourth


六.并发访问测试和远程访问速度测试
1.并发访问测试。
并发测试工具使用webbench，最多可以模拟3万个并发连接去测试网站的负载能力，个人感觉要比Apache自带的ab压力测试工具好，安装使用也特别方便。具体安装参见：

http://blog.s135.com/post/288/
测试流程：
如下，由于一个节点模拟三万并发会造成客户端僵死，所以先使用一台客户端模拟1万个并发去测试一个kt节点，发现kt节点无任何错误和异常。
#webbench -c 10000 -t 10 http://192.168.2.70:1978/rpc/get?key=00000001
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.2.70:1978/rpc/get?key=00000001
10000 clients, running 10 sec.

Speed=1117350 pages/min, 3538104 bytes/sec.
Requests: 186225 susceed, 0 failed.
然后使用四台客户端每台模拟一万个并发去测试一个kt节点，发现kt节点扔无错误，但kt节点进程占用资源开始飙高。
得出结论，一个kt节点可应对四万以上并发连接。

2.网络远程访问速度测试
使用kt自带的ktremotetest进程远程读写测试：
# ktremotetest  bulk   -set -host 192.168.2.70 -port 1978 100000
opening the database:
time: 0.000
memory: 208896
time: 19.875
closing the database:
time: 0.000
ok
# ktremotetest  bulk   -get -host 192.168.2.70 -port 1978 100000
opening the database:
time: 0.000
memory: 208896
time: 17.707
closing the database:
time: 0.000
ok
经多次测试，得出结论，同局域网断其他主机访问kt节点读写都为每秒5000条记录左右。本机访问，每秒为1万条左右。


附录一kstserver启动参数
-host name : 指定需要绑定的服务器域名或IP 地址。默认绑定这台服务器上的所有IP
地址。
-port num : 指定需要绑定的端口号。默认端口号为1978
-tout num : 指定每个会话的超时时间（单位为秒）。默认永不超时。
-th num : 指定线程数。默认为8 个线程。
-log file: 输出日志信息到指定文件（这里指定文件名）。
-li : 日志记录级别---notice。
-ls : 日志记录级别---system。
-le : 日志记录级别---error。
-lz : 不记录日志.
-ulog dir : 指定同步日志文件存放路径（这里指定目录名）。
-ulim num : 指定每个同步日志文件的大小（例如128m）。
-uasi num : 指定数据同步到磁盘的时间间隔，该选项默认是关闭的。
-sid num : 指定服务器ID 号（当使用主辅模式时，每台ktserver 需要不同的ID 号）
-ord : opens the database as a reader.
-oat : opens the database with the auto transaction option.
-oas : opens the database with the auto synchronization option.
-onl : opens the database with the no locking option.
-otl : opens the database with the try locking option.
-onr : opens the database with the no auto repair option.
-asi num : 指定自动同步间隔，默认关闭。
-ash : 当自动同步的时候也同步到物理磁盘上
-bgs dir : specifies the path of the background snapshot directory. By default, it is disabled.
-bgsi num : specifies the interval of background snapshotting. By default, it is 180.
-bgsc str : 指定快照的压缩格式。支持的格式有"zlib","lzo",lzma"
-dmn : 以守护进程方式运行。
-pid file: 输出进程ID 到指定文件（这里指定文件名）。
-cmd dir : 指定外部指令的搜寻路径，默认是当前路径。
-scr file : 指定脚本文件
-mhost str: 指定主辅同步模式下，主服务器的域名或IP 地址。
-mport num : 指定主辅同步模式下，主服务器的端口号。
-rts file: 指定用来存放同步时间戳的文件名。
-riv num : 指定每次同步操作的毫秒时间间隔，默认是0.04 毫秒。
-plsv file : specifies the shared library file of a pluggable server.
-plex str : specifies the configuration expression of a pluggable server.
-pldb file : 指定插件库的动态链接库文件。



附录二常用ktremotemgr参数
1.打印当前数据库状态信息
ktremotemgr report   
2.插入数据
ktremotemgr set      
 key value
3.删除数据
ktremotemgr remove      key
4.查询数据
ktremotemgr get         key
5.列出所有key
ktremotemgr list        
  
6.根据key 删除多条记录
ktremotemgr removebulk       key1 key2
key3 ...
7.根据key 取出多条记录
ktremotemgr getbulk        key1 key2
key3 ...
8.批量添加key、value
ktremotemgr setbulk        key
value ...
9.模拟一个客户端复制并打印更新记录
ktremotemgr slave        

10.删除数据库中的所有记录
ktremotemgr clear    
11.设定复制配置
ktremotemgr tunerepl      

选项说明
-host str: 指定主机名或IP
-port num: 指定端口号
-tout num: 指定超时时间.
-bin: 使用二进制协议.
-mport num: 指定主库端口号.
-ts num: 指定已经读取日志的最大时间戳。“now”意为当前时间戳。
-iv num: 指定每次同步操作的毫秒时间间隔。
-db str: 指定数据库名称
-st: 打印详细信息。
-hard: 启动设备的物理同步
-cmd str: specifies an outer command for postprocessing.
-add: 运行添加操作
-app: 追加操作
-rep: 替代操作
-inci: 使整数自增
-incd: 使实数自增
-sx: 将输入数据转换成16进制字符串
-xt num: 指定截止时间
-px: 将输出数据转换成16进制字符串
-pt: 打印截止时间
-pz: does not append line feed at the end of the output.
-des: visits records in descending order.
-max num: specifies the maximum number of shown records.
-pv: 打印value 值.
-step num: 指定步骤的数字.
-sid num: 指定server ID .
-ux: fetches update logs of the specified server ID number only.
-uw: 等待更新.
-uf: 打印每次更新日志的状态.
-ur:移除老的更新日志文件.


附录三安装遇到的错误及使用中可能遇到的问题
1.kc和kt都不使用最新版本，因为最新版本似乎存在环境依赖的bug，编译N多错误，很难通过。
2.安装中可能遇到以下错误
# ./configure
checking Kyoto Cabinet by pkg-config... no
configure: error: required version of Kyoto Cabinet was not detected
原因：版本不匹配
我测试匹配的版本如下
kyotocabinet-1.2.43.tar.gz
kyototycoon-0.9.33.tar.gz
注意事项：如果安装kyotocabinet 使用--prefix 参数指定安装目录了，再安装kyototycoon 时
就需要加参数--with-kc
例如：
# ./configure --with-kc=/usr/local/webserver/kyotocabinet/
3.生成数据时，是一个非常耗费内存的过程，如果灌入上亿条记录，需要12G内存以上为好，不然会非常慢，甚至可能需要耗费几天的时间。而且，按目前的观察，即使现在某一台生成kch数据，再拷贝到其他节点，再启动kt进程的时候似乎还要重新加载一次数据，这个过程也是非常慢的。