|
| 1 | +/* |
| 2 | + * Copyright 2025 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_zero/read_request_scheduler/read_request_scheduler.h" |
| 11 | + |
| 12 | +#include "cloud_topics/logger.h" |
| 13 | +#include "ssx/future-util.h" |
| 14 | + |
| 15 | +#include <chrono> |
| 16 | + |
| 17 | +using namespace std::chrono_literals; |
| 18 | + |
| 19 | +namespace cloud_topics::l0 { |
| 20 | + |
| 21 | +read_request_scheduler::read_request_scheduler( |
| 22 | + read_pipeline<ss::lowres_clock>::stage stage) |
| 23 | + : _stage(std::move(stage)) {} |
| 24 | + |
| 25 | +ss::future<> read_request_scheduler::start() { |
| 26 | + vlog(cd_log.debug, "Read Request Scheduler start"); |
| 27 | + ssx::spawn_with_gate(_gate, [this] { return bg_loop(); }); |
| 28 | + co_return; |
| 29 | +} |
| 30 | + |
| 31 | +ss::future<> read_request_scheduler::stop() { co_await _gate.close(); } |
| 32 | + |
| 33 | +namespace { |
| 34 | +ss::shard_id shard_for(const read_request<ss::lowres_clock>& req) { |
| 35 | + std::hash<ss::sstring> hasher; |
| 36 | + // The request is generated from the placeholder batch. |
| 37 | + // The placeholder batch can't span multiple objects so it's safe |
| 38 | + // to check only the first extent. |
| 39 | + auto h = hasher(req.query.meta.front().id.name); |
| 40 | + auto shard = h % ss::smp::count; |
| 41 | + return static_cast<ss::shard_id>(shard); |
| 42 | +} |
| 43 | + |
| 44 | +std::unique_ptr<read_request<ss::lowres_clock>> make_proxy( |
| 45 | + ss::shard_id target_shard, |
| 46 | + const read_request<ss::lowres_clock>& req, |
| 47 | + ss::lowres_clock::time_point timeout, |
| 48 | + retry_chain_node* target_rtc, |
| 49 | + pipeline_stage id) { |
| 50 | + vassert( |
| 51 | + ss::this_shard_id() == target_shard, |
| 52 | + "make_proxy called on the wrong shard"); |
| 53 | + dataplane_query query; |
| 54 | + query.output_size_estimate = req.query.output_size_estimate; |
| 55 | + query.meta = req.query.meta.copy(); |
| 56 | + auto proxy = std::make_unique<read_request<ss::lowres_clock>>( |
| 57 | + req.ntp, std::move(query), timeout, target_rtc, id); |
| 58 | + return proxy; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace |
| 62 | + |
| 63 | +void read_request_scheduler::schedule_on( |
| 64 | + read_request<ss::lowres_clock>& source_req, ss::shard_id target) { |
| 65 | + if (target == ss::this_shard_id()) { |
| 66 | + // Fast path, just push source_req down the pipeline |
| 67 | + _stage.push_next_stage(source_req); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Check shutdown before launching cross-shard RPC |
| 72 | + if (_stage.stopped()) { |
| 73 | + source_req.set_value(errc::shutting_down); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + auto proxy = container().invoke_on( |
| 78 | + target, [target, &source_req](read_request_scheduler& s) { |
| 79 | + return s.proxy_read_request(source_req, target); |
| 80 | + }); |
| 81 | + |
| 82 | + auto ack = proxy.then( |
| 83 | + [&source_req](read_request<ss::lowres_clock>::response_t resp) { |
| 84 | + if (resp.has_value()) { |
| 85 | + source_req.set_value(std::move(resp.value())); |
| 86 | + } else { |
| 87 | + source_req.set_value(resp.error()); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + // Note: We intentionally do NOT hold the gate here. The gate is only used |
| 92 | + // for the bg_loop fiber. These fire-and-forget continuations must be able |
| 93 | + // to complete even during shutdown to avoid deadlock. |
| 94 | + ssx::background = std::move(ack); |
| 95 | +} |
| 96 | + |
| 97 | +ss::future<read_request<ss::lowres_clock>::response_t> |
| 98 | +read_request_scheduler::proxy_read_request( |
| 99 | + const read_request<ss::lowres_clock>& source_req, ss::shard_id target) { |
| 100 | + auto now = ss::lowres_clock::now(); |
| 101 | + auto timeout = source_req.expiration_time; |
| 102 | + if (timeout < now) { |
| 103 | + co_return std::unexpected(errc::timeout); |
| 104 | + } |
| 105 | + // Use pipeline stage id from the _stage object and not from the request/ |
| 106 | + // The request belongs to another pipeline and its stage id doesn't make |
| 107 | + // sense on the current shard. |
| 108 | + auto proxy = make_proxy( |
| 109 | + target, source_req, timeout, &_stage.get_root_rtc(), _stage.id()); |
| 110 | + |
| 111 | + // Check if pipeline is shutting down before awaiting response |
| 112 | + if (_stage.stopped()) { |
| 113 | + co_return std::unexpected(errc::shutting_down); |
| 114 | + } |
| 115 | + |
| 116 | + auto f = proxy->response.get_future(); |
| 117 | + _stage.push_next_stage(*proxy); |
| 118 | + auto res = co_await ss::coroutine::as_future(std::move(f)); |
| 119 | + if (res.failed()) { |
| 120 | + auto ex = res.get_exception(); |
| 121 | + // Check for shutdown exceptions explicitly |
| 122 | + if (ssx::is_shutdown_exception(ex)) { |
| 123 | + co_return std::unexpected(errc::shutting_down); |
| 124 | + } |
| 125 | + co_return std::unexpected(errc::unexpected_failure); |
| 126 | + } |
| 127 | + co_return std::move(res.get()); |
| 128 | +} |
| 129 | + |
| 130 | +ss::future<> read_request_scheduler::bg_loop() { |
| 131 | + while (!_stage.stopped()) { |
| 132 | + // NOTE(1): requests are vectorized but it's not guaranteed |
| 133 | + // that all extents in the request target the same object. |
| 134 | + // If this is the case the scheduler will use first extent |
| 135 | + // to decide the target shard. This could lead to suboptimal |
| 136 | + // distribution of requests across shards and some edge cases. |
| 137 | + // To avoid this the caller of the 'materialize' must ensure |
| 138 | + // that the requests are split properly so that all extents |
| 139 | + // in the request target the same object. This is not a |
| 140 | + // correctness problem. The only side effect is that we may |
| 141 | + // download same objects on multiple shards in parallel in |
| 142 | + // cases. |
| 143 | + // |
| 144 | + // NOTE(2): cache locality is not a concern here because |
| 145 | + // unlike in cases of write path the read path is only used |
| 146 | + // when there is a cache miss. Normally, we will not hit this |
| 147 | + // code path if the cache is working well and there is no |
| 148 | + // leadership transfers. The goal here is to brute-force the |
| 149 | + // reconciliation of cache misses as fast as possible. |
| 150 | + auto res = co_await _stage.pull_fetch_requests(10_MiB); |
| 151 | + if (!res.has_value()) { |
| 152 | + if (res.error() == errc::shutting_down) { |
| 153 | + break; |
| 154 | + } |
| 155 | + vlog( |
| 156 | + _stage.logger().error, |
| 157 | + "Failed to pull fetch requests: {}", |
| 158 | + res.error()); |
| 159 | + _stage.register_pipeline_error(res.error()); |
| 160 | + continue; |
| 161 | + } |
| 162 | + auto list = std::move(res.value()); |
| 163 | + while (!list.requests.empty()) { |
| 164 | + auto front = &list.requests.front(); |
| 165 | + list.requests.pop_front(); |
| 166 | + auto target_shard = shard_for(*front); |
| 167 | + schedule_on(*front, target_shard); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +} // namespace cloud_topics::l0 |
0 commit comments