Skip to content

Commit 225708d

Browse files
authored
Added option useDiscovery enabled by default (#151)
2 parents fc0c30b + 26629e1 commit 225708d

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
lines changed

jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import tech.ydb.core.grpc.GrpcTransport;
1212
import tech.ydb.core.grpc.GrpcTransportBuilder;
13+
import tech.ydb.core.impl.SingleChannelTransport;
1314
import tech.ydb.core.settings.BaseRequestSettings;
1415
import tech.ydb.jdbc.YdbPrepareMode;
1516
import tech.ydb.jdbc.YdbTracer;
@@ -238,7 +239,7 @@ public static YdbContext createContext(YdbConfig config) throws SQLException {
238239
});
239240
});
240241

241-
grpcTransport = builder.build();
242+
grpcTransport = config.isUseDiscovery() ? builder.build() : new SingleChannelTransport(builder);
242243

243244
PooledTableClient.Builder tableClient = PooledTableClient.newClient(
244245
GrpcTableRpc.useTransport(grpcTransport)

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class YdbConfig {
4242
static final YdbProperty<String> USE_PREFIX_PATH = YdbProperty.string("usePrefixPath",
4343
"Add prefix path to all operations performed by driver");
4444

45+
static final YdbProperty<Boolean> USE_DISCOVERY = YdbProperty.bool("useDiscovery",
46+
"Use discovery (client balancing) for YDB cluster connection", true);
47+
4548
static final YdbProperty<Boolean> FULLSCAN_DETECTOR_ENABLED = YdbProperty.bool(
4649
"jdbcFullScanDetector", "Enable analizator for collecting query stats", false
4750
);
@@ -64,6 +67,7 @@ public class YdbConfig {
6467
private final int preparedStatementsCacheSize;
6568

6669
private final boolean useQueryService;
70+
private final boolean useDiscovery;
6771
private final YdbValue<String> usePrefixPath;
6872

6973
private final boolean fullScanDetectorEnabled;
@@ -83,6 +87,7 @@ private YdbConfig(
8387
this.preparedStatementsCacheSize = Math.max(0, PREPARED_STATEMENT_CACHE_SIZE.readValue(props).getValue());
8488

8589
this.useQueryService = USE_QUERY_SERVICE.readValue(props).getValue();
90+
this.useDiscovery = USE_DISCOVERY.readValue(props).getValue();
8691
this.usePrefixPath = USE_PREFIX_PATH.readValue(props);
8792

8893
this.fullScanDetectorEnabled = FULLSCAN_DETECTOR_ENABLED.readValue(props).getValue();
@@ -124,6 +129,10 @@ public boolean isUseQueryService() {
124129
return this.useQueryService;
125130
}
126131

132+
public boolean isUseDiscovery() {
133+
return this.useDiscovery;
134+
}
135+
127136
public boolean hasPrefixPath() {
128137
return usePrefixPath.hasValue();
129138
}
@@ -188,6 +197,7 @@ public DriverPropertyInfo[] toPropertyInfo() throws SQLException {
188197
YdbConfig.CACHE_CONNECTIONS_IN_DRIVER.toInfo(properties),
189198
YdbConfig.PREPARED_STATEMENT_CACHE_SIZE.toInfo(properties),
190199
YdbConfig.USE_QUERY_SERVICE.toInfo(properties),
200+
YdbConfig.USE_DISCOVERY.toInfo(properties),
191201
YdbConfig.USE_PREFIX_PATH.toInfo(properties),
192202

193203
YdbConnectionProperties.LOCAL_DATACENTER.toInfo(properties),

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverExampleTest.java

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import java.sql.Types;
99

1010
import org.junit.jupiter.api.Assertions;
11-
import org.junit.jupiter.api.Test;
1211
import org.junit.jupiter.api.extension.RegisterExtension;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.EnumSource;
1314

1415
import tech.ydb.test.junit5.YdbHelperExtension;
1516

@@ -21,23 +22,10 @@ public class YdbDriverExampleTest {
2122
@RegisterExtension
2223
private static final YdbHelperExtension ydb = new YdbHelperExtension();
2324

24-
private static String jdbcURL() {
25-
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
26-
.append(ydb.useTls() ? "grpcs://" : "grpc://")
27-
.append(ydb.endpoint())
28-
.append("/?database=")
29-
.append(ydb.database());
30-
31-
if (ydb.authToken() != null) {
32-
jdbc.append("&").append("token=").append(ydb.authToken());
33-
}
34-
35-
return jdbc.toString();
36-
}
37-
38-
@Test
39-
public void testYdb() throws SQLException {
40-
try (Connection connection = DriverManager.getConnection(jdbcURL())) {
25+
@ParameterizedTest
26+
@EnumSource(Mode.class)
27+
public void testYdb(Mode mode) throws SQLException {
28+
try (Connection connection = DriverManager.getConnection(mode.getJdbcURL())) {
4129
try {
4230
connection.createStatement()
4331
.execute("drop table table_sample");
@@ -148,9 +136,10 @@ public void testYdb() throws SQLException {
148136
}
149137
}
150138

151-
@Test
152-
public void testYdbNotNull() throws SQLException {
153-
try (Connection connection = DriverManager.getConnection(jdbcURL())) {
139+
@ParameterizedTest
140+
@EnumSource(Mode.class)
141+
public void testYdbNotNull(Mode mode) throws SQLException {
142+
try (Connection connection = DriverManager.getConnection(mode.getJdbcURL())) {
154143
try {
155144
connection.createStatement().execute("drop table table_sample");
156145
} catch (SQLException e) {
@@ -249,4 +238,57 @@ public void testYdbNotNull() throws SQLException {
249238
}
250239
}
251240
}
241+
242+
private enum Mode {
243+
BASE {
244+
@Override
245+
String getJdbcURL() {
246+
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
247+
.append(ydb.useTls() ? "grpcs://" : "grpc://")
248+
.append(ydb.endpoint())
249+
.append("/")
250+
.append(ydb.database());
251+
252+
if (ydb.authToken() != null) {
253+
jdbc.append("?").append("token=").append(ydb.authToken());
254+
}
255+
256+
return jdbc.toString();
257+
}
258+
},
259+
OLD_STYLE {
260+
@Override
261+
String getJdbcURL() {
262+
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
263+
.append(ydb.useTls() ? "grpcs://" : "grpc://")
264+
.append(ydb.endpoint())
265+
.append("/?database=")
266+
.append(ydb.database());
267+
268+
if (ydb.authToken() != null) {
269+
jdbc.append("&").append("token=").append(ydb.authToken());
270+
}
271+
272+
return jdbc.toString();
273+
}
274+
},
275+
NO_DISCOVERY {
276+
@Override
277+
String getJdbcURL() {
278+
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
279+
.append(ydb.useTls() ? "grpcs://" : "grpc://")
280+
.append(ydb.endpoint())
281+
.append(ydb.database())
282+
.append("?useDiscovery=false");
283+
284+
if (ydb.authToken() != null) {
285+
jdbc.append("&").append("token=").append(ydb.authToken());
286+
}
287+
288+
return jdbc.toString();
289+
}
290+
};
291+
292+
abstract String getJdbcURL();
293+
}
252294
}

jdbc/src/test/java/tech/ydb/jdbc/settings/YdbDriverProperitesTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ static DriverPropertyInfo[] defaultPropertyInfo(@Nullable String localDatacenter
306306
new DriverPropertyInfo("cacheConnectionsInDriver", "true"),
307307
new DriverPropertyInfo("preparedStatementCacheQueries", "256"),
308308
new DriverPropertyInfo("useQueryService", "true"),
309+
new DriverPropertyInfo("useDiscovery", "true"),
309310
new DriverPropertyInfo("usePrefixPath", ""),
310311
new DriverPropertyInfo("localDatacenter", localDatacenter),
311312
new DriverPropertyInfo("secureConnection", ""),
@@ -354,6 +355,7 @@ static DriverPropertyInfo[] customizedPropertyInfo() {
354355
new DriverPropertyInfo("cacheConnectionsInDriver", "false"),
355356
new DriverPropertyInfo("preparedStatementCacheQueries", "100"),
356357
new DriverPropertyInfo("useQueryService", "false"),
358+
new DriverPropertyInfo("useDiscovery", "false"),
357359
new DriverPropertyInfo("usePrefixPath", "/demo/oltp"),
358360
new DriverPropertyInfo("localDatacenter", "sas"),
359361
new DriverPropertyInfo("secureConnection", "true"),

0 commit comments

Comments
 (0)