美文网首页
php-fpm reload流程

php-fpm reload流程

作者: 一切都女子 | 来源:发表于2018-08-15 14:33 被阅读0次

    php-fpm.conf

    process_control_timeout 里面有这个参数,默认是0,这个参数跟reload有极大关系

    php-fpm reload 简介

    php-fpm 通过接受用户发送的SIGUSR2信号实现自身服务的reload

    kill -USR2 `cat /tmp/php-fpm.pid`
    

    以下代码均来自 php-src-PHP-7.1.0 版本

    主进程信号处理

    主进程收到reload信号调用信号回掉函数:

    fpm/fpm_signals.c

    static void sig_handler(int signo) /* {{{ */
    {
    static const char sig_chars[NSIG + 1] = {
        [SIGTERM] = 'T',
        [SIGINT]  = 'I',
        [SIGUSR1] = '1',
        [SIGUSR2] = '2',
        [SIGQUIT] = 'Q',
        [SIGCHLD] = 'C'
    };
    char s;
    int saved_errno;
    
    if (fpm_globals.parent_pid != getpid()) {
        /* prevent a signal race condition when child process
            have not set up it's own signal handler yet */
        return;
    }
    
    saved_errno = errno;
    s = sig_chars[signo];
    write(sp[1], &s, sizeof(s));
    errno = saved_errno;
    }
    

    信号处理函数的主要作用是向套接字 sp[1] 中写入信号 2 。在sp[0]上注册了事件回掉函数fpm_got_signal

    fpm/fpm_events.c

    函数fpm_got_signal 从sp[0] 读取信号值,执行相应的操作

    case '2' :                  /* SIGUSR2 */
                zlog(ZLOG_DEBUG, "received SIGUSR2");
                zlog(ZLOG_NOTICE, "Reloading in progress ...");
                //下面这掉语句进行重启操纵
                fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
                break;
    

    fpm/fpm_process_ctl.c

    void fpm_pctl(int new_state, int action) /* {{{ */
    

    {
    switch (action) {
    case FPM_PCTL_ACTION_SET :
    if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
    return;
    }

            switch (fpm_state) { /* check which states can be overridden */
                case FPM_PCTL_STATE_NORMAL :
                    /* 'normal' can be overridden by any other state */
                    break;
                case FPM_PCTL_STATE_RELOADING :
                    /* 'reloading' can be overridden by 'finishing' */
                    if (new_state == FPM_PCTL_STATE_FINISHING) break;
                case FPM_PCTL_STATE_FINISHING :
                    /* 'reloading' and 'finishing' can be overridden by 'terminating' */
                    if (new_state == FPM_PCTL_STATE_TERMINATING) break;
                case FPM_PCTL_STATE_TERMINATING :
                    /* nothing can override 'terminating' state */
                    zlog(ZLOG_DEBUG, "not switching to '%s' state, because already in '%s' state",
                        fpm_state_names[new_state], fpm_state_names[fpm_state]);
                    return;
            }
    
            fpm_signal_sent = 0;
            fpm_state = new_state;
    
            zlog(ZLOG_DEBUG, "switching to '%s' state", fpm_state_names[fpm_state]);
            /* fall down */
    
        case FPM_PCTL_ACTION_TIMEOUT :
            fpm_pctl_action_next();
            break;
        case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
            fpm_pctl_action_last();
            break;
    
      }
    }
    

    case FPM_PCTL_ACTION_SET 没有break,所以会接着向下执行

    case FPM_PCTL_ACTION_TIMEOUT:
      //下面这个函数会向所有的子进程发送结束信号
     fpm_pctl_action_next();
    

    fpm_pctl_action_next 函数实现:

    static void fpm_pctl_action_next() /* {{{ */
    {
            int sig, timeout;
        
            if (!fpm_globals.running_children) {
                fpm_pctl_action_last();
            }
        
            if (fpm_signal_sent == 0) {
                if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
                    sig = SIGTERM;
                } else {
                    sig = SIGQUIT;
                }
                timeout = fpm_global_config.process_control_timeout;
            } else {
                if (fpm_signal_sent == SIGQUIT) {
                    sig = SIGTERM;
                } else {
                    sig = SIGKILL;
                }
                timeout = 1;
            }
        
            fpm_pctl_kill_all(sig);
            fpm_signal_sent = sig;
            //设置定时器
            fpm_pctl_timeout_set(timeout);
    }
    

    第一次向子进程发送 SIGQUIT ,同时注册定时器时间,timeout的值为'fpm_global_config.process_control_timeout'。在规定时间之内子进程还没有结束,则再向子进程发送SIGTERM信号,再次设置定时器,timeout 值设为1秒。如果在1秒之内还没有结束,则直接向子进程发送SIGKILL信号,强制杀死。

    子进程信号处理

    子进程只对SIGQUIT 信号做了捕获,其他信号使用默认处理方式
    fpm/fpm_signals.c

    int fpm_signals_init_child() /* {{{ */
    {
            struct sigaction act, act_dfl;
        
            memset(&act, 0, sizeof(act));
            memset(&act_dfl, 0, sizeof(act_dfl));
        
            act.sa_handler = &sig_soft_quit;
            act.sa_flags |= SA_RESTART;
        
            act_dfl.sa_handler = SIG_DFL;//默认信号处理程序
        
            close(sp[0]);
            close(sp[1]);
        
            if (0 > sigaction(SIGTERM,  &act_dfl,  0) ||
                0 > sigaction(SIGINT,   &act_dfl,  0) ||
                0 > sigaction(SIGUSR1,  &act_dfl,  0) ||
                0 > sigaction(SIGUSR2,  &act_dfl,  0) ||
                0 > sigaction(SIGCHLD,  &act_dfl,  0) ||
                0 > sigaction(SIGQUIT,  &act,      0)) {
        
                zlog(ZLOG_SYSERROR, "failed to init child signals: sigaction()");
                return -1;
            }
        
            zend_signal_init();
            return 0;
    }
    

    SIGQUIT 信号处理函数 sig_soft_quit 最终会调用 fcgi_terminate 将 in_shutdown 设置为1,不在接受任何请求处理。

    主进程等待子进程结束之后

    最后master 等待所有的子进程结束,执行 execvp(saved_argv[0], saved_argv); 根据之前保存的启动参数重新启动一个进程,然后调用exit(FPM_EXIT_SOFTWARE); 退出。另外在处理socket的时候,新的进程会继承 socket fds. (on PARENT EXEC we want socket fds to be inherited through environment variable)

    总结

    php-fpm 收到reload信号,会向所有子进程发送SIGGUIT信号,同时注册一个定时器,在规定的时间之内子进程没有退出,接着在发送SIGTERM信号,结束子进程。如果在一秒之内子进程还是没结束 直接发送SIGKILL 强制杀死。子进程收到SIGQUIT信号,停止接受请求,继续完成正在执行的请求,如果没有在父进程规定的时间结束,会收到SIGTERM信号(自己行对SIGTERM 信号采取的是默认处理程序),结束生命。理论上子进程在收到SIGKILL 肯定结束了。

    相关文章

      网友评论

          本文标题:php-fpm reload流程

          本文链接:https://www.haomeiwen.com/subject/rnhwbftx.html