disk_space_monitor: add space_source_registration

Register the current space_source_fn in an RAII
object that resets monitor._space_source to the
previous function when the RAII object is destroyed.

Use space_source_registration in database_test::
mutation_dump_generated_schema_deterministic_id_version
to prevent use-after-stack-return in the test.

Fixes #24314

Signed-off-by: Benny Halevy <bhalevy@scylladb.com>

Closes scylladb/scylladb#24342

(cherry picked from commit 8b387109fc)

Closes scylladb/scylladb#24392
This commit is contained in:
Benny Halevy
2025-06-01 10:38:17 +03:00
committed by Pavel Emelyanov
parent fa420f8644
commit afa2b40ac9
3 changed files with 27 additions and 2 deletions

View File

@@ -1615,7 +1615,7 @@ SEASTAR_TEST_CASE(test_disk_space_monitor_capacity_override) {
.free = 12,
.available = 11,
};
monitor.set_space_source([&] { return make_ready_future<std::filesystem::space_info>(orig_space); });
auto reg = monitor.set_space_source([&] { return make_ready_future<std::filesystem::space_info>(orig_space); });
utils::phased_barrier poll_barrier("poll_barrier"); // new operation started whenever monitor calls listeners.
auto op = poll_barrier.start();

View File

@@ -43,6 +43,16 @@ disk_space_monitor::~disk_space_monitor() {
SCYLLA_ASSERT(_poller_fut.available());
}
disk_space_monitor::space_source_registration::space_source_registration(disk_space_monitor& m)
: _monitor(m)
, _prev_space_source(m._space_source)
{
}
disk_space_monitor::space_source_registration::~space_source_registration() {
_monitor._space_source = _prev_space_source;
}
future<> disk_space_monitor::start() {
_space_info = co_await get_filesystem_space();
_poller_fut = poll();

View File

@@ -80,9 +80,22 @@ public:
signal_connection_type listen(signal_callback_type callback);
// Registers a new space source function and returns an object that
// restores the previous one when it goes out of scope.
class space_source_registration {
disk_space_monitor& _monitor;
space_source_fn _prev_space_source;
public:
space_source_registration(disk_space_monitor& m);
~space_source_registration();
};
// Replaces default way of obtaining file system usage information.
void set_space_source(space_source_fn space_source) {
space_source_registration set_space_source(space_source_fn space_source) {
auto ret = space_source_registration(*this);
_space_source = std::move(space_source);
return ret;
}
void trigger_poll() noexcept;
@@ -93,6 +106,8 @@ private:
future<std::filesystem::space_info> get_filesystem_space();
clock_type::duration get_polling_interval() const noexcept;
friend class space_source_registration;
};
} // namespace utils