﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-lxyfirst</title><link>http://www.cppblog.com/lxyfirst/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 10 Apr 2026 16:11:07 GMT</lastBuildDate><pubDate>Fri, 10 Apr 2026 16:11:07 GMT</pubDate><ttl>60</ttl><item><title>分布式高可用id服务器设计实现</title><link>http://www.cppblog.com/lxyfirst/archive/2015/09/17/211854.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Thu, 17 Sep 2015 06:09:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2015/09/17/211854.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/211854.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2015/09/17/211854.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/211854.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/211854.html</trackback:ping><description><![CDATA[服务端/后台开发中如何生成id是每个开发者都会遇到的问题，在电商、游戏领域尤其突出。<br />如何保证生成id的唯一性、可靠性、高可用性，如何组织id的格式，在不同的应用场景和限制下实现方式也不尽相同。<br /><br />我们的应用场景类似电商，在一个订单的生命周期内，有多个逻辑需要生成各自的id，还要考虑到可读性和灵活性，我们决定实现一个独立的id服务。<br />首先，id服务必须具有高可用性，业务逻辑处理中创建id失败是不可接受的，所以id服务必须分布式部署，有多个节点同时对外服务，一个节点失败则重试其他节点，保证成功创建id。<br />在分布式系统中保证数据的一致性成本是很高的，为了简化设计和实现，每个节点都设计成对等的、独立的，不需要保持数据同步。<br />其次，id服务必须可靠，数据不能丢失，因此数据的存储放在独立的mysql数据库中，使用replace方式更新数据，id服务本身记录更新日志。<br />最后，id服务必须灵活，可以自定义id格式，可以高效灵活的实现客户端，因此通讯协议使用json over udp方式，在id服务端使用lua实现id格式的灵活定义。<br /><div>ID规则</div><div><div>&nbsp; &nbsp; 具体规则有lua脚本定义，修改脚本后需要reload生效，需要实现4个函数</div><div>&nbsp; &nbsp; min_counter : &nbsp; 计数器最小值</div><div>&nbsp; &nbsp; max_counter : &nbsp; 计数器最大值</div><div>&nbsp; &nbsp; reset_seconds : 计数器重置周期</div><div>&nbsp; &nbsp; create_id : 根据计数器、自定义参数和时间参数创建ID。<br />&nbsp; &nbsp; 例如：<br /><div>&nbsp; &nbsp; function min_counter()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return 0</div><div>&nbsp; &nbsp; end</div><div>&nbsp; &nbsp; function max_counter()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return 9999</div><div>&nbsp; &nbsp; end</div><div>&nbsp; &nbsp; function reset_seconds()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return 86400</div><div>&nbsp; &nbsp; end</div><div>&nbsp; &nbsp; function create_id(counter,now,salt)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; local seq = counter:generate_counter()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; local new_id = string.format("%01d%02d%02d%04d",now:year()%10 ,now:month(),now:day(),seq)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return new_id</div><div>&nbsp; &nbsp; end</div><div></div></div></div><div>接口</div><div></div><div></div><div></div><div>&nbsp; &nbsp; 采用udp协议，数据格式为json ，字段定义：</div><div>&nbsp; &nbsp; action: 请求类型 get： 创建ID ， &nbsp;monitor：监控</div><div>&nbsp; &nbsp; rule_name: 规则名字， 由服务端定义</div><div>&nbsp; &nbsp; app_name : 应用名或命名空间 ， 客户端自定义，rule_name和app_name一起决定生成ID的唯一性</div><div>&nbsp; &nbsp; salt : &nbsp;自定义参数 ，可选项 ，</div><div>&nbsp; &nbsp; seq : 自定义参数，可选项，原样返回<br />&nbsp; &nbsp; 例如:<br />&nbsp; &nbsp; 创建ID请求: &nbsp;{"action":"get","rule_name":"o2o","app_name":"test"}</div><div>&nbsp; &nbsp; 响应：{"code":0,"message":"success","data":"505140001"}</div><div></div><div>&nbsp; &nbsp; 监控请求：{"action":"monitor","rule_name":"o2o","app_name":"test"}</div><div>&nbsp; &nbsp; 响应：{"code":0,"message":"ok","data":{"counter":3,"node_offset":1}}</div><div></div><div>性能</div>&nbsp; &nbsp; id服务器使用c++实现，性能测试做的比较简单，因为性能不是id服务的主要关注点， 简单以php为客户端进行测试。<br />&nbsp; &nbsp; 4个php并发进程，每个进程不停发送20万个请求，测试结果：<div><div>&nbsp; &nbsp; total:200000 fail:0 min:0.000214 max:0.087330 avg:0.000393</div><div>&nbsp; &nbsp; total:200000 fail:0 min:0.000215 max:0.087129 avg:0.000391</div><div>&nbsp; &nbsp; total:200000 fail:0 min:0.000221 max:0.087252 avg:0.000391</div><div>&nbsp; &nbsp; total:200000 fail:0 min:0.000218 max:0.087484 avg:0.000391<br />&nbsp; &nbsp; 说明 &nbsp;min : 最小耗时(秒) max : 最大耗时(秒) avg : 平均耗时(秒)<br />&nbsp; &nbsp; 服务器TPS达到近1万/秒时，平均延迟在0.3毫秒。<br /><br />经过在生产环境使用，运行稳定，现在将整个系统开源出来，欢迎试用，有任何意见和建议欢迎反馈到lxyfirst＠163.com 。<br /><div>项目源代码位置 : https://github.com/lxyfirst/id_server<br /><br />版本更新9.19<br />1.增加数据落地的预保存和批量保存机制，一方面减少数据库压力，一方面增加异步保存的可靠性。<br />2.由于主线程和数据库线程只需要传递sql语句，将线程间通信由pipe方式改为eventfd + lockfree queue方式。</div></div></div><img src ="http://www.cppblog.com/lxyfirst/aggbug/211854.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2015-09-17 14:09 <a href="http://www.cppblog.com/lxyfirst/archive/2015/09/17/211854.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用svn管理svn账号和权限</title><link>http://www.cppblog.com/lxyfirst/archive/2014/05/23/207063.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Fri, 23 May 2014 03:03:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2014/05/23/207063.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/207063.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2014/05/23/207063.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/207063.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/207063.html</trackback:ping><description><![CDATA[svn的账号和权限管理是基于文件的，修改时需要更新到服务器，多有不便，可利用svn管理账号和权限，利用svn的pos-commit 钩子监测账号和权限文件变化，多个库可共享同一账号和权限文件。<br /><br /><span style="font-size: 13px; background-color: #eeeeee;">/home/svn/conf/目录下存放了多个库共用的passwd和authz文件，用来控制这些库的账号和访问权限，</span>独立的svn_admin库中存放对应的passwd和authz文件，有更新时自动同步到/home/svn/conf/下。<br />svn_admin库的post-commit 脚本如下:<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->REPOS="$1"<br />REV="$2"<br />FILE_DIR="/home/svn/conf"<br />UPDATE_FILE_LIST="passwd&nbsp;authz"<br /><br /><br /><span style="color: #0000FF; ">for</span>&nbsp;FILENAME&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;$UPDATE_FILE_LIST&nbsp;;&nbsp;<span style="color: #0000FF; ">do</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;svnlook&nbsp;changed&nbsp;$REPOS&nbsp;-r&nbsp;$REV&nbsp;|grep&nbsp;$FILENAME&nbsp;&gt;/dev/<span style="color: #0000FF; ">null</span>&nbsp;;&nbsp;then<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DST_FILE=$FILE_DIR/$FILENAME<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mv&nbsp;$DST_FILE&nbsp;$DST_FILE.old&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;svnlook&nbsp;cat&nbsp;$REPOS&nbsp;$FILENAME&nbsp;&gt;&nbsp;$DST_FILE<br />&nbsp;&nbsp;&nbsp;&nbsp;fi<br />done</div><img src ="http://www.cppblog.com/lxyfirst/aggbug/207063.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2014-05-23 11:03 <a href="http://www.cppblog.com/lxyfirst/archive/2014/05/23/207063.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>twemproxy(nutcracker)源代码简要分析</title><link>http://www.cppblog.com/lxyfirst/archive/2014/03/09/206112.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Sun, 09 Mar 2014 05:42:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2014/03/09/206112.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/206112.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2014/03/09/206112.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/206112.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/206112.html</trackback:ping><description><![CDATA[<div>twemproxy(nutcracker)是twitter实现的开源memcached和redis代理，主要功能是根据key分发请求到后端的memcached和redis服务器，简化memcached和redis集群服务的实现。<br />出于对twemproxy实现机制的好奇，简要阅读了代码，特别是网络处理部分，一般这部分是网络服务器的核心，这里记录下其代码实现逻辑和发现的问题。<br /><br />twemproxy作为代理服务器，主体逻辑都围绕着数据流转，采用了单线程非阻塞模型，在linux下由epoll驱动整个程序的运行，对于事件驱动模块的封装在event目录下，event_base对象是引擎，conn对象是具体的连接，conn对象中定义一系列事件处理的回调函数，典型的reactor机制，linux下的实现文件是nc_epoll.c 。&nbsp;<br />事件引擎模块使用了两层回调机制， event_base上有个基本的回调函数，这个回调函数进一步调用conn对象的相应回调函数 &nbsp;（注：一般直接使用conn的回调也就够了）。<br />面向客户端的conn回调：<br /><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv = msg_recv;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv_next = req_recv_next;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv_done = req_recv_done;</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;send = msg_send;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;send_next = rsp_send_next;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;send_done = rsp_send_done;</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;close = client_close;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;active = client_active;<br /><br /><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;enqueue_outq = req_client_enqueue_omsgq;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;dequeue_outq = req_client_dequeue_omsgq;</div>面向后端memcached和redis的conn回调：</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv = msg_recv;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv_next = rsp_recv_next;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv_done = rsp_recv_done;</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;send = msg_send;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;send_next = req_send_next;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;send_done = req_send_done;</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;close = server_close;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;active = server_active;</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;enqueue_inq = req_server_enqueue_imsgq;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;dequeue_inq = req_server_dequeue_imsgq;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;enqueue_outq = req_server_enqueue_omsgq;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;dequeue_outq = req_server_dequeue_omsgq;<br /></div>twemproxy面向客户端时，由proxy_accept接收连接，创建客户端conn对象，并将其加入到事件引擎中。<br />twemproxy面向后端时，由server_pool管理各个到后端的conn对象，同样会加入到事件引擎中。<br /><br />在请求处理模块有2个主要的概念是 mbuf对象和msg对象，mbuf对象是数据缓冲区，发送和接收的数据都存放在mbuf中， 采用链式管理。msg对象是具体的逻辑请求，采用链式管理，形成请求/响应队列。请求和响应的处理模块分别在nc_request.c和nc_response.c中实现。<br /><br />客户端连接的处理逻辑：<br /><br /><div>&nbsp; &nbsp; core_recv&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv &nbsp; &nbsp; &nbsp; &nbsp;即msg_recv ，read事件处理</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv_next &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 即req_recv_next ，获得msg对象，没有则创建</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg_recv_chain &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 创建mbuf对象，接收并处理数据</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [create mbuf]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn_recv &nbsp; &nbsp; &nbsp; 真正的read数据</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg_parse &nbsp; &nbsp; &nbsp;解析 ， mbuf-&gt;msg</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msg_parsed &nbsp; 解析完成</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conn-&gt;recv_done &nbsp; 即req_recv_done &nbsp; &nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;req_filter &nbsp; &nbsp; &nbsp; &nbsp;过滤器，暂无操作</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;req_forward &nbsp; &nbsp;分发请求</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;server_pool_conn 根据key获得后端conn对象</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;将s_conn加入写事件监控，将msg加入转发队列，可写事件被触发后转发队列内请求</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s_conn-&gt;enqueue_inq req_server_enqueue_imsgq</div><div>&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn-&gt;recv_next &nbsp; &nbsp; &nbsp;即req_recv_next，继续下一个</div><br />注：从代码实现看回调逻辑的层次性不强，收发数据放入mbuf列表，然后用writev处理，在遇到发送不完时还要拆分mbuf，重新组织iovec，实现上有些复杂。<br />另外conn对象的数据采用一次读/写完的方式处理，在高压力下可能会产生大量的mbuf对象。<br /><br />未完待续。<br /></div><img src ="http://www.cppblog.com/lxyfirst/aggbug/206112.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2014-03-09 13:42 <a href="http://www.cppblog.com/lxyfirst/archive/2014/03/09/206112.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用libdrizzle实现mysql代理服务器的问题及优化</title><link>http://www.cppblog.com/lxyfirst/archive/2014/01/07/205212.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Tue, 07 Jan 2014 02:07:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2014/01/07/205212.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/205212.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2014/01/07/205212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/205212.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/205212.html</trackback:ping><description><![CDATA[近期项目需要一个mysql代理服务器，实现mysql协议代理和路由功能，形成简单的mysql集群服务。现成的开源方案是mysql-proxy ， 分析功能和源代码后发现跟我们的应用场景不太匹配，于是决定重新实现一个符合需求的mysql代理服务器，考虑到需要完美支持mysql协议，优先选择了libdrizzle库， libdrizzle是开源项目drizzle中的协议库，而drizzle可以看作mysql的分支版本，目前稳定版本是7.1.36 ， 下面主要是记录使用libdrizzle中遇到的一些问题。<br />
<span style="font-size: 10.5pt; font-family: 宋体;">1. 关于nonblock模式的问题，现代应用服务器典型架构一般是使用reactor/proactor模式的事件驱动模型，如何把libdrizzle和应用服务器的驱动模型很好的结合起来尤其重要，&nbsp;</span><span style="font-size: 10.5pt; font-family: 宋体;">libdrizzle支持nonblock模式，独立实现了事件驱动机制，使用poll监控网络事件，具体在drizzle_con_wait()中实现，然后通过drizzle_con_ready()遍历产生事件的网络连接，即drizzle_con_st对象，该接口难以与通常的网络事件驱动机制配合使用，性能也不太理想，具体用法可参见其自带的样例程序examples/client.cc , 也就是说libdrizzle的驱动模型需要重新封装成跟应用服务器相匹配，才能真正发挥nonblock模式的性能。<br /></span><span style="font-size: 10.5pt; font-family: 宋体;"><br />2. drizzle_result_st<font face="宋体">对象初始时一些内部数据没有初始化，容易造成程序崩溃，因此需要修改构造函数，初始化所有内部数据。涉及文件</font><font face="Times New Roman">libdrizzle-2.0/structs.h&nbsp;</font><font face="宋体">。</font></span><span style="font-size: 10.5pt; font-family: 宋体;">相应字段为field, field_buffer,row 。<br /><br />
</span>
<p class="p0" style="margin-bottom:0pt; margin-top:0pt; "><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">3. libdrizzle<font face="宋体">中运行时产生的内部对象都以双链表形式挂接在其上级对象中，例如</font><font face="Times New Roman">drizzle_st</font><font face="宋体">对象中有个双链表维护其创建的</font><font face="Times New Roman">drizzle_con_st</font><font face="宋体">对象，类似地，</font><font face="Times New Roman">drizzle_con_st</font><font face="宋体">对象中有个双链表维护其创建的</font><font face="Times New Roman">drizzle_result_st</font><font face="宋体">对象，所有的对象通过这种形式级联管理，</font></span><span style="font-family: 宋体;">并且这些对象中保存着上下文相关的状态，</span><span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">这样的实现方便资源管理，防止资源泄露，但在</font><font face="Times New Roman">代理服务器</font><font face="宋体">中，请求和结果在不断转发过程中会形成大量的内存拷贝，为了减少转发过程中的内存拷贝，需要把</font><font face="Times New Roman">drizzle_result_st</font><font face="宋体">显式的从</font><font face="Times New Roman">drizzle_con_st</font><font face="宋体">中移除，当数据发往客户端完成后再删除，因此增加了</font><font face="Times New Roman">drizzle_result_detach()</font><font face="宋体">接口，用于从</font><font face="Times New Roman">drizzle_con_st</font><font face="宋体">对象中移除</font><font face="Times New Roman">drizzle_result_st</font><font face="宋体">对象&nbsp;，&nbsp;涉及文件</font><font face="Times New Roman">libdrizzle-2.0/result.h&nbsp;,&nbsp;libdrizzle-2.0/result.cc&nbsp;</font><font face="宋体">。<br />
<br />
</font></span></p>
<div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">void</span>&nbsp;drizzle_result_detach(drizzle_result_st&nbsp;*result)<br />
{<br />
<br />
&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(result-&gt;con)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;result-&gt;con-&gt;result_count--;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(result-&gt;con-&gt;result_list&nbsp;==&nbsp;result)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result-&gt;con-&gt;result_list=&nbsp;result-&gt;next;<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(result-&gt;prev)<br />
&nbsp;&nbsp;&nbsp;&nbsp;result-&gt;prev-&gt;next=&nbsp;result-&gt;next;<br />
<br />
&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(result-&gt;next)<br />
&nbsp;&nbsp;&nbsp;&nbsp;result-&gt;next-&gt;prev=&nbsp;result-&gt;prev;<br />
<br />
&nbsp;&nbsp;result-&gt;con&nbsp;=&nbsp;NULL&nbsp;;<br />
&nbsp;&nbsp;result-&gt;prev&nbsp;=&nbsp;NULL&nbsp;;<br />
&nbsp;&nbsp;result-&gt;next&nbsp;=&nbsp;NULL&nbsp;;<br />
}</div><img src ="http://www.cppblog.com/lxyfirst/aggbug/205212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2014-01-07 10:07 <a href="http://www.cppblog.com/lxyfirst/archive/2014/01/07/205212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>keepalived配置注意事项</title><link>http://www.cppblog.com/lxyfirst/archive/2011/11/16/160237.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Wed, 16 Nov 2011 03:11:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2011/11/16/160237.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/160237.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2011/11/16/160237.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/160237.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/160237.html</trackback:ping><description><![CDATA[keepalived是用来进行网络fail-over的强大工具，配置简单灵活，功能强大，实现了vrrp协议和健康检查，配合lvs使用可实现高可用性。<br />典型双机热备配置方案：<br />两台机器的keepalived实例都配置成BACKUP状态，priority高的自动作为master , priority低的自动作为slave ，也可以将priority设置为相同，先启动的作为master 。两边都设置nopreempt，防止出现故障-&gt;恢复过程中的再切换。<br />1.在master发生故障-&gt;恢复过程中，原backup会替换为master对外服务，当原master恢复后，一般希望原master作为新backup，以避免master的再次切换，可以使用nopreempt参数，防止priority高的发起切换。<br />2. 当keepalived设置为随系统启动自动启动时，应加上一定的延迟，防止网络或系统未准备好影响keepalived的状态。<br />3. 当后端的RS有状态(https)时，lvs一般需要使用sh负载算法或使用持久性连接，以便同一来源的请求分发到同一RS，当http和https的请求分发需要一致时，可以使用iptable对报文做fmark，使用fmark配置lvs 。<br /><img src ="http://www.cppblog.com/lxyfirst/aggbug/160237.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2011-11-16 11:11 <a href="http://www.cppblog.com/lxyfirst/archive/2011/11/16/160237.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>LVS配置注意事项</title><link>http://www.cppblog.com/lxyfirst/archive/2011/10/10/157974.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Mon, 10 Oct 2011 07:55:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2011/10/10/157974.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/157974.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2011/10/10/157974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/157974.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/157974.html</trackback:ping><description><![CDATA[1. arp问题<br />&nbsp;&nbsp; 在DR或者tunnel模式下，RS需要绑定VIP以便直接将报文发回客户端。因此需要在RS上屏蔽网络内对VIP进行arp查询的响应 。<br /><div>echo 1 &gt; /proc/sys/net/ipv4/conf/all/arp_ignore<br />echo 2 &gt; /proc/sys/net/ipv4/conf/all/arp_announce<br /><br />2. mtu问题<br />&nbsp;&nbsp; 在tunnel模式下 ，LD将客户端报文进行封装（加IPIP头）后发送给RS ， 因此RS需要调整MTU大小，预留IPIP头空间，以便客户端正确分包。<br /><div>ifconfig "$OUT_DEV" mtu 1480</div><br />3.报文转发问题<br />&nbsp;&nbsp;&nbsp; 在DR或者tunnel模式下，报文直接转发到RS。<br /><div>echo 1 &gt; /proc/sys/net/ipv4/ip_forward</div><br />4.LD支持连接数问题<br />&nbsp;&nbsp; 内核ip_vs模块的参数conn_tab_bits指定了conn表的大小，最大为20 ，支持1M个连接。<br /><br />5.LD做HA时VIP接管问题<br />&nbsp;&nbsp; 新LD接管故障LD的VIP时，需要及时做arp广播，keepalived会自动完成，也通过arping命令查看。<br /><br />6.LD的cpu负载问题<br />&nbsp;&nbsp; LD的网卡软中断(ksoftirqd)由一个cpu处理，无法利用多核，调整软中断的smp_affinity可以改变绑定的cpu，但无法做多核负载均衡。<br />&nbsp; &nbsp;内核2.6.32之后已经支持软中断的负载均衡。<br />&nbsp;&nbsp; 使用支持RSS的网卡，有多个队列，或者使用多个网卡做bonding 。<br /><div><span style="font-size: 12pt;">&nbsp; echo</span><span style="font-size: 12pt;"> "alias bond0 bonding"<span style="background: none repeat scroll 0% 0% #fff0f0;"></span><span style="color: black;"> &gt;&gt; /etc/modprobe.conf</span><br /></span></div>&nbsp; 修改ifcfg-bond0 , ifcfg-ethX配置文件。<br /><br />7. 系统内核参数调整参考<br /><div>net.ipv4.tcp_tw_recyle = 1<br />net.ipv4.tcp_tw_reuse = 1<br />net.ipv4.tcp_max_syn_backlog = 40960<br />net.ipv4.tcp_keepalive_time = 1800<br />net.ipv4.tcp_fin_timeout = 30<br />net.ipv4.tcp_synack_retries = 1<br />net.ipv4.tcp_syn_retries = 1<br />net.ipv4.ip_local_port_range = 1024 65000<br />net.ipv4.tcp_max_tw_buckets = 8192<br />net.ipv4.tcp_syncookies = 1<br />net.ipv4.tcp_max_orphans = 40960<br />#net.ipv4.tcp_timestamps = 0<br /><br />net.ipv4.tcp_rmem = 4194304 8388608&nbsp;16777216<br />net.ipv4.tcp_wmem = 4194304 8388608 16777216<br />net.ipv4.udp_mem =&nbsp;4194304&nbsp;8388608 16777216<br />net.ipv4.udp_rmem_min =&nbsp;1048576<br />net.ipv4.udp_wmem_min =&nbsp;1048576<br /><br />net.core.somaxconn = 40960<br />net.core.netdev_max_backlog = 40960<br />net.core.rmem_default = 1048576 <br />net.core.wmem_default = 1048576<br />net.core.rmem_max = 16777216<br />net.core.wmem_max = 16777216</div></div>&nbsp;参考:http://www.austintek.com/LVS/<img src ="http://www.cppblog.com/lxyfirst/aggbug/157974.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2011-10-10 15:55 <a href="http://www.cppblog.com/lxyfirst/archive/2011/10/10/157974.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>网卡负载均衡</title><link>http://www.cppblog.com/lxyfirst/archive/2011/08/04/152425.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Thu, 04 Aug 2011 02:44:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2011/08/04/152425.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/152425.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2011/08/04/152425.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/152425.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/152425.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #444444; line-height: 22px; font-family: Arial, Helvetica, sans-serif; ">1.服务器网卡软中断的cpu负载均衡。 1.网卡支持RSS（Receive Side Scaling，接收方扩展）。 2.内核支持RSS。</span><br />2.网卡bonding 。多块网卡绑定同一IP地址对外提供服务，通过bonding，虚拟一块网卡对外提供服务。<div><a href="http://t.chinaunix.net/archiver/tid-1927269.html">http://t.chinaunix.net/archiver/tid-1927269.html</a></div><img src ="http://www.cppblog.com/lxyfirst/aggbug/152425.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2011-08-04 10:44 <a href="http://www.cppblog.com/lxyfirst/archive/2011/08/04/152425.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux上mq和socketpair的通信性能比较</title><link>http://www.cppblog.com/lxyfirst/archive/2011/08/04/152424.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Thu, 04 Aug 2011 02:36:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2011/08/04/152424.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/152424.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2011/08/04/152424.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/152424.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/152424.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #444444; line-height: 22px; font-family: Arial, Helvetica, sans-serif; "><span style="color: #444444; font-size: 14px; line-height: 22px; font-family: Arial, Helvetica, sans-serif; ">多线程系统中通知用哪种方式效率更好，在一台4核Xeon 3.00GHZ的机器上对比了linux下mq和socketpair通信性能，一写线程，一读线程，初步结论是mq胜出，mq 46w/s ，socketpair 40w/s 。</span></span><img src ="http://www.cppblog.com/lxyfirst/aggbug/152424.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2011-08-04 10:36 <a href="http://www.cppblog.com/lxyfirst/archive/2011/08/04/152424.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>典型网络模型性能比较</title><link>http://www.cppblog.com/lxyfirst/archive/2011/07/07/150386.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Thu, 07 Jul 2011 05:24:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2011/07/07/150386.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/150386.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2011/07/07/150386.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/150386.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/150386.html</trackback:ping><description><![CDATA[<div>      <p><span style="font-family: 宋体;">使用几个经典网络模型实现非阻塞简单</span>http<span style="font-family: 宋体;">服务，部署在一台</span>4<span style="font-family: 宋体;">核</span>Xeon 3.00GHZ<span style="font-family: 宋体;">的机器上进行压力测试。</span></p>  <table style="border-collapse: collapse; border: medium none;" border="1" cellpadding="0" cellspacing="0">  <tbody><tr>   <td style="width: 106.5pt; border: 1pt solid black; padding: 0cm 5.4pt;" valign="top" width="142">   <p>&nbsp;</p>   </td>   <td style="width: 148.25pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: black black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="198">   <p><span style="font-family: 宋体;">短连接</span></p>   </td>   <td style="width: 127.6pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: black black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="170">   <p><span style="font-family: 宋体;">长连接</span></p>   </td>   <td style="width: 4cm; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: black black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="151">   <p><span style="font-family: 宋体;">客户端</span></p>   </td>  </tr>  <tr>   <td style="width: 106.5pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color black black; padding: 0cm 5.4pt;" valign="top" width="142">   <p>1<span style="font-family: 宋体;">个线程</span></p>   </td>   <td style="width: 148.25pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="198">   <p>97%cpu<span style="font-family: 宋体;">，多核分担</span></p>   <p>60%cpu<span style="font-family: 宋体;">网卡中断</span></p>   <p>1.6w/s</p>   <p><span style="font-family: 宋体;">平均响应时间</span>10ms</p>   </td>   <td style="width: 127.6pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="170">   <p>100%cpu</p>   <p>15%cpu<span style="font-family: 宋体;">网卡软中断</span></p>   <p>2.8w/s</p>   <p><span style="font-family: 宋体;">平均响应时间</span>7ms</p>   </td>   <td style="width: 4cm; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="151">   <p>100<span style="font-family: 宋体;">并发</span>/<span style="font-family: 宋体;">客户端</span></p>   <p>100w<span style="font-family: 宋体;">请求</span>/<span style="font-family: 宋体;">客户端</span></p>   <p>2<span style="font-family: 宋体;">个客户端</span></p>   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td style="width: 106.5pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color black black; padding: 0cm 5.4pt;" valign="top" width="142">   <p>4<span style="font-family: 宋体;">个线程</span></p>   </td>   <td style="width: 148.25pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="198">   <p><span style="font-family: 宋体;">每个线程</span>70%cpu </p>   <p>99%cpu<span style="font-family: 宋体;">网卡中断</span></p>   <p>2.1w/s</p>   <p><span style="font-family: 宋体;">平均响应时间</span>9ms</p>   </td>   <td style="width: 127.6pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="170">   <p><span style="font-family: 宋体;">每个线程</span>100%cpu</p>   <p>40%cpu<span style="font-family: 宋体;">网卡软中断</span></p>   <p>6.5w/s</p>   <p><span style="font-family: 宋体;">平均响应时间</span>3ms</p>   </td>   <td style="width: 4cm; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="151">   <p>100<span style="font-family: 宋体;">并发</span>/<span style="font-family: 宋体;">客户端</span></p>   <p>100w<span style="font-family: 宋体;">请求</span>/<span style="font-family: 宋体;">客户端</span></p>   <p>2<span style="font-family: 宋体;">个客户端</span></p>   <p>&nbsp;</p>   </td>  </tr>  <tr>   <td style="width: 106.5pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color black black; padding: 0cm 5.4pt;" valign="top" width="142">   <p>1<span style="font-family: 宋体;">个</span>leader<span style="font-family: 宋体;">线程，接受连接</span></p>   <p>4<span style="font-family: 宋体;">个</span>worker<span style="font-family: 宋体;">线程，处理请求</span></p>   </td>   <td style="width: 148.25pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="198">   <p>leader<span style="font-family: 宋体;">线程</span>90%cpu</p>   <p>worker<span style="font-family: 宋体;">线程</span>40%cpu</p>   <p>75%<span style="font-family: 宋体;">网卡中断</span></p>   <p>1.8w/s</p>   <p><span style="font-family: 宋体;">平均响应时间</span>10ms</p>   </td>   <td style="width: 127.6pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="170">   <p>leader<span style="font-family: 宋体;">线程</span>1%cpu</p>   <p>worker<span style="font-family: 宋体;">线程</span>100%cpu</p>   <p>40%<span style="font-family: 宋体;">网卡中断</span></p>   <p>6.0w/s</p>   <p><span style="font-family: 宋体;">平均响应时间</span>3ms</p>   </td>   <td style="width: 4cm; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color black black -moz-use-text-color; padding: 0cm 5.4pt;" valign="top" width="151">   <p>100<span style="font-family: 宋体;">并发</span>/<span style="font-family: 宋体;">客户端</span></p>   <p>100w<span style="font-family: 宋体;">请求</span>/<span style="font-family: 宋体;">客户端</span></p>   <p>2<span style="font-family: 宋体;">个客户端</span></p>   <p>&nbsp;</p>   </td>  </tr> </tbody></table>  <p>&nbsp;</p>  <p><span style="font-family: 宋体;">结论：</span></p>  <p style="margin-left: 18pt; text-indent: -18pt;"><span><span>1.<span style="font: 7pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family: 宋体;">短连接中，建立连接是很耗费资源的。</span></p>  <p style="margin-left: 18pt; text-indent: -18pt;"><span><span>2.<span style="font: 7pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family: 宋体;">长连接中，多线程在提高处理能力方面是很有价值的，尤其是运算量多的请求。</span></p>  <p style="margin-left: 18pt; text-indent: -18pt;"><span><span>3.<span style="font: 7pt &quot;Times New Roman&quot;;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family: 宋体;">多个线程同时接受连接会造成</span>cpu<span style="font-family: 宋体;">软中断的</span>overhead<span style="font-family: 宋体;">。</span></p>  </div><img src ="http://www.cppblog.com/lxyfirst/aggbug/150386.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2011-07-07 13:24 <a href="http://www.cppblog.com/lxyfirst/archive/2011/07/07/150386.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>消息队列beanstalkd简介</title><link>http://www.cppblog.com/lxyfirst/archive/2011/06/29/149729.html</link><dc:creator>star</dc:creator><author>star</author><pubDate>Wed, 29 Jun 2011 06:43:00 GMT</pubDate><guid>http://www.cppblog.com/lxyfirst/archive/2011/06/29/149729.html</guid><wfw:comment>http://www.cppblog.com/lxyfirst/comments/149729.html</wfw:comment><comments>http://www.cppblog.com/lxyfirst/archive/2011/06/29/149729.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lxyfirst/comments/commentRss/149729.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lxyfirst/services/trackbacks/149729.html</trackback:ping><description><![CDATA[beanstalkd源于fackbook，是一个快速、简单的内存消息队列，也可以开启binlog，消息将被写入日志文件，用于重启时恢复数据。<br />1.消息被称作job，在服务器端储存在内存队列中，具有DELAYED,READY,RESERVED,BURIED状态，状态转换图如下<br /><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">Here&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;a&nbsp;picture&nbsp;of&nbsp;the&nbsp;typical&nbsp;job&nbsp;lifecycle:<br /><br /><br />&nbsp;&nbsp;&nbsp;put&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reserve&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete<br />&nbsp;&nbsp;</span><span style="color: #000000;">-----&gt;</span><span style="color: #000000;">&nbsp;[READY]&nbsp;</span><span style="color: #000000;">---------&gt;</span><span style="color: #000000;">&nbsp;[RESERVED]&nbsp;</span><span style="color: #000000;">--------&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">poof</span><span style="color: #000000;">*</span><span style="color: #000000;"><br /><br /><br /><br />Here&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;a&nbsp;picture&nbsp;with&nbsp;more&nbsp;possibilities:<br /><br /><br /><br />&nbsp;&nbsp;&nbsp;put&nbsp;with&nbsp;delay&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;release&nbsp;with&nbsp;delay<br />&nbsp;&nbsp;</span><span style="color: #000000;">----------------&gt;</span><span style="color: #000000;">&nbsp;[DELAYED]&nbsp;</span><span style="color: #000000;">&lt;------------</span><span style="color: #000000;">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;(time&nbsp;passes)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;put&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reserve&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete<br />&nbsp;&nbsp;</span><span style="color: #000000;">-----------------&gt;</span><span style="color: #000000;">&nbsp;[READY]&nbsp;</span><span style="color: #000000;">---------&gt;</span><span style="color: #000000;">&nbsp;[RESERVED]&nbsp;</span><span style="color: #000000;">--------&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">poof</span><span style="color: #000000;">*</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;\&nbsp;&nbsp;release&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;`</span><span style="color: #000000;">-------------</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;|</span><span style="color: #000000;"><br /></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;kick&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bury&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[BURIED]&nbsp;</span><span style="color: #000000;">&lt;---------------</span><span style="color: #000000;">'<br /></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;&nbsp;delete<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`</span><span style="color: #000000;">--------&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">poof</span><span style="color: #000000;">*</span><span style="color: #000000;"><br /></span></div><br />消息支持优先级，生存时间的设置。不同状态的消息分别处于相应状态的队列中。<br />2. 消息属于某个tube，tube类似于namespace或者消息主题的概念，消费者可以订阅一个或多个tube ，从而接收这些tube的消息 。<br />3. beanstalkd的代码实现和协议定义很类似memcached的风格。<br /><img src ="http://www.cppblog.com/lxyfirst/aggbug/149729.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lxyfirst/" target="_blank">star</a> 2011-06-29 14:43 <a href="http://www.cppblog.com/lxyfirst/archive/2011/06/29/149729.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>