Skip to content

Commit

Permalink
[KYUUBI #4216] Support to transfer client version for kyuubi hive jdb…
Browse files Browse the repository at this point in the history
…c and rest client sdk

### _Why are the changes needed?_

To close #4216

### _How was this patch tested?_
- [x] 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.readthedocs.io/en/master/develop_tools/testing.html#running-tests) locally before make a pull request

Closes #4370 from turboFei/client_version.

Closes #4216

efd934c [fwang12] save
cf8acd5 [fwang12] version properties

Authored-by: fwang12 <fwang12@ebay.com>
Signed-off-by: fwang12 <fwang12@ebay.com>
  • Loading branch information
turboFei committed Feb 20, 2023
1 parent dec4327 commit 3f6ba77
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.kyuubi.config

object KyuubiReservedKeys {
final val KYUUBI_CLIENT_IP_KEY = "kyuubi.client.ipAddress"
final val KYUUBI_CLIENT_VERSION_KEY = "kyuubi.client.version"
final val KYUUBI_SERVER_IP_KEY = "kyuubi.server.ipAddress"
final val KYUUBI_SESSION_USER_KEY = "kyuubi.session.user"
final val KYUUBI_SESSION_SIGN_PUBLICKEY = "kyuubi.session.sign.publickey"
Expand Down
8 changes: 8 additions & 0 deletions kyuubi-hive-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@
</dependencies>

<build>

<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ private void openSession() throws SQLException {
} catch (UnknownHostException e) {
LOG.debug("Error getting Kyuubi session local client ip address", e);
}
openConf.put(Utils.KYUUBI_CLIENT_VERSION_KEY, Utils.getVersion());
openReq.setConfiguration(openConf);

// Store the user name in the open request in case no non-sasl authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,21 @@ public static String getCanonicalHostName(String hostName) {
public static boolean isKyuubiOperationHint(String hint) {
return KYUUBI_OPERATION_HINT_PATTERN.matcher(hint).matches();
}

public static final String KYUUBI_CLIENT_VERSION_KEY = "kyuubi.client.version";
private static String KYUUBI_CLIENT_VERSION;

public static synchronized String getVersion() {
if (KYUUBI_CLIENT_VERSION == null) {
try {
Properties prop = new Properties();
prop.load(Utils.class.getClassLoader().getResourceAsStream("version.properties"));
KYUUBI_CLIENT_VERSION = prop.getProperty(KYUUBI_CLIENT_VERSION_KEY, "unknown");
} catch (Exception e) {
LOG.error("Error getting kyuubi client version", e);
KYUUBI_CLIENT_VERSION = "unknown";
}
}
return KYUUBI_CLIENT_VERSION;
}
}
18 changes: 18 additions & 0 deletions kyuubi-hive-jdbc/src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

kyuubi.client.version = ${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -138,4 +139,10 @@ public void testExtractURLComponents() throws JdbcUriParseException {
assertEquals(expectedDb, jdbcConnectionParams1.getDbName());
assertEquals(expectedHiveConf, jdbcConnectionParams1.getHiveConfs());
}

@Test
public void testGetVersion() {
Pattern pattern = Pattern.compile("^\\d+\\.\\d+\\.\\d+.*");
assert pattern.matcher(Utils.getVersion()).matches();
}
}
7 changes: 7 additions & 0 deletions kyuubi-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@

<build>

<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>

<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import org.apache.kyuubi.client.api.v1.dto.*;
import org.apache.kyuubi.client.util.JsonUtils;
import org.apache.kyuubi.client.util.VersionUtils;

public class BatchRestApi {

Expand All @@ -36,11 +37,13 @@ public BatchRestApi(KyuubiRestClient client) {
}

public Batch createBatch(BatchRequest request) {
setClientVersion(request);
String requestBody = JsonUtils.toJson(request);
return this.getClient().post(API_BASE_PATH, requestBody, Batch.class, client.getAuthHeader());
}

public Batch createBatch(BatchRequest request, File resourceFile) {
setClientVersion(request);
Map<String, MultiPart> multiPartMap = new HashMap<>();
multiPartMap.put("batchRequest", new MultiPart(MultiPart.MultiPartType.JSON, request));
multiPartMap.put("resourceFile", new MultiPart(MultiPart.MultiPartType.FILE, resourceFile));
Expand Down Expand Up @@ -96,4 +99,13 @@ public CloseBatchResponse deleteBatch(String batchId, String hs2ProxyUser) {
private IRestClient getClient() {
return this.client.getHttpClient();
}

private void setClientVersion(BatchRequest request) {
if (request != null) {
Map<String, String> newConf = new HashMap<>();
newConf.putAll(request.getConf());
newConf.put(VersionUtils.KYUUBI_CLIENT_VERSION_KEY, VersionUtils.getVersion());
request.setConf(newConf);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.client.util;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VersionUtils {
static final Logger LOG = LoggerFactory.getLogger(VersionUtils.class);

public static final String KYUUBI_CLIENT_VERSION_KEY = "kyuubi.client.version";
private static String KYUUBI_CLIENT_VERSION;

public static synchronized String getVersion() {
if (KYUUBI_CLIENT_VERSION == null) {
try {
Properties prop = new Properties();
prop.load(VersionUtils.class.getClassLoader().getResourceAsStream("version.properties"));
KYUUBI_CLIENT_VERSION = prop.getProperty(KYUUBI_CLIENT_VERSION_KEY, "unknown");
} catch (Exception e) {
LOG.error("Error getting kyuubi client version", e);
KYUUBI_CLIENT_VERSION = "unknown";
}
}
return KYUUBI_CLIENT_VERSION;
}
}
18 changes: 18 additions & 0 deletions kyuubi-rest-client/src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

kyuubi.client.version = ${project.version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.client.util;

import java.util.regex.Pattern;
import org.junit.Test;

public class VersionUtilsTest {

@Test
public void testGetClientVersion() {
Pattern pattern = Pattern.compile("^\\d+\\.\\d+\\.\\d+.*");
assert pattern.matcher(VersionUtils.getVersion()).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import scala.collection.JavaConverters._
import org.apache.hive.service.rpc.thrift._
import org.scalatest.time.SpanSugar.convertIntToGrainOfTime

import org.apache.kyuubi.WithKyuubiServer
import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.{KYUUBI_VERSION, WithKyuubiServer}
import org.apache.kyuubi.config.{KyuubiConf, KyuubiReservedKeys}
import org.apache.kyuubi.config.KyuubiConf.SESSION_CONF_ADVISOR
import org.apache.kyuubi.engine.ApplicationState
import org.apache.kyuubi.jdbc.KyuubiHiveDriver
Expand Down Expand Up @@ -272,6 +272,14 @@ class KyuubiOperationPerConnectionSuite extends WithKyuubiServer with HiveJDBCTe
assert(MetricsSystem.counterValue(connFailedMetric).getOrElse(0L) > connFailedCount)
}
}

test("support to transfer client version when opening jdbc connection") {
withJdbcStatement() { stmt =>
val rs = stmt.executeQuery(s"set spark.${KyuubiReservedKeys.KYUUBI_CLIENT_VERSION_KEY}")
assert(rs.next())
assert(rs.getString(2) === KYUUBI_VERSION)
}
}
}

class TestSessionConfAdvisor extends SessionConfAdvisor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import java.util.Base64

import org.scalatest.time.SpanSugar.convertIntToGrainOfTime

import org.apache.kyuubi.{BatchTestHelper, RestClientTestHelper}
import org.apache.kyuubi.{BatchTestHelper, KYUUBI_VERSION, RestClientTestHelper}
import org.apache.kyuubi.client.{BatchRestApi, KyuubiRestClient}
import org.apache.kyuubi.client.api.v1.dto.Batch
import org.apache.kyuubi.client.exception.KyuubiRestException
import org.apache.kyuubi.config.KyuubiReservedKeys
import org.apache.kyuubi.metrics.{MetricsConstants, MetricsSystem}
import org.apache.kyuubi.session.{KyuubiSession, SessionHandle}

Expand Down Expand Up @@ -215,4 +216,22 @@ class BatchRestApiSuite extends RestClientTestHelper with BatchTestHelper {
batchRestApi.listBatches(null, null, null, 0, 0, 0, 1)
batchRestApi.listBatches(null, null, null, 0, 0, 0, 1)
}

test("support to transfer client version when creating batch") {
val spnegoKyuubiRestClient: KyuubiRestClient =
KyuubiRestClient.builder(baseUri.toString)
.authHeaderMethod(KyuubiRestClient.AuthHeaderMethod.SPNEGO)
.spnegoHost("localhost")
.build()
val batchRestApi: BatchRestApi = new BatchRestApi(spnegoKyuubiRestClient)
// create batch
val requestObj =
newSparkBatchRequest(Map("spark.master" -> "local"))

val batch = batchRestApi.createBatch(requestObj)
val batchSession =
server.backendService.sessionManager.getSession(SessionHandle.fromUUID(batch.getId))
assert(
batchSession.conf.get(KyuubiReservedKeys.KYUUBI_CLIENT_VERSION_KEY) == Some(KYUUBI_VERSION))
}
}

0 comments on commit 3f6ba77

Please sign in to comment.