2021-01-26 13:29:45 +00:00
|
|
|
#include <Disks/LocalDirectorySyncGuard.h>
|
2022-07-13 13:29:22 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2021-01-26 13:29:45 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Disks/IDisk.h>
|
2022-07-13 13:29:22 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2021-01-26 13:29:45 +00:00
|
|
|
#include <fcntl.h> // O_RDWR
|
|
|
|
|
|
|
|
/// OSX does not have O_DIRECTORY
|
|
|
|
#ifndef O_DIRECTORY
|
|
|
|
#define O_DIRECTORY O_RDWR
|
|
|
|
#endif
|
|
|
|
|
2022-07-13 13:29:22 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event DirectorySync;
|
|
|
|
extern const Event DirectorySyncElapsedMicroseconds;
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:29:45 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_FSYNC;
|
|
|
|
extern const int FILE_DOESNT_EXIST;
|
|
|
|
extern const int CANNOT_OPEN_FILE;
|
|
|
|
extern const int CANNOT_CLOSE_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalDirectorySyncGuard::LocalDirectorySyncGuard(const String & full_path)
|
|
|
|
: fd(::open(full_path.c_str(), O_DIRECTORY))
|
|
|
|
{
|
|
|
|
if (-1 == fd)
|
|
|
|
throwFromErrnoWithPath("Cannot open file " + full_path, full_path,
|
|
|
|
errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalDirectorySyncGuard::~LocalDirectorySyncGuard()
|
|
|
|
{
|
2022-07-13 13:29:22 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::DirectorySync);
|
|
|
|
|
2021-01-26 13:29:45 +00:00
|
|
|
try
|
|
|
|
{
|
2022-07-13 13:29:22 +00:00
|
|
|
Stopwatch watch;
|
|
|
|
|
2021-01-26 13:29:45 +00:00
|
|
|
#if defined(OS_DARWIN)
|
|
|
|
if (fcntl(fd, F_FULLFSYNC, 0))
|
|
|
|
throwFromErrno("Cannot fcntl(F_FULLFSYNC)", ErrorCodes::CANNOT_FSYNC);
|
2021-11-12 07:49:21 +00:00
|
|
|
#else
|
2021-11-10 09:23:46 +00:00
|
|
|
if (-1 == ::fdatasync(fd))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_FSYNC, "Cannot fdatasync");
|
2021-11-12 07:49:21 +00:00
|
|
|
#endif
|
2021-01-26 13:29:45 +00:00
|
|
|
if (-1 == ::close(fd))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_CLOSE_FILE, "Cannot close file");
|
2022-07-13 13:29:22 +00:00
|
|
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::DirectorySyncElapsedMicroseconds, watch.elapsedMicroseconds());
|
2021-01-26 13:29:45 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|