88import java .util .logging .Logger ;
99
1010import tech .ydb .core .grpc .GrpcTransport ;
11+ import tech .ydb .core .grpc .GrpcTransportBuilder ;
1112import tech .ydb .jdbc .exception .YdbConfigurationException ;
1213import tech .ydb .jdbc .settings .ParsedProperty ;
1314import tech .ydb .jdbc .settings .YdbClientProperties ;
1415import tech .ydb .jdbc .settings .YdbClientProperty ;
1516import tech .ydb .jdbc .settings .YdbConnectionProperties ;
17+ import tech .ydb .jdbc .settings .YdbConnectionProperty ;
1618import tech .ydb .jdbc .settings .YdbOperationProperties ;
1719import tech .ydb .scheme .SchemeClient ;
1820import tech .ydb .table .TableClient ;
2628
2729public class YdbContext implements AutoCloseable {
2830 private static final Logger LOGGER = Logger .getLogger (YdbContext .class .getName ());
29- private static final int SESSION_POOL_STEP = 50 ;
30- private static final int SESSION_POOL_THRESHOLD = 10 ;
31+
32+ private static final int SESSION_POOL_DEFAULT_MIN_SIZE = 0 ;
33+ private static final int SESSION_POOL_DEFAULT_MAX_SIZE = 50 ;
34+ private static final int SESSION_POOL_RESIZE_STEP = 50 ;
35+ private static final int SESSION_POOL_RESIZE_THRESHOLD = 10 ;
3136
3237 private final YdbConfig config ;
3338
@@ -62,6 +67,10 @@ public String getUrl() {
6267 return config .getUrl ();
6368 }
6469
70+ public int getConnectionsCount () {
71+ return connectionsCount .get ();
72+ }
73+
6574 public YdbOperationProperties getOperationProperties () {
6675 return config .getOperationProperties ();
6776 }
@@ -80,8 +89,8 @@ public void close() {
8089 public void register () {
8190 int actual = connectionsCount .incrementAndGet ();
8291 int maxSize = tableClient .sessionPoolStats ().getMaxSize ();
83- if (autoResizeSessionPool && actual > maxSize - SESSION_POOL_THRESHOLD ) {
84- int newSize = maxSize + SESSION_POOL_STEP ;
92+ if (autoResizeSessionPool && actual > maxSize - SESSION_POOL_RESIZE_THRESHOLD ) {
93+ int newSize = maxSize + SESSION_POOL_RESIZE_STEP ;
8594 if (maxSize == tableClient .sessionPoolStats ().getMaxSize ()) {
8695 tableClient .updatePoolMaxSize (newSize );
8796 }
@@ -91,9 +100,9 @@ public void register() {
91100 public void deregister () {
92101 int actual = connectionsCount .decrementAndGet ();
93102 int maxSize = tableClient .sessionPoolStats ().getMaxSize ();
94- if (autoResizeSessionPool && maxSize > SESSION_POOL_STEP ) {
95- if (actual < maxSize - SESSION_POOL_STEP + SESSION_POOL_THRESHOLD ) {
96- int newSize = maxSize - SESSION_POOL_STEP ;
103+ if (autoResizeSessionPool && maxSize > SESSION_POOL_RESIZE_STEP ) {
104+ if (actual < maxSize - SESSION_POOL_RESIZE_STEP - 2 * SESSION_POOL_RESIZE_THRESHOLD ) {
105+ int newSize = maxSize - SESSION_POOL_RESIZE_STEP ;
97106 if (maxSize == tableClient .sessionPoolStats ().getMaxSize ()) {
98107 tableClient .updatePoolMaxSize (newSize );
99108 }
@@ -108,7 +117,7 @@ public static YdbContext createContext(YdbConfig config) throws SQLException {
108117
109118 LOGGER .log (Level .INFO , "Creating new YDB connection to {0}" , connProps .getConnectionString ());
110119
111- GrpcTransport grpcTransport = connProps . toGrpcTransport ( );
120+ GrpcTransport grpcTransport = buildGrpcTransport ( connProps );
112121 PooledTableClient .Builder tableClient = PooledTableClient .newClient (
113122 GrpcTableRpc .useTransport (grpcTransport )
114123 );
@@ -120,6 +129,21 @@ public static YdbContext createContext(YdbConfig config) throws SQLException {
120129 }
121130 }
122131
132+ public static GrpcTransport buildGrpcTransport (YdbConnectionProperties props ) {
133+ GrpcTransportBuilder builder = GrpcTransport .forConnectionString (props .getConnectionString ());
134+ for (Map .Entry <YdbConnectionProperty <?>, ParsedProperty > entry : props .getParams ().entrySet ()) {
135+ if (entry .getValue () != null ) {
136+ entry .getKey ().getSetter ().accept (builder , entry .getValue ().getParsedValue ());
137+ }
138+ }
139+
140+ if (props .hasStaticCredentials ()) {
141+ builder = builder .withAuthProvider (props .getStaticCredentials ());
142+ }
143+
144+ return builder .build ();
145+ }
146+
123147 private static boolean buildTableClient (TableClient .Builder builder , YdbClientProperties props ) {
124148 for (Map .Entry <YdbClientProperty <?>, ParsedProperty > entry : props .getParams ().entrySet ()) {
125149 if (entry .getValue () != null ) {
@@ -134,15 +158,15 @@ private static boolean buildTableClient(TableClient.Builder builder, YdbClientPr
134158 return true ;
135159 }
136160
137- int minSize = 0 ;
138- int maxSize = 50 ;
161+ int minSize = SESSION_POOL_DEFAULT_MIN_SIZE ;
162+ int maxSize = SESSION_POOL_DEFAULT_MAX_SIZE ;
139163
140164 if (minSizeConfig != null ) {
141165 minSize = Math .max (0 , minSizeConfig .getParsedValue ());
142- maxSize = Math .min (maxSize , minSize );
166+ maxSize = Math .max (maxSize , minSize );
143167 }
144168 if (maxSizeConfig != null ) {
145- maxSize = Math .max (minSize , maxSizeConfig .getParsedValue ());
169+ maxSize = Math .max (minSize + 1 , maxSizeConfig .getParsedValue ());
146170 }
147171
148172 builder .sessionPoolSize (minSize , maxSize );
0 commit comments