Skip to content

Commit 5c18577

Browse files
authored
release the transport when version verification failed (#440)
1 parent 2034bad commit 5c18577

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

client/src/main/java/com/vesoft/nebula/client/graph/net/SyncConnection.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void open(HostAddress address, int timeout, SSLParam sslParam)
5151
try {
5252

5353
this.serverAddr = address;
54-
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
54+
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
5555
this.enabledSsl = true;
5656
this.sslParam = sslParam;
5757
if (sslSocketFactory == null) {
@@ -78,6 +78,7 @@ public void open(HostAddress address, int timeout, SSLParam sslParam)
7878
Charsets.UTF_8));
7979
}
8080
} catch (TException | IOException e) {
81+
close();
8182
throw new IOErrorException(IOErrorException.E_UNKNOWN, e.getMessage());
8283
}
8384
}
@@ -87,7 +88,7 @@ public void open(HostAddress address, int timeout)
8788
throws IOErrorException, ClientServerIncompatibleException {
8889
try {
8990
this.serverAddr = address;
90-
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
91+
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
9192
this.transport = new TSocket(
9293
address.getHost(), address.getPort(), this.timeout, this.timeout);
9394
this.transport.open();
@@ -136,18 +137,18 @@ public AuthResult authenticate(String user, String password)
136137
throw new AuthFailedException(new String(resp.error_msg));
137138
} else {
138139
throw new AuthFailedException(
139-
"The error_msg is null, "
140-
+ "maybe the service not set or the response is disorder.");
140+
"The error_msg is null, "
141+
+ "maybe the service not set or the response is disorder.");
141142
}
142143
}
143144
return new AuthResult(resp.getSession_id(), resp.getTime_zone_offset_seconds());
144145
} catch (TException e) {
145146
if (e instanceof TTransportException) {
146-
TTransportException te = (TTransportException)e;
147+
TTransportException te = (TTransportException) e;
147148
if (te.getType() == TTransportException.END_OF_FILE) {
148149
throw new IOErrorException(IOErrorException.E_CONNECT_BROKEN, te.getMessage());
149150
} else if (te.getType() == TTransportException.TIMED_OUT
150-
|| te.getMessage().contains("Read timed out")) {
151+
|| te.getMessage().contains("Read timed out")) {
151152
reopen();
152153
throw new IOErrorException(IOErrorException.E_TIME_OUT, te.getMessage());
153154
} else if (te.getType() == TTransportException.NOT_OPEN) {
@@ -170,7 +171,7 @@ public ExecutionResponse execute(long sessionID, String stmt)
170171
} else if (te.getType() == TTransportException.NOT_OPEN) {
171172
throw new IOErrorException(IOErrorException.E_NO_OPEN, te.getMessage());
172173
} else if (te.getType() == TTransportException.TIMED_OUT
173-
|| te.getMessage().contains("Read timed out")) {
174+
|| te.getMessage().contains("Read timed out")) {
174175
try {
175176
reopen();
176177
} catch (ClientServerIncompatibleException ex) {
@@ -224,7 +225,7 @@ public boolean ping() {
224225
}
225226

226227
public void close() {
227-
if (transport != null) {
228+
if (transport != null && transport.isOpen()) {
228229
transport.close();
229230
}
230231
}

client/src/main/java/com/vesoft/nebula/client/meta/AbstractMetaClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public AbstractMetaClient(List<HostAddress> addresses, int timeout,
3232
int port = address.getPort();
3333
// check if the address is a valid ip, uri address or domain name and port is valid
3434
if (!(InetAddresses.isInetAddress(host)
35-
|| InetAddresses.isUriInetAddress(host)
36-
|| InternetDomainName.isValid(host))
35+
|| InetAddresses.isUriInetAddress(host)
36+
|| InternetDomainName.isValid(host))
3737
|| (port <= 0 || port >= 65535)) {
3838
throw new IllegalArgumentException(String.format("%s:%d is not a valid address",
3939
host, port));

client/src/main/java/com/vesoft/nebula/client/meta/MetaClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,16 @@ private void getClient(String host, int port)
149149
client = new MetaService.Client(protocol);
150150

151151
// check if client version matches server version
152-
VerifyClientVersionResp resp =
153-
client.verifyClientVersion(new VerifyClientVersionReq());
152+
VerifyClientVersionResp resp = null;
153+
try {
154+
resp = client.verifyClientVersion(new VerifyClientVersionReq());
155+
} catch (Exception e) {
156+
LOGGER.error("failed to verify the version between server and client,", e);
157+
close();
158+
throw e;
159+
}
154160
if (resp.getCode() != ErrorCode.SUCCEEDED) {
155-
client.getInputProtocol().getTransport().close();
161+
close();
156162
throw new ClientServerIncompatibleException(new String(resp.getError_msg(),
157163
Charsets.UTF_8));
158164
}

0 commit comments

Comments
 (0)