1年前和阿里的大牛们聊天。被告知etao网使用 gearman 做分布式的中间件使访问达到了 百万的qps。从次对gearman有了初步的认识。之前主导项目的时候决定使用gearman做 分布式。 但是当压力到达一定级别的时候。
gearman 产生了大量的 close wait 导致大量的worker 无法连到 gearman。试了各种办法无法解决从而产生了研究gearman源码的冲动。不说废话啦,从main说起。
首先命令行参数解析 gearman 竟然用boost_program.so 真tm蛋疼用这么一个小功能竟然要装个boost
boost::program_options::options_description general("General options");
general.add_options()
("backlog,b", boost::program_options::value(&backlog)->default_value(32),
"Number of backlog connections for listen.")
("daemon,d", boost::program_options::bool_switch(&opt_daemon)->default_value(false),
"Daemon, detach and run in the background.")
("exceptions", boost::program_options::bool_switch(&opt_exceptions)->default_value(false),
"Enable protocol exceptions by default.")
("file-descriptors,f", boost::program_options::value(&fds),
"Number of file descriptors to allow for the process (total connections will be slightly less). Default is max allowed for user.")
("help,h", "Print this help menu.")
在参数解析时比较重要的一个逻辑就是
if (threads == 0)
{
uint32_t number_of_threads= libtest::number_of_cpus();
if (number_of_threads > 4)
{
threads= number_of_threads;
}
}
当没有指定-t的时候 默认工作线程的数量是cpu的数量,而当核数少于4个的时候将会变成单线程程序。当然大家都应该知道在 没有复杂业务逻辑和持久化要求的情况下。单纯提高线程数其实对于并发是没有太多帮助的。
下回将细说gearman如何利用libevent架构网络环境。