s3fs: add option free_space_ratio to control cache size (#2351)
* Try to cleanup cache directory when initing without enough disk space Also optimize log messages to print detailed errors to the user. Signed-off-by: Qinqi Qu <quqinqi@linux.alibaba.com> * s3fs: add option free_space_ratio to control cache size Since the ensure_diskfree option is not convenient enough, we have added a new option "-o free_space_ratio" to control the space used by the s3fs cache based on the current disk size. The value of this option can be between 0 and 100. It will control the size of the cache according to this ratio to ensure that the idle ratio of the disk is greater than this value. For example, when the value is 10 and the disk space is 50GB, it will ensure that the disk will reserve at least 50GB * 10% = 5GB of remaining space. Signed-off-by: Qinqi Qu <quqinqi@linux.alibaba.com> --------- Signed-off-by: Qinqi Qu <quqinqi@linux.alibaba.com>
This commit is contained in:
@ -266,9 +266,38 @@ bool FdManager::InitFakeUsedDiskSize(off_t fake_freesize)
|
||||
return true;
|
||||
}
|
||||
|
||||
off_t FdManager::GetTotalDiskSpaceByRatio(int ratio)
|
||||
{
|
||||
return FdManager::GetTotalDiskSpace(nullptr) * ratio / 100;
|
||||
}
|
||||
|
||||
off_t FdManager::GetTotalDiskSpace(const char* path)
|
||||
{
|
||||
struct statvfs vfsbuf;
|
||||
int result = FdManager::GetVfsStat(path, &vfsbuf);
|
||||
if(result == -1){
|
||||
return 0;
|
||||
}
|
||||
|
||||
off_t actual_totalsize = vfsbuf.f_blocks * vfsbuf.f_frsize;
|
||||
|
||||
return actual_totalsize;
|
||||
}
|
||||
|
||||
off_t FdManager::GetFreeDiskSpace(const char* path)
|
||||
{
|
||||
struct statvfs vfsbuf;
|
||||
int result = FdManager::GetVfsStat(path, &vfsbuf);
|
||||
if(result == -1){
|
||||
return 0;
|
||||
}
|
||||
|
||||
off_t actual_freesize = vfsbuf.f_bavail * vfsbuf.f_frsize;
|
||||
|
||||
return (FdManager::fake_used_disk_space < actual_freesize ? (actual_freesize - FdManager::fake_used_disk_space) : 0);
|
||||
}
|
||||
|
||||
int FdManager::GetVfsStat(const char* path, struct statvfs* vfsbuf){
|
||||
std::string ctoppath;
|
||||
if(!FdManager::cache_dir.empty()){
|
||||
ctoppath = FdManager::cache_dir + "/";
|
||||
@ -284,14 +313,12 @@ off_t FdManager::GetFreeDiskSpace(const char* path)
|
||||
}else{
|
||||
ctoppath += ".";
|
||||
}
|
||||
if(-1 == statvfs(ctoppath.c_str(), &vfsbuf)){
|
||||
if(-1 == statvfs(ctoppath.c_str(), vfsbuf)){
|
||||
S3FS_PRN_ERR("could not get vfs stat by errno(%d)", errno);
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
off_t actual_freesize = vfsbuf.f_bavail * vfsbuf.f_frsize;
|
||||
|
||||
return (FdManager::fake_used_disk_space < actual_freesize ? (actual_freesize - FdManager::fake_used_disk_space) : 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool FdManager::IsSafeDiskSpace(const char* path, off_t size)
|
||||
@ -300,6 +327,18 @@ bool FdManager::IsSafeDiskSpace(const char* path, off_t size)
|
||||
return size + FdManager::GetEnsureFreeDiskSpace() <= fsize;
|
||||
}
|
||||
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FdManager::HaveLseekHole()
|
||||
{
|
||||
if(FdManager::checked_lseek){
|
||||
|
||||
Reference in New Issue
Block a user