Skip to content

Commit ae3c550

Browse files
authored
replace Fastjson with Jackson (#370)
1 parent 72517a3 commit ae3c550

File tree

10 files changed

+103
-50
lines changed

10 files changed

+103
-50
lines changed

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@
112112
</dependency>
113113

114114
<dependency>
115-
<groupId>com.alibaba</groupId>
116-
<artifactId>fastjson</artifactId>
117-
<version>1.2.83</version>
115+
<groupId>com.fasterxml.jackson.core</groupId>
116+
<artifactId>jackson-databind</artifactId>
117+
<version>2.19.0</version>
118118
</dependency>
119119

120120
<dependency>
@@ -164,6 +164,10 @@
164164
<artifactId>visible-assertions</artifactId>
165165
<groupId>org.rnorth.visible-assertions</groupId>
166166
</exclusion>
167+
<exclusion>
168+
<artifactId>jackson-annotations</artifactId>
169+
<groupId>com.fasterxml.jackson.core</groupId>
170+
</exclusion>
167171
</exclusions>
168172
</dependency>
169173
<dependency>
@@ -388,14 +392,15 @@
388392
</plugin>
389393
<plugin>
390394
<artifactId>maven-shade-plugin</artifactId>
391-
<version>3.2.4</version>
395+
<version>3.6.0</version>
392396
<executions>
393397
<execution>
394398
<phase>package</phase>
395399
<goals>
396400
<goal>shade</goal>
397401
</goals>
398402
<configuration>
403+
<createDependencyReducedPom>false</createDependencyReducedPom>
399404
<shadedArtifactAttached>true</shadedArtifactAttached>
400405
<shadedClassifierName>shade</shadedClassifierName>
401406
<artifactSet>

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.alipay.oceanbase.rpc;
1919

20-
import com.alibaba.fastjson.JSON;
2120
import com.alipay.oceanbase.rpc.checkandmutate.CheckAndInsUp;
2221
import com.alipay.oceanbase.rpc.constant.Constants;
2322
import com.alipay.oceanbase.rpc.exception.*;
@@ -46,6 +45,9 @@
4645
import com.alipay.oceanbase.rpc.threadlocal.ThreadLocalMap;
4746
import com.alipay.oceanbase.rpc.util.*;
4847
import com.alipay.remoting.util.StringUtils;
48+
import com.fasterxml.jackson.core.JsonProcessingException;
49+
import com.fasterxml.jackson.databind.ObjectMapper;
50+
import com.fasterxml.jackson.databind.SerializationFeature;
4951
import org.slf4j.Logger;
5052

5153
import java.util.*;
@@ -69,6 +71,15 @@
6971
public class ObTableClient extends AbstractObTableClient implements Lifecycle {
7072
private static final Logger logger = getLogger(ObTableClient.class);
7173

74+
private static final ObjectMapper objectMapper = new ObjectMapper();
75+
76+
static {
77+
// FAIL_ON_EMPTY_BEANS means that whether throwing exception if there is no any serializable member with getter or setter in an object
78+
// considering partitionElements in range partDesc is a list of Comparable interface and Comparable has no getter and setter
79+
// we have to set this configuration as false because tableEntry may be serialized in debug log
80+
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
81+
}
82+
7283
private static final String usernameSeparators = ":;-;.";
7384

7485
private AtomicInteger tableEntryRefreshContinuousFailureCount = new AtomicInteger(
@@ -456,19 +467,19 @@ private void initMetadata() throws Exception {
456467

457468
List<ObServerAddr> rsList = ocpModel.getObServerAddrs();
458469
BOOT.info("{} success to get rsList, paramURL: {}, rsList: {},idc2Region: {}",
459-
this.database, paramURL, JSON.toJSON(rsList), JSON.toJSON(ocpModel.getIdc2Region()));
470+
this.database, paramURL, objectMapper.writeValueAsString(rsList), objectMapper.writeValueAsString(ocpModel.getIdc2Region()));
460471

461472
TableEntry tableEntry = loadTableEntryRandomly(rsList,//
462473
rootServerKey,//
463474
tableEntryAcquireConnectTimeout,//
464475
tableEntryAcquireSocketTimeout, sysUA, initialized);
465476
BOOT.info("{} success to get tableEntry with rootServerKey all_dummy_tables {}",
466-
this.database, JSON.toJSON(tableEntry));
477+
this.database, objectMapper.writeValueAsString(tableEntry));
467478

468479
List<ReplicaLocation> replicaLocations = tableEntry.getTableLocation()
469480
.getReplicaLocations();
470481
BOOT.info("{} success to get replicaLocation {}", this.database,
471-
JSON.toJSON(replicaLocations));
482+
objectMapper.writeValueAsString(replicaLocations));
472483

473484
for (ReplicaLocation replicaLocation : replicaLocations) {
474485
ObServerInfo info = replicaLocation.getInfo();
@@ -500,11 +511,11 @@ private void initMetadata() throws Exception {
500511
}
501512
if (servers.isEmpty()) {
502513
BOOT.error("{} failed to connect any replicaLocation server: {}", this.database,
503-
JSON.toJSON(replicaLocations));
514+
objectMapper.writeValueAsString(replicaLocations));
504515
throw new Exception("failed to connect any replicaLocation server");
505516
}
506517

507-
BOOT.info("{} success to build server connection {}", this.database, JSON.toJSON(servers));
518+
BOOT.info("{} success to build server connection {}", this.database, objectMapper.writeValueAsString(servers));
508519
this.tableRoster = tableRoster;
509520
this.serverRoster.reset(servers);
510521

@@ -525,7 +536,7 @@ private void initMetadata() throws Exception {
525536

526537
if (BOOT.isInfoEnabled()) {
527538
BOOT.info("{} finish refresh serverRoster: {}", this.database,
528-
JSON.toJSON(serverRoster));
539+
objectMapper.writeValueAsString(serverRoster));
529540
BOOT.info("finish initMetadata for all tables for database {}", this.database);
530541
}
531542

@@ -1031,7 +1042,7 @@ public void syncRefreshMetadata(boolean forceRenew) throws Exception {
10311042
currentIDC, regionFromOcp));
10321043

10331044
if (logger.isInfoEnabled()) {
1034-
logger.info("finish refresh serverRoster: {}", JSON.toJSON(serverRoster));
1045+
logger.info("finish refresh serverRoster: {}", objectMapper.writeValueAsString(serverRoster));
10351046
}
10361047
this.lastRefreshMetadataTimestamp = System.currentTimeMillis();
10371048
} finally {
@@ -1365,7 +1376,8 @@ public TableEntry getOrRefreshTableEntry(final String tableName, final boolean r
13651376
* @throws ObTableEntryRefreshException
13661377
*/
13671378
private TableEntry refreshTableEntry(TableEntry tableEntry, String tableName)
1368-
throws ObTableEntryRefreshException {
1379+
throws ObTableEntryRefreshException,
1380+
JsonProcessingException {
13691381
return refreshTableEntry(tableEntry, tableName, false);
13701382
}
13711383

@@ -1476,7 +1488,8 @@ public TableEntry refreshTableLocationByTabletId(TableEntry tableEntry, String t
14761488
* @throws ObTableEntryRefreshException
14771489
*/
14781490
private TableEntry refreshTableEntry(TableEntry tableEntry, String tableName, boolean fetchAll)
1479-
throws ObTableEntryRefreshException {
1491+
throws ObTableEntryRefreshException,
1492+
JsonProcessingException {
14801493
TableEntryKey tableEntryKey = new TableEntryKey(clusterName, tenantName, database,
14811494
tableName);
14821495
try {
@@ -1552,7 +1565,7 @@ private TableEntry refreshTableEntry(TableEntry tableEntry, String tableName, bo
15521565
if (logger.isDebugEnabled()) {
15531566
logger.debug(
15541567
"refresh table entry, dataSource: {}, tableName: {}, refresh: {} key:{} entry:{} ",
1555-
dataSourceName, tableName, true, tableEntryKey, JSON.toJSON(tableEntry));
1568+
dataSourceName, tableName, true, tableEntryKey, objectMapper.writeValueAsString(tableEntry));
15561569
}
15571570
return tableEntry;
15581571
}

src/main/java/com/alipay/oceanbase/rpc/bolt/transport/ObTableConnection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.alipay.oceanbase.rpc.bolt.transport;
1919

20-
import com.alibaba.fastjson.JSONObject;
2120
import com.alipay.oceanbase.rpc.ObGlobal;
2221
import com.alipay.oceanbase.rpc.exception.*;
2322
import com.alipay.oceanbase.rpc.location.LocationUtil;
@@ -26,6 +25,7 @@
2625
import com.alipay.oceanbase.rpc.table.ObTable;
2726
import com.alipay.oceanbase.rpc.util.*;
2827
import com.alipay.remoting.Connection;
28+
import com.fasterxml.jackson.databind.ObjectMapper;
2929
import org.slf4j.Logger;
3030

3131
import java.net.ConnectException;
@@ -40,6 +40,7 @@ public class ObTableConnection {
4040

4141
private static final Logger LOGGER = TableClientLoggerFactory
4242
.getLogger(ObTableConnection.class);
43+
private static ObjectMapper objectMapper = new ObjectMapper();
4344
private ObBytesString credential;
4445
private long tenantId = 1; //默认值切勿不要随意改动
4546
private Connection connection;
@@ -153,8 +154,8 @@ private void login() throws Exception {
153154
// When the caller doesn't provide any parameters, configsMap is empty.
154155
// In this case, we don't generate any JSON to avoid creating an empty object.
155156
if (loginWithConfigs && !obTable.getConfigs().isEmpty()) {
156-
JSONObject json = new JSONObject(obTable.getConfigs());
157-
request.setConfigsStr(json.toJSONString());
157+
String configStr = objectMapper.writeValueAsString(obTable.getConfigs());
158+
request.setConfigsStr(configStr);
158159
loginWithConfigs = false;
159160
}
160161
generatePassSecret(request);

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
package com.alipay.oceanbase.rpc.location;
1919

20-
import com.alibaba.fastjson.JSON;
21-
import com.alibaba.fastjson.JSONException;
22-
import com.alibaba.fastjson.JSONObject;
23-
import com.alibaba.fastjson.parser.ParserConfig;
20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.DeserializationFeature;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.fasterxml.jackson.databind.SerializationFeature;
2424
import com.alipay.oceanbase.rpc.ObGlobal;
2525
import com.alipay.oceanbase.rpc.constant.Constants;
2626
import com.alipay.oceanbase.rpc.exception.*;
@@ -58,8 +58,13 @@ public class LocationUtil {
5858

5959
private static final Logger logger = TableClientLoggerFactory
6060
.getLogger(LocationUtil.class);
61+
62+
private static final ObjectMapper objectMapper = new ObjectMapper();
63+
6164
static {
62-
ParserConfig.getGlobalInstance().setSafeMode(true);
65+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
66+
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
67+
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
6368
loadJdbcDriver();
6469
}
6570

@@ -774,7 +779,7 @@ private static TableEntry getTableEntryFromRemote(Connection connection, TableEn
774779

775780
if (!initialized) {
776781
if (BOOT.isInfoEnabled()) {
777-
BOOT.info("get table entry from remote, entry={}", JSON.toJSON(tableEntry));
782+
BOOT.info("get table entry from remote, entry={}", objectMapper.writeValueAsString(tableEntry));
778783
}
779784
} else {
780785
if (logger.isInfoEnabled()) {
@@ -793,7 +798,7 @@ private static TableEntry getTableEntryFromRemote(Connection connection, TableEn
793798
} catch (Exception e) {
794799
RUNTIME.error(LCD.convert("01-00009"), key, e);
795800
if (e instanceof ObTableEntryRefreshException) {
796-
throw e;
801+
throw (ObTableEntryRefreshException) e;
797802
}
798803
throw new ObTableEntryRefreshException(format(
799804
"fail to get table entry from remote, key=%s", key), e);
@@ -1113,15 +1118,15 @@ private static void fetchFirstPart(Connection connection, TableEntry tableEntry,
11131118

11141119
if (logger.isInfoEnabled()) {
11151120
logger.info(format("uuid:%s, get first ranges from remote for %s, bounds=%s",
1116-
uuid, tableName, JSON.toJSON(bounds)));
1121+
uuid, tableName, objectMapper.writeValueAsString(bounds)));
11171122
}
11181123

11191124
} else if (obPartFuncType.isListPart()) {
11201125
Map<ObPartitionKey, Long> sets = parseFirstPartSets(rs, tableEntry);
11211126
((ObListPartDesc) tableEntry.getPartitionInfo().getFirstPartDesc()).setSets(sets);
11221127
if (logger.isInfoEnabled()) {
11231128
logger.info(format("uuid:%s, get first list sets from remote for %s, sets=%s",
1124-
uuid, tableName, JSON.toJSON(sets)));
1129+
uuid, tableName, objectMapper.writeValueAsString(sets)));
11251130
}
11261131
} else if (ObGlobal.obVsnMajor() >= 4
11271132
&& (obPartFuncType.isKeyPart() || obPartFuncType.isHashPart())) {
@@ -1186,14 +1191,14 @@ private static void fetchSubPart(Connection connection, TableEntry tableEntry,
11861191
.setHighBoundValues(highBoundVals);
11871192
if (logger.isInfoEnabled()) {
11881193
logger.info(format("uuid:%s, get sub ranges from remote for %s, bounds=%s",
1189-
uuid, tableName, JSON.toJSON(bounds)));
1194+
uuid, tableName, objectMapper.writeValueAsString(bounds)));
11901195
}
11911196
} else if (subPartFuncType.isListPart()) {
11921197
Map<ObPartitionKey, Long> sets = parseSubPartSets(rs, tableEntry);
11931198
((ObListPartDesc) tableEntry.getPartitionInfo().getSubPartDesc()).setSets(sets);
11941199
if (logger.isInfoEnabled()) {
11951200
logger.info(format("uuid:%s, get sub list sets from remote, sets=%s", uuid,
1196-
JSON.toJSON(sets)));
1201+
objectMapper.writeValueAsString(sets)));
11971202
}
11981203
} else if (ObGlobal.obVsnMajor() >= 4
11991204
&& (subPartFuncType.isKeyPart() || subPartFuncType.isHashPart())) {
@@ -1499,7 +1504,7 @@ private static void fetchPartitionInfo(Connection connection, TableEntry tableEn
14991504
info = parsePartitionInfo(rs);
15001505

15011506
if (logger.isInfoEnabled()) {
1502-
logger.info("get part info from remote info:{}", JSON.toJSON(info));
1507+
logger.info("get part info from remote info:{}", objectMapper.writeValueAsString(info));
15031508
}
15041509
tableEntry.setPartitionInfo(info);
15051510
} catch (SQLException e) {
@@ -2085,7 +2090,7 @@ private static OcpResponse getRemoteOcpResponseOrNull(String paramURL, String da
20852090
for (; tries < tryTimes; tries++) {
20862091
try {
20872092
content = loadStringFromUrl(paramURL, connectTimeout, readTimeout);
2088-
ocpResponse = JSONObject.parseObject(content, OcpResponse.class);
2093+
ocpResponse = objectMapper.readValue(content, OcpResponse.class);
20892094
if (ocpResponse != null && ocpResponse.validate()) {
20902095
if (dataSourceName != null && !dataSourceName.isEmpty()) {
20912096
saveLocalContent(dataSourceName, content);
@@ -2132,7 +2137,7 @@ private static OcpResponse getRemoteOcpIdcRegionOrNull(String paramURL, int conn
21322137
for (; tries < tryTimes; tries++) {
21332138
try {
21342139
content = loadStringFromUrl(paramURL, connectTimeout, readTimeout);
2135-
ocpResponse = JSONObject.parseObject(content, OcpResponse.class);
2140+
ocpResponse = objectMapper.readValue(content, OcpResponse.class);
21362141
if (ocpResponse != null) {
21372142
return ocpResponse;
21382143
}
@@ -2150,8 +2155,8 @@ private static OcpResponse getRemoteOcpIdcRegionOrNull(String paramURL, int conn
21502155
return null;
21512156
}
21522157

2153-
private static OcpResponse parseOcpResponse(String content) throws JSONException {
2154-
return JSONObject.parseObject(content, OcpResponse.class);
2158+
private static OcpResponse parseOcpResponse(String content) throws JsonProcessingException {
2159+
return objectMapper.readValue(content, OcpResponse.class);
21552160
}
21562161

21572162
private static OcpResponse getLocalOcpResponseOrNull(String fileName) {

src/main/java/com/alipay/oceanbase/rpc/location/model/OcpResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.alipay.oceanbase.rpc.location.model;
1919

20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
2022
public class OcpResponse {
2123
private int Code;
2224
private String Message;
@@ -26,6 +28,7 @@ public class OcpResponse {
2628
/*
2729
* Get code.
2830
*/
31+
@JsonProperty("Code")
2932
public int getCode() {
3033
return Code;
3134
}
@@ -40,6 +43,7 @@ public void setCode(int code) {
4043
/*
4144
* Get message.
4245
*/
46+
@JsonProperty("Message")
4347
public String getMessage() {
4448
return Message;
4549
}
@@ -58,6 +62,11 @@ public boolean isSuccess() {
5862
return Success;
5963
}
6064

65+
@JsonProperty("Success")
66+
public boolean getSuccess() {
67+
return Success;
68+
}
69+
6170
/*
6271
* Set success.
6372
*/
@@ -68,6 +77,7 @@ public void setSuccess(boolean success) {
6877
/*
6978
* Get data.
7079
*/
80+
@JsonProperty("Data")
7181
public OcpResponseData getData() {
7282
return Data;
7383
}

0 commit comments

Comments
 (0)