diff --git a/core/pom.xml b/core/pom.xml index 94491e45a..dbd3f7cb8 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -57,8 +57,8 @@ ${arthas.deps.package}.io.netty - com.alibaba.fastjson - ${arthas.deps.package}.com.alibaba.fastjson + com.alibaba.fastjson2 + ${arthas.deps.package}.com.alibaba.fastjson2 @@ -204,8 +204,8 @@ true - com.alibaba - fastjson + com.alibaba.fastjson2 + fastjson2 ognl @@ -258,7 +258,6 @@ provided true - diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/DashboardCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/DashboardCommand.java index e118a08fd..49b14c87c 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/DashboardCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/DashboardCommand.java @@ -10,8 +10,8 @@ import java.util.concurrent.atomic.AtomicLong; import com.alibaba.arthas.deps.org.slf4j.Logger; import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.taobao.arthas.core.command.Constants; import com.taobao.arthas.core.command.model.DashboardModel; import com.taobao.arthas.core.command.model.GcInfoVO; diff --git a/core/src/main/java/com/taobao/arthas/core/distribution/ResultConsumerHelper.java b/core/src/main/java/com/taobao/arthas/core/distribution/ResultConsumerHelper.java index ee29b9acb..57712fe70 100644 --- a/core/src/main/java/com/taobao/arthas/core/distribution/ResultConsumerHelper.java +++ b/core/src/main/java/com/taobao/arthas/core/distribution/ResultConsumerHelper.java @@ -1,6 +1,6 @@ package com.taobao.arthas.core.distribution; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import com.taobao.arthas.core.command.model.Countable; import com.taobao.arthas.core.command.model.ResultModel; import org.slf4j.Logger; diff --git a/core/src/main/java/com/taobao/arthas/core/distribution/impl/PackingResultDistributorImpl.java b/core/src/main/java/com/taobao/arthas/core/distribution/impl/PackingResultDistributorImpl.java index 9903b8e48..257b44669 100644 --- a/core/src/main/java/com/taobao/arthas/core/distribution/impl/PackingResultDistributorImpl.java +++ b/core/src/main/java/com/taobao/arthas/core/distribution/impl/PackingResultDistributorImpl.java @@ -2,7 +2,7 @@ package com.taobao.arthas.core.distribution.impl; import com.alibaba.arthas.deps.org.slf4j.Logger; import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import com.taobao.arthas.core.command.model.ResultModel; import com.taobao.arthas.core.distribution.PackingResultDistributor; import com.taobao.arthas.core.shell.session.Session; diff --git a/core/src/main/java/com/taobao/arthas/core/distribution/impl/ResultConsumerImpl.java b/core/src/main/java/com/taobao/arthas/core/distribution/impl/ResultConsumerImpl.java index d3f3a9eab..e2db5fc07 100644 --- a/core/src/main/java/com/taobao/arthas/core/distribution/impl/ResultConsumerImpl.java +++ b/core/src/main/java/com/taobao/arthas/core/distribution/impl/ResultConsumerImpl.java @@ -2,7 +2,7 @@ package com.taobao.arthas.core.distribution.impl; import com.alibaba.arthas.deps.org.slf4j.Logger; import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import com.taobao.arthas.core.command.model.ResultModel; import com.taobao.arthas.core.distribution.DistributorOptions; import com.taobao.arthas.core.distribution.ResultConsumer; diff --git a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java index bef8aaf36..6b6ef7c12 100644 --- a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java +++ b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java @@ -34,8 +34,8 @@ import com.alibaba.bytekit.asm.instrument.InstrumentTransformer; import com.alibaba.bytekit.asm.matcher.SimpleClassMatcher; import com.alibaba.bytekit.utils.AsmUtils; import com.alibaba.bytekit.utils.IOUtils; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONWriter; import com.taobao.arthas.common.AnsiLog; import com.taobao.arthas.common.ArthasConstants; import com.taobao.arthas.common.PidUtils; @@ -177,14 +177,9 @@ public class ArthasBootstrap { } private void initFastjson() { - // disable fastjson circular reference feature - JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask(); - // add date format option for fastjson - JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteDateUseDateFormat.getMask(); // ignore getter error #1661 - JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.IgnoreErrorGetter.getMask(); // #2081 - JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteNonStringKeyAsString.getMask(); + JSON.config(JSONWriter.Feature.IgnoreErrorGetter, JSONWriter.Feature.WriteNonStringKeyAsString); } private void initBeans() { diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java index eb1b7eba2..abae1af02 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java @@ -64,13 +64,11 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler byteBufPool = new ArrayBlockingQueue(poolSize); - private ArrayBlockingQueue charsBufPool = new ArrayBlockingQueue(poolSize); - private ArrayBlockingQueue bytesPool = new ArrayBlockingQueue(poolSize); - public HttpApiHandler(HistoryManager historyManager, SessionManager sessionManager) { this.historyManager = historyManager; this.sessionManager = sessionManager; commandManager = this.sessionManager.getCommandManager(); jobController = this.sessionManager.getJobController(); - - //init buf pool - JsonUtils.setSerializeWriterBufferThreshold(jsonBufferSize); - for (int i = 0; i < poolSize; i++) { - byteBufPool.offer(Unpooled.buffer(jsonBufferSize)); - charsBufPool.offer(new char[jsonBufferSize]); - bytesPool.offer(new byte[jsonBufferSize]); - } } public HttpResponse handle(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { @@ -113,77 +95,13 @@ public class HttpApiHandler { } result.setRequestId(requestId); + byte[] jsonBytes = JSON.toJSONBytes(result, JSON_FILTERS); - //http response content - ByteBuf content = null; - //fastjson buf - char[] charsBuf = null; - byte[] bytesBuf = null; - - try { - //apply response content buf first - content = byteBufPool.poll(2000, TimeUnit.MILLISECONDS); - if (content == null) { - throw new ApiException("get response content buf failure"); - } - - //apply fastjson buf from pool - charsBuf = charsBufPool.poll(); - bytesBuf = bytesPool.poll(); - if (charsBuf == null || bytesBuf == null) { - throw new ApiException("get json buf failure"); - } - JsonUtils.setSerializeWriterBufThreadLocal(charsBuf, bytesBuf); - - //create http response - DefaultFullHttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), - HttpResponseStatus.OK, content.retain()); - response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8"); - writeResult(response, result); - return response; - } catch (Exception e) { - //response is discarded - if (content != null) { - content.release(); - byteBufPool.offer(content); - } - throw e; - } finally { - //give back json buf to pool - JsonUtils.setSerializeWriterBufThreadLocal(null, null); - if (charsBuf != null) { - charsBufPool.offer(charsBuf); - } - if (bytesBuf != null) { - bytesPool.offer(bytesBuf); - } - } - } - - public void onCompleted(DefaultFullHttpResponse httpResponse) { - ByteBuf content = httpResponse.content(); - content.clear(); - if (content.capacity() == jsonBufferSize) { - if (!byteBufPool.offer(content)) { - content.release(); - } - } else { - //replace content ByteBuf - content.release(); - if (byteBufPool.remainingCapacity() > 0) { - byteBufPool.offer(Unpooled.buffer(jsonBufferSize)); - } - } - } - - private void writeResult(DefaultFullHttpResponse response, Object result) throws IOException { - ByteBufOutputStream out = new ByteBufOutputStream(response.content()); - try { - JSON.writeJSONString(out, IOUtils.UTF8, result, SerializeConfig.globalInstance, JSON_FILTERS, null, JSON.DEFAULT_GENERATE_FEATURE); - } catch (IOException e) { - logger.error("write json to response failed", e); - throw e; - } + // create http response + DefaultFullHttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), + HttpResponseStatus.OK, Unpooled.wrappedBuffer(jsonBytes)); + response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8"); + return response; } private ApiRequest parseRequest(String requestBody) throws ApiException { diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/ObjectVOFilter.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/ObjectVOFilter.java index daf375755..5535c9b21 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/ObjectVOFilter.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/ObjectVOFilter.java @@ -1,6 +1,6 @@ package com.taobao.arthas.core.shell.term.impl.http.api; -import com.alibaba.fastjson.serializer.ValueFilter; +import com.alibaba.fastjson2.filter.ValueFilter; import com.taobao.arthas.core.command.model.ObjectVO; import com.taobao.arthas.core.util.StringUtils; import com.taobao.arthas.core.view.ObjectView; @@ -12,8 +12,7 @@ import com.taobao.arthas.core.view.ObjectView; public class ObjectVOFilter implements ValueFilter { @Override - public Object process(Object object, String name, Object value) { - + public Object apply(Object object, String name, Object value) { if (value instanceof ObjectVO) { ObjectVO vo = (ObjectVO) value; String resultStr = StringUtils.objectToString(vo.needExpand() ? new ObjectView(vo).draw() : value); diff --git a/core/src/main/java/com/taobao/arthas/core/util/JsonUtils.java b/core/src/main/java/com/taobao/arthas/core/util/JsonUtils.java deleted file mode 100644 index a2b184b8b..000000000 --- a/core/src/main/java/com/taobao/arthas/core/util/JsonUtils.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.taobao.arthas.core.util; - -import com.alibaba.fastjson.serializer.SerializeWriter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Field; - -/** - * @author gongdewei 2020/5/15 - */ -public class JsonUtils { - private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class); - private static Field serializeWriterBufLocalField; - private static Field serializeWriterBytesBufLocal; - private static Field serializeWriterBufferThreshold; - - /** - * Set Fastjson SerializeWriter Buffer Threshold - * @param value - */ - public static void setSerializeWriterBufferThreshold(int value) { - Class clazz = SerializeWriter.class; - try { - if (serializeWriterBufferThreshold == null) { - serializeWriterBufferThreshold = clazz.getDeclaredField("BUFFER_THRESHOLD"); - } - serializeWriterBufferThreshold.setAccessible(true); - serializeWriterBufferThreshold.set(null, value); - } catch (Throwable e) { - logger.error("update SerializeWriter.BUFFER_THRESHOLD value failed", e); - } - } - - /** - * Set Fastjson SerializeWriter ThreadLocal value - * @param bufSize - */ - public static void setSerializeWriterBufThreadLocal(int bufSize) { - Class clazz = SerializeWriter.class; - try { - //set threadLocal value - if (serializeWriterBufLocalField == null) { - serializeWriterBufLocalField = clazz.getDeclaredField("bufLocal"); - } - serializeWriterBufLocalField.setAccessible(true); - ThreadLocal bufLocal = (ThreadLocal) serializeWriterBufLocalField.get(null); - char[] charsLocal = bufLocal.get(); - if (charsLocal == null || charsLocal.length < bufSize) { - bufLocal.set(new char[bufSize]); - } - - if (serializeWriterBytesBufLocal == null) { - serializeWriterBytesBufLocal = clazz.getDeclaredField("bytesBufLocal"); - } - serializeWriterBytesBufLocal.setAccessible(true); - ThreadLocal bytesBufLocal = (ThreadLocal) serializeWriterBytesBufLocal.get(null); - byte[] bytesLocal = bytesBufLocal.get(); - if (bytesLocal == null || bytesLocal.length < bufSize) { - bytesBufLocal.set(new byte[bufSize]); - } - } catch (Throwable e) { - logger.error("update SerializeWriter.BUFFER_THRESHOLD value failed", e); - } - } - - /** - * Set Fastjson SerializeWriter ThreadLocal value - */ - public static void setSerializeWriterBufThreadLocal(char[] charsBuf, byte[] bytesBuf) { - Class clazz = SerializeWriter.class; - try { - //set threadLocal value - if (serializeWriterBufLocalField == null) { - serializeWriterBufLocalField = clazz.getDeclaredField("bufLocal"); - } - serializeWriterBufLocalField.setAccessible(true); - ThreadLocal bufLocal = (ThreadLocal) serializeWriterBufLocalField.get(null); - bufLocal.set(charsBuf); - - if (serializeWriterBytesBufLocal == null) { - serializeWriterBytesBufLocal = clazz.getDeclaredField("bytesBufLocal"); - } - serializeWriterBytesBufLocal.setAccessible(true); - ThreadLocal bytesBufLocal = (ThreadLocal) serializeWriterBytesBufLocal.get(null); - bytesBufLocal.set(bytesBuf); - } catch (Throwable e) { - logger.error("update SerializeWriter.BUFFER_THRESHOLD value failed", e); - } - } - - -} diff --git a/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java b/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java index 2b59c18b1..8ebff13db 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java @@ -1,7 +1,7 @@ package com.taobao.arthas.core.util; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.taobao.arthas.common.IOUtils; import java.io.BufferedReader; diff --git a/core/src/main/java/com/taobao/arthas/core/view/ObjectView.java b/core/src/main/java/com/taobao/arthas/core/view/ObjectView.java index 1883632e4..3e5391db6 100644 --- a/core/src/main/java/com/taobao/arthas/core/view/ObjectView.java +++ b/core/src/main/java/com/taobao/arthas/core/view/ObjectView.java @@ -2,8 +2,8 @@ package com.taobao.arthas.core.view; import com.alibaba.arthas.deps.org.slf4j.Logger; import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONWriter; import com.taobao.arthas.common.ArthasConstants; import com.taobao.arthas.core.GlobalOptions; import com.taobao.arthas.core.command.model.ObjectVO; @@ -54,7 +54,7 @@ public class ObjectView implements View { StringBuilder buf = new StringBuilder(); try { if (GlobalOptions.isUsingJson) { - return JSON.toJSONString(object, SerializerFeature.IgnoreErrorGetter); + return JSON.toJSONString(object, JSONWriter.Feature.IgnoreErrorGetter); } renderObject(object, 0, deep, buf); return buf.toString(); diff --git a/pom.xml b/pom.xml index 1d69538b6..606c0cb75 100644 --- a/pom.xml +++ b/pom.xml @@ -136,9 +136,9 @@ 0.0.13 - com.alibaba - fastjson - 1.2.83_noneautotype + com.alibaba.fastjson2 + fastjson2 + 2.0.29 com.taobao.text