1. 概述
coconut 是一款应用缓存服务器,主要用于场景化的缓存服务。coconut 目前提供了两种场景模式:全局序列号发生器、全局额度管理器,可成为分布式、集群化系统架构中高性能独立功能部件。
2. 全局序列号发生器
2.1. 序列号格式
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
反解出来包含如下信息
2.2. 服务接口
coconut 的全局序列号发生器提供了 2 个 HTTP 接口:
2.2.1. 获取序列号
方法 : GET
URL : http://(domain|ip):[port]/fetch
返回值 : 如果调用成功,返回 HTTP 状态码 200,HTTP 体中回送一个有序增长的全局唯一的序列号,如 aR2011o_cWG00002;如果发生系统级错误,返回 HTTP 状态码非 200
备注 : 可利用备注区区分业务类型;可利用服务器编号部署序列号发生器集群
2.2.2. 反解序列号
方法 : GET
URL : http://(domain|ip):[port]/explain?sequence=(序列号)
返回值 : 如果调用成功,返回 HTTP 状态码 200,HTTP 体中回送反解文本;如果发生系统级错误,返回 HTTP 状态码非 200
备注 : 反解文本格式"reserve: (保留值) server_no: (服务器编号) secondstamp: (1970 年至今秒戳)((人可阅读的日期时间格式)) serial_no: (序号)"
2.3. 场景示例
2.3.1. 启动服务
$ coconut -M SEQUENCE -l 127.0.0.1 -p 9527 -c 1 --loglevel-warn --reserve 2 --server-no 1
复制代码
2.3.2. 获取序列号
$ curl http://127.0.0.1:9527/fetchaR2011pfizz00001
复制代码
2.3.3. 反解序列号
$ 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
复制代码
2.3.4. 停止服务
$ ps -ef | grep -w coconut | awk '{if($3==1)print $2}' | xargs kill
复制代码
2.4. 性能压测
2.4.1. 获取序列号(短连接)
$ 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 requestsCompleted 20000 requestsCompleted 30000 requestsCompleted 40000 requestsCompleted 50000 requestsCompleted 60000 requestsCompleted 70000 requestsCompleted 80000 requestsCompleted 90000 requestsCompleted 100000 requestsFinished 100000 requests
Server Software: Server Hostname: 127.0.0.1Server Port: 9527Document Path: /fetchDocument Length: 19 bytes
Concurrency Level: 100Time taken for tests: 2.439 secondsComplete requests: 100000Failed requests: 0Write errors: 0Total transferred: 5800000 bytesHTML transferred: 1900000 bytesRequests 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 maxConnect: 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)
复制代码
3. 全局额度管理器
全局额度管理器提供高性能无锁接口对额度、库存等高频热点对象做查询额度、申请额度、撤销流水、补充额度、扣减额度、清空额度等处理。
3.1. 使用说明
命令行指定额度、导出结果流水文件名启动 coconut,coconut 对外提供 HTTP 接口,客户端可长/短连接发送指令给 coconut 以操作额度。期间还可以补充、扣减甚至清空额度。当额度为 0 时自动导出结果流水文件,可能会重复导出覆盖导出文件。
导出结果流水文件格式为每行一条流水,白字符分割为两列:申请流水号、申请额度,如果某流水被撤销则再加一列:撤销流水号。
3.2. 服务接口
coconut 的全局额度管理器提供了 6 个 HTTP 接口:
3.2.1. 查询额度
3.2.2. 申请额度
返回值 : 如果调用成功,返回 HTTP 状态码 200,HTTP 体中回送"(申请流水号) (剩余额度值)",如果额度已空或额度不够则返回"0",如果发生参数错误则返回"-1";如果发生系统级错误,返回 HTTP 状态码非 200
3.2.3. 撤销流水
3.2.4. 补充额度
3.2.5. 扣减额度
3.2.6. 清空额度
3.3. 场景示例
3.3.1. 启动服务
$ 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
复制代码
3.3.2. 查询额度
$ curl http://127.0.0.1:9527/query1000000
复制代码
3.3.3. 申请额度
$ 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
复制代码
3.3.4. 撤销流水
$ curl http://127.0.0.1:9527/cancel?jnlsno=46 999989
复制代码
3.3.5. 补充额度
$ curl http://127.0.0.1:9527/increase?amt=10000001000000
复制代码
3.3.6. 扣减额度
$ curl http://127.0.0.1:9527/decrease?amt=100000900000
复制代码
3.3.7. 清空额度
$ curl http://127.0.0.1:9527/empty0
复制代码
3.3.8. 查看导出的申请流水文件
$ 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
复制代码
3.4. 性能压测
3.4.1. 申请额度(长连接)
$ 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 requestsCompleted 200000 requestsCompleted 300000 requestsCompleted 400000 requestsCompleted 500000 requestsCompleted 600000 requestsCompleted 700000 requestsCompleted 800000 requestsCompleted 900000 requestsCompleted 1000000 requestsFinished 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 secondsComplete requests: 1000000Failed requests: 999982 (Connect: 0, Receive: 0, Length: 999982, Exceptions: 0)Write errors: 0Keep-Alive requests: 1000000Total transferred: 79777768 bytesHTML transferred: 16777786 bytesRequests 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 maxConnect: 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
复制代码
4. 附录 A.启动命令行参数
不带参数的执行 coconut 会显示所有参数提示
$ coconutcoconut v0.0.7.0Copyright by calvin 2017USAGE : 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)
复制代码
-M ( SEQUENCE | LIMITAMT ) : 场景模式 SEQUENCE 全局序列号发生器;LIMITAMT 全局额度管理器
-p (listen_port) : 侦听端口
-c (processor_count) : 并发进程数量。全局额度管理器模式目前只支持单并发
–loglevel-(debug|info|warn|error|fatal) : 日志等级,默认 warn 等级。日志文件输出到$HOME/log/coconut.log
全局序列号发生器 场景模式
全局额度管理器 场景模式
5. 最后
coconut 使用到了作者的其它开源项目 iLOG3,fasterhttp,tcpdaemon。
coconut 全套源码托管在 开源中国码云 和 github,如有疑问或建议可以通过 网易邮箱 和 GMAIL 邮箱 联系到作者。
作者介绍:
厉华,出生在杭州,上学在杭州,工作在杭州。求学期间爱好散文诗歌,作品多次在学校活动中朗诵,但语文成绩不咋地。1999 年考入浙江工业大学机械工程及自动化专业,大学期间获得的最高荣誉是 2000 年计算机二级全院第一、2001 年全国大学生数据建模国家二等奖、若干年度奖学金等,奖金给自己攒了台 PC。2003 年参加工作后一直从事软件核心研发,目前在杭州银行信息技术部负责基础架构。
评论