Skip to content

Commit e2fabca

Browse files
author
chengyitian
committed
AJ-768: optimize 'readTimeout' logic;
1 parent c31e41d commit e2fabca

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

src/com/xxdb/DBConnection.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package com.xxdb;
22

33
import java.io.*;
4-
import java.net.ConnectException;
5-
import java.net.InetAddress;
6-
import java.net.InetSocketAddress;
7-
import java.net.Socket;
8-
import java.rmi.RemoteException;
4+
import java.net.*;
95
import java.security.PublicKey;
106
import java.security.cert.CertificateException;
117
import java.security.cert.X509Certificate;
@@ -460,9 +456,14 @@ private Entity run(String script, String scriptType, ProgressListener listener,
460456
header = in_.readLine();
461457
}
462458
}catch (IOException ex){
463-
isConnected_ = false;
464-
socket_ = null;
465-
throw new IOException("Failed to read response header from the socket with IO error " + ex.getMessage());
459+
if (ex instanceof SocketTimeoutException) {
460+
// isConnected_ = true;
461+
throw ex;
462+
} else {
463+
isConnected_ = false;
464+
socket_ = null;
465+
throw new IOException("Failed to read response header from the socket with IO error " + ex.getMessage());
466+
}
466467
}
467468

468469
String[] headers = header.split(" ");
@@ -1062,7 +1063,7 @@ else if (type == ExceptionType.ET_NODENOTAVAIL)
10621063
return false;
10631064
}
10641065

1065-
public ExceptionType parseException(String msg, Node node){
1066+
public ExceptionType parseException(String msg, Node node) {
10661067
if(msg==null){
10671068
node.hostName = "";
10681069
node.port = 0;
@@ -1095,7 +1096,7 @@ public ExceptionType parseException(String msg, Node node){
10951096
node.hostName = "";
10961097
node.port = 0;
10971098
return ExceptionType.ET_NOINITIALIZED;
1098-
} else if (msg.contains("Failed to read response header from the socket with IO error Read timed out")) {
1099+
} else if (msg.contains("Read timed out")) {
10991100
conn_.getNode(node);
11001101
return ExceptionType.ET_READTIMEDOUT;
11011102
} else {
@@ -1240,9 +1241,14 @@ public Entity run(String script, ProgressListener listener, int priority, int pa
12401241
return new Void();
12411242
else if (type == ExceptionType.ET_UNKNOW)
12421243
throw e;
1243-
}else {
1244-
parseException(e.getMessage(), node);
1244+
else if (type == ExceptionType.ET_READTIMEDOUT)
1245+
cancelConsoleJob(enableSeqNo, currentSeqNo, e);
1246+
} else {
1247+
ExceptionType type = parseException(e.getMessage(), node);
1248+
if (type == ExceptionType.ET_READTIMEDOUT)
1249+
cancelConsoleJob(enableSeqNo, currentSeqNo, e);
12451250
}
1251+
12461252
switchDataNode(node);
12471253
}
12481254
}
@@ -1255,6 +1261,29 @@ else if (type == ExceptionType.ET_UNKNOW)
12551261
}
12561262
}
12571263

1264+
private void cancelConsoleJob(boolean enableSeqNo, long currentSeqNo, IOException e) throws IOException {
1265+
String cancelConsoleJobScript =
1266+
"jobs = exec rootJobId from getConsoleJobs() where sessionId = " + conn_.sessionID_ + "\n" +
1267+
(conn_.python_ ? "if size(jobs):\n" : "if (size(jobs))\n") +
1268+
" cancelConsoleJob(jobs)\n";
1269+
conn_.ifUrgent_ = true;
1270+
// conn_.asynTask_ = true;
1271+
1272+
if (enableSeqNo)
1273+
currentSeqNo = newSeqNo();
1274+
1275+
try {
1276+
conn_.run(cancelConsoleJobScript, currentSeqNo);
1277+
conn_.ifUrgent_ = false;
1278+
} catch (IOException ioe) {
1279+
conn_.ifUrgent_ = false;
1280+
throw new RuntimeException("Execute cancelConsoleJob failed after current connnection read timed out. ");
1281+
}
1282+
1283+
log.error(e.getMessage());
1284+
throw e;
1285+
}
1286+
12581287
public Entity run(String script, ProgressListener listener, int priority, int parallelism, int fetchSize, boolean clearSessionMemory, String tableName) throws IOException{
12591288
return run(script, listener, priority, parallelism, fetchSize, clearSessionMemory, tableName, true);
12601289
}

0 commit comments

Comments
 (0)