Skip to content

Commit 7f4ad14

Browse files
committed
[Refactor] oceanbase jdbc
Refactor jdbc. Using oceanbase jdbc driver from now on. Link: https://code.alibaba-inc.com/oceanbase/obkv-table-client-java/codereview/12653755 * [Refactor] oceanbase jdbc
1 parent 00758df commit 7f4ad14

File tree

10 files changed

+100
-78
lines changed

10 files changed

+100
-78
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@
109109
</dependency>
110110

111111
<dependency>
112-
<groupId>mysql</groupId>
113-
<artifactId>mysql-connector-java</artifactId>
114-
<version>8.0.32</version>
112+
<groupId>com.oceanbase</groupId>
113+
<artifactId>oceanbase-client</artifactId>
114+
<version>2.4.3</version>
115115
</dependency>
116116

117117
<dependency>

src/main/java/com/alipay/oceanbase/rpc/ObTableClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,8 @@ private ObPair<Long, ReplicaLocation> getPartitionReplica(TableEntry tableEntry,
11481148
long logicID = partId;
11491149
if (tableEntry != null && tableEntry.getPartitionInfo() != null
11501150
&& tableEntry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_TWO) {
1151-
logicID = ObPartIdCalculator.getPartIdx(partId, tableEntry.getPartitionInfo().getSubPartDesc().getPartNum());
1151+
logicID = ObPartIdCalculator.getPartIdx(partId, tableEntry.getPartitionInfo()
1152+
.getSubPartDesc().getPartNum());
11521153
}
11531154
return new ObPair<Long, ReplicaLocation>(partId, getPartitionLocation(tableEntry, logicID,
11541155
route));
@@ -1319,8 +1320,10 @@ public ObPair<Long, ObTableParam> getTable(String tableName, TableEntry tableEnt
13191320
ObTableParam param = new ObTableParam(obTable);
13201321
if (ObGlobal.OB_VERSION >= 4 && tableEntry != null) {
13211322
long logicID = partId;
1322-
if (tableEntry.getPartitionInfo() != null && tableEntry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_TWO) {
1323-
logicID = ObPartIdCalculator.getPartIdx(partId, tableEntry.getPartitionInfo().getSubPartDesc().getPartNum());
1323+
if (tableEntry.getPartitionInfo() != null
1324+
&& tableEntry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_TWO) {
1325+
logicID = ObPartIdCalculator.getPartIdx(partId, tableEntry.getPartitionInfo()
1326+
.getSubPartDesc().getPartNum());
13241327
}
13251328
partId = tableEntry.isPartitionTable() ? tableEntry.getPartitionInfo()
13261329
.getPartTabletIdMap().get(logicID) : partId;

src/main/java/com/alipay/oceanbase/rpc/location/LocationUtil.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static List<ObServerLdcItem> getServerLdc(ServerRoster serverRoster,
233233
private static String formatObServerUrl(ObServerAddr obServerAddr, long connectTimeout,
234234
long socketTimeout) {
235235
return format(
236-
"jdbc:mysql://%s/oceanbase?useUnicode=true&characterEncoding=utf-8&connectTimeout=%d&socketTimeout=%d",
236+
"jdbc:oceanbase://%s/oceanbase?useUnicode=true&characterEncoding=utf-8&connectTimeout=%d&socketTimeout=%d",
237237
obServerAddr.getIp() + ":" + obServerAddr.getSqlPort(), connectTimeout, socketTimeout);
238238
}
239239

@@ -247,11 +247,11 @@ private static String formatObServerUrl(ObServerAddr obServerAddr, long connectT
247247
private static Connection getMetaRefreshConnection(String url, ObUserAuth sysUA)
248248
throws ObTableEntryRefreshException {
249249
try {
250-
Class.forName("com.mysql.jdbc.Driver");
250+
Class.forName("com.alipay.oceanbase.jdbc.Driver");
251251
} catch (ClassNotFoundException e) {
252252
RUNTIME.error(LCD.convert("01-00006"), e.getMessage(), e);
253253
throw new ObTableEntryRefreshException(format(
254-
"fail to find com.mysql.jdbc.Driver, errMsg=%s", e.getMessage()), e);
254+
"fail to find com.alipay.oceanbase.jdbc.Driver, errMsg=%s", e.getMessage()), e);
255255
} catch (Exception e) {
256256
RUNTIME.error(LCD.convert("01-00005"), e.getMessage(), e);
257257
throw new ObTableEntryRefreshException("fail to decode proxyro password", e);
@@ -617,7 +617,8 @@ public static TableEntry getTableEntryLocationFromRemote(Connection connection,
617617
}
618618
sql = MessageFormat.format(PROXY_LOCATION_SQL_PARTITION_V4, sb.toString());
619619
} else {
620-
if (tableEntry.isPartitionTable() && null != tableEntry.getPartitionInfo().getSubPartDesc()) {
620+
if (tableEntry.isPartitionTable()
621+
&& null != tableEntry.getPartitionInfo().getSubPartDesc()) {
621622
long firstPartNum = tableEntry.getPartitionInfo().getFirstPartDesc().getPartNum();
622623
long subPartNum = tableEntry.getPartitionInfo().getSubPartDesc().getPartNum();
623624
for (long i = 0; i < firstPartNum; ++i) {
@@ -713,7 +714,8 @@ private static void fetchFirstPart(Connection connection, TableEntry tableEntry,
713714
}
714715
} else if (ObGlobal.OB_VERSION >= 4
715716
&& (obPartFuncType.isKeyPart() || obPartFuncType.isHashPart())) {
716-
tableEntry.getPartitionInfo().setPartTabletIdMap(parseFirstPartKeyHash(rs, tableEntry));
717+
tableEntry.getPartitionInfo().setPartTabletIdMap(
718+
parseFirstPartKeyHash(rs, tableEntry));
717719
}
718720
} catch (Exception e) {
719721
RUNTIME.error(LCD.convert("01-00011"), tableEntry, obPartFuncType, e);
@@ -778,7 +780,8 @@ private static void fetchSubPart(Connection connection, TableEntry tableEntry,
778780
}
779781
} else if (ObGlobal.OB_VERSION >= 4
780782
&& (subPartFuncType.isKeyPart() || subPartFuncType.isHashPart())) {
781-
tableEntry.getPartitionInfo().setPartTabletIdMap(parseSubPartKeyHash(rs, tableEntry));
783+
tableEntry.getPartitionInfo().setPartTabletIdMap(
784+
parseSubPartKeyHash(rs, tableEntry));
782785
}
783786
} catch (Exception e) {
784787
RUNTIME.error(LCD.convert("01-00012"), tableEntry, subPartFuncType, e);
@@ -851,8 +854,10 @@ private static ObPartitionEntry getPartitionLocationFromResultSet(TableEntry tab
851854
partitionId = rs.getLong("tablet_id");
852855
} else {
853856
partitionId = rs.getLong("partition_id");
854-
if (tableEntry.isPartitionTable() && null != tableEntry.getPartitionInfo().getSubPartDesc()) {
855-
partitionId = ObPartIdCalculator.getPartIdx(partitionId, tableEntry.getPartitionInfo().getSubPartDesc().getPartNum());
857+
if (tableEntry.isPartitionTable()
858+
&& null != tableEntry.getPartitionInfo().getSubPartDesc()) {
859+
partitionId = ObPartIdCalculator.getPartIdx(partitionId, tableEntry
860+
.getPartitionInfo().getSubPartDesc().getPartNum());
856861
}
857862
}
858863
if (!replica.isValid()) {
@@ -1183,9 +1188,10 @@ private static Map<String, Long> buildPartNameIdMap(ObPartitionInfo partitionInf
11831188
return partNameIdMap;
11841189
}
11851190

1186-
private static Map<Long, Long> parseFirstPartKeyHash(ResultSet rs, TableEntry tableEntry) throws SQLException,
1187-
IllegalArgumentException,
1188-
FeatureNotSupportedException {
1191+
private static Map<Long, Long> parseFirstPartKeyHash(ResultSet rs, TableEntry tableEntry)
1192+
throws SQLException,
1193+
IllegalArgumentException,
1194+
FeatureNotSupportedException {
11891195
return parseKeyHashPart(rs, tableEntry, false);
11901196
}
11911197

@@ -1219,15 +1225,17 @@ private static Map<ObPartitionKey, Long> parseSubPartSets(ResultSet rs, TableEnt
12191225
return parseListPartSets(rs, tableEntry, true);
12201226
}
12211227

1222-
private static Map<Long, Long> parseSubPartKeyHash(ResultSet rs, TableEntry tableEntry) throws SQLException,
1223-
IllegalArgumentException,
1224-
FeatureNotSupportedException {
1228+
private static Map<Long, Long> parseSubPartKeyHash(ResultSet rs, TableEntry tableEntry)
1229+
throws SQLException,
1230+
IllegalArgumentException,
1231+
FeatureNotSupportedException {
12251232
return parseKeyHashPart(rs, tableEntry, true);
12261233
}
12271234

1228-
private static Map<Long, Long> parseKeyHashPart(ResultSet rs, TableEntry tableEntry, boolean isSubPart) throws SQLException,
1229-
IllegalArgumentException,
1230-
FeatureNotSupportedException {
1235+
private static Map<Long, Long> parseKeyHashPart(ResultSet rs, TableEntry tableEntry,
1236+
boolean isSubPart) throws SQLException,
1237+
IllegalArgumentException,
1238+
FeatureNotSupportedException {
12311239
long idx = 0L;
12321240
Map<Long, Long> partTabletIdMap = new HashMap<Long, Long>();
12331241
while (rs.next()) {
@@ -1274,14 +1282,15 @@ private static List<ObComparableKV<ObPartitionKey, Long>> parseRangePart(ResultS
12741282
List<ObComparableKV<ObPartitionKey, Long>> bounds = new ArrayList<ObComparableKV<ObPartitionKey, Long>>();
12751283
Map<String, Long> partNameIdMap = new HashMap<String, Long>();
12761284
Map<Long, Long> partTabletIdMap = new HashMap<Long, Long>();
1277-
ObRangePartDesc subRangePartDesc = (ObRangePartDesc) tableEntry.getPartitionInfo().getSubPartDesc();
1285+
ObRangePartDesc subRangePartDesc = (ObRangePartDesc) tableEntry.getPartitionInfo()
1286+
.getSubPartDesc();
12781287
long idx = 0L;
12791288
while (rs.next()) {
12801289
if (null != subRangePartDesc && !isSubPart && subRangePartDesc.getPartNum() == 0) {
12811290
// client only support template partition table
12821291
// so the sub_part_num is a constant and will store in subPartDesc which is different from proxy
12831292
long subPartNum = rs.getLong("sub_part_num");
1284-
subRangePartDesc.setPartNum((int)subPartNum);
1293+
subRangePartDesc.setPartNum((int) subPartNum);
12851294
}
12861295

12871296
String highBoundVal = rs.getString("high_bound_val");

src/main/java/com/alipay/oceanbase/rpc/location/model/partition/ObRangePartDesc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public ObRangePartDesc() {
4444
setPartFuncType(ObPartFuncType.RANGE);
4545
}
4646

47-
private int partSpace = 0;
48-
private int partNum = 0;
47+
private int partSpace = 0;
48+
private int partNum = 0;
4949
private List<ObColumn> orderedCompareColumns = null;
5050
private List<ObObjType> orderedCompareColumnTypes = null;
5151
private List<ObComparableKV<ObPartitionKey, Long>> bounds = null;

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public interface Constants {
2121

2222
long OB_INVALID_ID = -1;
23-
23+
2424
long INVALID_TABLET_ID = 0;
2525

2626
short UNSIGNED_INT8_MAX = (1 << 8) - 1;

src/main/java/com/alipay/oceanbase/rpc/util/ObHashUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public static long varcharHash(Object varchar, ObCollationType collationType, lo
6666
switch (collationType) {
6767
case CS_TYPE_UTF8MB4_GENERAL_CI:
6868
if (partFuncType == ObPartFuncType.KEY_V3
69-
|| partFuncType == ObPartFuncType.KEY_IMPLICIT_V2
70-
|| ObGlobal.OB_VERSION >= 4) {
69+
|| partFuncType == ObPartFuncType.KEY_IMPLICIT_V2 || ObGlobal.OB_VERSION >= 4) {
7170
hashCode = ObHashSortUtf8mb4.obHashSortUtf8Mb4(bytes, bytes.length, hashCode,
7271
seed, true);
7372
} else {
@@ -77,8 +76,7 @@ public static long varcharHash(Object varchar, ObCollationType collationType, lo
7776
break;
7877
case CS_TYPE_UTF8MB4_BIN:
7978
if (partFuncType == ObPartFuncType.KEY_V3
80-
|| partFuncType == ObPartFuncType.KEY_IMPLICIT_V2
81-
|| ObGlobal.OB_VERSION >= 4) {
79+
|| partFuncType == ObPartFuncType.KEY_IMPLICIT_V2 || ObGlobal.OB_VERSION >= 4) {
8280
hashCode = MurmurHash.hash64a(bytes, bytes.length, hashCode);
8381
} else {
8482
hashCode = ObHashSortUtf8mb4.obHashSortMbBin(bytes, bytes.length, hashCode,
@@ -87,8 +85,7 @@ public static long varcharHash(Object varchar, ObCollationType collationType, lo
8785
break;
8886
case CS_TYPE_BINARY:
8987
if (partFuncType == ObPartFuncType.KEY_V3
90-
|| partFuncType == ObPartFuncType.KEY_IMPLICIT_V2
91-
|| ObGlobal.OB_VERSION >= 4) {
88+
|| partFuncType == ObPartFuncType.KEY_IMPLICIT_V2 || ObGlobal.OB_VERSION >= 4) {
9289
hashCode = MurmurHash.hash64a(bytes, bytes.length, hashCode);
9390
} else {
9491
hashCode = ObHashSortBin.obHashSortBin(bytes, bytes.length, hashCode, seed);

src/test/java/com/alipay/oceanbase/rpc/ObTableClientPartitionHashTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,12 @@ public void testPartitionLocation() throws Exception {
524524
long c2 = rng.nextLong();
525525

526526
// use sql to insert data
527-
statement.execute("insert into " + testTable + "(c1, c2, c3) values (" + c1 + "," + c2 + "," + "'value')");
527+
statement.execute("insert into " + testTable + "(c1, c2, c3) values (" + c1 + ","
528+
+ c2 + "," + "'value')");
528529

529530
// get data by obkv interface
530-
Map<String, Object> result = obTableClient.get(testTable,
531-
new Object[] { c1, c2 }, new String[] { "c1", "c2", "c3" });
531+
Map<String, Object> result = obTableClient.get(testTable, new Object[] { c1, c2 },
532+
new String[] { "c1", "c2", "c3" });
532533
Assert.assertEquals(3, result.size());
533534
}
534535
} catch (Exception e) {

src/test/java/com/alipay/oceanbase/rpc/ObTableClientPartitionRangeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,12 @@ public void testPartitionLocation() throws Exception {
627627

628628
// use sql to insert data
629629
statement.execute("insert into " + testTable + "(c1, c2, c3, c4, c5) values (" + c1
630-
+ "," + c2 + ",'" + c3 + "','" + c4 + "'," + "'value')");
630+
+ "," + c2 + ",'" + c3 + "','" + c4 + "'," + "'value')");
631631

632632
// get data by obkv interface
633633
Map<String, Object> result = obTableClient.get(testTable,
634-
new Object[] { c1, c2, c3.getBytes(), c4 }, new String[] { "c1", "c2", "c3",
635-
"c4", "c5" });
634+
new Object[] { c1, c2, c3.getBytes(), c4 }, new String[] { "c1", "c2", "c3",
635+
"c4", "c5" });
636636
Assert.assertEquals(5, result.size());
637637
}
638638
} catch (Exception e) {

src/test/java/com/alipay/oceanbase/rpc/ObTableConnectionTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232

3333
public class ObTableConnectionTest {
3434
public ObTableClient client;
35-
private int connCnt = 100;
35+
private int connCnt = 100;
36+
3637
@Before
3738
public void setup() throws Exception {
3839
System.setProperty("ob_table_min_rslist_refresh_interval_millis", "1");
3940

4041
final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient();
41-
obTableClient.addProperty(Property.SERVER_CONNECTION_POOL_SIZE.getKey(), Integer.toString(connCnt));
42+
obTableClient.addProperty(Property.SERVER_CONNECTION_POOL_SIZE.getKey(),
43+
Integer.toString(connCnt));
4244
obTableClient.init();
4345

4446
this.client = obTableClient;
@@ -48,7 +50,8 @@ int getGvConnections() throws Exception {
4850
int resCnt = 0;
4951
Connection connection = ObTableClientTestUtil.getConnection();
5052
Statement statement = connection.createStatement();
51-
statement.execute("select count(*) from oceanbase.gv$ob_kv_connections group by svr_ip, svr_port limit 1");
53+
statement
54+
.execute("select count(*) from oceanbase.gv$ob_kv_connections group by svr_ip, svr_port limit 1");
5255
ResultSet resultSet = statement.getResultSet();
5356
while (resultSet.next()) {
5457
resCnt = resultSet.getInt(1);
@@ -123,7 +126,7 @@ class GetWorker extends Thread {
123126
private int id;
124127
private String tableName;
125128
private ObTableClient obTableClient;
126-
private long executeTime; // in millisecond
129+
private long executeTime; // in millisecond
127130

128131
public GetWorker(int id, String tableName, ObTableClient obTableClient, long executeTime) {
129132
this.id = id;
@@ -136,11 +139,12 @@ public void run() {
136139
long start = System.currentTimeMillis();
137140
while ((System.currentTimeMillis() - start) < executeTime) {
138141
try {
139-
obTableClient.get(tableName, new String[] { "k1" }, new String[] {"c1"});
142+
obTableClient.get(tableName, new String[] { "k1" }, new String[] { "c1" });
140143
} catch (Exception e) {
141144
e.printStackTrace();
142145
System.out.println("thread " + id + " get occurs exception !");
143-
} finally {}
146+
} finally {
147+
}
144148
}
145149
}
146150
}

0 commit comments

Comments
 (0)