Fixed fake_diskfree option

This commit is contained in:
Takeshi Nakatani 2024-10-03 13:08:44 +00:00 committed by Andrew Gaul
parent a259981f16
commit 4605cc2035
3 changed files with 58 additions and 50 deletions

View File

@ -237,9 +237,8 @@ bool FdManager::CheckCacheDirExist()
return IsDir(cache_dir);
}
off_t FdManager::GetEnsureFreeDiskSpace()
off_t FdManager::GetEnsureFreeDiskSpaceHasLock()
{
const std::lock_guard<std::mutex> lock(FdManager::reserved_diskspace_lock);
return FdManager::free_disk_space;
}
@ -253,9 +252,11 @@ off_t FdManager::SetEnsureFreeDiskSpace(off_t size)
bool FdManager::InitFakeUsedDiskSize(off_t fake_freesize)
{
FdManager::fake_used_disk_space = 0; // At first, clear this value because this value is used in GetFreeDiskSpace.
const std::lock_guard<std::mutex> lock(FdManager::reserved_diskspace_lock);
off_t actual_freesize = FdManager::GetFreeDiskSpace(nullptr);
FdManager::fake_used_disk_space = 0; // At first, clear this value because this value is used in GetFreeDiskSpaceHasLock.
off_t actual_freesize = FdManager::GetFreeDiskSpaceHasLock(nullptr);
if(fake_freesize < actual_freesize){
FdManager::fake_used_disk_space = actual_freesize - fake_freesize;
@ -283,7 +284,7 @@ off_t FdManager::GetTotalDiskSpace(const char* path)
return actual_totalsize;
}
off_t FdManager::GetFreeDiskSpace(const char* path)
off_t FdManager::GetFreeDiskSpaceHasLock(const char* path)
{
struct statvfs vfsbuf;
int result = FdManager::GetVfsStat(path, &vfsbuf);
@ -320,22 +321,20 @@ int FdManager::GetVfsStat(const char* path, struct statvfs* vfsbuf){
return 0;
}
bool FdManager::IsSafeDiskSpace(const char* path, off_t size)
bool FdManager::IsSafeDiskSpace(const char* path, off_t size, bool withmsg)
{
off_t fsize = FdManager::GetFreeDiskSpace(path);
return size + FdManager::GetEnsureFreeDiskSpace() <= fsize;
}
const std::lock_guard<std::mutex> lock(FdManager::reserved_diskspace_lock);
bool FdManager::IsSafeDiskSpaceWithLog(const char* path, off_t size)
{
off_t fsize = FdManager::GetFreeDiskSpace(path);
off_t needsize = size + FdManager::GetEnsureFreeDiskSpace();
if(needsize <= fsize){
return true;
} else {
S3FS_PRN_EXIT("There is no enough disk space for used as cache(or temporary) directory by s3fs. Requires %.3f MB, already has %.3f MB.", static_cast<double>(needsize) / 1024 / 1024, static_cast<double>(fsize) / 1024 / 1024);
off_t fsize = FdManager::GetFreeDiskSpaceHasLock(path);
off_t needsize = size + FdManager::GetEnsureFreeDiskSpaceHasLock();
if(fsize < needsize){
if(withmsg){
S3FS_PRN_EXIT("There is no enough disk space for used as cache(or temporary) directory by s3fs. Requires %.3f MB, already has %.3f MB.", static_cast<double>(needsize) / 1024 / 1024, static_cast<double>(fsize) / 1024 / 1024);
}
return false;
}
return true;
}
bool FdManager::HaveLseekHole()
@ -861,7 +860,7 @@ bool FdManager::ReserveDiskSpace(off_t size)
{
if(IsSafeDiskSpace(nullptr, size)){
const std::lock_guard<std::mutex> lock(FdManager::reserved_diskspace_lock);
free_disk_space += size;
FdManager::free_disk_space += size;
return true;
}
return false;
@ -870,7 +869,7 @@ bool FdManager::ReserveDiskSpace(off_t size)
void FdManager::FreeReservedDiskSpace(off_t size)
{
const std::lock_guard<std::mutex> lock(FdManager::reserved_diskspace_lock);
free_disk_space -= size;
FdManager::free_disk_space -= size;
}
//

View File

@ -50,10 +50,11 @@ class FdManager
fdent_direct_map_t except_fent; // A map of delayed deletion fdentity
private:
static off_t GetFreeDiskSpace(const char* path);
static off_t GetFreeDiskSpaceHasLock(const char* path) REQUIRES(FdManager::reserved_diskspace_lock);
static off_t GetTotalDiskSpace(const char* path);
static bool IsDir(const std::string& dir);
static int GetVfsStat(const char* path, struct statvfs* vfsbuf);
static off_t GetEnsureFreeDiskSpaceHasLock() REQUIRES(FdManager::reserved_diskspace_lock);
int GetPseudoFdCount(const char* path);
bool UpdateEntityToTempPath();
@ -85,11 +86,14 @@ class FdManager
static bool CheckCacheDirExist();
static bool HasOpenEntityFd(const char* path);
static int GetOpenFdCount(const char* path);
static off_t GetEnsureFreeDiskSpace();
static off_t GetEnsureFreeDiskSpace()
{
const std::lock_guard<std::mutex> lock(FdManager::reserved_diskspace_lock);
return FdManager::GetEnsureFreeDiskSpaceHasLock();
}
static off_t SetEnsureFreeDiskSpace(off_t size);
static bool InitFakeUsedDiskSize(off_t fake_freesize);
static bool IsSafeDiskSpace(const char* path, off_t size);
static bool IsSafeDiskSpaceWithLog(const char* path, off_t size);
static bool IsSafeDiskSpace(const char* path, off_t size, bool withmsg = false);
static void FreeReservedDiskSpace(off_t size);
static bool ReserveDiskSpace(off_t size);
static bool HaveLseekHole();

View File

@ -5720,24 +5720,6 @@ int main(int argc, char* argv[])
exit(EXIT_FAILURE);
}
// set fake free disk space
if(-1 != fake_diskfree_size){
FdManager::InitFakeUsedDiskSize(fake_diskfree_size);
}
// Set default value of free_space_ratio to 10%
if(FdManager::GetEnsureFreeDiskSpace()==0){
int ratio = 10;
off_t dfsize = FdManager::GetTotalDiskSpaceByRatio(ratio);
S3FS_PRN_INFO("Free space ratio default to %d %%, ensure the available disk space is greater than %.3f MB", ratio, static_cast<double>(dfsize) / 1024 / 1024);
if(dfsize < S3fsCurl::GetMultipartSize()){
S3FS_PRN_WARN("specified size to ensure disk free space is smaller than multipart size, so set multipart size to it.");
dfsize = S3fsCurl::GetMultipartSize();
}
FdManager::SetEnsureFreeDiskSpace(dfsize);
}
// set user agent
S3fsCurl::InitUserAgent();
@ -5783,16 +5765,39 @@ int main(int argc, char* argv[])
max_dirty_data = -1;
}
// check free disk space
if(!FdManager::IsSafeDiskSpace(nullptr, S3fsCurl::GetMultipartSize() * S3fsCurl::GetMaxParallelCount())){
// clean cache dir and retry
S3FS_PRN_WARN("No enough disk space for s3fs, try to clean cache dir");
FdManager::get()->CleanupCacheDir();
// Free disk space
if(-1 != fake_diskfree_size){
// Case: Set fake disk space
// Not check free disk space for maultipart request
FdManager::InitFakeUsedDiskSize(fake_diskfree_size);
if(!FdManager::IsSafeDiskSpaceWithLog(nullptr, S3fsCurl::GetMultipartSize() * S3fsCurl::GetMaxParallelCount())){
S3fsCurl::DestroyS3fsCurl();
s3fs_destroy_global_ssl();
exit(EXIT_FAILURE);
}else{
if(0 == FdManager::GetEnsureFreeDiskSpace()){
// Case: Not set any ensure disk free space
// Set default value of free_space_ratio to 10%
//
int ratio = 10;
off_t dfsize = FdManager::GetTotalDiskSpaceByRatio(ratio);
S3FS_PRN_INFO("Free space ratio default to %d %%, ensure the available disk space is greater than %.3f MB", ratio, static_cast<double>(dfsize) / 1024 / 1024);
if(dfsize < S3fsCurl::GetMultipartSize()){
S3FS_PRN_WARN("specified size to ensure disk free space is smaller than multipart size, so set multipart size to it.");
dfsize = S3fsCurl::GetMultipartSize();
}
FdManager::SetEnsureFreeDiskSpace(dfsize);
}
// Check free disk space for maultipart request
if(!FdManager::IsSafeDiskSpace(nullptr, S3fsCurl::GetMultipartSize() * S3fsCurl::GetMaxParallelCount())){
// Try to clean cache dir and retry
S3FS_PRN_WARN("No enough disk space for s3fs, try to clean cache dir");
FdManager::get()->CleanupCacheDir();
if(!FdManager::IsSafeDiskSpace(nullptr, S3fsCurl::GetMultipartSize() * S3fsCurl::GetMaxParallelCount(), true)){
S3fsCurl::DestroyS3fsCurl();
s3fs_destroy_global_ssl();
exit(EXIT_FAILURE);
}
}
}