@@ -32,19 +32,19 @@ type Balancer struct {
3232 driverConfig config.Config
3333 balancerConfig balancerConfig.Config
3434 pool * conn.Pool
35- discoveryClient func (ctx context.Context ) (discoveryClient , error )
35+ discoveryClient func (ctx context.Context , address string ) (discoveryClient , error )
3636 discoveryRepeater repeater.Repeater
3737 localDCDetector func (ctx context.Context , endpoints []endpoint.Endpoint ) (string , error )
3838
3939 mu xsync.RWMutex
4040 connectionsState * connectionsState
4141
42- onDiscovery []func (ctx context.Context , endpoints []endpoint.Info )
42+ onApplyDiscoveredEndpoints []func (ctx context.Context , endpoints []endpoint.Info )
4343}
4444
45- func (b * Balancer ) OnUpdate (onDiscovery func (ctx context.Context , endpoints []endpoint.Info )) {
45+ func (b * Balancer ) OnUpdate (onApplyDiscoveredEndpoints func (ctx context.Context , endpoints []endpoint.Info )) {
4646 b .mu .WithLock (func () {
47- b .onDiscovery = append (b .onDiscovery , onDiscovery )
47+ b .onApplyDiscoveredEndpoints = append (b .onApplyDiscoveredEndpoints , onApplyDiscoveredEndpoints )
4848 })
4949}
5050
@@ -66,22 +66,18 @@ func (b *Balancer) clusterDiscovery(ctx context.Context) (err error) {
6666
6767func (b * Balancer ) clusterDiscoveryAttempt (ctx context.Context ) (err error ) {
6868 var (
69- onDone = trace .DriverOnBalancerUpdate (
69+ address = "ydb:///" + b .driverConfig .Endpoint ()
70+ onDone = trace .DriverOnBalancerClusterDiscoveryAttempt (
7071 b .driverConfig .Trace (),
7172 & ctx ,
72- b . balancerConfig . DetectlocalDC ,
73+ address ,
7374 )
7475 endpoints []endpoint.Endpoint
7576 localDC string
7677 cancel context.CancelFunc
7778 )
78-
7979 defer func () {
80- nodes := make ([]trace.EndpointInfo , 0 , len (endpoints ))
81- for _ , e := range endpoints {
82- nodes = append (nodes , e .Copy ())
83- }
84- onDone (nodes , localDC , err )
80+ onDone (err )
8581 }()
8682
8783 if dialTimeout := b .driverConfig .DialTimeout (); dialTimeout > 0 {
@@ -91,7 +87,7 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context) (err error) {
9187 }
9288 defer cancel ()
9389
94- client , err := b .discoveryClient (ctx )
90+ client , err := b .discoveryClient (ctx , address )
9591 if err != nil {
9692 return xerrors .WithStackTrace (err )
9793 }
@@ -117,6 +113,19 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context) (err error) {
117113}
118114
119115func (b * Balancer ) applyDiscoveredEndpoints (ctx context.Context , endpoints []endpoint.Endpoint , localDC string ) {
116+ onDone := trace .DriverOnBalancerUpdate (
117+ b .driverConfig .Trace (),
118+ & ctx ,
119+ b .balancerConfig .DetectlocalDC ,
120+ )
121+ defer func () {
122+ nodes := make ([]trace.EndpointInfo , 0 , len (endpoints ))
123+ for _ , e := range endpoints {
124+ nodes = append (nodes , e .Copy ())
125+ }
126+ onDone (nodes , localDC , nil )
127+ }()
128+
120129 connections := endpointsToConnections (b .pool , endpoints )
121130 for _ , c := range connections {
122131 b .pool .Allow (ctx , c )
@@ -133,8 +142,8 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, endpoints []end
133142
134143 b .mu .WithLock (func () {
135144 b .connectionsState = state
136- for _ , onDiscovery := range b .onDiscovery {
137- onDiscovery (ctx , endpointsInfo )
145+ for _ , onApplyDiscoveredEndpoints := range b .onApplyDiscoveredEndpoints {
146+ onApplyDiscoveredEndpoints (ctx , endpointsInfo )
138147 }
139148 })
140149}
@@ -166,7 +175,13 @@ func New(
166175 driverConfig .Trace (),
167176 & ctx ,
168177 )
169- discoveryConfig = discoveryConfig .New (opts ... )
178+ discoveryConfig = discoveryConfig .New (append (opts ,
179+ discoveryConfig .With (driverConfig .Common ),
180+ discoveryConfig .WithEndpoint (driverConfig .Endpoint ()),
181+ discoveryConfig .WithDatabase (driverConfig .Database ()),
182+ discoveryConfig .WithSecure (driverConfig .Secure ()),
183+ discoveryConfig .WithMeta (driverConfig .Meta ()),
184+ )... )
170185 )
171186 defer func () {
172187 onDone (err )
@@ -176,11 +191,16 @@ func New(
176191 driverConfig : driverConfig ,
177192 pool : pool ,
178193 localDCDetector : detectLocalDC ,
179- discoveryClient : func (ctx context.Context ) (_ discoveryClient , err error ) {
180- cc , err := grpc .DialContext (ctx ,
181- "dns:///" + b .driverConfig .Endpoint (),
182- b .driverConfig .GrpcDialOptions ()... ,
194+ discoveryClient : func (ctx context.Context , address string ) (_ discoveryClient , err error ) {
195+ onBalancerDialEntrypointDone := trace .DriverOnBalancerDialEntrypoint (
196+ b .driverConfig .Trace (),
197+ & ctx ,
198+ address ,
183199 )
200+ defer func () {
201+ onBalancerDialEntrypointDone (err )
202+ }()
203+ cc , err := grpc .DialContext (ctx , address , b .driverConfig .GrpcDialOptions ()... )
184204 if err != nil {
185205 return nil , xerrors .WithStackTrace (err )
186206 }
@@ -195,11 +215,9 @@ func New(
195215 }
196216
197217 if b .balancerConfig .SingleConn {
198- b .connectionsState = newConnectionsState (
199- endpointsToConnections (pool , []endpoint.Endpoint {
200- endpoint .New (driverConfig .Endpoint ()),
201- }),
202- nil , balancerConfig.Info {}, false )
218+ b .applyDiscoveredEndpoints (ctx , []endpoint.Endpoint {
219+ endpoint .New (driverConfig .Endpoint ()),
220+ }, "" )
203221 } else {
204222 // initialization of balancer state
205223 if err = b .clusterDiscovery (ctx ); err != nil {
0 commit comments