diff --git a/dataplane/globalbase.cpp b/dataplane/globalbase.cpp index 83f7249a..d802f671 100644 --- a/dataplane/globalbase.cpp +++ b/dataplane/globalbase.cpp @@ -1611,22 +1611,22 @@ inline uint64_t generation::count_real_connections(uint32_t counter_id) balancer_real_id_t* generation::rebuild_service_ring_one_wrr( balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service) + const balancer_service_t& service) { utils::Deferer defer([]() { YADECAP_MEMORY_BARRIER_COMPILE; }); - for (uint32_t real_idx = service->real_start; - real_idx < service->real_start + service->real_size; + for (uint32_t real_idx = service.real_start; + real_idx < service.real_start + service.real_size; ++real_idx) { - uint32_t real_id = balancer_service_reals[real_idx]; - balancer_real_state_t* state = balancer_real_states + real_id; + balancer_real_id_t real_id = balancer_service_reals[real_idx]; + const balancer_real_state_t& state = balancer_real_states[real_id]; - if (state->weight == 0) + if (state.weight == 0) { continue; } - auto weight = state->weight; + auto weight = state.weight; while (weight-- > 0) { @@ -1643,16 +1643,15 @@ balancer_real_id_t* generation::rebuild_service_ring_one_wrr( return start; } -std::vector> -generation::ServiceWeights(const balancer_service_t* service) +std::vector generation::ServiceWeights(const balancer_service_t& service) { - std::vector> weights; - for (uint32_t real_idx = service->real_start; - real_idx < service->real_start + service->real_size; + std::vector weights; + for (uint32_t real_idx = service.real_start; + real_idx < service.real_start + service.real_size; ++real_idx) { - uint32_t real_id = balancer_service_reals[real_idx]; - uint32_t weight = (balancer_real_states + real_id)->weight; + balancer_real_id_t real_id = balancer_service_reals[real_idx]; + uint32_t weight = balancer_real_states[real_id].weight; weights.emplace_back(real_id, weight); } @@ -1662,14 +1661,15 @@ generation::ServiceWeights(const balancer_service_t* service) balancer_real_id_t* generation::rebuild_service_ring_one_chash( balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service) + const balancer_service_t& service) { std::vector> reals; - for (uint32_t real_idx = service->real_start; - real_idx < service->real_start + service->real_size; + reals.reserve(service.real_size); + for (uint32_t real_idx = service.real_start; + real_idx < service.real_start + service.real_size; ++real_idx) { - uint32_t real_id = balancer_service_reals[real_idx]; + balancer_real_id_t real_id = balancer_service_reals[real_idx]; ipv6_address_t& ip = balancer_reals[real_id].destination; reals.emplace_back(ip, real_id); } @@ -1679,27 +1679,27 @@ balancer_real_id_t* generation::rebuild_service_ring_one_chash( YANET_DEFAULT_BALANCER_CELLS_PER_WEIGHT_UNIT); if (!updater) { - YANET_LOG_ERROR("Failed to intialize updater for balancer service reals: %ld\n", - reals.size()); + YANET_THROW("Failed to intialize updater for balancer service reals"); std::abort(); } updater.value().InitLookup(ServiceWeights(service), start); - chash_updaters.emplace(service, std::move(updater.value())); + chash_updaters.emplace(&service, std::move(updater.value())); return start + updater.value().LookupSize(); } balancer_real_id_t* generation::update_service_ring_one_chash( balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service) + const balancer_service_t& service) { - if (chash_updaters.find(service) == chash_updaters.end()) + auto up = chash_updaters.find(&service); + if (up == chash_updaters.end()) { YANET_LOG_ERROR("No state information for updating requested service.\n"); return start; } - auto& updater = chash_updaters.at(service); + auto& updater = up->second; updater.UpdateLookup(ServiceWeights(service), start); return start + updater.LookupSize(); } @@ -1708,11 +1708,11 @@ balancer_real_id_t* generation::evaluate_service_ring_one( ServiceRingOp op, balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service) + const balancer_service_t& service) { balancer_real_id_t* end{start}; using scheduler = ::balancer::scheduler; - switch (service->scheduler) + switch (service.scheduler) { case scheduler::rr: case scheduler::wrr: @@ -1739,7 +1739,7 @@ balancer_real_id_t* generation::evaluate_service_ring_one( } break; default: - YANET_LOG_ERROR("Unknown balancer service scheduler type\n"); + YANET_THROW("Unknown balancer service scheduler type"); } return end; } @@ -1752,17 +1752,17 @@ void generation::evaluate_service_ring(ServiceRingOp op) service_idx < balancer_services_count; ++service_idx, service_start += YANET_CONFIG_BALANCER_REAL_WEIGHT_MAX) { - balancer_service_t* service = balancer_services + balancer_active_services[service_idx]; + const balancer_service_t& service = balancer_services[balancer_active_services[service_idx]]; - balancer_service_range_t* range = ring->ranges + balancer_active_services[service_idx]; + balancer_service_range_t& range = ring->ranges[balancer_active_services[service_idx]]; - range->start = std::distance(ring->reals, service_start); + range.start = std::distance(ring->reals, service_start); auto service_end = evaluate_service_ring_one( op, service_start, service_start + YANET_CONFIG_BALANCER_REAL_WEIGHT_MAX, service); - range->size = std::distance(service_start, service_end); + range.size = std::distance(service_start, service_end); } } diff --git a/dataplane/globalbase.h b/dataplane/globalbase.h index 491c2e3d..59c44ee0 100644 --- a/dataplane/globalbase.h +++ b/dataplane/globalbase.h @@ -194,25 +194,26 @@ class generation Rebuild }; - std::vector> - ServiceWeights(const balancer_service_t* service); + using RealWeight = std::pair; + + std::vector ServiceWeights(const balancer_service_t& service); balancer_real_id_t* rebuild_service_ring_one_wrr( balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service); + const balancer_service_t& service); balancer_real_id_t* rebuild_service_ring_one_chash( balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service); + const balancer_service_t& service); balancer_real_id_t* update_service_ring_one_chash( balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service); + const balancer_service_t& service); balancer_real_id_t* evaluate_service_ring_one( ServiceRingOp op, balancer_real_id_t* start, const balancer_real_id_t* const do_not_exceed, - const balancer_service_t* service); + const balancer_service_t& service); void evaluate_service_ring(ServiceRingOp op); inline uint64_t count_real_connections(uint32_t counter_id);