|
| 1 | +/* |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Licensed as a Redpanda Enterprise file under the Redpanda Community |
| 5 | + * License (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/redpanda-data/redpanda/blob/master/licenses/rcl.md |
| 9 | + */ |
| 10 | +#include "cloud_topics/level_one/metastore/flush_loop.h" |
| 11 | + |
| 12 | +#include "base/vlog.h" |
| 13 | +#include "cloud_topics/level_one/metastore/metastore.h" |
| 14 | +#include "cloud_topics/logger.h" |
| 15 | +#include "config/configuration.h" |
| 16 | +#include "ssx/semaphore.h" |
| 17 | +#include "ssx/time.h" |
| 18 | + |
| 19 | +namespace cloud_topics::l1 { |
| 20 | + |
| 21 | +// Loop that flushes the metastore periodically until stopped. |
| 22 | +class flush_loop { |
| 23 | +public: |
| 24 | + explicit flush_loop( |
| 25 | + metastore* metastore, |
| 26 | + config::binding<std::chrono::milliseconds> flush_interval) |
| 27 | + : metastore_(metastore) |
| 28 | + , flush_interval_(std::move(flush_interval)) { |
| 29 | + flush_interval_.watch([this] { sem_.signal(); }); |
| 30 | + } |
| 31 | + |
| 32 | + void start() { |
| 33 | + ssx::spawn_with_gate(gate_, [this] { return run_loop(); }); |
| 34 | + } |
| 35 | + |
| 36 | + ss::future<> stop_and_wait() { |
| 37 | + vlog(cd_log.debug, "Metastore flush loop stopping..."); |
| 38 | + as_.request_abort(); |
| 39 | + sem_.broken(); |
| 40 | + co_await gate_.close(); |
| 41 | + vlog(cd_log.debug, "Metastore flush loop stopped"); |
| 42 | + } |
| 43 | + |
| 44 | +private: |
| 45 | + ss::future<> run_loop() { |
| 46 | + const auto retry_interval = ssx::duration::seconds(10); |
| 47 | + while (!as_.abort_requested()) { |
| 48 | + auto start = ssx::instant::from_chrono(ss::lowres_clock::now()); |
| 49 | + auto res = co_await metastore_->flush(); |
| 50 | + auto finish = ssx::instant::from_chrono(ss::lowres_clock::now()); |
| 51 | + |
| 52 | + ssx::duration sleep_duration; |
| 53 | + if (!res.has_value()) { |
| 54 | + vlog( |
| 55 | + cd_log.warn, |
| 56 | + "Failed to flush metastore, retrying in {}: {}", |
| 57 | + retry_interval, |
| 58 | + res.error()); |
| 59 | + sleep_duration = retry_interval; |
| 60 | + } else { |
| 61 | + auto flush_interval = ssx::duration::from_chrono( |
| 62 | + flush_interval_()); |
| 63 | + auto flush_time = finish - start; |
| 64 | + sleep_duration = flush_interval - flush_time; |
| 65 | + } |
| 66 | + |
| 67 | + if (sleep_duration > ssx::duration::zero()) { |
| 68 | + try { |
| 69 | + co_await sem_.wait( |
| 70 | + sleep_duration.to_chrono<std::chrono::milliseconds>(), |
| 71 | + std::max(sem_.current(), size_t(1))); |
| 72 | + } catch (const ss::semaphore_timed_out&) { |
| 73 | + // Time to wake up! Continue onto the next iteration. |
| 74 | + } catch (...) { |
| 75 | + auto eptr = std::current_exception(); |
| 76 | + auto log_lvl = ssx::is_shutdown_exception(eptr) |
| 77 | + ? ss::log_level::debug |
| 78 | + : ss::log_level::warn; |
| 79 | + vlogl( |
| 80 | + cd_log, |
| 81 | + log_lvl, |
| 82 | + "Metastore flush loop hit exception while sleeping: {}", |
| 83 | + eptr); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + ss::gate gate_; |
| 90 | + ss::abort_source as_; |
| 91 | + metastore* metastore_; |
| 92 | + config::binding<std::chrono::milliseconds> flush_interval_; |
| 93 | + ssx::semaphore sem_{0, "flush_loop"}; |
| 94 | +}; |
| 95 | + |
| 96 | +flush_loop_manager::flush_loop_manager(metastore* metastore) |
| 97 | + : metastore_(metastore) {} |
| 98 | + |
| 99 | +flush_loop_manager::~flush_loop_manager() = default; |
| 100 | + |
| 101 | +ss::future<> flush_loop_manager::reset_flush_loop( |
| 102 | + flush_loop_manager::needs_loop needs_loop) { |
| 103 | + if (!needs_loop) { |
| 104 | + // We should not have a running loop. Stop it if one exists. |
| 105 | + if (flush_loop_) { |
| 106 | + auto loop = std::exchange(flush_loop_, nullptr); |
| 107 | + auto stop_fut = co_await ss::coroutine::as_future( |
| 108 | + loop->stop_and_wait()); |
| 109 | + if (stop_fut.failed()) { |
| 110 | + auto ex = stop_fut.get_exception(); |
| 111 | + vlog(cd_log.error, "Stopping flush loop failed: {}", ex); |
| 112 | + } |
| 113 | + } |
| 114 | + co_return; |
| 115 | + } |
| 116 | + if (flush_loop_) { |
| 117 | + // We need a loop and already have one. |
| 118 | + co_return; |
| 119 | + } |
| 120 | + auto loop = std::make_unique<flush_loop>( |
| 121 | + metastore_, |
| 122 | + config::shard_local_cfg().cloud_topics_long_term_flush_interval.bind()); |
| 123 | + loop->start(); |
| 124 | + flush_loop_ = std::move(loop); |
| 125 | +} |
| 126 | + |
| 127 | +void flush_loop_manager::enqueue_loop_reset( |
| 128 | + flush_loop_manager::needs_loop needs_loop) { |
| 129 | + tell(needs_loop); |
| 130 | +} |
| 131 | + |
| 132 | +ss::future<> |
| 133 | +flush_loop_manager::process(flush_loop_manager::needs_loop needs_loop) { |
| 134 | + return reset_flush_loop(needs_loop); |
| 135 | +} |
| 136 | + |
| 137 | +void flush_loop_manager::on_error(std::exception_ptr ex) noexcept { |
| 138 | + vlog(cd_log.error, "Unexpected flush loop manager error: {}", ex); |
| 139 | +} |
| 140 | + |
| 141 | +ss::future<> flush_loop_manager::stop() { |
| 142 | + co_await actor::stop(); |
| 143 | + if (flush_loop_) { |
| 144 | + auto fut = co_await ss::coroutine::as_future( |
| 145 | + flush_loop_->stop_and_wait()); |
| 146 | + if (fut.failed()) { |
| 147 | + auto ex = fut.get_exception(); |
| 148 | + vlog(cd_log.error, "Error stopping flush loop manager: {}", ex); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace cloud_topics::l1 |
0 commit comments