Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend JDBC URL pattern to support failover #411

Merged
merged 3 commits into from
Mar 5, 2022

Conversation

loneylee
Copy link
Contributor

@loneylee loneylee commented Mar 3, 2022

This PR proposes to extend the JDBC URL pattern as follows to support failover.

jdbc:clickhouse://host1:port1,host2:port2[/database][?prop1=value1&prop2=value2]

To be clear, do not expect it to act as load balancing, the mechanism of failover is:

  1. try to connect the first host at the beginning
  2. if the first host is unreachable, then try the second, and so on
  3. when the connection is broken, go 1

@codecov
Copy link

codecov bot commented Mar 3, 2022

Codecov Report

Merging #411 (747b00a) into master (c5d1c14) will increase coverage by 0.39%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #411      +/-   ##
============================================
+ Coverage     62.60%   62.99%   +0.39%     
- Complexity     1256     1263       +7     
============================================
  Files           135      135              
  Lines          6662     6662              
  Branches        519      519              
============================================
+ Hits           4171     4197      +26     
+ Misses         2216     2197      -19     
+ Partials        275      268       -7     
Impacted Files Coverage Δ
...ava/com/github/housepower/client/NativeClient.java 84.41% <100.00%> (+10.73%) ⬆️
...m/github/housepower/jdbc/ClickHouseConnection.java 65.35% <100.00%> (+11.14%) ⬆️
...ithub/housepower/jdbc/ClickhouseJdbcUrlParser.java 95.34% <100.00%> (+15.63%) ⬆️
...m/github/housepower/settings/ClickHouseConfig.java 77.84% <100.00%> (+0.72%) ⬆️
...m/github/housepower/buffer/SocketBuffedReader.java 78.57% <0.00%> (+3.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c5d1c14...747b00a. Read the comment docs.

@baibaichen
Copy link
Collaborator

LGTM

public static final String HOST_DELIMITER = ",";
public static final String PORT_DELIMITER = ":";

public static final Pattern CONNECTION_PATTERN = Pattern.compile("//(?<hosts>[^/?\\s]+)(?:/(?<database>([a-zA-Z0-9_]+)))?(?:\\?(?<query>.*))?");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does ck1,ck2:1234 be interpreted?

  1. ck1:default_port and ck2:1234 or
  2. ck1:1234 and ck2:1234

Copy link
Member

@pan3793 pan3793 Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any behavior looks reasonable, please just add clear comments here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query is confusing, we can learn something from MySQL[1] or Apache Hive[2], maybe vars or properties is a proper name.

[1] https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html
[2] https://cwiki.apache.org/confluence/display/hive/hiveserver2+clients#HiveServer2Clients-ConnectionURLs

Copy link
Member

@pan3793 pan3793 Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some bad but recoverable patterns ck1:, ,ck1, ck1,ck2,, ck1:,ck2

Copy link
Member

@pan3793 pan3793 Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another bad but recoverable pattern jdbc:clickhouse://ck1,ck2/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already fix

lastException = e;
}
tryIndex++;
} while (nativeClient == null && tryIndex < configure.hosts().size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the former node always has high priority, so it does not a load-balanced implementation? If yes, let's add clear comments on the regex pattern to avoid surprising users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does not a load-balanced implementation

"//,ck1",
"//ck1,ck2,",
"//ck1:,ck2",
"//ck1,ck2/"
Copy link
Member

@pan3793 pan3793 Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no objection to treating them illegal except the last one, even though I think they are recoverable.
Can we make jdbc:clickhouse//ck1,ck2/ acceptable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal, as long as document it clearly.

@pan3793 pan3793 changed the title Support failover Extend JDBC URL pattern to support failover Mar 5, 2022
@pan3793
Copy link
Member

pan3793 commented Mar 5, 2022

Thanks a lot to @loneylee for bringing this amazing feature, I updated the PR description to make it clear, and please double-check if I misunderstand something.

@loneylee
Copy link
Contributor Author

loneylee commented Mar 5, 2022

I am honored to be able to help the community, and if there are issues related to this issue in the future, I am willing to assist in solving it

@sundy-li
Copy link
Member

sundy-li commented Mar 5, 2022

LGTM, Let's merge !

@sundy-li sundy-li merged commit ffd0a65 into housepower:master Mar 5, 2022
baibaichen pushed a commit to baibaichen/ClickHouse-Native-JDBC that referenced this pull request Mar 6, 2022
* support failover

* fix pattern and add ut

* fix ut wrong pair host and port
baibaichen pushed a commit that referenced this pull request Mar 6, 2022
* support failover

* fix pattern and add ut

* fix ut wrong pair host and port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants