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 1 commit
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
48 changes: 39 additions & 9 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,15 +266,17 @@ public Connection connect(String url, Properties info) throws SQLException {
altHostList.add(st.nextToken());
}

if (connProperties.getConnLoadBal()) {
Collections.shuffle(altHostList);
}
try {
u_con =
(UClientSideConnection)
UJCIManager.connect(altHostList, db, user, pass, resolvedUrl);
} catch (CUBRIDException e) {
throw e;
String loadBalValue = connProperties.getConnLoadBal();

synchronized (this) {
Copy link
Contributor

@mhoh3963 mhoh3963 Nov 29, 2024

Choose a reason for hiding this comment

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

이 구간에 synchronized 를 사용하지 말고 conn_count 함수를 증가시키는 함수를 만들고 해당 함수에만 synchronized로 하는 것이 어떨까요 ?
private synchronized int increment_conn_count() {
conn_count = conn_count+1;
if (conn_count >= Interger.MAX_VALUE) conn_count=1;
return conn_count;
}
그리고 adjustHostList() 함수에서 conn_count++ 대신 count = increment_conn_count(); 변경한 후 local 변수인 count을 이용하면 thread safety 해 보임.

혹시 UJCIManager.connect()가 thread safety 하지 않나요 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

의견 감사합니다. 의견대로 적용하겠습니다.

adjustHostList(loadBalValue, altHostList);
try {
u_con =
(UClientSideConnection)
UJCIManager.connect(altHostList, db, user, pass, resolvedUrl);
} catch (CUBRIDException e) {
throw e;
}
}
} else {
try {
Expand Down Expand Up @@ -346,4 +349,31 @@ private boolean existPropertiesFile(String filePath) {
File file = new File(filePath);
return file.exists();
}

private void adjustHostList(String loadBalValue, ArrayList<String> altHostList) {
switch (loadBalValue) {
case ConnectionProperties.LB_VAL_TRUE:
Collections.shuffle(altHostList);
break;
case ConnectionProperties.LB_VAL_FALSE:
break;
case ConnectionProperties.LB_VAL_ROUND_ROBIN:
conn_count++;
int index =
(conn_count > altHostList.size())
? (conn_count - 1) % altHostList.size()
: conn_count - 1;
Collections.rotate(altHostList, -index);

if (conn_count >= Integer.MAX_VALUE) {
conn_count = 0;
}
break;
case ConnectionProperties.LB_VAL_SHUFFLE:
Collections.shuffle(altHostList);
break;
default:
throw new IllegalArgumentException("Invalid loadBalValue: " + loadBalValue);
}
}
}
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