11package tech .ydb .jdbc .context ;
22
33import java .sql .SQLException ;
4+ import java .util .Map ;
45import java .util .Objects ;
6+ import java .util .concurrent .atomic .AtomicInteger ;
57import java .util .logging .Level ;
68import java .util .logging .Logger ;
79
810import tech .ydb .core .grpc .GrpcTransport ;
911import tech .ydb .jdbc .exception .YdbConfigurationException ;
12+ import tech .ydb .jdbc .settings .ParsedProperty ;
1013import tech .ydb .jdbc .settings .YdbClientProperties ;
14+ import tech .ydb .jdbc .settings .YdbClientProperty ;
1115import tech .ydb .jdbc .settings .YdbConnectionProperties ;
1216import tech .ydb .jdbc .settings .YdbOperationProperties ;
1317import tech .ydb .scheme .SchemeClient ;
1418import tech .ydb .table .TableClient ;
19+ import tech .ydb .table .impl .PooledTableClient ;
20+ import tech .ydb .table .rpc .grpc .GrpcTableRpc ;
1521
1622/**
1723 *
2026
2127public class YdbContext implements AutoCloseable {
2228 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 ;
2331
2432 private final YdbConfig config ;
2533
2634 private final GrpcTransport grpcTransport ;
27- private final TableClient tableClient ;
35+ private final PooledTableClient tableClient ;
2836 private final SchemeClient schemeClient ;
37+ private final boolean autoResizeSessionPool ;
2938
30- private YdbContext (YdbConfig config , GrpcTransport grpcTransport , TableClient tableClient ) {
39+ private final AtomicInteger connectionsCount = new AtomicInteger ();
40+
41+ private YdbContext (YdbConfig config , GrpcTransport transport , PooledTableClient tableClient , boolean autoResize ) {
3142 this .config = config ;
32- this .grpcTransport = Objects .requireNonNull (grpcTransport );
43+ this .grpcTransport = Objects .requireNonNull (transport );
3344 this .tableClient = Objects .requireNonNull (tableClient );
34- this .schemeClient = SchemeClient .newClient (grpcTransport ).build ();
45+ this .schemeClient = SchemeClient .newClient (transport ).build ();
46+ this .autoResizeSessionPool = autoResize ;
3547 }
3648
3749 public String getDatabase () {
@@ -65,6 +77,30 @@ public void close() {
6577 }
6678 }
6779
80+ public void register () {
81+ int actual = connectionsCount .incrementAndGet ();
82+ int maxSize = tableClient .sessionPoolStats ().getMaxSize ();
83+ if (autoResizeSessionPool && actual > maxSize - SESSION_POOL_THRESHOLD ) {
84+ int newSize = maxSize + SESSION_POOL_STEP ;
85+ if (maxSize == tableClient .sessionPoolStats ().getMaxSize ()) {
86+ tableClient .updatePoolMaxSize (newSize );
87+ }
88+ }
89+ }
90+
91+ public void deregister () {
92+ int actual = connectionsCount .decrementAndGet ();
93+ 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 ;
97+ if (maxSize == tableClient .sessionPoolStats ().getMaxSize ()) {
98+ tableClient .updatePoolMaxSize (newSize );
99+ }
100+ }
101+ }
102+ }
103+
68104 public static YdbContext createContext (YdbConfig config ) throws SQLException {
69105 try {
70106 YdbConnectionProperties connProps = config .getConnectionProperties ();
@@ -73,12 +109,43 @@ public static YdbContext createContext(YdbConfig config) throws SQLException {
73109 LOGGER .log (Level .INFO , "Creating new YDB connection to {0}" , connProps .getConnectionString ());
74110
75111 GrpcTransport grpcTransport = connProps .toGrpcTransport ();
112+ PooledTableClient .Builder tableClient = PooledTableClient .newClient (
113+ GrpcTableRpc .useTransport (grpcTransport )
114+ );
115+ boolean autoResize = buildTableClient (tableClient , clientProps );
76116
77- TableClient tableClient = clientProps .toTableClient (grpcTransport );
78-
79- return new YdbContext (config , grpcTransport , tableClient );
117+ return new YdbContext (config , grpcTransport , tableClient .build (), autoResize );
80118 } catch (Exception ex ) {
81119 throw new YdbConfigurationException ("Cannot connect to YDB" , ex );
82120 }
83121 }
122+
123+ private static boolean buildTableClient (TableClient .Builder builder , YdbClientProperties props ) {
124+ for (Map .Entry <YdbClientProperty <?>, ParsedProperty > entry : props .getParams ().entrySet ()) {
125+ if (entry .getValue () != null ) {
126+ entry .getKey ().getSetter ().accept (builder , entry .getValue ().getParsedValue ());
127+ }
128+ }
129+
130+ ParsedProperty minSizeConfig = props .getProperty (YdbClientProperty .SESSION_POOL_SIZE_MIN );
131+ ParsedProperty maxSizeConfig = props .getProperty (YdbClientProperty .SESSION_POOL_SIZE_MAX );
132+
133+ if (minSizeConfig == null && maxSizeConfig == null ) {
134+ return true ;
135+ }
136+
137+ int minSize = 0 ;
138+ int maxSize = 50 ;
139+
140+ if (minSizeConfig != null ) {
141+ minSize = Math .max (0 , minSizeConfig .getParsedValue ());
142+ maxSize = Math .min (maxSize , minSize );
143+ }
144+ if (maxSizeConfig != null ) {
145+ maxSize = Math .max (minSize , maxSizeConfig .getParsedValue ());
146+ }
147+
148+ builder .sessionPoolSize (minSize , maxSize );
149+ return false ;
150+ }
84151}
0 commit comments