mkdir仍然是原子的吗? (安装在SAN上的Windows 7文件系统) [英] Is mkdir still atomic? (Windows 7 filesystems mounted on SAN)

查看:123
本文介绍了mkdir仍然是原子的吗? (安装在SAN上的Windows 7文件系统)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些旧的应用程序,它们通过基于目录的队列进行通信.队列中的每个项目都是一个文件,并且有一个头文件,该文件维护队列中项目的文件名的有序列表.

We have some old applications that communicate via directory-based queues. Each item in the queue is a file, and there's a header file that maintains an ordered list of the filenames for the items in the queue.

自然,此旧代码需要在推送和弹出项目时锁定队列.它的工作是在假定mkdir()是原子操作的情况下创建一个锁子目录-如果多个进程尝试创建目录,则其中只有一个会成功.

Naturally, this old code needs to lock the queue while items are pushed and popped. What it's doing is creating a lock subdirectory, on the assumption that mkdir() is an atomic operation - if multiple processes attempt to create a directory, only one of them is going to succeed.

我的一位同事一直试图解决一个晦涩的问题,他认为原因是,当进程在不同的计算机上运行以及挂载有问题的文件系统时,此锁定不再起作用在SAN上.

One of my co-workers has been trying to chase down an obscure problem, and he thinks the causes is that this locking is no longer working, when the processes are running on different machines, and when the filesystem in question is mounted on a SAN.

他是否有可能是正确的?

Is there any possibility that he might be correct?

推荐答案

我知道一个很老的问题,但我希望有人会发现这个有趣的问题.

Very old question I know, but I hope someone finds this interesting.

使用PowerShell创建共享文件夹用作互斥体时,我也感到困惑,所以我创建了一个测试脚本.

I was also getting confusing results using PowerShell to create a shared folder for use as a mutex, so I created a test script.

function New-FolderMutex {
    try {
        New-Item -ItemType directory -Path .\TheMutex -ErrorAction Stop > $null
        $true
    } catch {
        $false
    }
}

function Remove-FolderMutex {
    Remove-Item -Path .\TheMutex
}

1..100 | % {
    if (New-FolderMutex) {
        Write-Host "Inside loop $_"
        Remove-FolderMutex
    }
}

此脚本在当前目录位于网络共享中时运行.

This script is run while the current directory is in a network share.

当我在两个单独的PowerShell控制台中同时运行此脚本时,从错误消息中很明显该方法注定要失败.即使仅由创建文件夹的进程调用,对Remove-Item的调用也会产生许多不同的错误.似乎在幕后发生了许多非原子步骤.

When I ran this script simultaneously in two separate PowerShell consoles, it was clear from the error messages that the approach was doomed. There are a number of different errors produced by the call to Remove-Item, even though it is being called only by the process that created the folder. It seems that behind the scenes there are a whole bunch of non-atomic steps occurring.

当然,OP询问的是mkdir(可能是系统调用),我的示例使用了更高级别的PowerShell cmdlet,但我希望这会引起人们的兴趣.

Of course, the OP was asking about mkdir (probably the system call) and my example uses the much higher level PowerShell cmdlets, but I hope this is of some interest.

两个过程之一的示例输出(为简便起见进行了编辑)

Example output from one of two processes (edited for brevity)

Inside loop 30
Inside loop 31
Inside loop 32
Inside loop 33
Inside loop 34
Remove-Item : Access to the path 'H:\My Documents\PowerShell\MutexFolder\TheMutex' is denied.
At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5
+     Remove-Item -Path .\TheMutex
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (H:\My Documents...Folder\TheMutex:String) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand

Inside loop 39
Remove-Item : H:\My Documents\PowerShell\MutexFolder\TheMutex is a NTFS junction point. Use the Force parameter to delete or modify.
At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5
+     Remove-Item -Path .\TheMutex
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (H:\My Documents...Folder\TheMutex:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Inside loop 42
Remove-Item : Could not find a part of the path 'H:\My Documents\PowerShell\MutexFolder\TheMutex'.
At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5
+     Remove-Item -Path .\TheMutex
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (H:\My Documents...Folder\TheMutex:String) [Remove-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

Inside loop 44
Inside loop 45

这篇关于mkdir仍然是原子的吗? (安装在SAN上的Windows 7文件系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆