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

[APIS-1005] Extends the loadBalance Property of JDBC. #56

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/jdbc/cubrid/jdbc/driver/CUBRIDDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class CUBRIDDriver implements Driver {
"jdbc:cubrid(-oracle|-mysql)?:([a-zA-Z_0-9\\.-]*):([0-9]*):([^:]+):([^:]*):([^:]*):(\\?[a-zA-Z_0-9]+=[^&=?]+(&[a-zA-Z_0-9]+=[^&=?]+)*)?";
private static final String CUBRID_JDBC_URL_HEADER = "jdbc:cubrid";
private static final String ENV_JDBC_PROP_NAME = "CUBRID_JDBC_PROP";
private int conn_count = 0;

static {
try {
Expand Down Expand Up @@ -265,9 +266,9 @@ public Connection connect(String url, Properties info) throws SQLException {
altHostList.add(st.nextToken());
}

if (connProperties.getConnLoadBal()) {
Collections.shuffle(altHostList);
}
String loadBalValue = connProperties.getConnLoadBal();

adjustHostList(loadBalValue, altHostList);
try {
u_con =
(UClientSideConnection)
Expand Down Expand Up @@ -346,4 +347,24 @@ private boolean existPropertiesFile(String filePath) {
File file = new File(filePath);
return file.exists();
}

private void adjustHostList(String loadBalValue, ArrayList<String> altHostList) {
if (ConnectionProperties.LB_VAL_TRUE.equals(loadBalValue)
|| ConnectionProperties.LB_VAL_ROUND_ROBIN.equals(loadBalValue)) {
int count = increment_conn_count();
int dist = (count > altHostList.size()) ? (count - 1) % altHostList.size() : count - 1;
Collections.rotate(altHostList, -dist);
} else if (ConnectionProperties.LB_VAL_SHUFFLE.equals(loadBalValue)) {
Collections.shuffle(altHostList);
} else if (ConnectionProperties.LB_VAL_FALSE.equals(loadBalValue)) {
// do nothing
} else {
throw new IllegalArgumentException("Invalid loadBalValue: " + loadBalValue);
}
}

private synchronized int increment_conn_count() {
conn_count = (conn_count >= Integer.MAX_VALUE) ? 1 : conn_count + 1;
return conn_count;
Copy link
Contributor

Choose a reason for hiding this comment

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

conn_count가 INT_MAX가 넘는 경우에 대해 처리가 필요해보입니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

conn_count 의 overflow에 대한 처리를 추가 했습니다.

}
}
39 changes: 34 additions & 5 deletions src/jdbc/cubrid/jdbc/driver/ConnectionProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@

public class ConnectionProperties {
static ArrayList<Field> PROPERTY_LIST = new ArrayList<Field>();
public static final String LB_VAL_TRUE = "true";
public static final String LB_VAL_FALSE = "false";
public static final String LB_VAL_SHUFFLE = "sh";
public static final String LB_VAL_ROUND_ROBIN = "rr";

static {
try {
Expand Down Expand Up @@ -108,8 +112,8 @@ public void setProperties(Properties info) throws SQLException {
CUBRIDJDBCErrorCode.invalid_url, " illegal access properties", null);
}
}
if (this.getConnLoadBal() && this.getAltHosts() == null) {
this.connLoadBal.setValue("false");
if (!this.getConnLoadBal().equals(LB_VAL_FALSE) && this.getAltHosts() == null) {
this.connLoadBal.setValue(LB_VAL_FALSE);
}
if (this.getReconnectTime() < (BrokerHealthCheck.MONITORING_INTERVAL / 1000)) {
this.rcTime.setValue((Integer) (BrokerHealthCheck.MONITORING_INTERVAL / 1000));
Expand Down Expand Up @@ -335,6 +339,30 @@ boolean validateValue(Object o) {
}
}

class StringLoadBalanceProperty extends ConnectionProperty {
String[] allowableValues = {LB_VAL_TRUE, LB_VAL_FALSE, LB_VAL_SHUFFLE, LB_VAL_ROUND_ROBIN};

StringLoadBalanceProperty(String propertyName, Object defaultValue) {
super(propertyName, defaultValue, 0, 0);
}

String getValueAsString() {
return (String) valueAsObject;
}

@Override
boolean validateValue(Object o) {
if (o instanceof String) {
for (int i = 0; i < allowableValues.length; i++) {
if (allowableValues[i].equalsIgnoreCase((String) o)) {
return true;
}
}
}
return false;
}
}

BooleanConnectionProperty logOnException =
new BooleanConnectionProperty("logOnException", false);

Expand Down Expand Up @@ -369,7 +397,8 @@ private int getDefaultConnectTimeout() {

StringConnectionProperty altHosts = new StringConnectionProperty("altHosts", null);

BooleanConnectionProperty connLoadBal = new BooleanConnectionProperty("loadBalance", false);
StringLoadBalanceProperty connLoadBal =
new StringLoadBalanceProperty("loadBalance", LB_VAL_FALSE);

ZeroDateTimeBehaviorConnectionProperty zeroDateTimeBehavior =
new ZeroDateTimeBehaviorConnectionProperty(
Expand Down Expand Up @@ -440,8 +469,8 @@ public String getAltHosts() {
return altHosts.getValueAsString();
}

public boolean getConnLoadBal() {
return connLoadBal.getValueAsBoolean();
public String getConnLoadBal() {
return connLoadBal.getValueAsString();
}

public String getZeroDateTimeBehavior() {
Expand Down
Loading