@ -16,7 +16,7 @@
# include <grpc/support/port_platform.h>
# include <stdlib .h>
# include <string .h>
# include <algorithm>
# include <cstdint>
@ -26,6 +26,7 @@
# include <utility>
# include <vector>
# include "absl/random/random.h"
# include "absl/status/status.h"
# include "absl/status/statusor.h"
# include "absl/strings/str_cat.h"
@ -141,7 +142,7 @@ class WeightedTargetLb : public LoadBalancingPolicy {
// range proportional to the child's weight. The start of the range
// is the previous value in the vector and is 0 for the first element.
using PickerList =
std : : vector < std : : pair < uint32 _t , RefCountedPtr < ChildPickerWrapper > > > ;
std : : vector < std : : pair < uint64 _t , RefCountedPtr < ChildPickerWrapper > > > ;
explicit WeightedPicker ( PickerList pickers )
: pickers_ ( std : : move ( pickers ) ) { }
@ -150,6 +151,7 @@ class WeightedTargetLb : public LoadBalancingPolicy {
private :
PickerList pickers_ ;
absl : : BitGen bit_gen_ ;
} ;
// Each WeightedChild holds a ref to its parent WeightedTargetLb.
@ -226,7 +228,7 @@ class WeightedTargetLb : public LoadBalancingPolicy {
const std : : string name_ ;
uint32_t weight_ ;
uint32_t weight_ = 0 ;
OrphanablePtr < LoadBalancingPolicy > child_policy_ ;
@ -260,7 +262,8 @@ class WeightedTargetLb : public LoadBalancingPolicy {
WeightedTargetLb : : PickResult WeightedTargetLb : : WeightedPicker : : Pick (
PickArgs args ) {
// Generate a random number in [0, total weight).
const uint32_t key = rand ( ) % pickers_ [ pickers_ . size ( ) - 1 ] . first ;
const uint64_t key =
absl : : Uniform < uint64_t > ( bit_gen_ , 0 , pickers_ . back ( ) . first ) ;
// Find the index in pickers_ corresponding to key.
size_t mid = 0 ;
size_t start_index = 0 ;
@ -393,9 +396,9 @@ void WeightedTargetLb::UpdateStateLocked() {
// the range proportional to its weight, such that the total range is the
// sum of the weights of all children.
WeightedPicker : : PickerList ready_picker_list ;
uint32 _t ready_end = 0 ;
uint64 _t ready_end = 0 ;
WeightedPicker : : PickerList tf_picker_list ;
uint32 _t tf_end = 0 ;
uint64 _t tf_end = 0 ;
// Also count the number of children in CONNECTING and IDLE, to determine
// the aggregated state.
size_t num_connecting = 0 ;
@ -409,7 +412,7 @@ void WeightedTargetLb::UpdateStateLocked() {
}
if ( GRPC_TRACE_FLAG_ENABLED ( grpc_lb_weighted_target_trace ) ) {
gpr_log ( GPR_INFO ,
" [weighted_target_lb %p] child=%s state=%s weight=%d picker=%p " ,
" [weighted_target_lb %p] child=%s state=%s weight=%u picker=%p " ,
this , child_name . c_str ( ) ,
ConnectivityStateName ( child - > connectivity_state ( ) ) ,
child - > weight ( ) , child - > picker_wrapper ( ) . get ( ) ) ;
@ -586,6 +589,11 @@ absl::Status WeightedTargetLb::WeightedChild::UpdateLocked(
const std : : string & resolution_note , const ChannelArgs & args ) {
if ( weighted_target_policy_ - > shutting_down_ ) return absl : : OkStatus ( ) ;
// Update child weight.
if ( weight_ ! = config . weight & &
GRPC_TRACE_FLAG_ENABLED ( grpc_lb_weighted_target_trace ) ) {
gpr_log ( GPR_INFO , " [weighted_target_lb %p] WeightedChild %p %s: weight=%u " ,
weighted_target_policy_ . get ( ) , this , name_ . c_str ( ) , config . weight ) ;
}
weight_ = config . weight ;
// Reactivate if needed.
if ( delayed_removal_timer_ ! = nullptr ) {