On the morning of the same day, operations colleagues reported a serious fault, which resulted in a very slow website page speed and basic unavailability. Engineers began to trace the problem. First, introduce the system architecture: front-end
On the morning of the same day, operations colleagues reported a serious fault, which resulted in a very slow website page speed and basic unavailability. Engineers began to trace the problem.
First, introduce the system architecture: front-end Apache, intermediate PHP, backend MySQL, and classic LAMP architecture.
Guessing database performance problems
First, we suspect that the database data volume is too large. We regularly clean up databases to ensure that the data volume of a single table is within a certain range. During this period, the data volume may be too large. Execute the delete statement immediately to reduce the number of records in a single table. However, the problem persists.
Later, DBA found that there was filesort in the slow query. Decisively add indexes and slow queries disappear. However, the problem persists.
In desperation, I doubt the network problems of the data center. However, no problem exists in other product lines, and no fault notification is reported in the network group. Therefore, reject this possibility.
Offline environment recurrence fault
At five o'clock P.M., I decided to reorganize my thinking and observe the fault phenomenon repeatedly. Summary:
- The response speed of the page is unstable, slow or fast, or a little slow, or odd slow, which lacks a regularity.
- Even a colleague's computer refreshed several times quickly.
You cannot obtain valid information from logs because you do not have the permission to view online logs. To facilitate debugging, we decided to deploy code in an online environment and try to reproduce the fault.
As a result, faults are essential in the offline environment. Certainly, it has nothing to do with the network environment and the running status of online memcahced/mysql.
We inserted the debugging code echo 'XXX'; exit; in different locations of the PHP code to try to determine where the code is blocked.
We are surprised to find that when a page is waiting for a response, the subsequent page will inevitably be waiting. In addition, no PHP code is executed on the subsequent page. We infer that the Aapache process is blocked!
Strace Apache process
To find out where the Apache process block is located, use the strace tool to observe the system calls of the Apache process.
First, find the Apache process number.
[root@vm11030032 ~]# ps aux|grep httpdroot 3553 0.0 2.6 312672 13476 ? Ss Aug03 0:03 /usr/local/apache2/sbin/httpd -k restartwww 4759 0.0 4.4 320664 22584 ? S 20:31 0:00 /usr/local/apache2/sbin/httpd -k restartwww 4760 0.0 3.2 316548 16672 ? S 20:31 0:00 /usr/local/apache2/sbin/httpd -k restartwww 4761 0.0 3.2 316548 16672 ? S 20:31 0:00 /usr/local/apache2/sbin/httpd -k restartwww 4762 0.0 3.2 316548 16672 ? S 20:31 0:00 /usr/local/apache2/sbin/httpd -k restartwww 4763 0.1 4.9 319552 25036 ? S 20:31 0:00 /usr/local/apache2/sbin/httpd -k restartwww 4766 0.0 3.2 316548 16672 ? S 20:32 0:00 /usr/local/apache2/sbin/httpd -k restartroot 4890 0.0 0.1 61188 732 pts/3 R+ 20:35 0:00 grep httpdwww 30809 0.0 6.0 327304 30772 ? T 16:43 0:01 /usr/local/apache2/sbin/httpd -k restart
Then, pick a process number and run strace: strace-p 4759.
Then, refresh the page repeatedly, and a request will always be sent to a process with a PID of 4759.
Finally, a request is sent, and the screen is refreshed. the output content is fixed:
connect(107, {sa_family=AF_INET, sin_port=htons(7634), sin_addr=inet_addr("10.73.19.246")}, 16) = -1 EINPROGRESS (Operation now in progress)poll([{fd=107, events=POLLOUT}], 1, 1000) = 1 ([{fd=107, revents=POLLOUT}])connect(107, {sa_family=AF_INET, sin_port=htons(7634), sin_addr=inet_addr("10.73.19.246")}, 16) = 0write(107, "get APPS_SCREEN_API_CURR_TOKEN r"..., 33) = 33read(107, 0x14130e48, 8196) = -1 EAGAIN (Resource temporarily unavailable)poll([{fd=107, events=POLLIN}], 1, 200) = 1 ([{fd=107, revents=POLLIN}])read(107, "VALUE APPS_SCREEN_API_CURR_TOKEN"..., 8196) = 107write(107, "quitrn", 6) = 6read(107, 0x14130e48, 8196) = -1 EAGAIN (Resource temporarily unavailable)poll([{fd=107, events=POLLIN}], 1, 200) = 1 ([{fd=107, revents=POLLIN}])read(107, "", 8196) = 0shutdown(107, 2 /* send and receive */) = 0close(107) = 0open("/dev/random", O_RDONLY) = 107read(107,
Note: the output of the last line is incomplete, indicating that the Apache process is blocked on the read system call. The read object is/dev/random, which seems to be related to the random number. But where does the code use random numbers?
Find key code
According to the IP address and PORT output above, including the call parameters, we are sure this is accessing memcached. As a result, you can find a piece of code after accessing memcached:
$size = mcrypt_get_iv_size (MCRYPT_BLOWFISH,MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM); $m = mcrypt_ecb (MCRYPT_BLOWFISH,$key,$dmcryptText,MCRYPT_DECRYPT,$iv);
The second line of code shows RANDOM. check the php Manual. when the second parameter is MCRYPT_DEV_RANDOM, mcrypt_create_iv may be blocked. MCRYPT_DEV_URANDOM is not blocked.
Online fault resolution
Although I still don't know the specific cause, I decided to replace the parameter and launch it immediately based on the principle of quick solution.
Sure enough, the problem was solved, and the page was never refreshed!
Fault Analysis
The online Apache process is likely to go through the above code, so it is likely to be blocked for a period of time. Therefore, the current page will be slow.
When all Apache processes are blocked, subsequent requests must wait for idle Apache processes, so the subsequent pages will become slow.
The unblock time of the Apache process is uncertain, so the wait time for subsequent pages is also short.
Mining reason
Although the problem is solved, it is still unclear why MCRYPT_DEV_RANDOM is blocked, while MCRYPT_DEV_URANDOM is never blocked.
Google/dev/random, Wikipedia is always reliable:
In this implementation, the generator stores the estimated number of bits from the noise in the entropy pool, and the random number is created from the entropy pool.
During reading, the/dev/random device only returns random bytes of noise data in the entropy pool.
/Dev/random should be applicable to applications that require high quality randomness, such as generating public keys or one-time cryptographic books.
If the entropy pool is empty, read operations on/dev/random will be blocked until sufficient environmental noise is collected [3].
This design makes/dev/random a real random number generator and provides the maximum possible random data entropy. we recommend that you use it to generate keys that protect high value or long protection periods.
A copy of/dev/random is/dev/urandom ("unlocked", non-blocking random number generator [4]). It will reuse data in the internal pool to generate pseudo-random data.
This indicates that read operations on/dev/urandom will not be blocked, but the output entropy may be less than/dev/random.
The device file is a pseudo-random number generator designed for cryptographic security and can be used for applications with low security.
Probably, when/dev/random generates a random number, it depends on the entropy pool. If the entropy pool is empty or not enough, read from/dev/random will be blocked until the entropy pool is sufficient. /Dev/urandom will not be blocked. There must be Loss. the randomness of urandom is weaker than that of random.
Entropy pool
The entropy pool is essentially several bytes. The current size of the entropy pool is stored in/proc/sys/kernel/random/entropy_avail./proc/sys/kernel/random/poolsize is the maximum capacity of the entropy pool, and the unit is bit. If the value of entropy_avail is smaller than the number of random bits to be generated,/dev/random will be blocked.
So why is the entropy pool insufficient?
The entropy pool actually retrieves data from various noice sources, which may be keyboard events, mouse events, and moderate device clock. When the Linux kernel is upgraded from 2.4 to 2.6, some sources are discarded due to security considerations. The source is reduced, and the replenishment speed of the entropy pool is also slow, which is not enough.
In fact, DOS attacks can be constructed by consuming the entropy pool. The principle is very simple. the entropy pool is empty, and services dependent on random numbers (such as SSL and encryption) cannot work normally.
Supplemental entropy pool
When a Linux server is running, there are neither keyboard events nor mouse events. how can we quickly accumulate entropy pools?
After google's profile, we found that some programs can automatically supplement the entropy pool, such as rngd or rng-tools.
I tried rngd on Linode VPS, and the effect was very obvious.
Observe the size of the entropy pool before rngd is started: watch cat/proc/sys/kernel/random/entropy_avail ~ In the range of 200.
Then start rngd: sudo rngd-r/dev/urandom-o/dev/random-f-t 1
The entropy pool immediately soared to 3712, close to the upper limit of 4096.
Summary
Spout first: the system without logs is too nonsense. tracing problems can only be inferred or guessed, delaying a lot of time.
Conclusion: If the business depends on a random number, it is best to use a tool to actively supplement the entropy pool.
Let's try again: the entropy pool is always enough, and today it is not enough. The reason is still unclear. Codenon is really bitter!