2016-05-19 23:40:54 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2016-05-19 23:40:54 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2020-09-23 19:54:29 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
#include "port/win/io_win.h"
|
|
|
|
|
2021-08-16 14:30:57 +00:00
|
|
|
#include "env_win.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "monitoring/iostats_context_imp.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/sync_point.h"
|
2016-05-19 23:40:54 +00:00
|
|
|
#include "util/aligned_buffer.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "util/coding.h"
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-05-19 23:40:54 +00:00
|
|
|
namespace port {
|
|
|
|
|
2017-01-09 23:37:57 +00:00
|
|
|
/*
|
2021-01-06 18:48:24 +00:00
|
|
|
* DirectIOHelper
|
|
|
|
*/
|
2017-01-09 23:37:57 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const size_t kSectorSize = 512;
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline bool IsPowerOfTwo(const size_t alignment) {
|
2017-01-09 23:37:57 +00:00
|
|
|
return ((alignment) & (alignment - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline bool IsAligned(size_t alignment, const void* ptr) {
|
2017-01-09 23:37:57 +00:00
|
|
|
return ((uintptr_t(ptr)) & (alignment - 1)) == 0;
|
|
|
|
}
|
2021-01-06 18:48:24 +00:00
|
|
|
} // namespace
|
2017-01-09 23:37:57 +00:00
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
std::string GetWindowsErrSz(DWORD err) {
|
|
|
|
LPSTR lpMsgBuf;
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
2021-01-06 18:48:24 +00:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
|
|
|
0, // Default language
|
|
|
|
reinterpret_cast<LPSTR>(&lpMsgBuf), 0, NULL);
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
std::string Err = lpMsgBuf;
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
return Err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We preserve the original name of this interface to denote the original idea
|
|
|
|
// behind it.
|
|
|
|
// All reads happen by a specified offset and pwrite interface does not change
|
|
|
|
// the position of the file pointer. Judging from the man page and errno it does
|
|
|
|
// execute
|
|
|
|
// lseek atomically to return the position of the file back where it was.
|
|
|
|
// WriteFile() does not
|
|
|
|
// have this capability. Therefore, for both pread and pwrite the pointer is
|
|
|
|
// advanced to the next position
|
|
|
|
// which is fine for writes because they are (should be) sequential.
|
|
|
|
// Because all the reads/writes happen by the specified offset, the caller in
|
|
|
|
// theory should not
|
|
|
|
// rely on the current file offset.
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus pwrite(const WinFileData* file_data, const Slice& data,
|
|
|
|
uint64_t offset, size_t& bytes_written) {
|
|
|
|
IOStatus s;
|
2018-05-01 20:38:36 +00:00
|
|
|
bytes_written = 0;
|
|
|
|
|
|
|
|
size_t num_bytes = data.size();
|
|
|
|
if (num_bytes > std::numeric_limits<DWORD>::max()) {
|
|
|
|
// May happen in 64-bit builds where size_t is 64-bits but
|
|
|
|
// long is still 32-bit, but that's the API here at the moment
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::InvalidArgument(
|
|
|
|
"num_bytes is too large for a single write: " + file_data->GetName());
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
OVERLAPPED overlapped = {0};
|
2016-05-19 23:40:54 +00:00
|
|
|
ULARGE_INTEGER offsetUnion;
|
|
|
|
offsetUnion.QuadPart = offset;
|
|
|
|
|
|
|
|
overlapped.Offset = offsetUnion.LowPart;
|
|
|
|
overlapped.OffsetHigh = offsetUnion.HighPart;
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
DWORD bytesWritten = 0;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
if (FALSE == WriteFile(file_data->GetFileHandle(), data.data(),
|
|
|
|
static_cast<DWORD>(num_bytes), &bytesWritten,
|
|
|
|
&overlapped)) {
|
2018-05-01 20:38:36 +00:00
|
|
|
auto lastError = GetLastError();
|
|
|
|
s = IOErrorFromWindowsError("WriteFile failed: " + file_data->GetName(),
|
2021-01-06 18:48:24 +00:00
|
|
|
lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
} else {
|
2018-05-01 20:38:36 +00:00
|
|
|
bytes_written = bytesWritten;
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
return s;
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See comments for pwrite above
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus pread(const WinFileData* file_data, char* src, size_t num_bytes,
|
|
|
|
uint64_t offset, size_t& bytes_read) {
|
|
|
|
IOStatus s;
|
2018-05-01 20:38:36 +00:00
|
|
|
bytes_read = 0;
|
|
|
|
|
|
|
|
if (num_bytes > std::numeric_limits<DWORD>::max()) {
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::InvalidArgument(
|
|
|
|
"num_bytes is too large for a single read: " + file_data->GetName());
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
OVERLAPPED overlapped = {0};
|
2016-05-19 23:40:54 +00:00
|
|
|
ULARGE_INTEGER offsetUnion;
|
|
|
|
offsetUnion.QuadPart = offset;
|
|
|
|
|
|
|
|
overlapped.Offset = offsetUnion.LowPart;
|
|
|
|
overlapped.OffsetHigh = offsetUnion.HighPart;
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
DWORD bytesRead = 0;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
if (FALSE == ReadFile(file_data->GetFileHandle(), src,
|
|
|
|
static_cast<DWORD>(num_bytes), &bytesRead,
|
|
|
|
&overlapped)) {
|
2018-05-01 20:38:36 +00:00
|
|
|
auto lastError = GetLastError();
|
|
|
|
// EOF is OK with zero bytes read
|
|
|
|
if (lastError != ERROR_HANDLE_EOF) {
|
|
|
|
s = IOErrorFromWindowsError("ReadFile failed: " + file_data->GetName(),
|
2021-01-06 18:48:24 +00:00
|
|
|
lastError);
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
} else {
|
2018-05-01 20:38:36 +00:00
|
|
|
bytes_read = bytesRead;
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
return s;
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFileInformationByHandle() is capable of fast pre-allocates.
|
|
|
|
// However, this does not change the file end position unless the file is
|
|
|
|
// truncated and the pre-allocated space is not considered filled with zeros.
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus fallocate(const std::string& filename, HANDLE hFile,
|
|
|
|
uint64_t to_size) {
|
|
|
|
IOStatus status;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
FILE_ALLOCATION_INFO alloc_info;
|
|
|
|
alloc_info.AllocationSize.QuadPart = to_size;
|
|
|
|
|
|
|
|
if (!SetFileInformationByHandle(hFile, FileAllocationInfo, &alloc_info,
|
2021-01-06 18:48:24 +00:00
|
|
|
sizeof(FILE_ALLOCATION_INFO))) {
|
2016-05-19 23:40:54 +00:00
|
|
|
auto lastError = GetLastError();
|
|
|
|
status = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"Failed to pre-allocate space: " + filename, lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus ftruncate(const std::string& filename, HANDLE hFile, uint64_t toSize) {
|
|
|
|
IOStatus status;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
FILE_END_OF_FILE_INFO end_of_file;
|
|
|
|
end_of_file.EndOfFile.QuadPart = toSize;
|
|
|
|
|
|
|
|
if (!SetFileInformationByHandle(hFile, FileEndOfFileInfo, &end_of_file,
|
2021-01-06 18:48:24 +00:00
|
|
|
sizeof(FILE_END_OF_FILE_INFO))) {
|
2016-05-19 23:40:54 +00:00
|
|
|
auto lastError = GetLastError();
|
|
|
|
status = IOErrorFromWindowsError("Failed to Set end of file: " + filename,
|
2021-01-06 18:48:24 +00:00
|
|
|
lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-10-12 01:18:14 +00:00
|
|
|
size_t GetUniqueIdFromFile(HANDLE /*hFile*/, char* /*id*/,
|
|
|
|
size_t /*max_size*/) {
|
|
|
|
// Returning 0 is safe as it causes the table reader to generate a unique ID.
|
|
|
|
// This is suboptimal for performance as it prevents multiple table readers
|
|
|
|
// for the same file from sharing cached blocks. For example, if users have
|
|
|
|
// a low value for `max_open_files`, there can be many table readers opened
|
|
|
|
// for the same file.
|
|
|
|
//
|
|
|
|
// TODO: this is a temporarily solution as it is safe but not optimal for
|
|
|
|
// performance. For more details see discussion in
|
|
|
|
// https://github.com/facebook/rocksdb/pull/5844.
|
|
|
|
return 0;
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 14:30:57 +00:00
|
|
|
WinFileData::WinFileData(const std::string& filename, HANDLE hFile,
|
|
|
|
bool direct_io)
|
|
|
|
: filename_(filename),
|
|
|
|
hFile_(hFile),
|
|
|
|
use_direct_io_(direct_io),
|
|
|
|
sector_size_(WinFileSystem::GetSectorSize(filename)) {}
|
|
|
|
|
|
|
|
bool WinFileData::IsSectorAligned(const size_t off) const {
|
|
|
|
return (off & (sector_size_ - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WinMmapReadableFile
|
|
|
|
|
2016-12-22 20:51:29 +00:00
|
|
|
WinMmapReadableFile::WinMmapReadableFile(const std::string& fileName,
|
|
|
|
HANDLE hFile, HANDLE hMap,
|
|
|
|
const void* mapped_region,
|
|
|
|
size_t length)
|
|
|
|
: WinFileData(fileName, hFile, false /* use_direct_io */),
|
|
|
|
hMap_(hMap),
|
|
|
|
mapped_region_(mapped_region),
|
|
|
|
length_(length) {}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
WinMmapReadableFile::~WinMmapReadableFile() {
|
2018-02-02 20:14:42 +00:00
|
|
|
BOOL ret __attribute__((__unused__));
|
|
|
|
ret = ::UnmapViewOfFile(mapped_region_);
|
2017-06-12 20:08:57 +00:00
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
ret = ::CloseHandle(hMap_);
|
|
|
|
assert(ret);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapReadableFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& /*options*/, Slice* result,
|
|
|
|
char* scratch,
|
|
|
|
IODebugContext* /*dbg*/) const {
|
|
|
|
IOStatus s;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (offset > length_) {
|
|
|
|
*result = Slice();
|
2016-10-13 23:36:34 +00:00
|
|
|
return IOError(filename_, EINVAL);
|
2016-05-19 23:40:54 +00:00
|
|
|
} else if (offset + n > length_) {
|
2018-09-06 01:07:53 +00:00
|
|
|
n = length_ - static_cast<size_t>(offset);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
2021-01-06 18:48:24 +00:00
|
|
|
*result = Slice(reinterpret_cast<const char*>(mapped_region_) + offset, n);
|
2016-05-19 23:40:54 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapReadableFile::InvalidateCache(size_t offset, size_t length) {
|
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t WinMmapReadableFile::GetUniqueId(char* id, size_t max_size) const {
|
|
|
|
return GetUniqueIdFromFile(hFile_, id, max_size);
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinMmapFile
|
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
// Can only truncate or reserve to a sector size aligned if
|
|
|
|
// used on files that are opened with Unbuffered I/O
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::TruncateFile(uint64_t toSize) {
|
2016-05-19 23:40:54 +00:00
|
|
|
return ftruncate(filename_, hFile_, toSize);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::UnmapCurrentRegion() {
|
|
|
|
IOStatus status;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (mapped_begin_ != nullptr) {
|
|
|
|
if (!::UnmapViewOfFile(mapped_begin_)) {
|
|
|
|
status = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"Failed to unmap file view: " + filename_, GetLastError());
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move on to the next portion of the file
|
|
|
|
file_offset_ += view_size_;
|
|
|
|
|
|
|
|
// UnmapView automatically sends data to disk but not the metadata
|
|
|
|
// which is good and provides some equivalent of fdatasync() on Linux
|
|
|
|
// therefore, we donot need separate flag for metadata
|
|
|
|
mapped_begin_ = nullptr;
|
|
|
|
mapped_end_ = nullptr;
|
|
|
|
dst_ = nullptr;
|
|
|
|
|
|
|
|
last_sync_ = nullptr;
|
|
|
|
pending_sync_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::MapNewRegion(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
IOStatus status;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
assert(mapped_begin_ == nullptr);
|
|
|
|
|
2018-09-06 01:07:53 +00:00
|
|
|
size_t minDiskSize = static_cast<size_t>(file_offset_) + view_size_;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (minDiskSize > reserved_size_) {
|
2021-01-06 18:48:24 +00:00
|
|
|
status = Allocate(file_offset_, view_size_, options, dbg);
|
2016-05-19 23:40:54 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to remap
|
|
|
|
if (hMap_ == NULL || reserved_size_ > mapping_size_) {
|
|
|
|
if (hMap_ != NULL) {
|
|
|
|
// Unmap the previous one
|
2018-02-02 20:14:42 +00:00
|
|
|
BOOL ret __attribute__((__unused__));
|
2017-10-23 21:20:53 +00:00
|
|
|
ret = ::CloseHandle(hMap_);
|
2016-05-19 23:40:54 +00:00
|
|
|
assert(ret);
|
|
|
|
hMap_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULARGE_INTEGER mappingSize;
|
|
|
|
mappingSize.QuadPart = reserved_size_;
|
|
|
|
|
|
|
|
hMap_ = CreateFileMappingA(
|
2021-01-06 18:48:24 +00:00
|
|
|
hFile_,
|
|
|
|
NULL, // Security attributes
|
|
|
|
PAGE_READWRITE, // There is not a write only mode for mapping
|
|
|
|
mappingSize.HighPart, // Enable mapping the whole file but the actual
|
|
|
|
// amount mapped is determined by MapViewOfFile
|
|
|
|
mappingSize.LowPart,
|
|
|
|
NULL); // Mapping name
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (NULL == hMap_) {
|
|
|
|
return IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"WindowsMmapFile failed to create file mapping for: " + filename_,
|
|
|
|
GetLastError());
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mapping_size_ = reserved_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULARGE_INTEGER offset;
|
|
|
|
offset.QuadPart = file_offset_;
|
|
|
|
|
|
|
|
// View must begin at the granularity aligned offset
|
|
|
|
mapped_begin_ = reinterpret_cast<char*>(
|
2021-01-06 18:48:24 +00:00
|
|
|
MapViewOfFileEx(hMap_, FILE_MAP_WRITE, offset.HighPart, offset.LowPart,
|
|
|
|
view_size_, NULL));
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (!mapped_begin_) {
|
|
|
|
status = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"WindowsMmapFile failed to map file view: " + filename_,
|
|
|
|
GetLastError());
|
2016-05-19 23:40:54 +00:00
|
|
|
} else {
|
|
|
|
mapped_end_ = mapped_begin_ + view_size_;
|
|
|
|
dst_ = mapped_begin_;
|
|
|
|
last_sync_ = mapped_begin_;
|
|
|
|
pending_sync_ = false;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::PreallocateInternal(uint64_t spaceToReserve) {
|
2016-05-19 23:40:54 +00:00
|
|
|
return fallocate(filename_, hFile_, spaceToReserve);
|
|
|
|
}
|
|
|
|
|
Optionally wait on bytes_per_sync to smooth I/O (#5183)
Summary:
The existing implementation does not guarantee bytes reach disk every `bytes_per_sync` when writing SST files, or every `wal_bytes_per_sync` when writing WALs. This can cause confusing behavior for users who enable this feature to avoid large syncs during flush and compaction, but then end up hitting them anyways.
My understanding of the existing behavior is we used `sync_file_range` with `SYNC_FILE_RANGE_WRITE` to submit ranges for async writeback, such that we could continue processing the next range of bytes while that I/O is happening. I believe we can preserve that benefit while also limiting how far the processing can get ahead of the I/O, which prevents huge syncs from happening when the file finishes.
Consider this `sync_file_range` usage: `sync_file_range(fd_, 0, static_cast<off_t>(offset + nbytes), SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE)`. Expanding the range to start at 0 and adding the `SYNC_FILE_RANGE_WAIT_BEFORE` flag causes any pending writeback (like from a previous call to `sync_file_range`) to finish before it proceeds to submit the latest `nbytes` for writeback. The latest `nbytes` are still written back asynchronously, unless processing exceeds I/O speed, in which case the following `sync_file_range` will need to wait on it.
There is a second change in this PR to use `fdatasync` when `sync_file_range` is unavailable (determined statically) or has some known problem with the underlying filesystem (determined dynamically).
The above two changes only apply when the user enables a new option, `strict_bytes_per_sync`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5183
Differential Revision: D14953553
Pulled By: siying
fbshipit-source-id: 445c3862e019fb7b470f9c7f314fc231b62706e9
2019-04-22 18:48:45 +00:00
|
|
|
WinMmapFile::WinMmapFile(const std::string& fname, HANDLE hFile,
|
|
|
|
size_t page_size, size_t allocation_granularity,
|
2021-01-06 18:48:24 +00:00
|
|
|
const FileOptions& options)
|
Optionally wait on bytes_per_sync to smooth I/O (#5183)
Summary:
The existing implementation does not guarantee bytes reach disk every `bytes_per_sync` when writing SST files, or every `wal_bytes_per_sync` when writing WALs. This can cause confusing behavior for users who enable this feature to avoid large syncs during flush and compaction, but then end up hitting them anyways.
My understanding of the existing behavior is we used `sync_file_range` with `SYNC_FILE_RANGE_WRITE` to submit ranges for async writeback, such that we could continue processing the next range of bytes while that I/O is happening. I believe we can preserve that benefit while also limiting how far the processing can get ahead of the I/O, which prevents huge syncs from happening when the file finishes.
Consider this `sync_file_range` usage: `sync_file_range(fd_, 0, static_cast<off_t>(offset + nbytes), SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE)`. Expanding the range to start at 0 and adding the `SYNC_FILE_RANGE_WAIT_BEFORE` flag causes any pending writeback (like from a previous call to `sync_file_range`) to finish before it proceeds to submit the latest `nbytes` for writeback. The latest `nbytes` are still written back asynchronously, unless processing exceeds I/O speed, in which case the following `sync_file_range` will need to wait on it.
There is a second change in this PR to use `fdatasync` when `sync_file_range` is unavailable (determined statically) or has some known problem with the underlying filesystem (determined dynamically).
The above two changes only apply when the user enables a new option, `strict_bytes_per_sync`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5183
Differential Revision: D14953553
Pulled By: siying
fbshipit-source-id: 445c3862e019fb7b470f9c7f314fc231b62706e9
2019-04-22 18:48:45 +00:00
|
|
|
: WinFileData(fname, hFile, false),
|
2021-01-06 18:48:24 +00:00
|
|
|
FSWritableFile(options),
|
Optionally wait on bytes_per_sync to smooth I/O (#5183)
Summary:
The existing implementation does not guarantee bytes reach disk every `bytes_per_sync` when writing SST files, or every `wal_bytes_per_sync` when writing WALs. This can cause confusing behavior for users who enable this feature to avoid large syncs during flush and compaction, but then end up hitting them anyways.
My understanding of the existing behavior is we used `sync_file_range` with `SYNC_FILE_RANGE_WRITE` to submit ranges for async writeback, such that we could continue processing the next range of bytes while that I/O is happening. I believe we can preserve that benefit while also limiting how far the processing can get ahead of the I/O, which prevents huge syncs from happening when the file finishes.
Consider this `sync_file_range` usage: `sync_file_range(fd_, 0, static_cast<off_t>(offset + nbytes), SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE)`. Expanding the range to start at 0 and adding the `SYNC_FILE_RANGE_WAIT_BEFORE` flag causes any pending writeback (like from a previous call to `sync_file_range`) to finish before it proceeds to submit the latest `nbytes` for writeback. The latest `nbytes` are still written back asynchronously, unless processing exceeds I/O speed, in which case the following `sync_file_range` will need to wait on it.
There is a second change in this PR to use `fdatasync` when `sync_file_range` is unavailable (determined statically) or has some known problem with the underlying filesystem (determined dynamically).
The above two changes only apply when the user enables a new option, `strict_bytes_per_sync`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5183
Differential Revision: D14953553
Pulled By: siying
fbshipit-source-id: 445c3862e019fb7b470f9c7f314fc231b62706e9
2019-04-22 18:48:45 +00:00
|
|
|
hMap_(NULL),
|
|
|
|
page_size_(page_size),
|
|
|
|
allocation_granularity_(allocation_granularity),
|
|
|
|
reserved_size_(0),
|
|
|
|
mapping_size_(0),
|
|
|
|
view_size_(0),
|
|
|
|
mapped_begin_(nullptr),
|
|
|
|
mapped_end_(nullptr),
|
|
|
|
dst_(nullptr),
|
|
|
|
last_sync_(nullptr),
|
|
|
|
file_offset_(0),
|
|
|
|
pending_sync_(false) {
|
2016-05-19 23:40:54 +00:00
|
|
|
// Allocation granularity must be obtained from GetSystemInfo() and must be
|
|
|
|
// a power of two.
|
|
|
|
assert(allocation_granularity > 0);
|
|
|
|
assert((allocation_granularity & (allocation_granularity - 1)) == 0);
|
|
|
|
|
|
|
|
assert(page_size > 0);
|
|
|
|
assert((page_size & (page_size - 1)) == 0);
|
|
|
|
|
|
|
|
// Only for memory mapped writes
|
|
|
|
assert(options.use_mmap_writes);
|
|
|
|
|
|
|
|
// View size must be both the multiple of allocation_granularity AND the
|
|
|
|
// page size and the granularity is usually a multiple of a page size.
|
2021-01-06 18:48:24 +00:00
|
|
|
const size_t viewSize =
|
|
|
|
32 * 1024; // 32Kb similar to the Windows File Cache in buffered mode
|
2016-05-19 23:40:54 +00:00
|
|
|
view_size_ = Roundup(viewSize, allocation_granularity_);
|
|
|
|
}
|
|
|
|
|
|
|
|
WinMmapFile::~WinMmapFile() {
|
|
|
|
if (hFile_) {
|
2021-01-06 18:48:24 +00:00
|
|
|
this->Close(IOOptions(), nullptr);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::Append(const Slice& data, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
2016-05-19 23:40:54 +00:00
|
|
|
const char* src = data.data();
|
|
|
|
size_t left = data.size();
|
|
|
|
|
|
|
|
while (left > 0) {
|
|
|
|
assert(mapped_begin_ <= dst_);
|
|
|
|
size_t avail = mapped_end_ - dst_;
|
|
|
|
|
|
|
|
if (avail == 0) {
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus s = UnmapCurrentRegion();
|
2016-05-19 23:40:54 +00:00
|
|
|
if (s.ok()) {
|
2021-01-06 18:48:24 +00:00
|
|
|
s = MapNewRegion(options, dbg);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t n = std::min(left, avail);
|
|
|
|
memcpy(dst_, src, n);
|
|
|
|
dst_ += n;
|
|
|
|
src += n;
|
|
|
|
left -= n;
|
|
|
|
pending_sync_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure that the last partial page is padded with zeros if needed
|
|
|
|
size_t bytesToPad = Roundup(size_t(dst_), page_size_) - size_t(dst_);
|
|
|
|
if (bytesToPad > 0) {
|
|
|
|
memset(dst_, 0, bytesToPad);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Means Close() will properly take care of truncate
|
|
|
|
// and it does not need any additional information
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::Truncate(uint64_t size, const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::Close(const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
IOStatus s;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
assert(NULL != hFile_);
|
|
|
|
|
|
|
|
// We truncate to the precise size so no
|
|
|
|
// uninitialized data at the end. SetEndOfFile
|
|
|
|
// which we use does not write zeros and it is good.
|
2021-01-06 18:48:24 +00:00
|
|
|
uint64_t targetSize = GetFileSize(options, dbg);
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (mapped_begin_ != nullptr) {
|
|
|
|
// Sync before unmapping to make sure everything
|
|
|
|
// is on disk and there is not a lazy writing
|
|
|
|
// so we are deterministic with the tests
|
2021-01-06 18:48:24 +00:00
|
|
|
Sync(options, dbg);
|
2016-05-19 23:40:54 +00:00
|
|
|
s = UnmapCurrentRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != hMap_) {
|
|
|
|
BOOL ret = ::CloseHandle(hMap_);
|
|
|
|
if (!ret && s.ok()) {
|
|
|
|
auto lastError = GetLastError();
|
|
|
|
s = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"Failed to Close mapping for file: " + filename_, lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hMap_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hFile_ != NULL) {
|
|
|
|
TruncateFile(targetSize);
|
|
|
|
|
|
|
|
BOOL ret = ::CloseHandle(hFile_);
|
|
|
|
hFile_ = NULL;
|
|
|
|
|
|
|
|
if (!ret && s.ok()) {
|
|
|
|
auto lastError = GetLastError();
|
|
|
|
s = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"Failed to close file map handle: " + filename_, lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::Flush(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
// Flush only data
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::Sync(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
IOStatus s;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
// Some writes occurred since last sync
|
|
|
|
if (dst_ > last_sync_) {
|
|
|
|
assert(mapped_begin_);
|
|
|
|
assert(dst_);
|
|
|
|
assert(dst_ > mapped_begin_);
|
|
|
|
assert(dst_ < mapped_end_);
|
|
|
|
|
|
|
|
size_t page_begin =
|
2021-01-06 18:48:24 +00:00
|
|
|
TruncateToPageBoundary(page_size_, last_sync_ - mapped_begin_);
|
2016-05-19 23:40:54 +00:00
|
|
|
size_t page_end =
|
2021-01-06 18:48:24 +00:00
|
|
|
TruncateToPageBoundary(page_size_, dst_ - mapped_begin_ - 1);
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
// Flush only the amount of that is a multiple of pages
|
|
|
|
if (!::FlushViewOfFile(mapped_begin_ + page_begin,
|
2021-01-06 18:48:24 +00:00
|
|
|
(page_end - page_begin) + page_size_)) {
|
2016-05-19 23:40:54 +00:00
|
|
|
s = IOErrorFromWindowsError("Failed to FlushViewOfFile: " + filename_,
|
2021-01-06 18:48:24 +00:00
|
|
|
GetLastError());
|
2016-05-19 23:40:54 +00:00
|
|
|
} else {
|
|
|
|
last_sync_ = dst_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-06 18:48:24 +00:00
|
|
|
* Flush data as well as metadata to stable storage.
|
|
|
|
*/
|
|
|
|
IOStatus WinMmapFile::Fsync(const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
IOStatus s = Sync(options, dbg);
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
// Flush metadata
|
|
|
|
if (s.ok() && pending_sync_) {
|
|
|
|
if (!::FlushFileBuffers(hFile_)) {
|
|
|
|
s = IOErrorFromWindowsError("Failed to FlushFileBuffers: " + filename_,
|
2021-01-06 18:48:24 +00:00
|
|
|
GetLastError());
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
pending_sync_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-06 18:48:24 +00:00
|
|
|
* Get the size of valid data in the file. This will not match the
|
|
|
|
* size that is returned from the filesystem because we use mmap
|
|
|
|
* to extend file by map_size every time.
|
|
|
|
*/
|
|
|
|
uint64_t WinMmapFile::GetFileSize(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-05-19 23:40:54 +00:00
|
|
|
size_t used = dst_ - mapped_begin_;
|
|
|
|
return file_offset_ + used;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::InvalidateCache(size_t offset, size_t length) {
|
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinMmapFile::Allocate(uint64_t offset, uint64_t len,
|
|
|
|
const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
IOStatus status;
|
2021-05-05 22:49:29 +00:00
|
|
|
TEST_KILL_RANDOM("WinMmapFile::Allocate");
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
// Make sure that we reserve an aligned amount of space
|
|
|
|
// since the reservation block size is driven outside so we want
|
|
|
|
// to check if we are ok with reservation here
|
2021-01-06 18:48:24 +00:00
|
|
|
size_t spaceToReserve =
|
|
|
|
Roundup(static_cast<size_t>(offset + len), view_size_);
|
2016-05-19 23:40:54 +00:00
|
|
|
// Nothing to do
|
|
|
|
if (spaceToReserve <= reserved_size_) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOSTATS_TIMER_GUARD(allocate_nanos);
|
|
|
|
status = PreallocateInternal(spaceToReserve);
|
|
|
|
if (status.ok()) {
|
|
|
|
reserved_size_ = spaceToReserve;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WinMmapFile::GetUniqueId(char* id, size_t max_size) const {
|
|
|
|
return GetUniqueIdFromFile(hFile_, id, max_size);
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WinSequentialFile
|
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
WinSequentialFile::WinSequentialFile(const std::string& fname, HANDLE f,
|
2021-01-06 18:48:24 +00:00
|
|
|
const FileOptions& options)
|
2016-12-22 20:51:29 +00:00
|
|
|
: WinFileData(fname, f, options.use_direct_reads) {}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
WinSequentialFile::~WinSequentialFile() {
|
2016-10-13 23:36:34 +00:00
|
|
|
assert(hFile_ != INVALID_HANDLE_VALUE);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinSequentialFile::Read(size_t n, const IOOptions& /*opts*/,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
IOStatus s;
|
2016-05-19 23:40:54 +00:00
|
|
|
size_t r = 0;
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
assert(result != nullptr);
|
|
|
|
if (WinFileData::use_direct_io()) {
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::NotSupported("Read() does not support direct_io");
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
// Windows ReadFile API accepts a DWORD.
|
2018-05-01 20:38:36 +00:00
|
|
|
// While it is possible to read in a loop if n is too big
|
|
|
|
// it is an unlikely case.
|
|
|
|
if (n > std::numeric_limits<DWORD>::max()) {
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::InvalidArgument("n is too big for a single ReadFile: " +
|
|
|
|
filename_);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
DWORD bytesToRead =
|
|
|
|
static_cast<DWORD>(n); // cast is safe due to the check above
|
2016-05-19 23:40:54 +00:00
|
|
|
DWORD bytesRead = 0;
|
2016-10-13 23:36:34 +00:00
|
|
|
BOOL ret = ReadFile(hFile_, scratch, bytesToRead, &bytesRead, NULL);
|
2018-05-01 20:38:36 +00:00
|
|
|
if (ret != FALSE) {
|
2016-05-19 23:40:54 +00:00
|
|
|
r = bytesRead;
|
|
|
|
} else {
|
2018-05-01 20:38:36 +00:00
|
|
|
auto lastError = GetLastError();
|
|
|
|
if (lastError != ERROR_HANDLE_EOF) {
|
2021-01-06 18:48:24 +00:00
|
|
|
s = IOErrorFromWindowsError("ReadFile failed: " + filename_, lastError);
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = Slice(scratch, r);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinSequentialFile::PositionedReadInternal(char* src, size_t numBytes,
|
|
|
|
uint64_t offset,
|
|
|
|
size_t& bytes_read) const {
|
2018-05-01 20:38:36 +00:00
|
|
|
return pread(this, src, numBytes, offset, bytes_read);
|
2017-01-15 21:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinSequentialFile::PositionedRead(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& /*opts*/,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2018-05-01 20:38:36 +00:00
|
|
|
if (!WinFileData::use_direct_io()) {
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::NotSupported("This function is only used for direct_io");
|
2017-01-15 21:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 14:30:57 +00:00
|
|
|
assert(IsSectorAligned(static_cast<size_t>(offset)));
|
|
|
|
assert(IsSectorAligned(static_cast<size_t>(n)));
|
2017-01-15 21:11:04 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
size_t bytes_read = 0; // out param
|
|
|
|
IOStatus s = PositionedReadInternal(scratch, static_cast<size_t>(n), offset,
|
|
|
|
bytes_read);
|
2018-05-01 20:38:36 +00:00
|
|
|
*result = Slice(scratch, bytes_read);
|
2017-01-15 21:11:04 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinSequentialFile::Skip(uint64_t n) {
|
|
|
|
// Can't handle more than signed max as SetFilePointerEx accepts a signed
|
|
|
|
// 64-bit integer. As such it is a highly unlikley case to have n so large.
|
2018-05-01 20:38:36 +00:00
|
|
|
if (n > static_cast<uint64_t>(std::numeric_limits<LONGLONG>::max())) {
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::InvalidArgument(
|
|
|
|
"n is too large for a single SetFilePointerEx() call" + filename_);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER li;
|
2021-01-06 18:48:24 +00:00
|
|
|
li.QuadPart = static_cast<LONGLONG>(n); // cast is safe due to the check
|
|
|
|
// above
|
2016-10-13 23:36:34 +00:00
|
|
|
BOOL ret = SetFilePointerEx(hFile_, li, NULL, FILE_CURRENT);
|
2016-05-19 23:40:54 +00:00
|
|
|
if (ret == FALSE) {
|
2018-05-01 20:38:36 +00:00
|
|
|
auto lastError = GetLastError();
|
2020-09-23 19:54:29 +00:00
|
|
|
return IOErrorFromWindowsError("Skip SetFilePointerEx():" + filename_,
|
|
|
|
lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinSequentialFile::InvalidateCache(size_t offset, size_t length) {
|
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinRandomAccessBase
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinRandomAccessImpl::PositionedReadInternal(
|
|
|
|
char* src, size_t numBytes, uint64_t offset, size_t& bytes_read) const {
|
2018-05-01 20:38:36 +00:00
|
|
|
return pread(file_base_, src, numBytes, offset, bytes_read);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline WinRandomAccessImpl::WinRandomAccessImpl(WinFileData* file_base,
|
|
|
|
size_t alignment,
|
|
|
|
const FileOptions& options)
|
2021-08-16 14:30:57 +00:00
|
|
|
: file_base_(file_base),
|
|
|
|
alignment_(std::max(alignment, file_base->GetSectorSize())) {
|
2016-05-19 23:40:54 +00:00
|
|
|
assert(!options.use_mmap_reads);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinRandomAccessImpl::ReadImpl(uint64_t offset, size_t n,
|
|
|
|
Slice* result,
|
|
|
|
char* scratch) const {
|
2017-04-27 19:19:55 +00:00
|
|
|
// Check buffer alignment
|
|
|
|
if (file_base_->use_direct_io()) {
|
2021-08-16 14:30:57 +00:00
|
|
|
assert(file_base_->IsSectorAligned(static_cast<size_t>(offset)));
|
|
|
|
assert(IsAligned(alignment_, scratch));
|
2017-04-27 19:19:55 +00:00
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
*result = Slice(scratch, 0);
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
size_t bytes_read = 0;
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus s = PositionedReadInternal(scratch, n, offset, bytes_read);
|
2018-05-01 20:38:36 +00:00
|
|
|
*result = Slice(scratch, bytes_read);
|
2016-05-19 23:40:54 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinRandomAccessFile
|
|
|
|
|
2016-12-22 20:51:29 +00:00
|
|
|
WinRandomAccessFile::WinRandomAccessFile(const std::string& fname, HANDLE hFile,
|
|
|
|
size_t alignment,
|
2021-01-06 18:48:24 +00:00
|
|
|
const FileOptions& options)
|
2016-12-22 20:51:29 +00:00
|
|
|
: WinFileData(fname, hFile, options.use_direct_reads),
|
|
|
|
WinRandomAccessImpl(this, alignment, options) {}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
WinRandomAccessFile::~WinRandomAccessFile() {}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomAccessFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& /*options*/, Slice* result,
|
|
|
|
char* scratch,
|
|
|
|
IODebugContext* /*dbg*/) const {
|
2016-10-13 23:36:34 +00:00
|
|
|
return ReadImpl(offset, n, result, scratch);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomAccessFile::InvalidateCache(size_t offset, size_t length) {
|
|
|
|
return IOStatus::OK();
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
size_t WinRandomAccessFile::GetUniqueId(char* id, size_t max_size) const {
|
|
|
|
return GetUniqueIdFromFile(GetFileHandle(), id, max_size);
|
|
|
|
}
|
|
|
|
|
2017-01-15 21:11:04 +00:00
|
|
|
size_t WinRandomAccessFile::GetRequiredBufferAlignment() const {
|
|
|
|
return GetAlignment();
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WinWritableImpl
|
|
|
|
//
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::PreallocateInternal(uint64_t spaceToReserve) {
|
|
|
|
return fallocate(file_data_->GetName(), file_data_->GetFileHandle(),
|
|
|
|
spaceToReserve);
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline WinWritableImpl::WinWritableImpl(WinFileData* file_data,
|
|
|
|
size_t alignment)
|
|
|
|
: file_data_(file_data),
|
2021-08-16 14:30:57 +00:00
|
|
|
alignment_(std::max(alignment, file_data->GetSectorSize())),
|
2021-01-06 18:48:24 +00:00
|
|
|
next_write_offset_(0),
|
|
|
|
reservedsize_(0) {
|
2017-06-20 17:16:24 +00:00
|
|
|
// Query current position in case ReopenWritableFile is called
|
|
|
|
// This position is only important for buffered writes
|
|
|
|
// for unbuffered writes we explicitely specify the position.
|
|
|
|
LARGE_INTEGER zero_move;
|
2021-01-06 18:48:24 +00:00
|
|
|
zero_move.QuadPart = 0; // Do not move
|
2017-06-20 17:16:24 +00:00
|
|
|
LARGE_INTEGER pos;
|
|
|
|
pos.QuadPart = 0;
|
|
|
|
BOOL ret = SetFilePointerEx(file_data_->GetFileHandle(), zero_move, &pos,
|
2021-01-06 18:48:24 +00:00
|
|
|
FILE_CURRENT);
|
2017-06-20 17:16:24 +00:00
|
|
|
// Querying no supped to fail
|
2018-05-01 20:38:36 +00:00
|
|
|
if (ret != 0) {
|
2017-06-20 17:16:24 +00:00
|
|
|
next_write_offset_ = pos.QuadPart;
|
|
|
|
} else {
|
|
|
|
assert(false);
|
|
|
|
}
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::AppendImpl(const Slice& data) {
|
|
|
|
IOStatus s;
|
2017-01-09 23:37:57 +00:00
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
if (data.size() > std::numeric_limits<DWORD>::max()) {
|
2021-01-06 18:48:24 +00:00
|
|
|
return IOStatus::InvalidArgument("data is too long for a single write" +
|
|
|
|
file_data_->GetName());
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
size_t bytes_written = 0; // out param
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2017-01-13 20:01:08 +00:00
|
|
|
if (file_data_->use_direct_io()) {
|
2017-01-09 23:37:57 +00:00
|
|
|
// With no offset specified we are appending
|
|
|
|
// to the end of the file
|
2021-08-16 14:30:57 +00:00
|
|
|
assert(file_data_->IsSectorAligned(next_write_offset_));
|
|
|
|
assert(file_data_->IsSectorAligned(data.size()));
|
|
|
|
assert(IsAligned(static_cast<size_t>(GetAlignment()), data.data()));
|
|
|
|
s = pwrite(file_data_, data, next_write_offset_, bytes_written);
|
2017-01-09 23:37:57 +00:00
|
|
|
} else {
|
|
|
|
DWORD bytesWritten = 0;
|
|
|
|
if (!WriteFile(file_data_->GetFileHandle(), data.data(),
|
2021-01-06 18:48:24 +00:00
|
|
|
static_cast<DWORD>(data.size()), &bytesWritten, NULL)) {
|
2017-01-09 23:37:57 +00:00
|
|
|
auto lastError = GetLastError();
|
|
|
|
s = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"Failed to WriteFile: " + file_data_->GetName(), lastError);
|
2018-05-01 20:38:36 +00:00
|
|
|
} else {
|
|
|
|
bytes_written = bytesWritten;
|
2017-01-09 23:37:57 +00:00
|
|
|
}
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
2017-01-09 23:37:57 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
if (s.ok()) {
|
2018-05-01 20:38:36 +00:00
|
|
|
if (bytes_written == data.size()) {
|
|
|
|
// This matters for direct_io cases where
|
|
|
|
// we rely on the fact that next_write_offset_
|
|
|
|
// is sector aligned
|
|
|
|
next_write_offset_ += bytes_written;
|
|
|
|
} else {
|
2021-01-06 18:48:24 +00:00
|
|
|
s = IOStatus::IOError("Failed to write all bytes: " +
|
|
|
|
file_data_->GetName());
|
2018-05-01 20:38:36 +00:00
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::PositionedAppendImpl(const Slice& data,
|
|
|
|
uint64_t offset) {
|
|
|
|
if (file_data_->use_direct_io()) {
|
2021-08-16 14:30:57 +00:00
|
|
|
assert(file_data_->IsSectorAligned(static_cast<size_t>(offset)));
|
|
|
|
assert(file_data_->IsSectorAligned(data.size()));
|
|
|
|
assert(IsAligned(static_cast<size_t>(GetAlignment()), data.data()));
|
2017-01-09 23:37:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
size_t bytes_written = 0;
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus s = pwrite(file_data_, data, offset, bytes_written);
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
if (s.ok()) {
|
2018-05-01 20:38:36 +00:00
|
|
|
if (bytes_written == data.size()) {
|
|
|
|
// For sequential write this would be simple
|
|
|
|
// size extension by data.size()
|
|
|
|
uint64_t write_end = offset + bytes_written;
|
|
|
|
if (write_end >= next_write_offset_) {
|
|
|
|
next_write_offset_ = write_end;
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-06 18:48:24 +00:00
|
|
|
s = IOStatus::IOError("Failed to write all of the requested data: " +
|
|
|
|
file_data_->GetName());
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::TruncateImpl(uint64_t size) {
|
2018-05-01 20:38:36 +00:00
|
|
|
// It is tempting to check for the size for sector alignment
|
|
|
|
// but truncation may come at the end and there is not a requirement
|
|
|
|
// for this to be sector aligned so long as we do not attempt to write
|
|
|
|
// after that. The interface docs state that the behavior is undefined
|
|
|
|
// in that case.
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus s =
|
|
|
|
ftruncate(file_data_->GetName(), file_data_->GetFileHandle(), size);
|
2018-05-01 20:38:36 +00:00
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
if (s.ok()) {
|
2017-06-20 17:16:24 +00:00
|
|
|
next_write_offset_ = size;
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::CloseImpl() {
|
|
|
|
IOStatus s;
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
auto hFile = file_data_->GetFileHandle();
|
|
|
|
assert(INVALID_HANDLE_VALUE != hFile);
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2018-05-01 20:38:36 +00:00
|
|
|
if (!::FlushFileBuffers(hFile)) {
|
2016-05-19 23:40:54 +00:00
|
|
|
auto lastError = GetLastError();
|
2021-01-06 18:48:24 +00:00
|
|
|
s = IOErrorFromWindowsError(
|
|
|
|
"FlushFileBuffers failed at Close() for: " + file_data_->GetName(),
|
|
|
|
lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
if (!file_data_->CloseFile() && s.ok()) {
|
2016-05-19 23:40:54 +00:00
|
|
|
auto lastError = GetLastError();
|
2021-01-06 18:48:24 +00:00
|
|
|
s = IOErrorFromWindowsError(
|
|
|
|
"CloseHandle failed for: " + file_data_->GetName(), lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::SyncImpl(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
IOStatus s;
|
|
|
|
if (!::FlushFileBuffers(file_data_->GetFileHandle())) {
|
2016-05-19 23:40:54 +00:00
|
|
|
auto lastError = GetLastError();
|
2016-12-22 20:51:29 +00:00
|
|
|
s = IOErrorFromWindowsError(
|
2021-01-06 18:48:24 +00:00
|
|
|
"FlushFileBuffers failed at Sync() for: " + file_data_->GetName(),
|
|
|
|
lastError);
|
2016-05-19 23:40:54 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
inline IOStatus WinWritableImpl::AllocateImpl(uint64_t offset, uint64_t len) {
|
|
|
|
IOStatus status;
|
2021-05-05 22:49:29 +00:00
|
|
|
TEST_KILL_RANDOM("WinWritableFile::Allocate");
|
2016-05-19 23:40:54 +00:00
|
|
|
|
|
|
|
// Make sure that we reserve an aligned amount of space
|
|
|
|
// since the reservation block size is driven outside so we want
|
|
|
|
// to check if we are ok with reservation here
|
2021-01-06 18:48:24 +00:00
|
|
|
size_t spaceToReserve = Roundup(static_cast<size_t>(offset + len),
|
|
|
|
static_cast<size_t>(alignment_));
|
2016-05-19 23:40:54 +00:00
|
|
|
// Nothing to do
|
|
|
|
if (spaceToReserve <= reservedsize_) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOSTATS_TIMER_GUARD(allocate_nanos);
|
|
|
|
status = PreallocateInternal(spaceToReserve);
|
|
|
|
if (status.ok()) {
|
|
|
|
reservedsize_ = spaceToReserve;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinWritableFile
|
|
|
|
|
2016-12-22 20:51:29 +00:00
|
|
|
WinWritableFile::WinWritableFile(const std::string& fname, HANDLE hFile,
|
|
|
|
size_t alignment, size_t /* capacity */,
|
2021-01-06 18:48:24 +00:00
|
|
|
const FileOptions& options)
|
2016-12-22 20:51:29 +00:00
|
|
|
: WinFileData(fname, hFile, options.use_direct_writes),
|
Optionally wait on bytes_per_sync to smooth I/O (#5183)
Summary:
The existing implementation does not guarantee bytes reach disk every `bytes_per_sync` when writing SST files, or every `wal_bytes_per_sync` when writing WALs. This can cause confusing behavior for users who enable this feature to avoid large syncs during flush and compaction, but then end up hitting them anyways.
My understanding of the existing behavior is we used `sync_file_range` with `SYNC_FILE_RANGE_WRITE` to submit ranges for async writeback, such that we could continue processing the next range of bytes while that I/O is happening. I believe we can preserve that benefit while also limiting how far the processing can get ahead of the I/O, which prevents huge syncs from happening when the file finishes.
Consider this `sync_file_range` usage: `sync_file_range(fd_, 0, static_cast<off_t>(offset + nbytes), SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE)`. Expanding the range to start at 0 and adding the `SYNC_FILE_RANGE_WAIT_BEFORE` flag causes any pending writeback (like from a previous call to `sync_file_range`) to finish before it proceeds to submit the latest `nbytes` for writeback. The latest `nbytes` are still written back asynchronously, unless processing exceeds I/O speed, in which case the following `sync_file_range` will need to wait on it.
There is a second change in this PR to use `fdatasync` when `sync_file_range` is unavailable (determined statically) or has some known problem with the underlying filesystem (determined dynamically).
The above two changes only apply when the user enables a new option, `strict_bytes_per_sync`.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5183
Differential Revision: D14953553
Pulled By: siying
fbshipit-source-id: 445c3862e019fb7b470f9c7f314fc231b62706e9
2019-04-22 18:48:45 +00:00
|
|
|
WinWritableImpl(this, alignment),
|
2021-01-06 18:48:24 +00:00
|
|
|
FSWritableFile(options) {
|
2016-10-13 23:36:34 +00:00
|
|
|
assert(!options.use_mmap_writes);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
WinWritableFile::~WinWritableFile() {}
|
2016-10-13 23:36:34 +00:00
|
|
|
|
2016-12-22 20:51:29 +00:00
|
|
|
// Indicates if the class makes use of direct I/O
|
2021-01-06 18:48:24 +00:00
|
|
|
bool WinWritableFile::use_direct_io() const {
|
|
|
|
return WinFileData::use_direct_io();
|
|
|
|
}
|
2016-10-13 23:36:34 +00:00
|
|
|
|
|
|
|
size_t WinWritableFile::GetRequiredBufferAlignment() const {
|
2021-08-16 14:30:57 +00:00
|
|
|
return static_cast<size_t>(GetAlignment());
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::Append(const Slice& data,
|
|
|
|
const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return AppendImpl(data);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::PositionedAppend(const Slice& data, uint64_t offset,
|
|
|
|
const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return PositionedAppendImpl(data, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to implement this so the file is truncated correctly
|
|
|
|
// when buffered and unbuffered mode
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::Truncate(uint64_t size, const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return TruncateImpl(size);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::Close(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return CloseImpl();
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
// write out the cached data to the OS cache
|
|
|
|
// This is now taken care of the WritableFileWriter
|
|
|
|
IOStatus WinWritableFile::Flush(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
return IOStatus::OK();
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::Sync(const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
return SyncImpl(options, dbg);
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::Fsync(const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
return SyncImpl(options, dbg);
|
|
|
|
}
|
2017-12-22 02:37:27 +00:00
|
|
|
|
|
|
|
bool WinWritableFile::IsSyncThreadSafe() const { return true; }
|
2016-10-13 23:36:34 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
uint64_t WinWritableFile::GetFileSize(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2017-06-20 17:16:24 +00:00
|
|
|
return GetFileNextWriteOffset();
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinWritableFile::Allocate(uint64_t offset, uint64_t len,
|
|
|
|
const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return AllocateImpl(offset, len);
|
|
|
|
}
|
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
size_t WinWritableFile::GetUniqueId(char* id, size_t max_size) const {
|
2016-10-13 23:36:34 +00:00
|
|
|
return GetUniqueIdFromFile(GetFileHandle(), id, max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinRandomRWFile
|
|
|
|
|
2016-12-22 20:51:29 +00:00
|
|
|
WinRandomRWFile::WinRandomRWFile(const std::string& fname, HANDLE hFile,
|
2021-01-06 18:48:24 +00:00
|
|
|
size_t alignment, const FileOptions& options)
|
2016-12-22 20:51:29 +00:00
|
|
|
: WinFileData(fname, hFile,
|
|
|
|
options.use_direct_reads && options.use_direct_writes),
|
|
|
|
WinRandomAccessImpl(this, alignment, options),
|
|
|
|
WinWritableImpl(this, alignment) {}
|
2016-10-13 23:36:34 +00:00
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
bool WinRandomRWFile::use_direct_io() const {
|
|
|
|
return WinFileData::use_direct_io();
|
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
size_t WinRandomRWFile::GetRequiredBufferAlignment() const {
|
2021-08-16 14:30:57 +00:00
|
|
|
assert(WinRandomAccessImpl::GetAlignment() ==
|
|
|
|
WinWritableImpl::GetAlignment());
|
|
|
|
return static_cast<size_t>(WinRandomAccessImpl::GetAlignment());
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomRWFile::Write(uint64_t offset, const Slice& data,
|
|
|
|
const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return PositionedAppendImpl(data, offset);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomRWFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& /*options*/, Slice* result,
|
|
|
|
char* scratch, IODebugContext* /*dbg*/) const {
|
2016-10-13 23:36:34 +00:00
|
|
|
return ReadImpl(offset, n, result, scratch);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomRWFile::Flush(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
return IOStatus::OK();
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomRWFile::Sync(const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
return SyncImpl(options, dbg);
|
2016-10-13 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinRandomRWFile::Close(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
2016-10-13 23:36:34 +00:00
|
|
|
return CloseImpl();
|
|
|
|
}
|
|
|
|
|
2018-05-24 22:05:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinMemoryMappedBufer
|
|
|
|
WinMemoryMappedBuffer::~WinMemoryMappedBuffer() {
|
2019-11-27 05:40:16 +00:00
|
|
|
BOOL ret
|
|
|
|
#if defined(_MSC_VER)
|
2021-01-06 18:48:24 +00:00
|
|
|
= FALSE;
|
2019-11-27 05:40:16 +00:00
|
|
|
#else
|
2021-01-06 18:48:24 +00:00
|
|
|
__attribute__((__unused__));
|
2019-11-27 05:40:16 +00:00
|
|
|
#endif
|
2018-05-24 22:05:00 +00:00
|
|
|
if (base_ != nullptr) {
|
|
|
|
ret = ::UnmapViewOfFile(base_);
|
|
|
|
assert(ret);
|
|
|
|
base_ = nullptr;
|
|
|
|
}
|
|
|
|
if (map_handle_ != NULL && map_handle_ != INVALID_HANDLE_VALUE) {
|
|
|
|
ret = ::CloseHandle(map_handle_);
|
|
|
|
assert(ret);
|
|
|
|
map_handle_ = NULL;
|
|
|
|
}
|
|
|
|
if (file_handle_ != NULL && file_handle_ != INVALID_HANDLE_VALUE) {
|
|
|
|
ret = ::CloseHandle(file_handle_);
|
|
|
|
assert(ret);
|
|
|
|
file_handle_ = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:36:34 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinDirectory
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
IOStatus WinDirectory::Fsync(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
2016-05-19 23:40:54 +00:00
|
|
|
|
Explicitly closing all directory file descriptors (#10049)
Summary:
Currently, the DB directory file descriptor is left open until the deconstruction process (`DB::Close()` does not close the file descriptor). To verify this, comment out the lines between `db_ = nullptr` and `db_->Close()` (line 512, 513, 514, 515 in ldb_cmd.cc) to leak the ``db_'' object, build `ldb` tool and run
```
strace --trace=open,openat,close ./ldb --db=$TEST_TMPDIR --ignore_unknown_options put K1 V1 --create_if_missing
```
There is one directory file descriptor that is not closed in the strace log.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10049
Test Plan: Add a new unit test DBBasicTest.DBCloseAllDirectoryFDs: Open a database with different WAL directory and three different data directories, and all directory file descriptors should be closed after calling Close(). Explicitly call Close() after a directory file descriptor is not used so that the counter of directory open and close should be equivalent.
Reviewed By: ajkr, hx235
Differential Revision: D36722135
Pulled By: littlepig2013
fbshipit-source-id: 07bdc2abc417c6b30997b9bbef1f79aa757b21ff
2022-06-02 01:03:34 +00:00
|
|
|
IOStatus WinDirectory::Close(const IOOptions& /*options*/,
|
|
|
|
IODebugContext* /*dbg*/) {
|
|
|
|
IOStatus s = IOStatus::OK();
|
|
|
|
BOOL ret __attribute__((__unused__));
|
|
|
|
if (handle_ != INVALID_HANDLE_VALUE) {
|
|
|
|
ret = ::CloseHandle(handle_);
|
|
|
|
if (!ret) {
|
|
|
|
auto lastError = GetLastError();
|
|
|
|
s = IOErrorFromWindowsError("Directory closes failed for : " + GetName(),
|
|
|
|
lastError);
|
|
|
|
}
|
|
|
|
handle_ = NULL;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:47:42 +00:00
|
|
|
size_t WinDirectory::GetUniqueId(char* id, size_t max_size) const {
|
|
|
|
return GetUniqueIdFromFile(handle_, id, max_size);
|
|
|
|
}
|
2016-10-13 23:36:34 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
/// WinFileLock
|
|
|
|
|
2016-05-19 23:40:54 +00:00
|
|
|
WinFileLock::~WinFileLock() {
|
2018-02-02 20:14:42 +00:00
|
|
|
BOOL ret __attribute__((__unused__));
|
2017-10-23 21:20:53 +00:00
|
|
|
ret = ::CloseHandle(hFile_);
|
2016-05-19 23:40:54 +00:00
|
|
|
assert(ret);
|
|
|
|
}
|
|
|
|
|
2021-01-06 18:48:24 +00:00
|
|
|
} // namespace port
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2020-09-23 19:54:29 +00:00
|
|
|
|
|
|
|
#endif
|