Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support config for jackson buffer recycler pool #1192

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions serialization-jackson/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ pekko.serialization.jackson {
migrations {
}

# Controls the Buffer Recycler Pool implementation used by Jackson.
# https://javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.16.2/com/fasterxml/jackson/core/util/JsonRecyclerPools.html
# The default is "thread-local" which is the same as the default in Jackson 2.16.
buffer-recycler {
# the supported values are "thread-local", "lock-free", "shared-lock-free", "concurrent-deque",
# "shared-concurrent-deque", "bounded"
pool-instance = "thread-local"
He-Pin marked this conversation as resolved.
Show resolved Hide resolved
# the maximum size of bounded recycler pools - must be >=1 or an IllegalArgumentException will occur
# only applies to pool-instance type "bounded"
bounded-pool-size = 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bounded-pool-size = 100 , is it a recommended value in prod environments ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the Jackson default - ie the default in the Jackson code

}
}

#//#stream-read-constraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.fasterxml.jackson.core.{
StreamWriteFeature
}
import com.fasterxml.jackson.core.json.{ JsonReadFeature, JsonWriteFeature }
import com.fasterxml.jackson.core.util.{ BufferRecycler, JsonRecyclerPools, RecyclerPool }
import com.fasterxml.jackson.databind.{
DeserializationFeature,
MapperFeature,
Expand Down Expand Up @@ -103,10 +104,12 @@ object JacksonObjectMapperProvider extends ExtensionId[JacksonObjectMapperProvid
// instead of using JsonFactoryBuilder (new in Jackson 2.10.0).
factory.setStreamReadConstraints(streamReadConstraints)
factory.setStreamWriteConstraints(streamWriteConstraints)
factory.setRecyclerPool(getBufferRecyclerPool(config))
case None =>
new JsonFactoryBuilder()
.streamReadConstraints(streamReadConstraints)
.streamWriteConstraints(streamWriteConstraints)
.recyclerPool(getBufferRecyclerPool(config))
.build()
}

Expand Down Expand Up @@ -153,6 +156,18 @@ object JacksonObjectMapperProvider extends ExtensionId[JacksonObjectMapperProvid
jsonFactory
}

private def getBufferRecyclerPool(cfg: Config): RecyclerPool[BufferRecycler] = {
cfg.getString("buffer-recycler.pool-instance") match {
case "thread-local" => JsonRecyclerPools.threadLocalPool()
case "lock-free" => JsonRecyclerPools.newLockFreePool()
case "shared-lock-free" => JsonRecyclerPools.sharedLockFreePool()
case "concurrent-deque" => JsonRecyclerPools.newConcurrentDequePool()
case "shared-concurrent-deque" => JsonRecyclerPools.sharedConcurrentDequePool()
case "bounded" => JsonRecyclerPools.newBoundedPool(cfg.getInt("buffer-recycler.bounded-pool-size"))
case other => throw new IllegalArgumentException(s"Unknown recycler-pool: $other")
}
}

@nowarn("msg=deprecated")
private def configureObjectMapperFeatures(
bindingName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.pekko.serialization.jackson

import com.fasterxml.jackson.core.util.JsonRecyclerPools.BoundedPool
import com.typesafe.config.ConfigFactory
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
Expand Down Expand Up @@ -72,5 +73,29 @@ class JacksonFactorySpec extends TestKit(ActorSystem("JacksonFactorySpec"))
val streamWriteConstraints = mapper.getFactory.streamWriteConstraints()
streamWriteConstraints.getMaxNestingDepth shouldEqual maxNestingDepth
}
"support BufferRecycler (default)" in {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line.

val bindingName = "testJackson"
val jacksonConfig = JacksonObjectMapperProvider.configForBinding(bindingName, defaultConfig)
val mapper = JacksonObjectMapperProvider.createObjectMapper(
bindingName, None, objectMapperFactory, jacksonConfig, dynamicAccess, None)
val recyclerPool = mapper.getFactory._getRecyclerPool()
recyclerPool.getClass.getSimpleName shouldEqual "ThreadLocalPool"
}
"support BufferRecycler with config override" in {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

val bindingName = "testJackson"
val poolInstance = "bounded"
val boundedPoolSize = 1234
val config = ConfigFactory.parseString(
s"""pekko.serialization.jackson.buffer-recycler.pool-instance=$poolInstance
|pekko.serialization.jackson.buffer-recycler.bounded-pool-size=$boundedPoolSize
|""".stripMargin)
.withFallback(defaultConfig)
val jacksonConfig = JacksonObjectMapperProvider.configForBinding(bindingName, config)
val mapper = JacksonObjectMapperProvider.createObjectMapper(
bindingName, None, objectMapperFactory, jacksonConfig, dynamicAccess, None)
val recyclerPool = mapper.getFactory._getRecyclerPool()
recyclerPool.getClass.getSimpleName shouldEqual "BoundedPool"
recyclerPool.asInstanceOf[BoundedPool].capacity() shouldEqual boundedPoolSize
}
}
}
Loading