Skip to content

Commit f79ec33

Browse files
committed
Added handle of exceptions on channel creating
1 parent 1c13cb8 commit f79ec33

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

core/src/main/java/tech/ydb/core/impl/YdbDiscovery.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,30 @@ private void tick() {
140140

141141
private void runDiscovery() {
142142
lastUpdateTime = handler.instant();
143-
final GrpcTransport transport = handler.createDiscoveryTransport();
144143
try {
145-
logger.debug("execute list endpoints on {} with timeout {}", transport, discoveryTimeout);
146-
DiscoveryProtos.ListEndpointsRequest request = DiscoveryProtos.ListEndpointsRequest.newBuilder()
147-
.setDatabase(discoveryDatabase)
148-
.build();
149-
150-
GrpcRequestSettings grpcSettings = GrpcRequestSettings.newBuilder()
151-
.withDeadline(discoveryTimeout)
152-
.build();
153-
154-
transport.unaryCall(DiscoveryServiceGrpc.getListEndpointsMethod(), grpcSettings, request)
155-
.whenComplete((res, ex) -> transport.close()) // close transport for any result
156-
.thenApply(OperationBinder.bindSync(
157-
DiscoveryProtos.ListEndpointsResponse::getOperation,
158-
DiscoveryProtos.ListEndpointsResult.class
159-
))
160-
.whenComplete(this::handleDiscoveryResult);
144+
final GrpcTransport transport = handler.createDiscoveryTransport();
145+
try {
146+
logger.debug("execute list endpoints on {} with timeout {}", transport, discoveryTimeout);
147+
DiscoveryProtos.ListEndpointsRequest request = DiscoveryProtos.ListEndpointsRequest.newBuilder()
148+
.setDatabase(discoveryDatabase)
149+
.build();
150+
151+
GrpcRequestSettings grpcSettings = GrpcRequestSettings.newBuilder()
152+
.withDeadline(discoveryTimeout)
153+
.build();
154+
155+
transport.unaryCall(DiscoveryServiceGrpc.getListEndpointsMethod(), grpcSettings, request)
156+
.whenComplete((res, ex) -> transport.close()) // close transport for any result
157+
.thenApply(OperationBinder.bindSync(
158+
DiscoveryProtos.ListEndpointsResponse::getOperation,
159+
DiscoveryProtos.ListEndpointsResult.class
160+
))
161+
.whenComplete(this::handleDiscoveryResult);
162+
} catch (Throwable th) {
163+
transport.close();
164+
throw th;
165+
}
161166
} catch (Throwable th) {
162-
transport.close();
163167
handleDiscoveryResult(null, th);
164168
}
165169
}

core/src/main/java/tech/ydb/core/impl/pool/GrpcChannel.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ public class GrpcChannel {
2525
private final ReadyWatcher readyWatcher;
2626

2727
public GrpcChannel(EndpointRecord endpoint, ManagedChannelFactory factory) {
28-
logger.debug("Creating grpc channel with {}", endpoint);
29-
this.endpoint = endpoint;
30-
this.channel = factory.newManagedChannel(endpoint.getHost(), endpoint.getPort());
31-
this.connectTimeoutMs = factory.getConnectTimeoutMs();
32-
this.readyWatcher = new ReadyWatcher();
33-
this.readyWatcher.checkState();
28+
try {
29+
logger.debug("Creating grpc channel with {}", endpoint);
30+
this.endpoint = endpoint;
31+
this.channel = factory.newManagedChannel(endpoint.getHost(), endpoint.getPort());
32+
this.connectTimeoutMs = factory.getConnectTimeoutMs();
33+
this.readyWatcher = new ReadyWatcher();
34+
this.readyWatcher.checkState();
35+
} catch (Throwable th) {
36+
logger.error("cannot create channel", th);
37+
throw new RuntimeException("cannot create channel", th);
38+
}
3439
}
3540

3641
public EndpointRecord getEndpoint() {

0 commit comments

Comments
 (0)