ClickHouse/dbms/include/DB/Storages/MergeTree/DiskSpaceMonitor.h

150 lines
3.6 KiB
C++
Raw Normal View History

2014-03-13 12:48:07 +00:00
#pragma once
#include <mutex>
2014-03-13 12:48:07 +00:00
#include <sys/statvfs.h>
#include <memory>
2014-03-13 12:48:07 +00:00
#include <boost/noncopyable.hpp>
2015-09-29 19:19:54 +00:00
#include <common/logger_useful.h>
2015-10-05 01:35:28 +00:00
#include <DB/Common/Exception.h>
2014-05-14 17:51:37 +00:00
#include <DB/IO/WriteHelpers.h>
2015-06-11 00:35:36 +00:00
#include <DB/Common/formatReadable.h>
#include <DB/Common/CurrentMetrics.h>
2014-03-13 12:48:07 +00:00
namespace CurrentMetrics
{
extern const Metric DiskSpaceReservedForMerge;
}
2014-03-13 12:48:07 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_STATVFS;
extern const int NOT_ENOUGH_SPACE;
}
/** Determines amount of free space in filesystem.
* Could "reserve" space, for different operations to plan disk space usage.
* Reservations are not separated for different filesystems,
* instead it is assumed, that all reservations are done within same filesystem.
2014-03-13 12:48:07 +00:00
*/
class DiskSpaceMonitor
{
public:
class Reservation : private boost::noncopyable
{
public:
~Reservation()
{
try
{
std::lock_guard<std::mutex> lock(DiskSpaceMonitor::mutex);
2014-03-13 12:48:07 +00:00
if (DiskSpaceMonitor::reserved_bytes < size)
{
DiskSpaceMonitor::reserved_bytes = 0;
2014-08-04 11:41:59 +00:00
LOG_ERROR(&Logger::get("DiskSpaceMonitor"), "Unbalanced reservations size; it's a bug");
2014-03-13 12:48:07 +00:00
}
else
{
DiskSpaceMonitor::reserved_bytes -= size;
}
2014-08-04 11:41:59 +00:00
if (DiskSpaceMonitor::reservation_count == 0)
{
LOG_ERROR(&Logger::get("DiskSpaceMonitor"), "Unbalanced reservation count; it's a bug");
}
else
{
--DiskSpaceMonitor::reservation_count;
}
2014-03-13 12:48:07 +00:00
}
catch (...)
{
tryLogCurrentException("~DiskSpaceMonitor");
}
}
2014-08-04 11:41:59 +00:00
/// Change amount of reserved space. When new_size is greater than before, availability of free space is not checked.
2014-08-04 11:41:59 +00:00
void update(size_t new_size)
{
std::lock_guard<std::mutex> lock(DiskSpaceMonitor::mutex);
2014-08-04 11:41:59 +00:00
DiskSpaceMonitor::reserved_bytes -= size;
size = new_size;
DiskSpaceMonitor::reserved_bytes += size;
}
size_t getSize() const
{
return size;
}
Reservation(size_t size_)
: size(size_), metric_increment(CurrentMetrics::DiskSpaceReservedForMerge, size)
2014-03-13 12:48:07 +00:00
{
std::lock_guard<std::mutex> lock(DiskSpaceMonitor::mutex);
2014-03-13 12:48:07 +00:00
DiskSpaceMonitor::reserved_bytes += size;
2014-08-04 11:41:59 +00:00
++DiskSpaceMonitor::reservation_count;
2014-03-13 12:48:07 +00:00
}
private:
2014-03-13 12:48:07 +00:00
size_t size;
CurrentMetrics::Increment metric_increment;
2014-03-13 12:48:07 +00:00
};
2016-09-02 04:03:40 +00:00
using ReservationPtr = std::unique_ptr<Reservation>;
2014-03-13 12:48:07 +00:00
static size_t getUnreservedFreeSpace(const std::string & path)
{
struct statvfs fs;
if (statvfs(path.c_str(), &fs) != 0)
throwFromErrno("Could not calculate available disk space (statvfs)", ErrorCodes::CANNOT_STATVFS);
size_t res = fs.f_bfree * fs.f_bsize;
/// Heuristic by Michael Kolupaev: reserve 30 MB more, because statvfs shows few megabytes more space than df.
2014-05-14 17:51:37 +00:00
res -= std::min(res, 30 * (1ul << 20));
std::lock_guard<std::mutex> lock(mutex);
2014-03-13 12:48:07 +00:00
if (reserved_bytes > res)
res = 0;
else
res -= reserved_bytes;
return res;
}
2014-08-04 11:41:59 +00:00
static size_t getReservedSpace()
{
std::lock_guard<std::mutex> lock(mutex);
2014-08-04 11:41:59 +00:00
return reserved_bytes;
}
static size_t getReservationCount()
{
std::lock_guard<std::mutex> lock(mutex);
2014-08-04 11:41:59 +00:00
return reservation_count;
}
/// If not enough (approximately) space, throw an exception.
2014-03-13 12:48:07 +00:00
static ReservationPtr reserve(const std::string & path, size_t size)
{
2014-05-14 17:51:37 +00:00
size_t free_bytes = getUnreservedFreeSpace(path);
if (free_bytes < size)
2015-06-11 00:35:36 +00:00
throw Exception("Not enough free disk space to reserve: " + formatReadableSizeWithBinarySuffix(free_bytes) + " available, "
+ formatReadableSizeWithBinarySuffix(size) + " requested", ErrorCodes::NOT_ENOUGH_SPACE);
2016-09-02 04:03:40 +00:00
return std::make_unique<Reservation>(size);
2014-03-13 12:48:07 +00:00
}
private:
static size_t reserved_bytes;
2014-08-04 11:41:59 +00:00
static size_t reservation_count;
static std::mutex mutex;
2014-03-13 12:48:07 +00:00
};
}