如果 2 个或更多服务器尝试同时写入同一个文件,NFS 中会发生什么情况?

发布于 2024-09-25 17:56:26 字数 242 浏览 8 评论 0原文

我正在开发一个可以自动调整图像大小的 PHP Web 应用程序,并且我正在考虑将缓存的副本存储在 NFS 安装的 NAS 上,这样当图像更新时我可以轻松刷新缓存。

我唯一担心的是,如果集群中的 2 个或更多服务器尝试同时创建相同的图像缓存文件,NFS 通常会发生什么情况?

当缓存因内容更新而刷新时,它们很有可能会发生这样的冲突,但我没有很好的方法来测试开发中的这种情况,因为我只在一个盒子上工作。

有谁有这方面的经验吗?

I'm working on a PHP webapp that does automatic resizing of images and I'm thinking of storing the cached copies on the NFS mounted NAS so it's easy for me to flush the cache when images are updated.

The only thing I'm worried about is what happens in general with NFS if 2 or more of the servers in the cluster are trying to create the same image cache file at the same time?

There's a pretty good chance that when the cache gets flushed for content updates that they could collide like that, but I don't have a great way to test this scenario in development because I'm only working on a single box.

Anyone with experience on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2024-10-02 17:56:26

这取决于您打开文件的方式。如果您以“追加”模式打开文件,那么 Unix/Linux 实际上会将内容写入缓存,直到您创建换行符,然后它将新行粘贴到文件末尾(覆盖“文件结束”字节模式)并写入一个新的“文件结束”。在这种情况下,如果两个人尝试同时写入同一个文件,那么两条写入行都会通过,并按照接收的顺序一次附加一行。因此,您可以期待类似的情况:

This was the old contents
of the file
The first script added
The second script added
this line (script 1)
this line (script 2)

在两个“写入”命令完全同时到达(精确到纳秒精度)的罕见情况下,操作系统实际上会创建一个中断状态。这取决于操作系统如何处理这个问题,但大多数只会生成两个随机数来决定谁先走。

如果您以“写入”模式打开文件(假设您想在中间添加内容),那么您实际上必须锁定文件才能执行此操作。第二个 PHP 脚本将抛出错误,指出无法打开该文件。

It depends on how you open the file. If you open the file in "append" mode then Unix/Linux will actually write the contents to a cache until you create a new-line character, then it sticks the new line on to the end of the file (over-writing the "end-of-file" byte pattern) and writes a new "end-of-file". In this case if two people try to write to the same file simultaneously then both of the write lines will go through, attaching themselves one line at a time in the order that they were received. Thus you could expect something like:

This was the old contents
of the file
The first script added
The second script added
this line (script 1)
this line (script 2)

In the rare chance that two "write" commands arrive at EXACTLY the same time (down to nanosecond precision) then the Operating System actually creates an interrupt state. It's up to the OS how it handles this, but most will just generate two random numbers to decide who goes first.

If you open the file in "write" mode (say you wanted to add content to the middle) then you actually have to lock the file to do this. The second PHP script will throw an error saying that it couldn't open the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文