在HttpSM::handle_api_return和HttpSM::set_next_state函数分别打断点,进入函数的时候分别打印t_state.api_next_action和t_state.next_action,
一个缓存命中的请求完整流程的堆栈信息如下。
Breakpoint 1 at 0x596c40: file HttpSM.cc, line 1580.
Breakpoint 2 at 0x5a8f40: file HttpSM.cc, line 6841.
[Switching to Thread 0x2b06a7472700 (LWP 15194)]
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$1 = HttpTransact::HTTP_API_SM_START
"t_state.next_action: "$2 = HttpTransact::STATE_MACHINE_ACTION_UNDEFINED
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$3 = HttpTransact::HTTP_API_SM_START
"t_state.next_action: "$4 = HttpTransact::HTTP_API_READ_REQUEST_HDR
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$5 = HttpTransact::HTTP_API_READ_REQUEST_HDR
"t_state.next_action: "$6 = HttpTransact::HTTP_API_READ_REQUEST_HDR
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$7 = HttpTransact::HTTP_API_READ_REQUEST_HDR
"t_state.next_action: "$8 = HttpTransact::HTTP_API_PRE_REMAP
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$9 = HttpTransact::HTTP_API_PRE_REMAP
"t_state.next_action: "$10 = HttpTransact::HTTP_API_PRE_REMAP
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$11 = HttpTransact::HTTP_API_PRE_REMAP
"t_state.next_action: "$12 = HttpTransact::HTTP_REMAP_REQUEST
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$13 = HttpTransact::HTTP_API_PRE_REMAP
"t_state.next_action: "$14 = HttpTransact::HTTP_API_POST_REMAP
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$15 = HttpTransact::HTTP_API_POST_REMAP
"t_state.next_action: "$16 = HttpTransact::HTTP_API_POST_REMAP
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$17 = HttpTransact::HTTP_API_POST_REMAP
"t_state.next_action: "$18 = HttpTransact::CACHE_LOOKUP
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$19 = HttpTransact::HTTP_API_POST_REMAP
"t_state.next_action: "$20 = HttpTransact::HTTP_API_READ_CACHE_HDR
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$21 = HttpTransact::HTTP_API_READ_CACHE_HDR
"t_state.next_action: "$22 = HttpTransact::HTTP_API_READ_CACHE_HDR
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$23 = HttpTransact::HTTP_API_READ_CACHE_HDR
"t_state.next_action: "$24 = HttpTransact::HTTP_API_CACHE_LOOKUP_COMPLETE
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$25 = HttpTransact::HTTP_API_CACHE_LOOKUP_COMPLETE
"t_state.next_action: "$26 = HttpTransact::HTTP_API_CACHE_LOOKUP_COMPLETE
Breakpoint 2, HttpSM::set_next_state (this=0x2b06b9400000) at HttpSM.cc:6841
6841 switch (t_state.next_action) {
"t_state.api_next_action: "$27 = HttpTransact::HTTP_API_CACHE_LOOKUP_COMPLETE
"t_state.next_action: "$28 = HttpTransact::SERVE_FROM_CACHE
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$29 = HttpTransact::HTTP_API_SEND_REPONSE_HDR
"t_state.next_action: "$30 = HttpTransact::SERVE_FROM_CACHE
Breakpoint 1, HttpSM::handle_api_return (this=0x2b06b9400000) at HttpSM.cc:1580
1580 switch (t_state.api_next_action) {
"t_state.api_next_action: "$31 = HttpTransact::HTTP_API_SM_SHUTDOWN
"t_state.next_action: "$32 = HttpTransact::SERVE_FROM_CACHE
以进入HttpSM::handle_api_return的各个state为顺序进行分析如下:
第一个state是HttpTransact::HTTP_API_SM_START,这个阶段并没有直接执HttpSM::call_transact_and_set_next_state函数。一开始执行了读请求头的函数:HttpSM::setup_client_read_request_header,这个读操作对应的事件回调函数是之前在HttpSM::attach_client_session函数中设置的HttpSM::state_read_client_request_header。在HttpSM::state_read_client_request_header函数中如果读操作执行的顺利的话,最后执行了HttpSM::call_transact_and_set_next_state,并且带了一个参数,HttpTransact::ModifyRequest函数。HttpTransact::ModifyRequest函数提取了一些请求相关的信息,最后执行TRANSACT_RETURN函数,进入下一个state。
第二个state是HttpTransact::HTTP_API_READ_REQUEST_HDR,这个阶段call执行了HttpTransact::StartRemapRequest函数,这个函数判断了如果不需要remap,直接执行TRANSACT_RETURN函数。如果需要remap,设置一些debug和监控信息之后执行TRANSACT_RETURN函数,第一个参数为HTTP_API_PRE_REMAP,第二个参数为HttpTransact::PerformRemap。
第三个state是HttpTransact::HTTP_API_PRE_REMAP,这个阶段call的函数什么都没做直接TRANSACT_RETURN函数了,第一个参数是HTTP_REMAP_REQUEST,第二个参数是HttpTransact::EndRemapRequest。随后在HttpSM::set_next_state中,执行了HttpSM::do_remap_request函数。接着又执行了一次HttpSM::call_transact_and_set_next_state函数,这次call了HttpTransact::EndRemapRequest函数,也即上一次call的函数最后TRANSACT_RETURN函数的参数在这里生效了。HttpTransact::EndRemapRequest函数本质上是分析了remap执行的情况,顺利的话最后执行的TRANSACT_RETURN函数第一个参数是HTTP_API_POST_REMAP,第二个参数是HttpTransact::HandleRequest。
第四个state是HttpTransact::HTTP_API_POST_REMAP,这个阶段call了上个state设置的HttpTransact::HandleRequest函数,判断了请求是否有效,判断了缓存是否可以被查找,最后执行了HttpTransact::StartAccessControl函数,并且以HttpTransact::StartAccessControl->HttpTransact::HandleRequestAuthorized->HttpTransact::DecideCacheLookup的调用关系最后执行了TRANSACT_RETURN函数,第一个参数CACHE_LOOKUP,第二个参数NULL。在接下来的HttpSM::set_next_state函数中,首先为continuation函数设置了回调函数HttpSM::state_cache_open_read,之后执行了HttpSM::do_cache_lookup_and_read函数,HttpSM::do_cache_lookup_and_read函数执行完了之后会触发HttpSM::state_cache_open_read函数。HttpSM::do_cache_lookup_and_read函数执行了查缓存操作,如果缓存查询命中了,HttpSM::state_cache_open_read函数的event是CACHE_EVENT_OPEN_READ,最后执行了call_transact_and_set_next_state函数,并且带了一个参数,HttpTransact::HandleCacheOpenRead函数。HttpTransact::HandleCacheOpenRead函数确认是不是真的缓存命中了,最后执行了TRANSACT_RETURN函数,第一个参数HTTP_API_READ_CACHE_HDR,第二个参数HttpTransact::HandleCacheOpenReadHitFreshness。
第五个state是HttpTransact::HTTP_API_READ_CACHE_HDR,这个阶段call了上一次TRANSACT_RETURN函数的第二个参数HttpTransact::HandleCacheOpenReadHitFreshness函数,HttpTransact::HandleCacheOpenReadHitFreshness函数判断缓存是否过期了,如果没有过期最后执行了TRANSACT_RETURN函数,第一个参数为HTTP_API_CACHE_LOOKUP_COMPLETE,第二个参数是HttpTransact::HandleCacheOpenReadHit。随后的HttpSM::set_next_state函数直接把t_state.next_action赋值给了t_state.api_next_action,赋值完了t_state.api_next_action的值为HTTP_API_CACHE_LOOKUP_COMPLETE。
第六个state是HttpTransact::HTTP_API_CACHE_LOOKUP_COMPLETE,这个阶段call了HttpTransact::HandleCacheOpenReadHit函数,这个函数判断了缓存的东西是不是可以直接返回,因为有可能需要有刷新之类的操作。如果可以直接返回,执行了HttpTransact::build_response_from_cache函数。这个函数生成了响应数据,并且将HttpSM::next_action设置为SERVE_FROM_CACHE,在HttpSM::set_next_state中设置t_state.api_next_action 为HttpTransact::HTTP_API_SEND_REPONSE_HDR
第七个state是HttpTransact::HTTP_API_SEND_REPONSE_HDR,这个阶段并没有执行HttpSM::call_transact_and_set_next_state函数,只是设置了一个回调函数就break了。函数并没有退出HttpSM::handle_api_return,HttpSM::handle_api_return函数有两个switch,第一个是状态机的入口,以api_next_state为判断依据,第二个以next_state为依据。此时next_state是HttpTransact::SERVE_FROM_CACHE,执行了HttpSM::setup_cache_read_transfer函数,函数中设置了状态机的回调函数为HttpSM::tunnel_handler。之后一直执行,会执行write_to_net函数,并触发HttpSM::tunnel_handler函数。HttpSM::tunnel_handler函数执行会间接的执行到HttpSM::kill_this函数,函数中将t_state.api_next_action设置为最后一个state,HttpTransact::HTTP_API_SM_SHUTDOWN。
第八个state是HttpTransact::HTTP_API_SM_SHUTDOWN,执行清理操作,直接结束