Skip to content

Commit 957beb2

Browse files
Merge pull request #30857 from redpanda-data/stephan/inline-storage-batches
various: more alloc opts
2 parents 31a4027 + 857689a commit 957beb2

4 files changed

Lines changed: 29 additions & 16 deletions

File tree

src/v/raft/group_configuration.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class group_configuration
291291
void for_each_learner(Func&& f) const;
292292

293293
const std::vector<vnode>& all_nodes() const;
294+
const std::vector<vnode>& replicas() const { return _all_replicas; }
294295

295296
std::optional<vnode> find_by_node_id(model::node_id) const;
296297

@@ -493,7 +494,7 @@ void group_configuration::for_each_broker(Func&& f) const {
493494

494495
template<typename Func>
495496
void group_configuration::for_each_replica(Func&& f) const {
496-
std::ranges::for_each(_all_replicas, std::forward<Func>(f));
497+
std::ranges::for_each(replicas(), std::forward<Func>(f));
497498
}
498499

499500
template<typename Func, typename Ret>

src/v/raft/replicate_batcher.cc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ ss::future<> replicate_batcher::flush(
247247
auto meta = _ptr->meta();
248248
const auto term = model::term_id(meta.term);
249249
chunked_vector<model::record_batch> data;
250-
std::vector<item_ptr> notifications;
250+
notifications_t notifications;
251251
ssx::semaphore_units item_memory_units(_max_batch_size_sem, 0);
252252
auto force_flush_requested = false;
253253
auto has_quorum_ack_requests = false;
@@ -266,9 +266,16 @@ ss::future<> replicate_batcher::flush(
266266
has_quorum_ack_requests
267267
= has_quorum_ack_requests
268268
|| (n->get_consistency_level() == consistency_level::quorum_ack);
269-
for (auto& b : batches) {
270-
b.set_term(term);
271-
data.push_back(std::move(b));
269+
if (data.empty()) {
270+
data = std::move(batches);
271+
for (auto& b : data) {
272+
b.set_term(term);
273+
}
274+
} else {
275+
for (auto& b : batches) {
276+
b.set_term(term);
277+
data.push_back(std::move(b));
278+
}
272279
}
273280
notifications.push_back(std::move(n));
274281
} else {
@@ -323,7 +330,7 @@ ss::future<> replicate_batcher::flush(
323330
template<typename Predicate>
324331
static void propagate_result(
325332
result<replicate_result> r,
326-
std::vector<replicate_batcher::item_ptr>& notifications,
333+
replicate_batcher::notifications_t& notifications,
327334
const Predicate& pred) {
328335
if (r.has_error()) {
329336
// propagate an error
@@ -344,8 +351,8 @@ static void propagate_result(
344351
}
345352
}
346353

347-
static void propagate_current_exception(
348-
std::vector<replicate_batcher::item_ptr>& notifications) {
354+
static void
355+
propagate_current_exception(replicate_batcher::notifications_t& notifications) {
349356
// iterate backward to calculate last offsets
350357
auto e = std::current_exception();
351358
for (auto& n : notifications) {
@@ -354,7 +361,7 @@ static void propagate_current_exception(
354361
}
355362

356363
ss::future<> replicate_batcher::do_flush(
357-
std::vector<replicate_batcher::item_ptr> notifications,
364+
replicate_batcher::notifications_t notifications,
358365
append_entries_request req,
359366
std::vector<ssx::semaphore_units> u,
360367
absl::flat_hash_map<vnode, follower_req_seq> seqs) {

src/v/raft/replicate_batcher.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#pragma once
1313

1414
#include "absl/container/flat_hash_map.h"
15+
#include "absl/container/inlined_vector.h"
1516
#include "base/outcome.h"
1617
#include "container/chunked_vector.h"
1718
#include "raft/types.h"
@@ -83,6 +84,7 @@ class replicate_batcher {
8384
ss::optimized_optional<ss::abort_source::subscription> _abort_sub;
8485
};
8586
using item_ptr = ss::lw_shared_ptr<item>;
87+
using notifications_t = absl::InlinedVector<item_ptr, 5>;
8688
explicit replicate_batcher(consensus* ptr, size_t cache_size);
8789

8890
replicate_batcher(replicate_batcher&&) noexcept = default;
@@ -100,7 +102,7 @@ class replicate_batcher {
100102

101103
private:
102104
ss::future<> do_flush(
103-
std::vector<item_ptr>,
105+
notifications_t,
104106
append_entries_request,
105107
std::vector<ssx::semaphore_units>,
106108
absl::flat_hash_map<vnode, follower_req_seq>);

src/v/raft/replicate_entries_stm.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "raft/replicate_entries_stm.h"
1111

12+
#include "absl/container/inlined_vector.h"
1213
#include "base/outcome.h"
1314
#include "base/outcome_future_utils.h"
1415
#include "model/fundamental.h"
@@ -238,13 +239,15 @@ inline bool replicate_entries_stm::should_skip_follower_request(vnode id) {
238239

239240
ss::future<result<replicate_result>> replicate_entries_stm::apply(units_t u) {
240241
// first append lo leader log, no flushing
241-
auto cfg = _ptr->config();
242-
cfg.for_each_replica([this](const vnode& rni) {
242+
const auto& config_replicas = _ptr->config().replicas();
243+
absl::InlinedVector<vnode, 5> replicas(
244+
config_replicas.begin(), config_replicas.end());
245+
for (const auto& rni : replicas) {
243246
// suppress follower heartbeat, before appending to self log
244247
if (rni != _ptr->_self) {
245248
_inflight_appends.emplace(rni, _ptr->track_append_inflight(rni));
246249
}
247-
});
250+
}
248251
_units = ss::make_lw_shared<units_t>(std::move(u));
249252
_append_result = co_await append_to_self();
250253

@@ -255,12 +258,12 @@ ss::future<result<replicate_result>> replicate_entries_stm::apply(units_t u) {
255258
// store committed offset to check if it advanced
256259
_initial_committed_offset = _ptr->committed_offset();
257260
// dispatch requests to followers & leader flush
258-
cfg.for_each_replica([this](const vnode& rni) {
261+
for (const auto& rni : replicas) {
259262
// We are not dispatching request to followers that are
260263
// recovering
261264
if (should_skip_follower_request(rni)) {
262265
_inflight_appends[rni].mark_finished();
263-
return;
266+
continue;
264267
}
265268
if (rni != _ptr->self()) {
266269
auto it = _ptr->_fstates.find(rni);
@@ -271,7 +274,7 @@ ss::future<result<replicate_result>> replicate_entries_stm::apply(units_t u) {
271274
}
272275
++_requests_count;
273276
(void)dispatch_one(rni); // background
274-
});
277+
}
275278

276279
// wait for the requests to be dispatched in background and then release
277280
// units

0 commit comments

Comments
 (0)