diff --git a/locator/tablets.cc b/locator/tablets.cc index a4c79081dd..c1dd471ff5 100644 --- a/locator/tablets.cc +++ b/locator/tablets.cc @@ -406,6 +406,24 @@ future<> tablet_map::for_each_tablet(seastar::noncopyable_function(tabl } } +future<> tablet_map::for_each_sibling_tablets(seastar::noncopyable_function(tablet_desc, std::optional)> func) const { + auto make_desc = [this] (tablet_id tid) { + return tablet_desc{tid, &get_tablet_info(tid), get_tablet_transition_info(tid)}; + }; + if (_tablets.size() == 1) { + co_return co_await func(make_desc(first_tablet()), std::nullopt); + } + for (std::optional tid = first_tablet(); tid; tid = next_tablet(*tid)) { + auto tid1 = tid; + auto tid2 = tid = next_tablet(*tid); + if (!tid2) { + // Cannot happen with power-of-two invariant. + throw std::logic_error(format("Cannot retrieve sibling tablet with tablet count {}", tablet_count())); + } + co_await func(make_desc(*tid1), make_desc(*tid2)); + } +} + void tablet_map::clear_transitions() { _transitions.clear(); } diff --git a/locator/tablets.hh b/locator/tablets.hh index 34b326ddf3..728de957e0 100644 --- a/locator/tablets.hh +++ b/locator/tablets.hh @@ -348,6 +348,12 @@ struct repair_scheduler_config { using load_stats_ptr = lw_shared_ptr; +struct tablet_desc { + tablet_id tid; + const tablet_info* info; // cannot be null. + const tablet_transition_info* transition; // null if there's no transition. +}; + /// Stores information about tablets of a single table. /// /// The map contains a constant number of tablets, tablet_count(). @@ -453,6 +459,10 @@ public: /// Calls a given function for each tablet in the map in token ownership order. future<> for_each_tablet(seastar::noncopyable_function(tablet_id, const tablet_info&)> func) const; + /// Calls a given function for each sibling tablet in the map in token ownership order. + /// If tablet count == 1, then there will be only one call and 2nd tablet_desc is disengaged. + future<> for_each_sibling_tablets(seastar::noncopyable_function(tablet_desc, std::optional)> func) const; + const auto& transitions() const { return _transitions; }