Before you say Nginx, take a look at what is a "surprise group". In short, multithreaded/multi-process (Linux thread process is not much different) waiting for the same socket event, when this event occurs, these threads/processes are simultaneously awakened, is a surprise group. It can be expected that inefficient, many processes are being restarted by the kernel to respond to this event, and of course only one process can handle the success of the event, and other processes will hibernate after the event has failed (there are other options). This kind of performance wasting phenomenon is the startled group.
Panic groups usually occur on the server, when the parent process binds a port to listen for the socket, and then fork out multiple child processes, and the subprocess begins to loop (like accept) the socket. Each time a user initiates a TCP connection, the multiple child processes are awakened at the same time, and then one of the child processes accept the new connection succeeds, and the remainder fails and sleeps again.
Well, we can't just use one process to accept new connections. These new connections are then processed by synchronous methods such as Message Queuing, so that the surprise group is not avoided. Yes, the surprise group is avoided, but inefficient, because the process can only be used to accept connections. For multi-core machines, there is only one process to accept, which is what programmers create accept bottlenecks. So, I still insist on the need for multiple processes to handle accept events.
In fact, on the linux2.6 kernel, the Accept system call is no longer a surprise group (at least I don't exist on the 2.6.18 kernel version). You can write a simple program to try, Bind,listen in the parent process, and then fork out the child process, all of the child processes are accept this listener handle. In this way, when a new connection comes along, you will find that only one child process returns the new connection, and the other child processes continue to hibernate on the accept call without being awakened.
Unfortunately, usually our program is not so simple, will not be willing to block on the accept call, we have many other network read and write events to deal with Linux, we love to use Epoll to solve non-blocking sockets. So, even if the accept call is not surprised group, we also have to deal with the surprise group of this matter, because epoll have this problem. The test program mentioned above, if we are not blocking call accept within the subprocess, but with epoll_wait, we will find that when the new connection comes, many of the child processes will be awakened after epoll_wait.
Nginx, the master process listens to the port number (for example, 80), and all Nginx worker processes start using epoll_wait to handle new events (under Linux), and if a new connection arrives without any protection, there will be multiple worker processes in the Epoll Wake up after _wait and find yourself accept failure. Now, we can see how the Nginx deal with the surprise group problem.
Each worker process in Nginx handles events in function ngx_process_events_and_timers (void) ngx_process_events (cycle, timer, flags); Encapsulates different event-handling mechanisms , the epoll_wait call is encapsulated by default on Linux. Let's take a look at what Ngx_process_events_and_timers did to solve the surprise group:
void Ngx_process_events_and_timers (ngx_cycle_t *cycle) {... Ngx_use_accept_mutex indicates whether it is necessary to add a lock to the accept to solve the problem of surprise group. When the Nginx worker process number >1 and the Accept_mutex is opened in the configuration file, this flag is set to 1 if (Ngx_use_accept_mutex) {//ngx_accept_disabled to indicate full load at this time, not necessary To deal with the new connection, we have configured the maximum number of connections that each Nginx worker process can handle in nginx.conf, and when the maximum 7/8 is reached, ngx_accept_disabled is positive, indicating that the Nginx worker process is very busy,
The new connection will no longer be processed, which is also a simple load-balancing if (ngx_accept_disabled > 0) {ngx_accept_disabled--; else {//Get accept Lock, multiple worker only one can get this lock. Access to the lock is not a blocking process, both are immediately returned, the success of the words Ngx_accept_mutex_held was placed 1.
Getting the lock means that the listening handle is placed in the epoll of this process, and if the lock is not received, the listener handle is removed from the epoll.
if (Ngx_trylock_accept_mutex (cycle) = = Ngx_error) {return; //Get the lock, put flag as ngx_post_events, which means that in the Ngx_process_events function, any events will be deferred processing, the accept events will be placed in the Ngx_posted_accept_events list, Epollin|epollout events are placed in the Ngx_posted_events list if (Ngx_accept_mutex_held) {flags |= ngx_post_event
S else {//Can not get the lock, and will not handle the supervisorListen to the handle, this timer is actually passed to the Epoll_wait timeout, modified to the maximum ngx_accept_mutex_delay means that the epoll_wait a shorter timeout return, so that the new connection is not processed for a long time if (timer = = Ngx_timer_infinite | |
Timer > Ngx_accept_mutex_delay) {timer = Ngx_accept_mutex_delay;
} } } } 。。。 。。。
Linux, call the Ngx_epoll_process_events function to start processing (void) ngx_process_events (cycle, timer, flags); If there is data in the Ngx_posted_accept_events list, start accept to create a new connection if (ngx_posted_accept_events) {ngx_event_process_posted (cycl
E, &ngx_posted_accept_events);
//Release the lock and then process the following Epollin epollout request if (Ngx_accept_mutex_held) {ngx_shmtx_unlock (&ngx_accept_mutex);
} if (delta) {ngx_event_expire_timers ();
NGX_LOG_DEBUG1 (ngx_log_debug_event, Cycle->log, 0, "posted events%p", ngx_posted_events); The normal data read and write requests are then processed. Because these requests take a long time, the ngx_post_events flag in the Ngx_process_events the event into the Ngx_posted_events list, delay to the lock release and then placeActing
if (ngx_posted_events) {if (ngx_threaded) {ngx_wakeup_worker_thread (cycle);
else {ngx_event_process_posted (cycle, &ngx_posted_events); } } }
As you can see from the note above, no matter how many nginx worker processes there are, only one worker process can add a listener handle to its own epoll at the same time. This processing accept NGINX worker process flag to ngx_post_events, so that it is ngx_process_events in the next function (Linux) Events function) does not immediately handle the event, deferred, first processed all the Accept events, release the lock, and then handle the normal read and write socket events. Let's see what ngx_epoll_process_events does:
Static ngx_int_t ngx_epoll_process_events (ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags) {...
Events = Epoll_wait (EP, Event_list, (int) nevents, timer);
Ngx_mutex_lock (Ngx_posted_events_mutex);
for (i = 0; i < events; i++) {c = event_list[i].data.ptr;
。。。 。。。
Rev = c->read;
if (Revents & Epollin) && rev->active) {... If you have a ngx_post_events flag, put the Accept event in the ngx_posted_accept_events queue, and put the normal events in the Ngx_posted_events queue to defer processing if (flags ;
ngx_post_events) {queue = (ngx_event_t * *) (rev->accept?)
&ngx_posted_accept_events: &ngx_posted_events);
Ngx_locked_post_event (rev, queue);
else {Rev->handler (rev);
}} Wev = c->write;
if (Revents & epollout) && wev->active) {...
Similarly, if there is a ngx_post_events mark, write event deferred processing, put in the ngx_posted_events queue if (Flags & ngx_post_events) {ngx_locked_post_event (Wev, &ngx_posted_events);
else {Wev->handler (WEV);
}} ngx_mutex_unlock (Ngx_posted_events_mutex);
return NGX_OK; }
Look at the circumstances under which Ngx_use_accept_mutex will be opened:
if (Ccf->master && ccf->worker_processes > 1 && ecf->accept_mutex) {
Ngx_use_accept_ Mutex = 1;
Ngx_accept_mutex_held = 0;
Ngx_accept_mutex_delay = ecf->accept_mutex_delay;
} else {
Ngx_use_accept_mutex = 0;
}
When the Nginx worker number is greater than 1 o'clock, which means that multiple processes may accept the same listener handle, the Ngx_use_accept_mutex is set to 1 if the Accept_mutex switch is turned on in the configuration file.
Then look at some of the load-balancing effects of how the ngx_accept_disabled is maintained, in the Ngx_event_accept function:
ngx_accept_disabled = NGX_CYCLE->CONNECTION_N/8
-ngx_cycle->free_connection_n;
shows that when the number of connections that have been used accounts for more than 7/8 of the total number of worker_connections configured in nginx.conf, ngx_accept_disabled is positive, and this worker will ngx_accept_disabled minus 1, And the new connection is no longer processed this time.
Finally, let's look at how the Ngx_trylock_accept_mutex function is played:
ngx_int_t
Ngx_trylock_accept_mutex (ngx_cycle_t *cycle)
{
//ngx_shmtx_trylock is non-blocking lock, returns 1 for success, 0 for locks not taken
if (Ngx_shmtx_trylock (&ngx_accept_mutex)) {
//ngx_enable_accept_events will plug the Listener handle into the epoll of the worker process
if (ngx_enable_accept_events (cycle) = = Ngx_error) {
ngx_shmtx_unlock (&ngx_accept_mutex);
return ngx_error;
}
Ngx_accept_mutex_held to 1, said to get the lock, return
ngx_accept_events = 0;
Ngx_accept_mutex_held = 1;
return NGX_OK;
}
Handle the logic that does not get the lock, Ngx_disable_accept_events will remove the listener handle from the Epoll
if (ngx_accept_mutex_held) {
if (ngx_disable_ Accept_events (cycle) = = Ngx_error) {return
ngx_error;
}
Ngx_accept_mutex_held = 0;
}
return NGX_OK;
}
OK, about the details of the lock is how to achieve, this article is limited to the length of the story, the next post to say. Now we know how nginx deal with the surprise group. Simply put, only one nginx worker is allowed to handle the listening handle in its own epoll at the same time. Its load balance is also very simple, when the maximum connection 7/8, this worker will not try to get accept lock, and not to deal with the new connection, so that other nginx worker process has a better chance to process the listener handle, establish a new connection. Furthermore, because of the timeout, the worker process that does not get the lock has a higher frequency of getting locks.