简体   繁体   English

在 NFS 上查询 boost interprocess::file_lock

[英]Query on boost interprocess::file_lock on NFS

We have an application which user can run to generate some data at user specified path.我们有一个应用程序,用户可以运行它以在用户指定的路径上生成一些数据。 This unique output data is generated with respect to one unique input data-set - this input data is provided by the user.这个独特的 output 数据是针对一个独特的输入数据集生成的 - 该输入数据由用户提供。

When we initially developed the application, we never anticipated that number of unique input data-set will be large (due to nature of application).当我们最初开发应用程序时,我们从未预料到唯一输入数据集的数量会很大(由于应用程序的性质)。 Our expectation was number of unique input data-set could be of order of 10 where as one user has this as 1000. So, that particular user started 1000 jobs of our application on grid and all writing data to same path.我们的预期是唯一输入数据集的数量可能为 10 个,而一个用户的数量为 1000 个。因此,该特定用户在网格上启动了我们应用程序的 1000 个作业,并且所有数据都写入相同的路径。 Note - these 1000 jobs are not fired from our application and rather he spawned 1000 processes of our application on different machines.注意 - 这 1000 个工作不是从我们的应用程序中解雇的,而是他在不同的机器上产生了我们应用程序的 1000 个进程。

Now this lead to some collision and data loss.现在这会导致一些冲突和数据丢失。

To guard against it, I am planning to synchronization using boost::interprocess.为了防止它,我计划使用 boost::interprocess 进行同步。 This is what I am planning:这就是我的计划:

// usual processing of input data ...

boost::filesystem::path reportLockFilePath(boost::filesystem::system_complete(userDir));
rerportLockFilePath.append("report.lock");

// if lock file does not exist, create one
if (!boost::filesystem::exists(reportLockFilePath) {
    boost::interprocess::named_mutex reportLockMutex(boost::interprocess::open_or_create, "report_mutex");
    boost::interprocess::scoped_lock< boost::interprocess::named_mutex > lock(reportLockMutex);

    std::ofstream lockStrm(reportLockFilePath.string().c_str());
    lockStrm << "## report lock file ##" << std::endl;
    lockStrm.flush();
}

 boost::interprocess::file_lock reportFileLock(reportLockFilePath.string().c_str());
 boost::interprocess::scoped_lock< boost::interprocess::file_lock > lock(reportFileLock);

 // usual reporting code that we already have ...

Now, questions are -现在,问题是——

  • If this is correct synchronization for the problem at hand如果这是手头问题的正确同步
  • If this synchronization scheme will work, when jobs are on different machines and path is on NFS如果此同步方案有效,当作业在不同的机器上并且路径在 NFS 上时
  • If on NFS etc., this is not going to work, what are the C++ alternatives?如果在 NFS 等上,这不起作用,C++ 的替代品是什么? I prefer to avoid lower level C functions to avoid race condition due to lock being held when one instance of execution crashes etc.我更喜欢避免使用较低级别的 C 函数,以避免在一个执行实例崩溃等时由于锁定而导致的竞争条件。

I just removed the named mutex part (as that was causing problem on few machines due to permission issue - probably related to umask issue discussed in this context in some other post) and replaced with我刚刚删除了命名的互斥体部分(因为这在少数机器上由于权限问题而导致问题 - 可能与其他帖子中在此上下文中讨论的 umask 问题有关)并替换为

std::ofstream lockStrm(reportLockFilePath.string().c_str(), std::ios_base::app);
    

And it worked at least in our internal testing.它至少在我们的内部测试中起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM