Reuse MappingJsonFactory across all sessions 43/86143/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 13:35:27 +0000 (14:35 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 17:41:38 +0000 (18:41 +0100)
Heap dump analysis shows we are keeping a per-connection
MappingJsonFactory, which also means we have an ObjectMapper
associated with each connection.

Both MappingJsonFactory and ObjectMapper are documented as
thread-safe and noted to be best shared. Make sure we use a single
instance across all connections.

Change-Id: Ic13ea538eecba9a08d4dba9ac617f2f29abeaba7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
library/impl/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcDecoder.java

index 3e82761821c7dab92c839fadd1492003a760560c..6becb384e228769cdc1fa452b9c328f38f95ecba 100644 (file)
@@ -38,12 +38,12 @@ import org.slf4j.LoggerFactory;
  * in the stream.
  */
 public class JsonRpcDecoder extends ByteToMessageDecoder {
-
     private static final Logger LOG = LoggerFactory.getLogger(JsonRpcDecoder.class);
+    private static final JsonFactory JSON_FACTORY = new MappingJsonFactory();
+
     private final int maxFrameLength;
     //Indicates if the frame limit warning was issued
     private boolean maxFrameLimitWasReached = false;
-    private final JsonFactory jacksonJsonFactory = new MappingJsonFactory();
 
     private final IOContext jacksonIOContext = new IOContext(new BufferRecycler(), null, false);
 
@@ -55,15 +55,15 @@ public class JsonRpcDecoder extends ByteToMessageDecoder {
 
     private int recordsRead;
 
-    public JsonRpcDecoder(int maxFrameLength) {
+    public JsonRpcDecoder(final int maxFrameLength) {
         this.maxFrameLength = maxFrameLength;
     }
 
     @Override
-    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
-
-        LOG.trace("readable bytes {}, records read {}, incomplete record bytes {}",
-                buf.readableBytes(), recordsRead, lastRecordBytes);
+    protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out)
+            throws IOException {
+        LOG.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
+            recordsRead, lastRecordBytes);
 
         if (lastRecordBytes == 0) {
             if (buf.readableBytes() < 4) {
@@ -106,7 +106,7 @@ public class JsonRpcDecoder extends ByteToMessageDecoder {
 
             if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
                 ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex());
-                JsonParser jp = jacksonJsonFactory.createParser((InputStream) new ByteBufInputStream(slice));
+                JsonParser jp = JSON_FACTORY.createParser((InputStream) new ByteBufInputStream(slice));
                 JsonNode root = jp.readValueAsTree();
                 out.add(root);
                 leftCurlies = 0;
@@ -142,7 +142,7 @@ public class JsonRpcDecoder extends ByteToMessageDecoder {
         return recordsRead;
     }
 
-    private static void skipSpaces(ByteBuf byteBuf) throws IOException {
+    private static void skipSpaces(final ByteBuf byteBuf) throws IOException {
         while (byteBuf.isReadable()) {
             int ch = byteBuf.getByte(byteBuf.readerIndex()) & 0xFF;
             if (!(ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) {