Home Backend Development PHP Tutorial 一个Lararel队列引发的报警

一个Lararel队列引发的报警

Jun 20, 2016 pm 12:35 PM

一台服务器报警了,内存占用过高,奇怪的是集群里其它的服务器都没问题。不过从以往的经验来看:每一个匪夷所思的问题背后,都隐藏着一个啼笑皆非的答案。

首先通过「free -m」确认一下内存情况,发现用掉了 6893M,还剩 976M:

free

然后通过「top」查看一下哪些进程占用内存多,通过「shift + m」按内存排序:

top

虽然通过 free 命令我们能确认系统可用内存不足,但是通过 top 命令我们却没有发现有内存占用大户的进程,那么内存到底都去哪里了呢?

开头我们提到过,集群里只有一台服务器有问题,其它服务器皆正常,于是我们比较了一下问题服务器和正常服务器的进程列表,结果发现问题服务器多了几个进程:

/usr/local/bin/php artisan queue:listen

/usr/local/bin/php artisan queue:work

经过确认,它们是 Laravel 队列,虽然直觉告诉我们问题与其有关联,但是进程本身并没有占用多少内存,在不能立刻确诊原因的情况下,我们用排除法把队列换到另外一台正常的服务器上看看会不会重现问题,过了一会,果然再次出现同样问题。

既然 free,top 之类的命令不能确认内存的去向,那么我们不妨看看「meminfo」:

meminfo

如上图所示,大量内存被 Slab 消耗了,更进一步讲是被 SReclaimable 消耗了,也就是说内存被一些可回收的 Slab 消耗了,更进一步的信息可以通过「slabtop」获取:

slabtop

大量内存被 dentry 消耗了,如果你也跟我一样,搞不清楚它意味着什么,搜索吧,能翻墙用 Google,不能翻墙用 AOL,反正别用百度,我找到如下介绍:

  • Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决

  • Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决(续)

简而言之,内存 dentry 里缓存了最近访问过的文件信息,如果频繁的操作大量文件,那么 dentry 就会不断的增加,于是问题就变为确认 Laravel 队列有没有类似问题。

前面提到过,Laravel 队列有一个 listen 进程,还有一个 work 进程,从名字我们就能判断出来,前者是主进程,后者是子进程,子进程是干活的进程,可是当我直接 strace 跟踪子进程的时候,却提示我子进程不存在,进一步调试发现,原来子进程会不断重启!

既然我们不好直接跟踪子进程,那么不妨从父进程入手跟踪子进程的文件操作:

shell> strace -f -e trace=open,close,stat,unlink -p $(    ps aux | grep "[q]ueue:listen" | awk '{print $2}')
Copy after login

可惜 Laravel 本身号称是巨匠框架,依赖一坨一坨的文件,所以跟踪结果里充斥着大量框架文件本身正常的 open,close,stat 操作,改为只跟踪 unlik 操作试试:

shell> strace -f -e trace=unlink -p $(    ps aux | grep "[q]ueue:listen" | awk '{print $2}')
Copy after login

发现 Laravel 队列频繁的执行删除文件操作,每重启一次子进程就执行一次删除:

unlink(“/tmp/.ZendSem.aXaa3Z”)

unlink(“/tmp/.ZendSem.teQG0Y”)

unlink(“/tmp/.ZendSem.Bn3ien”)

unlink(“/tmp/.ZendSem.V4s8RX”)

unlink(“/tmp/.ZendSem.PnNuTN”)

因为临时文件的名字各不相同,所以消耗了大量的 dentry 缓存。查阅 Laravel 队列的 文档 ,发现 Laravel 队列实际上也提供了不重启的守护进程模式,这样就不会频繁创建大量临时文件,进而也就不会消耗大量的 dentry 缓存,推荐使用。

如果频繁创建大量临时文件的情况无法避免,那么按照 Linux 文档 的描述,我们可以通过设置 drop_caches 为 2 来删除可回收的 slab(包括 dentries 和 inodes),较粗野:

shell> echo 2 > /proc/sys/vm/drop_caches
Copy after login

此外还可以通过设置 vfs_cache_pressure 大于 100 来增加回收概率,较温柔:

shell> echo 1000 > /proc/sys/vm/vfs_cache_pressure
Copy after login

从测试结果看,vfs_cache_pressure 的作用有限,当然也可能是我姿势不对。还有一些资料说 min_free_kbytes 也可以解决此类问题,不过鉴于这个参数有一定危险性,我建议大家别乱改,这里就不多说了,有兴趣的可以自行查阅相关资料。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

See all articles