|
| 1 | +/* |
| 2 | + * Copyright 2025 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file licenses/BSL.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +#include "cluster/utils/partition_change_notifier_impl.h" |
| 13 | + |
| 14 | +#include "cluster/partition_manager.h" |
| 15 | +#include "cluster/topic_table.h" |
| 16 | +#include "raft/group_manager.h" |
| 17 | + |
| 18 | +#include <seastar/util/defer.hh> |
| 19 | + |
| 20 | +namespace cluster { |
| 21 | + |
| 22 | +partition_change_notifier_impl::partition_change_notifier_impl( |
| 23 | + ss::sharded<raft::group_manager>& group_mgr, |
| 24 | + ss::sharded<cluster::partition_manager>& partition_mgr, |
| 25 | + ss::sharded<cluster::topic_table>& topic_table) |
| 26 | + : _group_mgr(group_mgr) |
| 27 | + , _partition_mgr(partition_mgr) |
| 28 | + , _topic_table(topic_table) {} |
| 29 | + |
| 30 | +void partition_change_notifier_impl::do_notify_call_back( |
| 31 | + notification_type type, |
| 32 | + notification_id_type id, |
| 33 | + const model::ntp& ntp, |
| 34 | + ss::lw_shared_ptr<partition> partition) { |
| 35 | + auto it = _notification_ids.find(id); |
| 36 | + if (it == _notification_ids.end()) [[unlikely]] { |
| 37 | + return; |
| 38 | + } |
| 39 | + std::optional<partition_state> state; |
| 40 | + if (partition) { |
| 41 | + state = partition_state{ |
| 42 | + partition->term(), |
| 43 | + partition->is_leader(), |
| 44 | + _topic_table.local().get_topic_cfg( |
| 45 | + model::topic_namespace_view{partition->ntp()})}; |
| 46 | + } |
| 47 | + it->second.cb(type, ntp, std::move(state)); |
| 48 | +} |
| 49 | + |
| 50 | +notification_id_type |
| 51 | +partition_change_notifier_impl::register_partition_notifications( |
| 52 | + notification_cb_t cb) { |
| 53 | + notification_state nstate; |
| 54 | + auto id = _notification_id++; |
| 55 | + auto exceptional_cleanup = ss::defer( |
| 56 | + [this, &nstate] { do_unregister_partition_notifications(nstate); }); |
| 57 | + nstate.leadership = _group_mgr.local().register_leadership_notification( |
| 58 | + [this, id]( |
| 59 | + raft::group_id group, |
| 60 | + model::term_id, |
| 61 | + std::optional<model::node_id>) mutable { |
| 62 | + auto partition = _partition_mgr.local().partition_for(group); |
| 63 | + if (!partition) [[unlikely]] { |
| 64 | + // If the partition is not found, it means that the group is |
| 65 | + // being deleted concurrently, ignore |
| 66 | + return; |
| 67 | + } |
| 68 | + do_notify_call_back( |
| 69 | + notification_type::leadership_change, |
| 70 | + id, |
| 71 | + partition->ntp(), |
| 72 | + partition); |
| 73 | + }); |
| 74 | + // kafka namespace |
| 75 | + nstate.kafka_replica_assigned |
| 76 | + = _partition_mgr.local().register_manage_notification( |
| 77 | + model::kafka_namespace, |
| 78 | + [this, id](ss::lw_shared_ptr<cluster::partition> partition) mutable { |
| 79 | + vassert(partition, "Invalid partition in managed notification"); |
| 80 | + do_notify_call_back( |
| 81 | + notification_type::partition_replica_assigned, |
| 82 | + id, |
| 83 | + partition->ntp(), |
| 84 | + partition); |
| 85 | + }); |
| 86 | + nstate.kafka_replica_unassigned |
| 87 | + = _partition_mgr.local().register_unmanage_notification( |
| 88 | + model::kafka_namespace, |
| 89 | + [this, id](model::topic_partition_view tp) mutable { |
| 90 | + model::ntp ntp{model::kafka_namespace, tp.topic, tp.partition}; |
| 91 | + do_notify_call_back( |
| 92 | + notification_type::partition_replica_unassigned, |
| 93 | + id, |
| 94 | + ntp, |
| 95 | + _partition_mgr.local().get(ntp)); |
| 96 | + }); |
| 97 | + // kafka internal namespace |
| 98 | + nstate.kafka_int_replica_assigned |
| 99 | + = _partition_mgr.local().register_manage_notification( |
| 100 | + model::kafka_internal_namespace, |
| 101 | + [this, id](ss::lw_shared_ptr<cluster::partition> partition) mutable { |
| 102 | + vassert(partition, "Invalid partition in managed notification"); |
| 103 | + do_notify_call_back( |
| 104 | + notification_type::partition_replica_assigned, |
| 105 | + id, |
| 106 | + partition->ntp(), |
| 107 | + partition); |
| 108 | + }); |
| 109 | + nstate.kafka_int_replica_unassigned |
| 110 | + = _partition_mgr.local().register_unmanage_notification( |
| 111 | + model::kafka_internal_namespace, |
| 112 | + [this, id](model::topic_partition_view tp) mutable { |
| 113 | + model::ntp ntp{model::kafka_namespace, tp.topic, tp.partition}; |
| 114 | + do_notify_call_back( |
| 115 | + notification_type::partition_replica_unassigned, |
| 116 | + id, |
| 117 | + ntp, |
| 118 | + _partition_mgr.local().get(ntp)); |
| 119 | + }); |
| 120 | + // partition properties changes |
| 121 | + nstate.properties = _topic_table.local().register_ntp_delta_notification( |
| 122 | + [this, id](cluster::topic_table::ntp_delta_range_t range) mutable { |
| 123 | + for (const auto& entry : range) { |
| 124 | + if ( |
| 125 | + entry.type |
| 126 | + == cluster::topic_table_ntp_delta_type::properties_updated) { |
| 127 | + do_notify_call_back( |
| 128 | + notification_type::partition_properties_change, |
| 129 | + id, |
| 130 | + entry.ntp, |
| 131 | + _partition_mgr.local().get(entry.ntp)); |
| 132 | + } |
| 133 | + } |
| 134 | + }); |
| 135 | + exceptional_cleanup.cancel(); |
| 136 | + nstate.cb = std::move(cb); |
| 137 | + _notification_ids.emplace(id, std::move(nstate)); |
| 138 | + return id; |
| 139 | +} |
| 140 | + |
| 141 | +void partition_change_notifier_impl::do_unregister_partition_notifications( |
| 142 | + const notification_state& ids) { |
| 143 | + _group_mgr.local().unregister_leadership_notification(ids.leadership); |
| 144 | + _partition_mgr.local().unregister_manage_notification( |
| 145 | + ids.kafka_replica_assigned); |
| 146 | + _partition_mgr.local().unregister_unmanage_notification( |
| 147 | + ids.kafka_replica_unassigned); |
| 148 | + _partition_mgr.local().unregister_manage_notification( |
| 149 | + ids.kafka_int_replica_assigned); |
| 150 | + _partition_mgr.local().unregister_unmanage_notification( |
| 151 | + ids.kafka_int_replica_unassigned); |
| 152 | + _topic_table.local().unregister_ntp_delta_notification(ids.properties); |
| 153 | +} |
| 154 | + |
| 155 | +void partition_change_notifier_impl::unregister_partition_notifications( |
| 156 | + notification_id_type id) { |
| 157 | + auto it = _notification_ids.find(id); |
| 158 | + if (it == _notification_ids.end()) [[unlikely]] { |
| 159 | + return; |
| 160 | + } |
| 161 | + do_unregister_partition_notifications(it->second); |
| 162 | + _notification_ids.erase(it); |
| 163 | +} |
| 164 | + |
| 165 | +std::unique_ptr<cluster::partition_change_notifier> |
| 166 | +partition_change_notifier_impl::make_default( |
| 167 | + ss::sharded<raft::group_manager>& group_mgr, |
| 168 | + ss::sharded<cluster::partition_manager>& partition_mgr, |
| 169 | + ss::sharded<cluster::topic_table>& topic_table) { |
| 170 | + return std::make_unique<partition_change_notifier_impl>( |
| 171 | + group_mgr, partition_mgr, topic_table); |
| 172 | +} |
| 173 | +} // namespace cluster |
0 commit comments