How to correctly publish PHP code almost every PHP programmer has published code, which may be synchronized through ftp or rsync, or updated through svn or git. An active project may release code several times a day, but the reality is that few people pay attention to the details. In fact, there are many pitfalls in it, it is very likely that you are not aware of it.
A correctly implemented publishing system should at least support atomic Publishing. If each version represents an independent state, any request can only be executed in a single state during the release. This is called atomic release. if a request spans different states during the release period, it cannot be called atomic release. For example, assume that two PHP files need to be included in a request:. php and B. php, When include. after php is complete, release the code and then include B. php, if not properly handled, it may lead to an earlier version of. php and new version B. php also exists in the same request. In other words, it does not implement atomic Publishing.
There are many good release code tools in the open-source world, such as capistrano in the ruby community. The process is to release the code to a brand new directory, and then soft link to the real release directory.
.├── current -> releases/v1└── releases ├── v1 │ ├── foo.php │ └── bar.php └── v2 ├── foo.php └── bar.php
However, given the particularity of PHP itself, it would be difficult to implement real atomic release if the above process is simply applied. To clarify the cause, you also need to understand the concept of two caches in PHP:
- Opcode cache
- Realpath cache
Let's talk about opcode cache. we have been using apc earlier. now we mostly choose zend opcode. we are familiar with its role and don't have to talk about it, the only difference between apc and zend opcode is that apc selects the inode of the file and zend opcode selects the path of the file.
Let's talk about realpath cache, which is used to buffer IO operations for getting file information. most of the time it is transparent to us, so that many people do not know its existence, note that the realpath cache is process-level. that is to say, each php-fpm process has its own realpath cache.
The related technical details are very trivial. you are advised to read the following carefully:
- Realpath_cache
- PHP's OPCache extension review
- Atomic deploys at Etsy
- Cache invalidation for scripts in symlinked folders
When using soft links to publish code, most of the first problem we encounter is that the new code does not take effect! Whether apc is enabled. stat or opcache. the validate_timestamps configuration or the apc_clear_cache or opcache_reset method is not valid. restarting php-fpm can solve the problem, but it is too heavy for the script language to restart! Is there no other way except restart?
In fact, this problem occurs mainly because opcode cache obtains file information through realpath cache, even if the soft link has already pointed to a new location, however, if the realpath cache still stores old data, the opcode cache still cannot know the existence of the new code. by default, the realpath_cache_ttl cache is valid for two minutes, which means that after the code is released, it may take two minutes to take effect. To make the release take effect as soon as possible, you need to clear the realpath cache in process units:
This basically works in the apc environment, but there may be problems in the zend opcode environment. Because opcache by default. revalidate_path is disabled. The value of the symbolic link will be cached, which will never take effect even if the soft link points to the modified one. Therefore, when zend opcode is used, if a soft link is used, it is best to set the opcache as needed. revalidate_path is activated.
From this analysis, we may wish to reflect on the fact that sub-release in PHP is a tricky issue because of the conflict between soft links and caches. Both opcode cache and realpath cache are inherent cache features of PHP and cannot be bypassed based on objective needs. Is there a way to bypass soft links and make them a line of defense? The answer is $ realpath_root of nginx:
fastcgi_param SCRIPT_FILENAME $realpath_root $fastcgi_script_name;fastcgi_param DOCUMENT_ROOT $realpath_root;
With $ realpath_root, even if the DOCUMENT_ROOT directory contains soft links, nginx will send the real path to which the soft link points to PHP. that is to say, for PHP, the soft link does not exist anymore! However, at the cost, nginx obtains the value of $ realpath_root through relatively expensive IO operations for each request. through the strace command, we can monitor the process from current to foo:
Realpath
In this example, the stress test shows that the performance is reduced by about 5% after $ realpath_root is used. However, although $ realpath_root causes lstat and readlink operations, however, the number of lstat operations is proportional to the directory depth. that is to say, the deeper the directory, the more times the lstat is executed, the larger the performance decline. If the depth of the release directory can be reduced, the expected performance reduction can be controlled at around 1%.
Deployer is a good tool in PHP. it has many features, such as supporting parallel Publishing. for example, the demonstration is serial on the left and parallel on the right, use "vvv" for more details:
Deploy
However, Deployer has some flaws in atomic release. for details, see deploy: release code:
That is to say, when a soft link is switched, it is first deleted and then created. it is a two-step operation. Theoretically, if a request enters the two steps, the file cannot be found.
At this point, most people will think that using "ln-sfn" is good, but it is actually wrong:
shell> strace ln -sfn releases/foo currentsymlink("releases/foo", "current") = -1 EEXIST (File exists)unlink("current") = 0symlink("releases/foo", "current") = 0
Through strace, we can clearly see that although the use of "ln-sfn" is a step-by-step operation, the internal operation is still performed according to the logic of deleting and then creating, in fact, we should use ln & mv together 」:
shell> ln -sfn releases/foo current.tmpshell> mv -fT current.tmp current
Create a temporary soft link through ln and then perform atomic operations through mv. if strace is used for monitoring, you will find that only one rename operation is executed for the mv "T" option, so it is atomic.