1717package loadbalance
1818
1919import (
20+ "context"
2021 "sync"
2122
2223 "golang.org/x/sync/singleflight"
@@ -54,28 +55,31 @@ func NewWeightedRandomBalancer() Loadbalancer {
5455}
5556
5657// GetPicker implements the Loadbalancer interface.
57- func (wb * weightedBalancer ) GetPicker (e discovery.Result ) Picker {
58+ func (wb * weightedBalancer ) GetPicker (ctx context. Context , e discovery.Result ) Picker {
5859 if ! e .Cacheable {
59- picker := wb .createPicker (e )
60+ picker := wb .createPicker (ctx , e )
6061 return picker
6162 }
6263
6364 picker , ok := wb .pickerCache .Load (e .CacheKey )
6465 if ! ok {
6566 picker , _ , _ = wb .sfg .Do (e .CacheKey , func () (interface {}, error ) {
66- p := wb .createPicker (e )
67+ klog .CtxErrorf (ctx , "KITEX-DEBUG: start createPicker, CacheKey=%s, instanceSize=%d" , e .CacheKey , len (e .Instances ))
68+ p := wb .createPicker (ctx , e )
69+ klog .CtxErrorf (ctx , "KITEX-DEBUG: end createPicker, CacheKey=%s" , e .CacheKey )
6770 wb .pickerCache .Store (e .CacheKey , p )
6871 return p , nil
6972 })
7073 }
7174 return picker .(Picker )
7275}
7376
74- func (wb * weightedBalancer ) createPicker (e discovery.Result ) (picker Picker ) {
77+ func (wb * weightedBalancer ) createPicker (ctx context. Context , e discovery.Result ) (picker Picker ) {
7578 instances := make ([]discovery.Instance , len (e .Instances )) // removed zero weight instances
7679 weightSum := 0
7780 balance := true
7881 cnt := 0
82+ klog .CtxErrorf (ctx , "KITEX-DEBUG: createPicker, begin iterator" )
7983 for idx , instance := range e .Instances {
8084 weight := instance .Weight ()
8185 if weight <= 0 {
@@ -89,6 +93,7 @@ func (wb *weightedBalancer) createPicker(e discovery.Result) (picker Picker) {
8993 }
9094 cnt ++
9195 }
96+ klog .CtxErrorf (ctx , "KITEX-DEBUG: createPicker, end iterator, size=%d" , len (instances ))
9297 instances = instances [:cnt ]
9398 if len (instances ) == 0 {
9499 return new (DummyPicker )
@@ -99,7 +104,9 @@ func (wb *weightedBalancer) createPicker(e discovery.Result) (picker Picker) {
99104 if balance {
100105 picker = newRoundRobinPicker (instances )
101106 } else {
102- picker = newWeightedRoundRobinPicker (instances )
107+ klog .CtxErrorf (ctx , "KITEX-DEBUG: start newWeightedRoundRobinPicker" )
108+ picker = newWeightedRoundRobinPicker (ctx , instances )
109+ klog .CtxErrorf (ctx , "KITEX-DEBUG: end newWeightedRoundRobinPicker" )
103110 }
104111 default : // random
105112 if balance {
@@ -116,7 +123,7 @@ func (wb *weightedBalancer) Rebalance(change discovery.Change) {
116123 if ! change .Result .Cacheable {
117124 return
118125 }
119- wb .pickerCache .Store (change .Result .CacheKey , wb .createPicker (change .Result ))
126+ wb .pickerCache .Store (change .Result .CacheKey , wb .createPicker (context . Background (), change .Result ))
120127}
121128
122129// Delete implements the Rebalancer interface.
0 commit comments