Skip to content

Commit 2d05f22

Browse files
author
chengyitian
committed
AJ-679: fix issue about DBConnection when enableHigh with loadBalance;
1 parent 706bbf9 commit 2d05f22

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

src/com/xxdb/DBConnection.java

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -788,51 +788,62 @@ public boolean connect(String hostName, int port, String userId, String password
788788
if ( bt!=null && bt.getDataForm() != Entity.DATA_FORM.DF_TABLE)
789789
throw new IOException("Run getClusterPerf() failed.");
790790

791-
if (bt!=null && loadBalance_) {
792-
//ignore very high load nodes, rand one in low load nodes
793-
List<Node> lowLoadNodes=new ArrayList<>();
794-
BasicStringVector colHost = (BasicStringVector) bt.getColumn("host");
795-
BasicIntVector colPort = (BasicIntVector) bt.getColumn("port");
796-
BasicDoubleVector memLoad = (BasicDoubleVector) bt.getColumn("memLoad");
797-
BasicDoubleVector connLoad = (BasicDoubleVector) bt.getColumn("connLoad");
798-
BasicDoubleVector avgLoad = (BasicDoubleVector) bt.getColumn("avgLoad");
799-
for (int i = 0; i < colHost.rows(); i++) {
800-
Node nodex = new Node(colHost.getString(i), colPort.getInt(i));
801-
Node pexistNode = null;
802-
if (highAvailabilitySites != null) {
803-
for (Node node : nodes_) {
804-
if ((node.hostName.equals(nodex.hostName) || nodex.hostName.equals("localhost")) && node.port == nodex.port){
805-
pexistNode = node;
806-
break;
791+
if (bt!=null) {
792+
if (!loadBalance_) {
793+
BasicStringVector colHost = (BasicStringVector) bt.getColumn("host");
794+
BasicIntVector colPort = (BasicIntVector) bt.getColumn("port");
795+
for (int i = 0; i < colHost.rows(); i++) {
796+
Node curNode = new Node(colHost.getString(i), colPort.getInt(i));
797+
if (!(curNode.hostName.equals(hostName) && curNode.port == port))
798+
nodes_.add(curNode);
799+
}
800+
} else {
801+
// enable loadBalance
802+
//ignore very high load nodes, rand one in low load nodes
803+
List<Node> lowLoadNodes=new ArrayList<>();
804+
BasicStringVector colHost = (BasicStringVector) bt.getColumn("host");
805+
BasicIntVector colPort = (BasicIntVector) bt.getColumn("port");
806+
BasicDoubleVector memLoad = (BasicDoubleVector) bt.getColumn("memLoad");
807+
BasicDoubleVector connLoad = (BasicDoubleVector) bt.getColumn("connLoad");
808+
BasicDoubleVector avgLoad = (BasicDoubleVector) bt.getColumn("avgLoad");
809+
for (int i = 0; i < colHost.rows(); i++) {
810+
Node nodex = new Node(colHost.getString(i), colPort.getInt(i));
811+
Node pexistNode = null;
812+
if (highAvailabilitySites != null) {
813+
for (Node node : nodes_) {
814+
if ((node.hostName.equals(nodex.hostName) || nodex.hostName.equals("localhost")) && node.port == nodex.port){
815+
pexistNode = node;
816+
break;
817+
}
807818
}
819+
//node is out of highAvailabilitySites
820+
if (pexistNode == null)
821+
continue;
808822
}
809-
//node is out of highAvailabilitySites
810-
if (pexistNode == null)
811-
continue;
823+
double load=(memLoad.getDouble(i)+connLoad.getDouble(i)+avgLoad.getDouble(i))/3.0;
824+
if (pexistNode != null) {
825+
pexistNode.load = load;
826+
} else {
827+
pexistNode=new Node(colHost.getString(i), colPort.getInt(i), load);
828+
nodes_.add(pexistNode);
829+
}
830+
// low load
831+
if (memLoad.getDouble(i)<0.8 && connLoad.getDouble(i)<0.9 && avgLoad.getDouble(i)<0.8)
832+
lowLoadNodes.add(pexistNode);
812833
}
813-
double load=(memLoad.getDouble(i)+connLoad.getDouble(i)+avgLoad.getDouble(i))/3.0;
814-
if (pexistNode != null) {
815-
pexistNode.load = load;
834+
835+
Node pMinNode;
836+
if (!lowLoadNodes.isEmpty()) {
837+
pMinNode=lowLoadNodes.get(nodeRandom_.nextInt(lowLoadNodes.size()));
816838
} else {
817-
pexistNode=new Node(colHost.getString(i), colPort.getInt(i), load);
818-
nodes_.add(pexistNode);
839+
pMinNode=nodes_.get(nodeRandom_.nextInt(nodes_.size()));
819840
}
820-
// low load
821-
if (memLoad.getDouble(i)<0.8 && connLoad.getDouble(i)<0.9 && avgLoad.getDouble(i)<0.8)
822-
lowLoadNodes.add(pexistNode);
823-
}
824841

825-
Node pMinNode;
826-
if (!lowLoadNodes.isEmpty()) {
827-
pMinNode=lowLoadNodes.get(nodeRandom_.nextInt(lowLoadNodes.size()));
828-
} else {
829-
pMinNode=nodes_.get(nodeRandom_.nextInt(nodes_.size()));
830-
}
831-
832-
if (pMinNode != null && !pMinNode.equals(connectedNode)){
833-
log.info("Switch to node: " + pMinNode.hostName + ":" + pMinNode.port);
834-
conn_.close();
835-
switchDataNode(pMinNode);
842+
if (pMinNode != null && !pMinNode.equals(connectedNode)){
843+
log.info("Switch to node: " + pMinNode.hostName + ":" + pMinNode.port);
844+
conn_.close();
845+
switchDataNode(pMinNode);
846+
}
836847
}
837848
}
838849
} else {

0 commit comments

Comments
 (0)