Skip to content

Commit bfaa756

Browse files
committed
{182881698} cdb2jdbc: Fixing driver name and version
The JDBC driver did not send driver name and version to server. This patch fixes it. Signed-off-by: Rivers Zhang <hzhang320@bloomberg.net>
1 parent 75b79d4 commit bfaa756

File tree

4 files changed

+78
-51
lines changed

4 files changed

+78
-51
lines changed

cdb2jdbc/pom.xml

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License. -->
1616
<groupId>com.bloomberg.comdb2</groupId>
1717
<modelVersion>4.0.0</modelVersion>
1818
<artifactId>cdb2jdbc</artifactId>
19-
<version>2.8.0</version>
19+
<version>source</version>
2020
<packaging>jar</packaging>
2121
<properties>
2222
<maven.compiler.source>1.8</maven.compiler.source>
@@ -25,7 +25,7 @@ limitations under the License. -->
2525

2626
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
2727
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
28-
<protobuf-java.version>3.25.5</protobuf-java.version>
28+
<protobuf-java.version>3.25.7</protobuf-java.version>
2929
<slf4j.version>2.0.17</slf4j.version>
3030
</properties>
3131
<dependencies>
@@ -45,6 +45,12 @@ limitations under the License. -->
4545
<version>4.13.1</version>
4646
<scope>test</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.slf4j</groupId>
50+
<artifactId>slf4j-simple</artifactId>
51+
<version>${slf4j.version}</version>
52+
<scope>test</scope>
53+
</dependency>
4854
</dependencies>
4955
<build>
5056
<resources>
@@ -123,8 +129,8 @@ limitations under the License. -->
123129
<version>3.2.5</version>
124130
<configuration>
125131
<systemPropertyVariables>
126-
<testDriverName>${project.artifactId}</testDriverName>
127-
<testDriverVersion>${project.version}</testDriverVersion>
132+
<driverName>${project.artifactId}</driverName>
133+
<driverVersion>${project.version}</driverVersion>
128134
</systemPropertyVariables>
129135
<skipTests>${env.skipTests}</skipTests>
130136
</configuration>
@@ -155,22 +161,6 @@ limitations under the License. -->
155161
</execution>
156162
</executions>
157163
</plugin>
158-
<plugin>
159-
<groupId>org.apache.maven.plugins</groupId>
160-
<artifactId>maven-shade-plugin</artifactId>
161-
<version>2.4.2</version>
162-
<executions>
163-
<execution>
164-
<phase>package</phase>
165-
<goals>
166-
<goal>shade</goal>
167-
</goals>
168-
<configuration>
169-
<shadedArtifactAttached>true</shadedArtifactAttached>
170-
</configuration>
171-
</execution>
172-
</executions>
173-
</plugin>
174164
<plugin>
175165
<groupId>org.apache.maven.plugins</groupId>
176166
<artifactId>maven-dependency-plugin</artifactId>
@@ -285,4 +275,4 @@ limitations under the License. -->
285275
</properties>
286276
</profile>
287277
</profiles>
288-
</project>
278+
</project>

cdb2jdbc/src/main/java/com/bloomberg/comdb2/jdbc/Comdb2ClientInfo.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@
2020

2121
public class Comdb2ClientInfo {
2222
private static Logger logger = LoggerFactory.getLogger(Comdb2ClientInfo.class);
23+
private static final String driverName;
24+
private static final String driverVersion;
25+
26+
static {
27+
String name = Comdb2ClientInfo.class.getPackage().getImplementationTitle();
28+
String version = Comdb2ClientInfo.class.getPackage().getImplementationVersion();
29+
if (name == null || version == null) {
30+
/* If we're in junit integration test, get the cdb2jdbc jar
31+
and handle its manifest manually. Ignore errors. */
32+
try {
33+
JarFile jar = new JarFile(Comdb2ClientInfo.class.getProtectionDomain().getCodeSource().getLocation().getPath());
34+
Manifest manifest = jar.getManifest();
35+
if (manifest != null) {
36+
Attributes attributes = manifest.getMainAttributes();
37+
name = attributes.getValue("Implementation-Title");
38+
version = attributes.getValue("Implementation-Version");
39+
}
40+
} catch (IOException e) {
41+
logger.info("Unable to parse driver class manifest", e);
42+
}
43+
}
44+
45+
/* Defaults to cdb2jdbc.source */
46+
if (name == null)
47+
name = "Unknown cdb2jdbc build";
48+
if (version == null)
49+
version = "Unknown cdb2jdbc version";
50+
51+
driverName = name;
52+
driverVersion = version;
53+
}
2354

2455
public static String getCallerClass() {
2556
String pkg = Comdb2ClientInfo.class.getPackage().getName() + ".";
@@ -48,33 +79,11 @@ public static String getCallStack(int layers) {
4879
}
4980

5081
public static String getDriverName() {
51-
String name = Driver.class.getPackage().getImplementationTitle();
52-
if (name == null) {
53-
try {
54-
JarFile jar = new JarFile(Driver.class.getProtectionDomain().getCodeSource().getLocation().getPath());
55-
Manifest manifest = new Manifest(jar.getInputStream(jar.getEntry("META-INF/MANIFEST.MF")));
56-
Attributes attributes = manifest.getMainAttributes();
57-
name = attributes.getValue("Implementation-Title");
58-
} catch (IOException e) {
59-
logger.info("Unable to parse driver class manifest");
60-
}
61-
}
62-
return name == null ? "cdb2jdbc" : name;
82+
return driverName;
6383
}
6484

6585
public static String getDriverVersion() {
66-
String version = Driver.class.getPackage().getImplementationVersion();
67-
if (version == null) {
68-
try {
69-
JarFile jar = new JarFile(Driver.class.getProtectionDomain().getCodeSource().getLocation().getPath());
70-
Manifest manifest = new Manifest(jar.getInputStream(jar.getEntry("META-INF/MANIFEST.MF")));
71-
Attributes attributes = manifest.getMainAttributes();
72-
version = attributes.getValue("Implementation-Version");
73-
} catch (IOException e) {
74-
logger.info("Unable to parse driver class manifest");
75-
}
76-
}
77-
return version == null ? "1.0" : version;
86+
return driverVersion;
7887
}
7988

8089
public static int getDriverMajorVersion() {

cdb2jdbc/src/main/java/com/bloomberg/comdb2/jdbc/ProtobufProtocol.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,14 @@ public int pack(Cdb2Query query) {
174174
.setTzname(cdb2SqlQuery.tzName)
175175
.setMachClass(cdb2SqlQuery.machClass);
176176

177-
if (cdb2SqlQuery.cinfo != null)
177+
Cdb2ClientInfo cinfo = cdb2SqlQuery.cinfo;
178+
if (cinfo != null) {
178179
_sqlquery.setClientInfo(CDB2_SQLQUERY.cinfo.newBuilder().
179-
setPid(cdb2SqlQuery.cinfo.pid).setThId(cdb2SqlQuery.cinfo.tid).
180-
setHostId(cdb2SqlQuery.cinfo.host_id).setArgv0(cdb2SqlQuery.cinfo.argv0).
181-
setStack(cdb2SqlQuery.cinfo.stack));
180+
setPid(cinfo.pid).setThId(cinfo.tid).
181+
setHostId(cinfo.host_id).setArgv0(cinfo.argv0).
182+
setStack(cinfo.stack).setApiDriverName(cinfo.api_driver_name).
183+
setApiDriverVersion(cinfo.api_driver_version));
184+
}
182185

183186
if (cdb2SqlQuery.cnonce != null)
184187
_sqlquery.setCnonce(ByteString.copyFrom(cdb2SqlQuery.cnonce));
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
package com.bloomberg.comdb2.jdbc;
22

3+
import java.sql.*;
34
import org.junit.*;
45
import org.junit.Assert.*;
56

67
public class ClientInfoIT {
78
@Test public void getDriverInfoTest() {
8-
String[] expected_driver = { System.getProperty("testDriverName"), System.getProperty("testDriverVersion") };
9+
String[] expected_driver = { System.getProperty("driverName"), System.getProperty("driverVersion") };
910
String[] actual_driver = { Comdb2ClientInfo.getDriverName(), Comdb2ClientInfo.getDriverVersion() };
1011

1112
for ( String val : actual_driver ) { Assert.assertNotNull("driver value is not null", val); }
12-
Assert.assertFalse("driver version is not default", actual_driver[1].equals("1.0"));
1313
Assert.assertArrayEquals("driver name and version match pom.xml", expected_driver, actual_driver);
1414
}
15+
16+
@Test public void verifyDriverInfoInDatabase() throws SQLException {
17+
String db = System.getProperty("cdb2jdbc.test.database");
18+
String cluster = System.getProperty("cdb2jdbc.test.cluster");
19+
20+
Connection conn = DriverManager.getConnection(String.format("jdbc:comdb2://%s/%s", cluster, db));
21+
Statement stmt = conn.createStatement();
22+
ResultSet rs = stmt.executeQuery("SELECT comdb2_host()");
23+
String directcpu = rs.getString(1);
24+
rs.close();
25+
stmt.close();
26+
conn.close();
27+
28+
conn = DriverManager.getConnection(String.format("jdbc:comdb2://%s/%s", directcpu, db));
29+
PreparedStatement ps = conn.prepareStatement("SELECT COUNT(*) FROM comdb2_api_history WHERE api_driver_name = ? and api_driver_version = ?");
30+
ps.setString(1, System.getProperty("driverName"));
31+
ps.setString(2, System.getProperty("driverVersion"));
32+
33+
rs = ps.executeQuery();
34+
int cnt = rs.getInt(1);
35+
Assert.assertTrue(cnt > 0);
36+
rs.close();
37+
stmt.close();
38+
conn.close();
39+
}
1540
}

0 commit comments

Comments
 (0)