IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx模块开发(28)—主动健康检查模块函数

    cjhust发表于 2013-03-05 17:24:49
    love 0

    1、操作函数

    clip_image002

    * 分配空间

    ngx_http_upstream_check_create_main_conf(ngx_conf_t *cf)

    函数功能:对数据结构ngx_http_upstream_check_main_conf_t分配空间,并初始化某些参数;

    ngx_http_upstream_check_create_srv_conf(ngx_conf_t *cf)

    函数功能:对数据结构ngx_http_upstream_check_srv_conf_t分配空间,并初始化某些参数;

    ngx_http_upstream_check_create_loc_conf(ngx_conf_t *cf)

    函数功能:对数据结构ngx_http_upstream_check_loc_conf_t分配空间,并初始化某些参数;

    * 解析指令

    ngx_http_upstream_global_check(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析global_check指令,填充ngx_http_upstream_check_main_conf_t结构。如果是client模式,需要用ngx_http_upstream_check_client_read_file函数一行行读LBdown配置文件,并填充ucmcf->global_check_client_list。

    ngx_http_upstream_check(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析check指令,填充ngx_http_upstream_check_srv_conf_t结构。

    ngx_http_upstream_check_http_send(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析check_http_send指令,填充ngx_http_upstream_check_srv_conf_t结构。

    ngx_http_upstream_check_http_expect_alive(ngx_conf_t *cf, ngx_command_t *cmd,void *conf)

    函数功能:填充ngx_http_upstream_check_srv_conf_t。

    ucscf->code.status_alive = bit;

    ucscf->status_alive_flag = 1;

    ngx_http_upstream_check_http_expect_not_alive

    函数功能:填充ngx_http_upstream_check_srv_conf_t。

    ucscf->code.status_not_alive = bit;

    ucscf->status_alive_flag = 0;

    ngx_http_upstream_check_shm_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析check_shm_size指令,填充ngx_http_upstream_check_main_conf_t结构。

    ngx_http_upstream_check_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析check_status指令,handler设置为ngx_http_upstream_check_status_handler。

    ngx_http_upstream_check_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析check_enable指令,handler设置为ngx_http_upstream_check_enable_handler,并根据指令参数填充ngx_http_upstream_check_loc_conf_t数据结构,指令格式为check_enable $arg_upstream $arg_host。

    ngx_http_upstream_check_disable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

    函数功能:解析check_disable指令handler设置为ngx_http_upstream_check_disable_handler,并根据指令参数填充ngx_http_upstream_check_loc_conf_t数据结构,指令格式为check_disable $arg_upstream $arg_host。

    * 模块间调用

    ngx_http_check_add_peer(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us,ngx_peer_addr_t *peer_addr)

    函数功能:该函数比较重要,主要是负载均衡算法模块来调用该函数。

    clip_image001[4]

    clip_image002[10]

    ngx_http_check_peer_down(ngx_uint_t index)

    函数功能:根据index来判断peer是否down。

    clip_image002[12]

    ngx_http_upstream_check_init_main_conf(ngx_conf_t *cf, void *conf)

    ngx_http_upstream_check_init_srv_conf(cf, uscfp[i])

    函数功能:对ngx_http_upstream_check_srv_conf_t的数据结构进行最后的进行初始化(主要针对一些指令没配置的情况下)。

    ngx_http_upstream_check_init_shm(cf, conf)(handler.c文件中)

    函数功能:初始化srv配置和共享内存。

    ngx_http_upstream_check_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)

    函数功能:对ngx_http_upstream_check_loc_conf_t的数据结构进行继承式初始化。

    * 初始化共享内存

    ngx_http_upstream_check_client_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)

    函数功能:通过对比check_peers_list的方式,初始化ngx_http_check_peer_shm_t的down字段。

    ngx_http_upstream_check_client_find_host(ngx_http_check_peer_shm_t *peer_shm)

    函数功能:根据client模式下的file LBdown.conf生成的列表check_peers_list,查找该peer_shm是否在列表中,如果在,则用ngx_http_check_set_shm_peer(peer_shm, NULL, 1)函数将ngx_http_check_peer_shm_t->down设置为1。

    ngx_http_upstream_check_lb_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)

    函数功能:ngx_http_upstream_check_init_shm中设置了data=NULL,通过继承old共享内存来初始化ngx_http_check_peer_shm_t->down字段。

    * 初始化worker进程

    clip_image002[4]

    clip_image001

    clip_image002[6]

    ngx_http_check_init_process(ngx_cycle_t *cycle)

    ngx_http_check_add_timers(cycle)

    函数功能:关联ngx_http_check_peer_shm_t和ngx_http_check_peer_s,并填充ngx_http_check_peer_s数据结构,添加定时任务ngx_http_check_begin_handler。
    clip_image002[8]

    * 定时事件

    clip_image002

    ngx_http_check_need_exit()

    函数功能:判断事件是否需要退出,这里定义了收到了ngx_terminate(CTRL+C)、ngx_exiting(kill -QUIT)、ngx_quit(kill -WINCH)信号的时候,事件需要退出。

    ngx_http_check_finish_handler(ngx_event_t *event)

    函数功能:直接调用ngx_http_check_need_exit()。

    ngx_http_check_clean_event(ngx_http_check_peer_t *peer)

    函数功能:删除超时事件,并reinit数据结构check_data;

    ngx_http_check_status_update(ngx_http_check_peer_t *peer, ngx_int_t result)

    函数功能:LB模式下设置peer->shm->rise_count、peer->shm->fall_count、peer->shm->access_time和peer->shm->down。

    ngx_http_check_client_status_update(ngx_http_check_peer_t *peer, ngx_int_t result)

    函数功能:client模式下设置peer->shm->access_time、peer->shm->down。

    clip_image002[14]

    ngx_http_check_begin_handler(ngx_event_t *event)

    函数功能:再次添加定时器,并根据owner、interval等信息来判断是否需要connect_handler。

    ngx_http_check_connect_handler(ngx_event_t *event)

    函数功能:根据ngx_http_check_peer_s的server信息,填充peer->pc信息。

    clip_image001[6]

    c->write->handler = peer->send_handler; //ngx_http_check_send_handler

    c->read->handler = peer->recv_handler; //ngx_http_check_recv_handler

    ngx_event_connect_peer(ngx_peer_connection_t *pc)

    函数功能:新建socket、填充ngx_connection_t结构、设置rcvbuf大小、设置非阻塞、连接

    ngx_http_check_timeout_handler(ngx_event_t *event)

    函数功能:根据配置的http_404或http_2XX来更新超时事件时peer->shm->down。

    ngx_http_check_send_handler(ngx_event_t *event)

    函数功能:c->write->handler = peer->send_handler,写事件。

    ngx_http_check_recv_handler(ngx_event_t *event)

    函数功能:c->read->handler = peer->recv_handler,读事件。

    * API接口

    ngx_http_upstream_check_client_write_file(ngx_http_request_t *r)

    函数功能:client模式下,将标记为down的主机以upstream_name server_ip的格式写入文件。

    ngx_http_upstream_check_enable_handler(ngx_http_request_t *r)

    函数功能:根据upstream和host将指定的upstream下IP=host的主机的流量开启,只支持GET和HEAD指令。

    ngx_http_upstream_check_disable_handler(ngx_http_request_t *r)

    函数功能:根据upstream和host将指定的upstream下IP=host的主机的流量关闭,只支持GET和HEAD指令。

    ngx_http_upstream_check_status_handler(ngx_http_request_t *r)

    函数功能:将peers的信息返回给用户,client和lb模式的格式不同,但基本信息是:index、upstream_name、serverIP、status。

    负载均衡模块


    2、参考资料

    http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

    https://github.com/yaoweibin/nginx_upstream_check_module

    https://github.com/yaoweibin/nginx_upstream_check_module/commit/c8378494c8694c3b4614e38ac3d857f026194226

    https://github.com/yaoweibin/nginx_upstream_check_module/blob/master/check_1.2.1.patch



沪ICP备19023445号-2号
友情链接