Skip to content

Commit cf852bb

Browse files
committed
feat: no longer support multihost
1 parent ec587c7 commit cf852bb

File tree

4 files changed

+65
-178
lines changed

4 files changed

+65
-178
lines changed

databend-jdbc/src/main/java/com/databend/jdbc/DatabendDriverUri.java

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
import java.util.Comparator;
1717
import java.util.HashMap;
1818
import java.util.LinkedHashMap;
19-
import java.util.LinkedHashSet;
2019
import java.util.List;
2120
import java.util.Locale;
2221
import java.util.Map;
2322
import java.util.Properties;
24-
import java.util.Set;
2523
import java.util.concurrent.TimeUnit;
2624

2725
import static com.databend.client.OkHttpUtils.*;
@@ -257,61 +255,30 @@ private static Map.Entry<DatabendNodes, Map<String, String>> parse(String url)
257255
}
258256
Map<String, String> uriProperties = new LinkedHashMap<>();
259257
String raw = url.substring(pos + JDBC_URL_START.length());
260-
String host;
261-
int port = -1;
262258
raw = tryParseUriUserPassword(raw, uriProperties);
259+
ensureSingleHostAuthority(raw, url);
263260

264-
// Split the URL into hosts and the rest
265-
// todo process bracket wrapped comma
266-
String[] hosts = raw.split(",");
267261
List<URI> uris = new ArrayList<>();
268262
try {
269-
for (String raw_host : hosts) {
270-
String fullUri = (raw_host.startsWith("http://") || raw_host.startsWith("https://")) ?
271-
raw_host :
272-
"http://" + raw_host;
273-
274-
URI uri = new URI(fullUri);
275-
String authority = uri.getAuthority();
276-
String[] hostAndPort = authority.split(":");
277-
if (hostAndPort.length == 2) {
278-
host = hostAndPort[0];
279-
port = Integer.parseInt(hostAndPort[1]);
280-
} else if (hostAndPort.length == 1) {
281-
host = hostAndPort[0];
282-
} else {
283-
throw new SQLException("Invalid host and port, url: " + url);
284-
}
285-
if (host == null || host.isEmpty()) {
286-
throw new SQLException("Invalid host " + host);
287-
}
288-
// Set SSL properties based on the scheme
289-
if ("https".equals(uri.getScheme())) {
290-
uriProperties.put(SSL.getKey(), "true");
291-
uriProperties.put(SSL_MODE.getKey(), "enable");
292-
} else {
293-
uriProperties.put(SSL.getKey(), "false");
294-
uriProperties.put(SSL_MODE.getKey(), "disable");
295-
}
296-
uris.add(uri);
263+
URI uri = parseSingleUri(raw);
264+
if (uri.getHost() == null || uri.getHost().isEmpty()) {
265+
throw new SQLException("Invalid host " + uri.getHost());
297266
}
298-
URI lastUri = uris.get(uris.size() - 1);
299-
// Initialize database from the last URI
300-
initDatabase(lastUri, uriProperties);
301-
String uriPath = lastUri.getPath();
302-
String uriQuery = lastUri.getQuery();
303-
String uriFragment = lastUri.getFragment();
304-
// Sync path and query from lastUri for the rest of uri
305-
for (int i = 0; i < uris.size() - 1; i++) {
306-
URI uri = uris.get(i);
307-
uris.set(i, new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uriPath, uriQuery, uriFragment));
267+
268+
if ("https".equals(uri.getScheme())) {
269+
uriProperties.put(SSL.getKey(), "true");
270+
uriProperties.put(SSL_MODE.getKey(), "enable");
271+
} else {
272+
uriProperties.put(SSL.getKey(), "false");
273+
uriProperties.put(SSL_MODE.getKey(), "disable");
308274
}
309-
// remove duplicate uris
310-
Set<URI> uriSet = new LinkedHashSet<>(uris);
311-
uris.clear();
312-
uris.addAll(uriSet);
313-
// Create DatabendNodes object
314-
// You might want to make this configurable
275+
uris.add(uri);
276+
277+
initDatabase(uri, uriProperties);
278+
String uriPath = uri.getPath();
279+
String uriQuery = uri.getQuery();
280+
String uriFragment = uri.getFragment();
281+
315282
DatabendClientLoadBalancingPolicy policy = DatabendClientLoadBalancingPolicy.create(DatabendClientLoadBalancingPolicy.DISABLED);
316283
DatabendNodes databendNodes = new DatabendNodes(uris, policy, uriPath, uriQuery, uriFragment, 5 * 60 * 1000);
317284
return new AbstractMap.SimpleImmutableEntry<>(databendNodes, uriProperties);
@@ -320,6 +287,33 @@ private static Map.Entry<DatabendNodes, Map<String, String>> parse(String url)
320287
}
321288
}
322289

290+
private static URI parseSingleUri(String rawHost) throws URISyntaxException {
291+
String fullUri = (rawHost.startsWith("http://") || rawHost.startsWith("https://")) ?
292+
rawHost :
293+
"http://" + rawHost;
294+
return new URI(fullUri);
295+
}
296+
297+
private static void ensureSingleHostAuthority(String raw, String originalUrl) throws SQLException {
298+
int endOfAuthority = raw.length();
299+
int slashIndex = raw.indexOf('/');
300+
if (slashIndex != -1 && slashIndex < endOfAuthority) {
301+
endOfAuthority = slashIndex;
302+
}
303+
int questionIndex = raw.indexOf('?');
304+
if (questionIndex != -1 && questionIndex < endOfAuthority) {
305+
endOfAuthority = questionIndex;
306+
}
307+
int fragmentIndex = raw.indexOf('#');
308+
if (fragmentIndex != -1 && fragmentIndex < endOfAuthority) {
309+
endOfAuthority = fragmentIndex;
310+
}
311+
String authoritySection = raw.substring(0, endOfAuthority);
312+
if (authoritySection.contains(",")) {
313+
throw new SQLException("Multiple hosts in JDBC URL are not supported: " + originalUrl);
314+
}
315+
}
316+
323317
public static boolean acceptsURL(String url) {
324318
return url.startsWith(JDBC_URL_START);
325319
}

databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,8 @@ public void testBasic() throws SQLException {
5151
}
5252

5353
@Test(groups = {"UNIT"})
54-
public void testMultiHost() throws SQLException {
55-
DatabendDriverUri uri = DatabendDriverUri.create("jdbc:databend://localhost,localhost:9991,localhost:31919/d2?ssl=true", null);
56-
Assert.assertEquals(uri.getNodes().getUris().size(), 3);
57-
for (int i = 0; i < 3; i++) {
58-
Assert.assertEquals(uri.getNodes().getUris().get(i).getScheme(), "https");
59-
Assert.assertEquals(uri.getNodes().getUris().get(i).getHost(), "localhost");
60-
Assert.assertEquals(uri.getNodes().getUris().get(i).getPath(), "/d2");
61-
Assert.assertEquals(uri.getNodes().getUris().get(i).getQuery(), "ssl=true");
62-
}
63-
Assert.assertEquals(uri.getNodes().getUris().get(0).getPort(), 443);
64-
Assert.assertEquals(uri.getNodes().getUris().get(1).getPort(), 9991);
65-
Assert.assertEquals(uri.getNodes().getUris().get(2).getPort(), 31919);
66-
}
67-
68-
@Test(groups = {"UNIT"})
69-
public void testSameHost() throws SQLException {
70-
DatabendDriverUri uri = DatabendDriverUri.create("jdbc:databend://u1:p1@localhost,localhost:9991,localhost/d2?ssl=false", null);
71-
System.out.println(uri.getNodes().toString());
72-
Assert.assertEquals(uri.getNodes().getUris().size(), 2);
73-
74-
for (int i = 0; i < 2; i++) {
75-
Assert.assertEquals(uri.getNodes().getUris().get(i).getScheme(), "http");
76-
Assert.assertEquals(uri.getNodes().getUris().get(i).getHost(), "localhost");
77-
Assert.assertEquals(uri.getNodes().getUris().get(i).getPath(), "/d2");
78-
Assert.assertEquals(uri.getNodes().getUris().get(i).getQuery(), "ssl=false");
79-
}
80-
Assert.assertEquals(uri.getNodes().getUris().get(0).getPort(), 8000);
81-
Assert.assertEquals(uri.getNodes().getUris().get(1).getPort(), 9991);
54+
public void testMultiHostNotSupported() {
55+
assertInvalid("jdbc:databend://localhost:8000,localhost:8001/default", "Multiple hosts in JDBC URL are not supported");
8256
}
8357

8458
@Test(groups = {"UNIT"})

databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
@Test(timeOut = 10000, groups = "MULTI_HOST" )
1818
public class TestMultiHost {
19-
private final String DEFAULT_JDBC_URL = "jdbc:databend://localhost:8001,localhost:8002,localhost:8003/default";
20-
private final String RANDOM_JDBC_URL = "jdbc:databend://localhost:8001,localhost:8002,localhost:8003/default?load_balancing_policy=random";
21-
private final String ROUND_ROBIN_JDBC_URL = "jdbc:databend://localhost:8001,localhost:8002,localhost:8003/default?load_balancing_policy=round_robin";
22-
private final String FAIL_OVER_JDBC_URL = "jdbc:databend://localhost:7222,localhost:7223,localhost:7224,localhost:8001/default?load_balancing_policy=round_robin&max_failover_retry=4";
2319
private final String AUTO_DISCOVERY_JDBC_URL = "jdbc:databend://localhost:8001/default?load_balancing_policy=round_robin&auto_discovery=true";
2420
private final String UNSUPPORT_AUTO_DISCOVERY_JDBC_URL = "jdbc:databend://localhost:8001/default?load_balancing_policy=round_robin&auto_discovery=true&enable_mock=true";
2521

@@ -29,85 +25,6 @@ private Connection createConnection(String url)
2925
return DriverManager.getConnection(url, "databend", "databend");
3026
}
3127

32-
@Test(groups = {"IT", "MULTI_HOST"})
33-
public void testDefaultLoadBalancing()
34-
throws SQLException {
35-
HashMap<Integer, Integer> expect = new HashMap<>();
36-
expect.put(8001, 90);
37-
38-
HashMap<Integer, Integer> actual = get_hosts_used(DEFAULT_JDBC_URL);
39-
Assert.assertEquals(expect, actual);
40-
}
41-
42-
@Test(groups = {"IT", "MULTI_HOST"})
43-
public void testRandomLoadBalancing()
44-
throws SQLException {
45-
HashMap<Integer, Integer> actual = get_hosts_used(RANDOM_JDBC_URL);
46-
47-
int node8001 = actual.get(8001);
48-
int node8002 = actual.get(8002);
49-
int node8003 = actual.get(8003);
50-
51-
Assert.assertTrue(node8001 > 0 && node8002 > 0 && node8003 > 0, "got " + actual);
52-
Assert.assertEquals(node8001 + node8002 + node8003, 90, "got " + actual);
53-
}
54-
55-
@Test(groups = {"IT", "MULTI_HOST"})
56-
public void testRoundRobinLoadBalancing()
57-
throws SQLException {
58-
HashMap<Integer, Integer> expect = new HashMap<>();
59-
expect.put(8001, 30);
60-
expect.put(8002, 30);
61-
expect.put(8003, 30);
62-
63-
HashMap<Integer, Integer> actual = get_hosts_used(ROUND_ROBIN_JDBC_URL);
64-
Assert.assertEquals(expect, actual);
65-
}
66-
67-
@Test(groups = {"IT", "MULTI_HOST"})
68-
public void testRoundRobinTransaction()
69-
throws SQLException {
70-
// try to connect with three nodes 1000 times and count for each node
71-
try (Connection connection = createConnection(ROUND_ROBIN_JDBC_URL)) {
72-
DatabendStatement statement = (DatabendStatement) connection.createStatement();
73-
statement.execute("drop table if exists test_transaction;");
74-
statement.execute("create table if not exists test_transaction(id int);");
75-
76-
}
77-
for (int i = 0; i < 30; i++) {
78-
try (Connection connection = createConnection(ROUND_ROBIN_JDBC_URL)) {
79-
DatabendStatement statement = (DatabendStatement) connection.createStatement();
80-
// use transaction select a table, drop a table, insert data into table bring i index
81-
statement.execute("begin;");
82-
statement.execute("insert into test_transaction values(" + i + ");");
83-
statement.execute("select * from test_transaction;");
84-
statement.execute("commit;");
85-
}
86-
}
87-
88-
// query on test
89-
try (Connection connection = createConnection(ROUND_ROBIN_JDBC_URL)) {
90-
DatabendStatement statement = (DatabendStatement) connection.createStatement();
91-
statement.execute("select * from test_transaction;");
92-
ResultSet r = statement.getResultSet();
93-
int count = 0;
94-
while (r.next()) {
95-
count++;
96-
}
97-
Assert.assertEquals(count, 30);
98-
}
99-
}
100-
// @Test(groups = {"IT", "MULTI_HOST"})
101-
// skip since getConnection not support multihost for now
102-
// public void testFailOver()
103-
// throws SQLException {
104-
// HashMap<Integer, Integer> expect = new HashMap<>();
105-
// expect.put(8001, 90);
106-
//
107-
// HashMap<Integer, Integer> actual = get_hosts_used(FAIL_OVER_JDBC_URL);
108-
// Assert.assertEquals(expect, actual);
109-
// }
110-
11128
@Test(groups = {"IT", "MULTI_HOST"})
11229
public void testAutoDiscovery()
11330
throws SQLException {
@@ -136,18 +53,20 @@ public void testUnSupportedAutoDiscovery()
13653
@Test(groups = {"UNIT"})
13754
public void testAutoDiscoveryUriParsing() throws SQLException {
13855
DatabendDriverUri uri = DatabendDriverUri.create("jdbc:databend://localhost:8001?ssl=true", null);
139-
DatabendDriverUri uri2 = DatabendDriverUri.create("jdbc:databend://127.0.0.1:8001,127.0.0.1:8002,127.0.0.1:8003?ssl=true", null);
140-
List<URI> uris2 = uri2.getNodes().getUris();
141-
14256
DatabendNodes nodes = uri.getNodes();
14357
List<DiscoveryNode> discoveryNodes = new ArrayList<>();
14458
discoveryNodes.add(DiscoveryNode.create("127.0.0.1:8001"));
14559
discoveryNodes.add(DiscoveryNode.create("127.0.0.1:8002"));
14660
discoveryNodes.add(DiscoveryNode.create("127.0.0.1:8003"));
14761
List<URI> uris = nodes.parseURI(discoveryNodes);
14862
Assert.assertEquals(uris.size(), 3);
149-
Assert.assertEquals(uris2.size(), 3);
150-
Assert.assertEquals(uris2, uris);
63+
Assert.assertEquals(uris.get(0).getScheme(), "https");
64+
Assert.assertEquals(uris.get(0).getHost(), "127.0.0.1");
65+
Assert.assertEquals(uris.get(0).getPath(), "");
66+
Assert.assertEquals(uris.get(0).getQuery(), "ssl=true");
67+
Assert.assertEquals(uris.get(0).getPort(), 8001);
68+
Assert.assertEquals(uris.get(1).getPort(), 8002);
69+
Assert.assertEquals(uris.get(2).getPort(), 8003);
15170
}
15271

15372
private HashMap<Integer, Integer> get_hosts_used(String dsn) throws SQLException {

0 commit comments

Comments
 (0)