Skip to content

Commit 0109d89

Browse files
authored
Merge pull request #347 from eivanov89/connection_improvements
Connection improvements
2 parents 8989f98 + a054eac commit 0109d89

File tree

5 files changed

+74
-54
lines changed

5 files changed

+74
-54
lines changed

core/src/main/java/tech/ydb/core/impl/call/ReadStreamCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public CompletableFuture<Status> start(Observer<RespT> observer) {
5959
synchronized (call) {
6060
try {
6161
call.start(this, headers);
62-
call.request(1);
6362
if (logger.isTraceEnabled()) {
6463
logger.trace("ReadStreamCall[{}] --> {}", traceId, TextFormat.shortDebugString((Message) request));
6564
}
6665
call.sendMessage(request);
6766
// close stream by client side
6867
call.halfClose();
68+
call.request(1);
6969
} catch (Throwable t) {
7070
try {
7171
call.cancel(null, t);

core/src/main/java/tech/ydb/core/impl/call/UnaryCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public UnaryCall(String traceId, ClientCall<ReqT, RespT> call, GrpcStatusHandler
5050
public CompletableFuture<Result<RespT>> startCall(ReqT request, Metadata headers) {
5151
try {
5252
call.start(this, headers);
53-
call.request(1);
5453
if (logger.isTraceEnabled()) {
5554
logger.trace("UnaryCall[{}] --> {}", traceId, TextFormat.shortDebugString((Message) request));
5655
}
5756
call.sendMessage(request);
5857
call.halfClose();
58+
call.request(1);
5959
} catch (Exception ex) {
6060
future.completeExceptionally(ex);
6161
try {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public ManagedChannel newManagedChannel(String host, int port, String sslHostOve
7676
channelBuilder
7777
.maxInboundMessageSize(INBOUND_MESSAGE_SIZE)
7878
.withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT)
79+
.withOption(ChannelOption.TCP_NODELAY, true)
7980
.intercept(metadataInterceptor());
8081

8182
if (!useDefaultGrpcResolver) {
@@ -86,7 +87,8 @@ public ManagedChannel newManagedChannel(String host, int port, String sslHostOve
8687
}
8788

8889
if (grpcKeepAliveTimeMillis != null) {
89-
channelBuilder.keepAliveTime(grpcKeepAliveTimeMillis, TimeUnit.MILLISECONDS);
90+
channelBuilder.keepAliveTime(grpcKeepAliveTimeMillis, TimeUnit.MILLISECONDS)
91+
.keepAliveWithoutCalls(true);
9092
}
9193

9294
if (retryEnabled) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public ManagedChannel newManagedChannel(String host, int port, String sslHostOve
7676
channelBuilder
7777
.maxInboundMessageSize(INBOUND_MESSAGE_SIZE)
7878
.withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT)
79+
.withOption(ChannelOption.TCP_NODELAY, true)
7980
.intercept(metadataInterceptor());
8081

8182
if (!useDefaultGrpcResolver) {
@@ -86,7 +87,8 @@ public ManagedChannel newManagedChannel(String host, int port, String sslHostOve
8687
}
8788

8889
if (grpcKeepAliveTimeMillis != null) {
89-
channelBuilder.keepAliveTime(grpcKeepAliveTimeMillis, TimeUnit.MILLISECONDS);
90+
channelBuilder.keepAliveTime(grpcKeepAliveTimeMillis, TimeUnit.MILLISECONDS)
91+
.keepAliveWithoutCalls(true);
9092
}
9193

9294
if (retryEnabled) {

core/src/test/java/tech/ydb/core/impl/pool/DefaultChannelFactoryTest.java

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@
2020
import org.junit.Assert;
2121
import org.junit.Before;
2222
import org.junit.Test;
23-
import static org.mockito.ArgumentMatchers.any;
24-
import static org.mockito.ArgumentMatchers.anyInt;
23+
import org.mockito.ArgumentMatchers;
2524
import org.mockito.MockedStatic;
2625
import org.mockito.Mockito;
27-
import static org.mockito.Mockito.times;
28-
import static org.mockito.Mockito.verify;
29-
import static org.mockito.Mockito.when;
3026
import org.mockito.MockitoAnnotations;
3127

3228
import tech.ydb.core.grpc.GrpcTransport;
@@ -54,13 +50,20 @@ public void setUp() {
5450
channelStaticMock = Mockito.mockStatic(NettyChannelBuilder.class);
5551
channelStaticMock.when(FOR_ADDRESS).thenReturn(channelBuilderMock);
5652

57-
when(channelBuilderMock.negotiationType(any())).thenReturn(channelBuilderMock);
58-
when(channelBuilderMock.maxInboundMessageSize(anyInt())).thenReturn(channelBuilderMock);
59-
when(channelBuilderMock.withOption(any(), any())).thenReturn(channelBuilderMock);
60-
when(channelBuilderMock.intercept((ClientInterceptor)any())).thenReturn(channelBuilderMock);
61-
when(channelBuilderMock.nameResolverFactory(any())).thenReturn(channelBuilderMock);
62-
63-
when(channelBuilderMock.build()).thenReturn(channelMock);
53+
Mockito.when(channelBuilderMock.negotiationType(ArgumentMatchers.any()))
54+
.thenReturn(channelBuilderMock);
55+
Mockito.when(channelBuilderMock.maxInboundMessageSize(ArgumentMatchers.anyInt()))
56+
.thenReturn(channelBuilderMock);
57+
Mockito.when(channelBuilderMock.withOption(ArgumentMatchers.any(), ArgumentMatchers.any()))
58+
.thenReturn(channelBuilderMock);
59+
Mockito.when(channelBuilderMock.intercept(ArgumentMatchers.any(ClientInterceptor.class)))
60+
.thenReturn(channelBuilderMock);
61+
Mockito.when(channelBuilderMock.nameResolverFactory(ArgumentMatchers.any()))
62+
.thenReturn(channelBuilderMock);
63+
Mockito.when(channelBuilderMock.keepAliveTime(ArgumentMatchers.anyLong(), ArgumentMatchers.any()))
64+
.thenReturn(channelBuilderMock);
65+
66+
Mockito.when(channelBuilderMock.build()).thenReturn(channelMock);
6467
}
6568

6669
@After
@@ -73,20 +76,23 @@ public void tearDown() throws Exception {
7376
public void defaultParams() {
7477
GrpcTransportBuilder builder = GrpcTransport.forHost(MOCKED_HOST, MOCKED_PORT, "/Root");
7578
ManagedChannelFactory factory = ChannelFactoryLoader.load().buildFactory(builder);
76-
channelStaticMock.verify(FOR_ADDRESS, times(0));
79+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(0));
7780

7881
Assert.assertEquals(30_000l, factory.getConnectTimeoutMs());
7982
Assert.assertSame(channelMock, factory.newManagedChannel(MOCKED_HOST, MOCKED_PORT, null));
8083

81-
channelStaticMock.verify(FOR_ADDRESS, times(1));
82-
83-
verify(channelBuilderMock, times(0)).negotiationType(NegotiationType.TLS);
84-
verify(channelBuilderMock, times(1)).negotiationType(NegotiationType.PLAINTEXT);
85-
verify(channelBuilderMock, times(1)).maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
86-
verify(channelBuilderMock, times(1)).defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
87-
verify(channelBuilderMock, times(1)).withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
88-
verify(channelBuilderMock, times(0)).enableRetry();
89-
verify(channelBuilderMock, times(1)).disableRetry();
84+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(1));
85+
86+
Mockito.verify(channelBuilderMock, Mockito.times(0)).negotiationType(NegotiationType.TLS);
87+
Mockito.verify(channelBuilderMock, Mockito.times(1)).negotiationType(NegotiationType.PLAINTEXT);
88+
Mockito.verify(channelBuilderMock, Mockito.times(1))
89+
.maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
90+
Mockito.verify(channelBuilderMock, Mockito.times(1))
91+
.defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
92+
Mockito.verify(channelBuilderMock, Mockito.times(1))
93+
.withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
94+
Mockito.verify(channelBuilderMock, Mockito.times(0)).enableRetry();
95+
Mockito.verify(channelBuilderMock, Mockito.times(1)).disableRetry();
9096
}
9197

9298
@Test
@@ -97,20 +103,23 @@ public void defaultSslFactory() {
97103
.withConnectTimeout(Duration.ofMinutes(1));
98104

99105
ManagedChannelFactory factory = ChannelFactoryLoader.load().buildFactory(builder);
100-
channelStaticMock.verify(FOR_ADDRESS, times(0));
106+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(0));
101107

102108
Assert.assertEquals(60000l, factory.getConnectTimeoutMs());
103109
Assert.assertSame(channelMock, factory.newManagedChannel(MOCKED_HOST, MOCKED_PORT, null));
104110

105-
channelStaticMock.verify(FOR_ADDRESS, times(1));
106-
107-
verify(channelBuilderMock, times(1)).negotiationType(NegotiationType.TLS);
108-
verify(channelBuilderMock, times(0)).negotiationType(NegotiationType.PLAINTEXT);
109-
verify(channelBuilderMock, times(1)).maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
110-
verify(channelBuilderMock, times(1)).defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
111-
verify(channelBuilderMock, times(1)).withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
112-
verify(channelBuilderMock, times(1)).enableRetry();
113-
verify(channelBuilderMock, times(0)).disableRetry();
111+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(1));
112+
113+
Mockito.verify(channelBuilderMock, Mockito.times(1)).negotiationType(NegotiationType.TLS);
114+
Mockito.verify(channelBuilderMock, Mockito.times(0)).negotiationType(NegotiationType.PLAINTEXT);
115+
Mockito.verify(channelBuilderMock, Mockito.times(1))
116+
.maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
117+
Mockito.verify(channelBuilderMock, Mockito.times(1))
118+
.defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
119+
Mockito.verify(channelBuilderMock, Mockito.times(1))
120+
.withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
121+
Mockito.verify(channelBuilderMock, Mockito.times(1)).enableRetry();
122+
Mockito.verify(channelBuilderMock, Mockito.times(0)).disableRetry();
114123
}
115124

116125
@Test
@@ -119,20 +128,24 @@ public void customChannelInitializer() {
119128
.withUseDefaultGrpcResolver(true);
120129

121130
ManagedChannelFactory factory = ShadedNettyChannelFactory
122-
.withInterceptor(cb -> cb.withOption(ChannelOption.TCP_NODELAY, Boolean.TRUE))
131+
.withInterceptor(cb -> cb.enableFullStreamDecompression())
123132
.buildFactory(builder);
124133

125-
channelStaticMock.verify(FOR_ADDRESS, times(0));
134+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(0));
126135

127136
Assert.assertSame(channelMock, factory.newManagedChannel(MOCKED_HOST, MOCKED_PORT, null));
128137

129-
channelStaticMock.verify(FOR_ADDRESS, times(1));
130-
131-
verify(channelBuilderMock, times(1)).negotiationType(NegotiationType.PLAINTEXT);
132-
verify(channelBuilderMock, times(1)).maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
133-
verify(channelBuilderMock, times(0)).defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
134-
verify(channelBuilderMock, times(1)).withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
135-
verify(channelBuilderMock, times(1)).withOption(ChannelOption.TCP_NODELAY, Boolean.TRUE);
138+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(1));
139+
140+
Mockito.verify(channelBuilderMock, Mockito.times(1)).negotiationType(NegotiationType.PLAINTEXT);
141+
Mockito.verify(channelBuilderMock, Mockito.times(1))
142+
.maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
143+
Mockito.verify(channelBuilderMock, Mockito.times(0))
144+
.defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
145+
Mockito.verify(channelBuilderMock, Mockito.times(1))
146+
.withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
147+
Mockito.verify(channelBuilderMock, Mockito.times(1)).withOption(ChannelOption.TCP_NODELAY, Boolean.TRUE);
148+
Mockito.verify(channelBuilderMock, Mockito.times(1)).enableFullStreamDecompression();
136149
}
137150

138151
@Test
@@ -156,15 +169,18 @@ public void customSslFactory() throws CertificateException, IOException {
156169
selfSignedCert.delete();
157170
}
158171

159-
channelStaticMock.verify(FOR_ADDRESS, times(1));
160-
161-
verify(channelBuilderMock, times(1)).negotiationType(NegotiationType.TLS);
162-
verify(channelBuilderMock, times(0)).negotiationType(NegotiationType.PLAINTEXT);
163-
verify(channelBuilderMock, times(1)).maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
164-
verify(channelBuilderMock, times(1)).defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
165-
verify(channelBuilderMock, times(1)).withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
166-
verify(channelBuilderMock, times(0)).enableRetry();
167-
verify(channelBuilderMock, times(1)).disableRetry();
172+
channelStaticMock.verify(FOR_ADDRESS, Mockito.times(1));
173+
174+
Mockito.verify(channelBuilderMock, Mockito.times(1)).negotiationType(NegotiationType.TLS);
175+
Mockito.verify(channelBuilderMock, Mockito.times(0)).negotiationType(NegotiationType.PLAINTEXT);
176+
Mockito.verify(channelBuilderMock, Mockito.times(1))
177+
.maxInboundMessageSize(ShadedNettyChannelFactory.INBOUND_MESSAGE_SIZE);
178+
Mockito.verify(channelBuilderMock, Mockito.times(1))
179+
.defaultLoadBalancingPolicy(ShadedNettyChannelFactory.DEFAULT_BALANCER_POLICY);
180+
Mockito.verify(channelBuilderMock, Mockito.times(1))
181+
.withOption(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT);
182+
Mockito.verify(channelBuilderMock, Mockito.times(0)).enableRetry();
183+
Mockito.verify(channelBuilderMock, Mockito.times(1)).disableRetry();
168184
}
169185

170186
@Test

0 commit comments

Comments
 (0)