Skip to content

Commit

Permalink
Re-use control connection. Rename explicit-fallback-only and failed-h…
Browse files Browse the repository at this point in the history
…ost-ttl properties
  • Loading branch information
ashetkar committed Apr 23, 2024
1 parent a7eb111 commit 12ee252
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
29 changes: 9 additions & 20 deletions pgjdbc/src/main/java/com/yugabyte/ysql/LoadBalanceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class LoadBalanceManager {

private static ConcurrentHashMap<String, NodeInfo> clusterInfoMap = new ConcurrentHashMap<>();
private static Connection controlConnection = null;
protected static final String GET_SERVERS_QUERY = "select * from yb_servers()";
protected static final Logger LOGGER = Logger.getLogger(LoadBalanceManager.class.getName());
private static long lastRefreshTime;
Expand Down Expand Up @@ -104,13 +105,13 @@ private static synchronized boolean refresh(Connection conn, long refreshInterva
LOGGER.warning("Could not parse port " + port + " for host " + host + ", using 5433 instead.");
nodeInfo.port = 5433;
}
long failedHostTTL = Long.getLong("failed-host-ttl-seconds", DEFAULT_FAILED_HOST_TTL_SECONDS);
long failedHostTTL = Long.getLong("failed-host-reconnect-delay-secs", DEFAULT_FAILED_HOST_TTL_SECONDS);
if (nodeInfo.isDown) {
if (System.currentTimeMillis() - nodeInfo.isDownSince > (failedHostTTL * 1000)) {
LOGGER.fine("Marking " + nodeInfo.host + " as UP since failed-host-ttl has elapsed");
LOGGER.fine("Marking " + nodeInfo.host + " as UP since failed-host-reconnect-delay-secs has elapsed");
nodeInfo.isDown = false;
} else {
LOGGER.fine("Keeping " + nodeInfo.host + " marked as DOWN since failed-host-ttl not elapsed");
LOGGER.fine("Keeping " + nodeInfo.host + " as DOWN since failed-host-reconnect-delay-secs has not elapsed");
}
}
}
Expand Down Expand Up @@ -304,20 +305,17 @@ private static synchronized boolean checkAndRefresh(LoadBalanceProperties loadBa
Properties props = loadBalanceProperties.getOriginalProperties();
String url = loadBalanceProperties.getStrippedURL();
HostSpec[] hspec = hostSpecs(props);
Connection controlConnection = null;
boolean connectionCreated = false;
boolean gotException = false;

ArrayList<String> hosts = getAllAvailableHosts(new ArrayList<>());
while (true) {
try {
controlConnection = new PgConnection(hspec, user, dbName, props, url);
connectionCreated = true;
if (controlConnection == null || controlConnection.isClosed()) {
controlConnection = new PgConnection(hspec, user, dbName, props, url);
}
refresh(controlConnection, lb.getRefreshListSeconds());
controlConnection.close();
break;
} catch (SQLException ex) {
gotException = true;
if (PSQLState.UNDEFINED_FUNCTION.getState().equals(ex.getSQLState())) {
LOGGER.warning("Received error UNDEFINED_FUNCTION (42883)");
return false;
Expand All @@ -332,24 +330,15 @@ private static synchronized boolean checkAndRefresh(LoadBalanceProperties loadBa
hosts.remove(h.getHost());
}
if (hosts.isEmpty()) {
LOGGER.fine("Failed to establish control connection");
LOGGER.fine("Failed to establish control connection to available servers");
return false;
} else {
// Try the first host in the list (don't have to check least loaded one since it's
// just for the control connection)
HostSpec hs = new HostSpec(hosts.get(0), LoadBalanceManager.getPort(hosts.get(0)),
loadBalanceProperties.getOriginalProperties().getProperty("localSocketAddress"));
hspec = new HostSpec[]{hs};
}
} catch (Exception e) {
gotException = true;
throw e;
} finally {
if (gotException && connectionCreated) {
try {
controlConnection.close();
} catch (SQLException e) {
}
controlConnection = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class LoadBalanceProperties {
* cluster nodes. false means fallback to entire cluster nodes when nodes in explicit
* placements are unavailable.
*/
public static final String EXPLICIT_FALLBACK_ONLY_KEY = "explicit-fallback-only";
public static final String EXPLICIT_FALLBACK_ONLY_KEY = "fallback-to-topology-keys-only";
/**
* The default value should ideally match the interval at which the server-list is updated at
* cluster side for yb_servers() function. Here, kept it 5 seconds which is not too high (30s) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static void checkNodeDownBehavior() throws SQLException {
executeCmd(path + "/bin/yb-ctl stop_node 2", "Stop node 2", 10);
executeCmd(path + "/bin/yb-ctl stop_node 3", "Stop node 3", 10);
createConnectionsAndVerify(url, "aws.us-west.us-west-1a", expectedInput(-1, -1, -1, 4, 4, 4));
createConnectionsAndVerify("jdbc:yugabytedb://127.0.0.4:5433/yugabyte?load-balance=true&explicit-fallback-only=true&topology-keys=",
createConnectionsAndVerify("jdbc:yugabytedb://127.0.0.4:5433/yugabyte?load-balance=true&fallback-to-topology-keys-only=true&topology-keys=",
"aws.us-west.us-west-1a", expectedInput(-1, -1, -1, 12, 0, 0));
} finally {
LoadBalanceManager.clear();
Expand Down

0 comments on commit 12ee252

Please sign in to comment.