coconut是一款应用缓存服务器,主要用于场景化的缓存服务。coconut目前提供了两种场景模式:全局序列号发生器、全局额度管理器,可成为分布式、集群化系统架构中高性能独立功能部件。
coconut生成的序列号为16个64进制可见字符组成,具体格式如下:
区号 | 区名 | 说明 |
---|---|---|
第一区 | 分区目录(index) | 2个六十四进制字符 共12个二进制位第一段3个二进制位表示保留区六十四进制字符个数第二段3个二进制位表示服务器编号区六十四进制字符个数第三段3个二进制位表示秒戳区六十四进制字符个数第四段3个二进制位表示序号区六十四进制字符个数 |
第二区 | 保留区(reserve) | 1个六十四进制字符 有6个二进制位可用 |
第三区 | 服务器编号区(server_no) | 2个六十四进制字符 可表示4096台发起器服务器 |
第四区 | 秒戳区(secondstamp) | 6个六十四进制字符 可表示2179年的秒戳 |
第五区 | 序号区(serial_no) | 5个六十四进制字符 序号区间[1,10亿] |
共16个六十四进制字符 |
(64进制字符集合:0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_)
如序列号:aR2011o_cWG00002
反解出来包含如下信息
coconut的全局序列号发生器提供了2个HTTP接口:
$ coconut -M SEQUENCE -l 127.0.0.1 -p 9527 -c 1 --loglevel-warn --reserve 2 --server-no 1
$ curl http://127.0.0.1:9527/fetchaR2011pfizz00001
$ curl http://127.0.0.1:9527/explain?sequence=aR2011pfizz00001reserve: 2 server_no: 1 secondstamp: 1497180387 (2017-06-11 19:26:27) serial_no: 1
$ ps -ef | grep -w coconut | awk '{if($3==1)print $2}' | xargs kill
$ ab -c 100 -n 100000 http://127.0.0.1:9527/fetchThis is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 127.0.0.1 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: 127.0.0.1Server Port: 9527Document Path: /fetch
Document Length: 19 bytes
Concurrency Level: 100Time taken for tests: 2.439 seconds
Complete requests: 100000Failed requests: 0Write errors: 0Total transferred: 5800000 bytes
HTML transferred: 1900000 bytes
Requests per second: 41008.25 [#/sec] (mean)Time per request: 2.439 [ms] (mean)
Time per request: 0.024 [ms] (mean, across all concurrent requests)
Transfer rate: 2322.73 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 29.2 0 1003Processing: 0 0 3.6 0 401Waiting: 0 0 3.6 0 401Total: 0 1 30.2 0 1403Percentage of the requests served within a certain time (ms) 50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 1
100% 1403 (longest request)
全局额度管理器提供高性能无锁接口对额度、库存等高频热点对象做查询额度、申请额度、撤销流水、补充额度、扣减额度、清空额度等处理。
命令行指定额度、导出结果流水文件名启动coconut,coconut对外提供HTTP接口,客户端可长/短连接发送指令给coconut以操作额度。期间还可以补充、扣减甚至清空额度。当额度为0时自动导出结果流水文件,可能会重复导出覆盖导出文件。
导出结果流水文件格式为每行一条流水,白字符分割为两列:申请流水号、申请额度,如果某流水被撤销则再加一列:撤销流水号。
coconut的全局额度管理器提供了6个HTTP接口:
$ coconut -M LIMITAMT -l 127.0.0.1 -p 9527 -c 1 --loglevel-warn --limit-amt 1000000 --export-jnls-amt-pathfilename $HOME/coconut_JNLSNO_AMT.txt
$ curl http://127.0.0.1:9527/query1000000
$ curl http://127.0.0.1:9527/apply?amt=11 999999$ curl http://127.0.0.1:9527/apply?amt=22 999997$ curl http://127.0.0.1:9527/apply?amt=33 999994$ curl http://127.0.0.1:9527/apply?amt=44 999990$ curl http://127.0.0.1:9527/apply?amt=55 999985
$ curl http://127.0.0.1:9527/cancel?jnlsno=46 999989
$ curl http://127.0.0.1:9527/increase?amt=10000001000000
$ curl http://127.0.0.1:9527/decrease?amt=100000900000
$ curl http://127.0.0.1:9527/empty0
$ ls -l $HOME/coconut_JNLSNO_AMT.txt
-rw-rw-r-- 1 calvin calvin 22 6月 11 19:47 coconut_JNLSNO_AMT.txt
$ cat $HOME/coconut_JNLSNO_AMT.txt1 12 23 34 4 65 5
$ curl http://127.0.0.1:9527/increase?amt=10000001000000$ ab -kc 100 -n 1000000 http://127.0.0.1:9527/apply?amt=1This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 127.0.0.1 (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests
Server Software:
Server Hostname: 127.0.0.1Server Port: 9527Document Path: /apply?amt=1Document Length: 12 bytes
Concurrency Level: 100Time taken for tests: 3.389 seconds
Complete requests: 1000000Failed requests: 999982
(Connect: 0, Receive: 0, Length: 999982, Exceptions: 0)
Write errors: 0Keep-Alive requests: 1000000Total transferred: 79777768 bytes
HTML transferred: 16777786 bytes
Requests per second: 295029.46 [#/sec] (mean)Time per request: 0.339 [ms] (mean)
Time per request: 0.003 [ms] (mean, across all concurrent requests)
Transfer rate: 22985.15 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 8Processing: 0 0 0.1 0 8Waiting: 0 0 0.1 0 3Total: 0 0 0.1 0 10Percentage of the requests served within a certain time (ms) 50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 1
99% 1
100% 10 (longest request)
停止服务
$ ps -ef | grep -w coconut | awk '{if($3==1)print $2}' | xargs kill
不带参数的执行coconut会显示所有参数提示
$ coconut
coconut v0.0.7.0
Copyright by calvin 2017
USAGE : coconut -M ( SEQUENCE | LIMITAMT ) [ -l (listen_ip) ] -p (listen_port) [ -c (processor_count) ] [ --loglevel-(debug|info|warn|error|fatal) ] [ --cpu-affinity (begin_mask) ]
global serial service :
--reserve (reserve) --server-no (server_no)
global limit-amt service :
--limit-amt (amt) --export-jnls-amt-pathfilename (pathfilename)
全局序列号发生器 场景模式
全局额度管理器 场景模式
coconut使用到了作者的其它开源项目iLOG3,fasterhttp,tcpdaemon。
coconut全套源码托管在 开源中国码云 和 github,如有疑问或建议可以通过 网易邮箱 和 GMAIL邮箱 联系到作者。
厉华,出生在杭州,上学在杭州,工作在杭州。求学期间爱好散文诗歌,作品多次在学校活动中朗诵,但语文成绩不咋地。1999年考入浙江工业大学机械工程及自动化专业,大学期间获得的最高荣誉是2000年计算机二级全院第一、2001年全国大学生数据建模国家二等奖、若干年度奖学金等,奖金给自己攒了台PC。2003年参加工作后一直从事软件核心研发,目前在杭州银行信息技术部负责基础架构。
——The End——