44using Microsoft . Extensions . Logging . Abstractions ;
55using Ydb . Discovery ;
66using Ydb . Discovery . V1 ;
7+ using Ydb . Sdk . Ado ;
8+ using Ydb . Sdk . Ado . Internal ;
79using Ydb . Sdk . Pool ;
810
911namespace Ydb . Sdk ;
@@ -39,19 +41,15 @@ public async Task Initialize()
3941 {
4042 try
4143 {
42- var status = await DiscoverEndpoints ( ) ;
43- if ( status . IsSuccess )
44- {
45- _ = Task . Run ( PeriodicDiscovery ) ;
46-
47- return ;
48- }
44+ await DiscoverEndpoints ( ) ;
4945
50- Logger . LogCritical ( "Error during initial endpoint discovery: {status}" , status ) ;
46+ _ = Task . Run ( PeriodicDiscovery ) ;
47+
48+ return ;
5149 }
52- catch ( RpcException e )
50+ catch ( YdbException e )
5351 {
54- Logger . LogCritical ( "RPC error during initial endpoint discovery: {e.Status}" , e . Status ) ;
52+ Logger . LogError ( e , "RPC error during initial endpoint discovery: {e.Status}" , e . Code ) ;
5553
5654 if ( i == AttemptDiscovery - 1 )
5755 {
@@ -62,7 +60,7 @@ public async Task Initialize()
6260 await Task . Delay ( TimeSpan . FromMilliseconds ( i * 200 ) ) ; // await 0 ms, 200 ms, 400ms, ... 1.8 sec
6361 }
6462
65- throw new InitializationFailureException ( "Error during initial endpoint discovery" ) ;
63+ throw new YdbException ( "Error during initial endpoint discovery" ) ;
6664 }
6765
6866 protected override string GetEndpoint ( long nodeId ) => _endpointPool . GetEndpoint ( nodeId ) ;
@@ -90,7 +88,7 @@ Grpc.Core.StatusCode.DeadlineExceeded or
9088 _ = Task . Run ( DiscoverEndpoints ) ;
9189 }
9290
93- private async Task < Status > DiscoverEndpoints ( )
91+ private async Task DiscoverEndpoints ( )
9492 {
9593 using var channel = GrpcChannelFactory . CreateChannel ( Config . Endpoint ) ;
9694
@@ -111,30 +109,13 @@ private async Task<Status> DiscoverEndpoints()
111109 options : await GetCallOptions ( requestSettings )
112110 ) ;
113111
114- if ( ! response . Operation . Ready )
112+ var operation = response . Operation ;
113+ if ( operation . Status . IsNotSuccess ( ) )
115114 {
116- const string error = "Unexpected non-ready endpoint discovery operation." ;
117- Logger . LogError ( $ "Endpoint discovery internal error: { error } ") ;
118-
119- return new Status ( StatusCode . ClientInternalError , error ) ;
115+ throw YdbException . FromServer ( operation . Status , operation . Issues ) ;
120116 }
121117
122- var status = Status . FromProto ( response . Operation . Status , response . Operation . Issues ) ;
123- if ( status . IsNotSuccess )
124- {
125- Logger . LogWarning ( "Unsuccessful endpoint discovery: {Status}" , status ) ;
126- return status ;
127- }
128-
129- if ( response . Operation . Result is null )
130- {
131- const string error = "Unexpected empty endpoint discovery result." ;
132- Logger . LogError ( $ "Endpoint discovery internal error: { error } ") ;
133-
134- return new Status ( StatusCode . ClientInternalError , error ) ;
135- }
136-
137- var resultProto = response . Operation . Result . Unpack < ListEndpointsResult > ( ) ;
118+ var resultProto = operation . Result . Unpack < ListEndpointsResult > ( ) ;
138119
139120 Logger . LogDebug (
140121 "Successfully discovered endpoints: {EndpointsCount}, self location: {SelfLocation}, sdk info: {SdkInfo}" ,
@@ -151,8 +132,6 @@ await ChannelPool.RemoveChannels(
151132 . ToImmutableArray ( )
152133 )
153134 ) ;
154-
155- return new Status ( StatusCode . Success ) ;
156135 }
157136
158137 private async Task PeriodicDiscovery ( )
@@ -163,23 +142,16 @@ private async Task PeriodicDiscovery()
163142 {
164143 await Task . Delay ( Config . EndpointDiscoveryInterval ) ;
165144
166- _ = await DiscoverEndpoints ( ) ;
145+ await DiscoverEndpoints ( ) ;
167146 }
168- catch ( RpcException e )
147+ catch ( YdbException e )
169148 {
170- Logger . LogWarning ( "RPC error during endpoint discovery: {Status}" , e . Status ) ;
149+ Logger . LogWarning ( e , "Error during endpoint discovery" ) ;
171150 }
172151 catch ( Exception e )
173152 {
174153 Logger . LogError ( e , "Unexpected exception during session pool periodic check" ) ;
175154 }
176155 }
177156 }
178-
179- public class InitializationFailureException : Exception
180- {
181- internal InitializationFailureException ( string message ) : base ( message )
182- {
183- }
184- }
185157}
0 commit comments