yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

YII in nas hard disk has file lock issue #17734

Closed xiaoyi510 closed 4 years ago

xiaoyi510 commented 4 years ago

What steps will reproduce the problem?

What is the expected result?

What do you get instead?

Additional info

Q A
Yii version 2.0.14
PHP version 7.2.19
Operating system CentOS

Yii in Nas hard disk has file lock stuck issue The above problems will occur in the following file write operations 1.Log 2.Runtime 3.assets Now moving the Runtime Log file directory through configuration items but assets did not find a temporary solution

tomlutzenberger commented 4 years ago

Do the directories have the correct permissions set? They must be writable for the webserver user (in most cases www-data). Try chmod -R 775 <your_web_root>/runtime on the commandline (same with web/assets)

xiaoyi510 commented 4 years ago

I now have three node serversPHP code is stored on NAS shared hard diskWe currently have the problem that when YII writes log file data, multiple computers write at the same time, the file is locked and cannot be recoveredOnly 504 timeout

xiaoyi510 commented 4 years ago

Has nothing to do with file permissions Instead, multiple nodes write a log file at the same time, causing the file to be locked

tomlutzenberger commented 4 years ago

Then this seems to be an issue of your infrastructure configuration. As far as I know, Yii does not perform any file locking. Can you confirm, @samdark ? You could try to log to a dedicated DB/Schema instead of a single file.

gagatust commented 4 years ago

Are you trying to write to a file from different processes at the same time without synchronization? What do you expect?

xiaoyi510 commented 4 years ago

I mean If multiple processes write logs The flock in the yii log will be stuck and will not be restored! It will not be restored!

samdark commented 4 years ago

@xiaoyi510 would you please tell us more about your setup? How these nodes are configured? Is process manager used? Are these on separate IPs? What is the file system?

yii-bot commented 4 years ago

Thanks for posting in our issue tracker. In order to properly assist you, we need additional information:

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

xiaoyi510 commented 4 years ago

Code in vendor/yiisoft/yii2/log/FileTarget.php:111

    /**
     * Writes log messages to a file.
     * Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
     * @throws InvalidConfigException if unable to open the log file for writing
     * @throws LogRuntimeException if unable to write complete log to file
     */
    public function export()
    {
        $logPath = dirname($this->logFile);
        FileHelper::createDirectory($logPath, $this->dirMode, true);

        $text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
        if (($fp = @fopen($this->logFile, 'a')) === false) {
            throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
        }
        @flock($fp, LOCK_EX);
        if ($this->enableRotation) {
            // clear stat cache to ensure getting the real current file size and not a cached one
            // this may result in rotating twice when cached file size is used on subsequent calls
            clearstatcache();
        }

@flock($fp, LOCK_EX); Write log this line of code will be stuck in multiple processes

kamarton commented 4 years ago

The code is not the best.

I suggessts

$fp = fopen(...);
if(!...) { throw }
try {
  @flock($fp, LOCK_EX);
  ... throw ... throw ...
} finally {
            @flock($fp, LOCK_UN);
            @fclose($fp);
}
if(...) {
  $this->roratelog() ...
}
kamarton commented 4 years ago

Write log this line of code will be stuck in multiple processes

In product environment?

xiaoyi510 commented 4 years ago

Write log this line of code will be stuck in multiple processes

In product environment?

yes im in test environment not found this bug This problem was only discovered in my prod environment with NAS hard drives System on NAS disk Multiple PHP processes stuck at runtime / log and open / written lock files

kamarton commented 4 years ago

slow IO, disable debug component in product environment.

samdark commented 4 years ago

@xiaoyi510 would you please answer my questions?

xiaoyi510 commented 4 years ago

slow IO, disable debug component in product environment.

prod evn closing debug

xiaoyi510 commented 4 years ago

@xiaoyi510 would you please answer my questions? @samdark

For configuration items, YII_ENV = prod YII_DEBUG = false runtime log file path default

I used a load balancer, a total of three nodes, using SSD nas hard disks,

rob006 commented 4 years ago

NFS has known limitations regarding locking: https://stackoverflow.com/q/218451/5812455

samdark commented 4 years ago

@rob006 it's NAS, not NFS.

rob006 commented 4 years ago

@samdark What do you mean by NAS? NAS is just a server which shares disk storage by network (probably using NFS protocol).

mikk150 commented 4 years ago

@rob006 it could use iscsi, but @xiaoyi510 I wouldn't use filetarget that way. yii\log\SyslogTarget would be much better solution for you. Better yet, look at Graylog

kamarton commented 4 years ago

@rob006 it's NAS, not NFS.

@xiaoyi510 Does NAS run PHP directly or only use it for network storage?

samdark commented 4 years ago

What's the file system used?

mikk150 commented 4 years ago

What's the file system used?

if it is Centos, then probably XFS, but then again, if file system is mounted over network to NFS/ISCSI/SMB it could be literally anything

samdark commented 4 years ago

@xiaoyi510 sorry but I wasn't able to get/reproduce the problem :(

It could be FS issue but could be Yii code. If so, there should be something like "Unable to export log through file" in the log but it wasn't mentioned.

jk2K commented 4 years ago

I encountered the same problem, CentOS 7.6, Kubernetes

mikehaertl commented 4 years ago

Many NAS use the SMB protocol which is also known to have file locking issues: https://serverfault.com/questions/204812/how-to-prevent-samba-from-holding-a-file-lock-after-a-client-disconnects

Actually I would question a setup where a NAS drive is used for logging. You'd never want to do something like that in a production setup. If space is limited you should better move the rotated logfiles to the NAS (e.g. in a cronjob) and keep logfiles on the machine.

mikk150 commented 4 years ago

I encountered the same problem, CentOS 7.6, Kubernetes

What PersistentVolume/StorageClass