Skip to content

Commit

Permalink
Fix HiveDriverConsul to use service host when available
Browse files Browse the repository at this point in the history
  • Loading branch information
w.montaz committed Jun 12, 2024
1 parent c737bda commit c189855
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,22 @@ private List<HealthService> getHealthyEndPoints(String serviceName) {
*/
private String getEndPoint(String url) throws SQLException {
String serviceName = url.split("/")[0];
List<HealthService> nodes = getHealthyEndPoints(serviceName);
if (nodes.size() == 0) throw new SQLException("No nodes are available for service: " + serviceName);
HealthService electedNode = nodes.get(ThreadLocalRandom.current().nextInt(nodes.size()));
List<HealthService> healthServices = getHealthyEndPoints(serviceName);
if (healthServices.size() == 0) throw new SQLException("No nodes are available for service: " + serviceName);
HealthService randomHealthyService = healthServices.get(ThreadLocalRandom.current().nextInt(healthServices.size()));

String host = electedNode.getNode().getAddress();
String port = String.valueOf(electedNode.getService().getPort());

String nodeHost = randomHealthyService.getNode().getAddress();
String serviceHost = randomHealthyService.getService().getAddress();

String host;
if (serviceHost.isEmpty()) {
host = nodeHost;
} else {
host = serviceHost;
}

String port = String.valueOf(randomHealthyService.getService().getPort());

String hiveJdbcConf = url.replace(serviceName, "");
return "jdbc:hive2://" + host + ":" + port + hiveJdbcConf;
Expand Down

0 comments on commit c189855

Please sign in to comment.