Skip to content

Commit 2fd3e51

Browse files
author
chengyitian
committed
AJ-863: optimize reconnect logic for 'switchDataNode'; add 'isNotEmpty' for Utils;
1 parent f4deb2d commit 2fd3e51

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

src/com/xxdb/DBConnection.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,26 +1289,29 @@ public void switchDataNode(Node node) throws IOException{
12891289
boolean isConnected = false;
12901290
do {
12911291
attempt ++;
1292-
if (node.hostName != null && node.hostName.length() > 0) {
1292+
boolean ifReconnectSingleNodeTry = false;
1293+
if (Utils.isNotEmpty(node.hostName)) {
12931294
if (connectNode(node)) {
12941295
if (nodes_.size() > 1)
12951296
log.info("Switch to node: " + node.hostName + ":" + node.port + " successfully.");
12961297
isConnected = true;
12971298
break;
12981299
}
1299-
} else {
1300-
if (nodes_.isEmpty()){
1301-
log.error("Connect to " + node.hostName + ":" + node.port + " failed.");
1302-
throw new RuntimeException("Connect to " + node.hostName + ":" + node.port + " failed.");
1303-
}
13041300

1305-
int index = nodeRandom_.nextInt(nodes_.size());
1306-
if (connectNode(nodes_.get(index))) {
1307-
if (nodes_.size() > 1)
1308-
log.info("Switch to node: " + nodes_.get(index).hostName + ":" + nodes_.get(index).port + " successfully.");
1309-
isConnected = true;
1310-
break;
1311-
}
1301+
ifReconnectSingleNodeTry = nodes_.size() == 1;
1302+
}
1303+
1304+
if (nodes_.isEmpty()){
1305+
log.error("Connect to " + node.hostName + ":" + node.port + " failed.");
1306+
throw new RuntimeException("Connect to " + node.hostName + ":" + node.port + " failed.");
1307+
}
1308+
1309+
int index = nodeRandom_.nextInt(nodes_.size());
1310+
if (!ifReconnectSingleNodeTry && connectNode(nodes_.get(index))) {
1311+
if (nodes_.size() > 1)
1312+
log.info("Switch to node: " + nodes_.get(index).hostName + ":" + nodes_.get(index).port + " successfully.");
1313+
isConnected = true;
1314+
break;
13121315
}
13131316

13141317
try {

src/com/xxdb/data/Utils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,10 @@ public static boolean isEmpty(CharSequence cs) {
722722
return cs == null || cs.length() == 0;
723723
}
724724

725+
public static boolean isNotEmpty(CharSequence cs) {
726+
return cs != null && cs.length() > 0;
727+
}
728+
725729
private static final BigDecimal DECIMAL128_MIN_VALUE = new BigDecimal("-170141183460469231731687303715884105728");
726730
private static final BigDecimal DECIMAL128_MAX_VALUE = new BigDecimal("170141183460469231731687303715884105728");
727731

0 commit comments

Comments
 (0)