Skip to content

Commit

Permalink
[KYUUBI #3043] Clean up Kyuubi Hive JDBC client
Browse files Browse the repository at this point in the history
### _Why are the changes needed?_

Code clean up

### _How was this patch tested?_
- [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible

- [ ] Add screenshots for manual tests if appropriate

- [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request

Closes #3043 from pan3793/jdbc-cleanup.

Closes #3043

1d1f8fa [Cheng Pan] Clean up Kyuubi Hive JDBC client

Authored-by: Cheng Pan <chengpan@apache.org>
Signed-off-by: Cheng Pan <chengpan@apache.org>
  • Loading branch information
pan3793 committed Jul 13, 2022
1 parent a5f733b commit b99f25f
Show file tree
Hide file tree
Showing 24 changed files with 134 additions and 1,173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ public class KyuubiHiveDriver implements Driver {

@Override
public boolean acceptsURL(String url) throws SQLException {
return url != null
&& Utils.URL_PREFIX_LIST.stream()
.filter(pre -> url.startsWith(pre))
.findFirst()
.isPresent();
return url != null && Utils.URL_PREFIX_LIST.stream().anyMatch(url::startsWith);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@
*/
public class HttpKerberosRequestInterceptor extends HttpRequestInterceptorBase {

private static final ReentrantLock kerberosLock = new ReentrantLock(true);

String principal;
String host;
String serverHttpUrl;
Subject loggedInSubject;

// A fair reentrant lock
private static ReentrantLock kerberosLock = new ReentrantLock(true);

public HttpKerberosRequestInterceptor(
String principal,
String host,
String serverHttpUrl,
Subject loggedInSubject,
CookieStore cs,
String cn,
Expand All @@ -53,7 +50,6 @@ public HttpKerberosRequestInterceptor(
super(cs, cn, isSSL, additionalHeaders, customCookies);
this.principal = principal;
this.host = host;
this.serverHttpUrl = serverHttpUrl;
this.loggedInSubject = loggedInSubject;
}

Expand All @@ -65,7 +61,7 @@ protected void addHttpAuthHeader(HttpRequest httpRequest, HttpContext httpContex
// Locking ensures the tokens are unique in case of concurrent requests
kerberosLock.lock();
String kerberosAuthHeader =
HttpAuthUtils.getKerberosServiceTicket(principal, host, serverHttpUrl, loggedInSubject);
HttpAuthUtils.getKerberosServiceTicket(principal, host, loggedInSubject);
// Set the session key token (Base64 encoded) in the headers
httpRequest.addHeader(
HttpAuthUtils.AUTHORIZATION + ": " + HttpAuthUtils.NEGOTIATE + " ", kerberosAuthHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;

public abstract class HttpRequestInterceptorBase implements HttpRequestInterceptor {
Expand Down Expand Up @@ -62,7 +62,7 @@ public void process(HttpRequest httpRequest, HttpContext httpContext)
// cookiestore which can be send back or when the server returns a 401 error code
// indicating that the previous cookie has expired.
if (isCookieEnabled) {
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}
// Generate the kerberos ticket under the following scenarios:
// 1. Cookie Authentication is disabled OR
Expand All @@ -72,8 +72,7 @@ public void process(HttpRequest httpRequest, HttpContext httpContext)
if (!isCookieEnabled
|| ((httpContext.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY) == null
&& (cookieStore == null
|| (cookieStore != null
&& Utils.needToSendCredentials(cookieStore, cookieName, isSSL))))
|| Utils.needToSendCredentials(cookieStore, cookieName, isSSL)))
|| (httpContext.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY) != null
&& httpContext
.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY)
Expand All @@ -91,18 +90,22 @@ public void process(HttpRequest httpRequest, HttpContext httpContext)
}
// Add custom cookies if passed to the jdbc driver
if (customCookies != null) {
String cookieHeaderKeyValues = "";
StringBuilder cookieHeaderKeyValues = new StringBuilder();
Header cookieHeaderServer = httpRequest.getFirstHeader("Cookie");
if ((cookieHeaderServer != null) && (cookieHeaderServer.getValue() != null)) {
cookieHeaderKeyValues = cookieHeaderServer.getValue();
cookieHeaderKeyValues = new StringBuilder(cookieHeaderServer.getValue());
}
for (Map.Entry<String, String> entry : customCookies.entrySet()) {
cookieHeaderKeyValues += ";" + entry.getKey() + "=" + entry.getValue();
cookieHeaderKeyValues
.append(";")
.append(entry.getKey())
.append("=")
.append(entry.getValue());
}
if (cookieHeaderKeyValues.startsWith(";")) {
cookieHeaderKeyValues = cookieHeaderKeyValues.substring(1);
if (cookieHeaderKeyValues.toString().startsWith(";")) {
cookieHeaderKeyValues = new StringBuilder(cookieHeaderKeyValues.substring(1));
}
httpRequest.addHeader("Cookie", cookieHeaderKeyValues);
httpRequest.addHeader("Cookie", cookieHeaderKeyValues.toString());
}
} catch (Exception e) {
throw new HttpException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* these credentials to HTTP requests
*/
public class HttpTokenAuthInterceptor extends HttpRequestInterceptorBase {
private String tokenStr;
private final String tokenStr;
private static final String HIVE_DELEGATION_TOKEN_HEADER = "X-Hive-Delegation-Token";

public HttpTokenAuthInterceptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public String getType() {
return type;
}

public String getComment() {
return comment;
}

public int getOrdinalPos() {
return ordinalPos;
}

static String columnClassName(TTypeId tType, JdbcColumnAttributes columnAttributes)
throws SQLException {
int columnType = convertTTypeIdToSqlType(tType);
Expand Down Expand Up @@ -353,32 +361,4 @@ static int columnScale(int columnType, JdbcColumnAttributes columnAttributes)
throw new KyuubiSQLException("Invalid column type: " + columnType);
}
}

public Integer getNumPrecRadix() {
if (type.equalsIgnoreCase("tinyint")) {
return 10;
} else if (type.equalsIgnoreCase("smallint")) {
return 10;
} else if (type.equalsIgnoreCase("int")) {
return 10;
} else if (type.equalsIgnoreCase("bigint")) {
return 10;
} else if (type.equalsIgnoreCase("float")) {
return 10;
} else if (type.equalsIgnoreCase("double")) {
return 10;
} else if (type.equalsIgnoreCase("decimal")) {
return 10;
} else { // anything else including boolean and string is null
return null;
}
}

public String getComment() {
return comment;
}

public int getOrdinalPos() {
return ordinalPos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

package org.apache.kyuubi.jdbc.hive;

import java.sql.SQLException;

/** Table metadata. */
public class JdbcTable {
private String tableCatalog;
private String tableName;
private String type;
private String comment;
private final String tableCatalog;
private final String tableName;
private final String type;
private final String comment;

public JdbcTable(String tableCatalog, String tableName, String type, String comment) {
this.tableCatalog = tableCatalog;
Expand All @@ -45,7 +43,7 @@ public String getType() {
return type;
}

public String getSqlTableType() throws SQLException {
public String getSqlTableType() {
return KyuubiDatabaseMetaData.toJdbcTableType(type);
}

Expand Down
Loading

0 comments on commit b99f25f

Please sign in to comment.