Skip to content

Commit

Permalink
release the transport when version verification failed (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole00 committed Feb 10, 2022
1 parent 2034bad commit 5c18577
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void open(HostAddress address, int timeout, SSLParam sslParam)
try {

this.serverAddr = address;
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
this.enabledSsl = true;
this.sslParam = sslParam;
if (sslSocketFactory == null) {
Expand All @@ -78,6 +78,7 @@ public void open(HostAddress address, int timeout, SSLParam sslParam)
Charsets.UTF_8));
}
} catch (TException | IOException e) {
close();
throw new IOErrorException(IOErrorException.E_UNKNOWN, e.getMessage());
}
}
Expand All @@ -87,7 +88,7 @@ public void open(HostAddress address, int timeout)
throws IOErrorException, ClientServerIncompatibleException {
try {
this.serverAddr = address;
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
this.timeout = timeout <= 0 ? Integer.MAX_VALUE : timeout;
this.transport = new TSocket(
address.getHost(), address.getPort(), this.timeout, this.timeout);
this.transport.open();
Expand Down Expand Up @@ -136,18 +137,18 @@ public AuthResult authenticate(String user, String password)
throw new AuthFailedException(new String(resp.error_msg));
} else {
throw new AuthFailedException(
"The error_msg is null, "
+ "maybe the service not set or the response is disorder.");
"The error_msg is null, "
+ "maybe the service not set or the response is disorder.");
}
}
return new AuthResult(resp.getSession_id(), resp.getTime_zone_offset_seconds());
} catch (TException e) {
if (e instanceof TTransportException) {
TTransportException te = (TTransportException)e;
TTransportException te = (TTransportException) e;
if (te.getType() == TTransportException.END_OF_FILE) {
throw new IOErrorException(IOErrorException.E_CONNECT_BROKEN, te.getMessage());
} else if (te.getType() == TTransportException.TIMED_OUT
|| te.getMessage().contains("Read timed out")) {
|| te.getMessage().contains("Read timed out")) {
reopen();
throw new IOErrorException(IOErrorException.E_TIME_OUT, te.getMessage());
} else if (te.getType() == TTransportException.NOT_OPEN) {
Expand All @@ -170,7 +171,7 @@ public ExecutionResponse execute(long sessionID, String stmt)
} else if (te.getType() == TTransportException.NOT_OPEN) {
throw new IOErrorException(IOErrorException.E_NO_OPEN, te.getMessage());
} else if (te.getType() == TTransportException.TIMED_OUT
|| te.getMessage().contains("Read timed out")) {
|| te.getMessage().contains("Read timed out")) {
try {
reopen();
} catch (ClientServerIncompatibleException ex) {
Expand Down Expand Up @@ -224,7 +225,7 @@ public boolean ping() {
}

public void close() {
if (transport != null) {
if (transport != null && transport.isOpen()) {
transport.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public AbstractMetaClient(List<HostAddress> addresses, int timeout,
int port = address.getPort();
// check if the address is a valid ip, uri address or domain name and port is valid
if (!(InetAddresses.isInetAddress(host)
|| InetAddresses.isUriInetAddress(host)
|| InternetDomainName.isValid(host))
|| InetAddresses.isUriInetAddress(host)
|| InternetDomainName.isValid(host))
|| (port <= 0 || port >= 65535)) {
throw new IllegalArgumentException(String.format("%s:%d is not a valid address",
host, port));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,16 @@ private void getClient(String host, int port)
client = new MetaService.Client(protocol);

// check if client version matches server version
VerifyClientVersionResp resp =
client.verifyClientVersion(new VerifyClientVersionReq());
VerifyClientVersionResp resp = null;
try {
resp = client.verifyClientVersion(new VerifyClientVersionReq());
} catch (Exception e) {
LOGGER.error("failed to verify the version between server and client,", e);
close();
throw e;
}
if (resp.getCode() != ErrorCode.SUCCEEDED) {
client.getInputProtocol().getTransport().close();
close();
throw new ClientServerIncompatibleException(new String(resp.getError_msg(),
Charsets.UTF_8));
}
Expand Down

0 comments on commit 5c18577

Please sign in to comment.