Skip to content

Commit

Permalink
aggr: reduce allocations for encoding batches (#392)
Browse files Browse the repository at this point in the history
Avoid creating a copy of the payload array when creating
the ByteString object. Also uses a dedicated pool for
encoding the payload.
  • Loading branch information
brharrington authored Feb 22, 2023
1 parent f4a266c commit c256123
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ class AggrConfig(
override def validTagCharacters(): String = null

override def publisher(): Publisher = {
new AkkaPublisher(this, system)
new AkkaPublisher(registry, this, system)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Keep
import akka.stream.scaladsl.RestartFlow
import akka.stream.scaladsl.Sink
import akka.util.ByteString
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.StreamReadFeature
import com.fasterxml.jackson.core.StreamWriteFeature
import com.fasterxml.jackson.dataformat.smile.SmileFactory
import com.netflix.atlas.akka.AccessLogger
import com.netflix.atlas.akka.CustomMediaTypes
import com.netflix.atlas.akka.StreamOps
import com.netflix.atlas.akka.ThreadPools
import com.netflix.spectator.api.Measurement
import com.netflix.spectator.api.Registry
import com.netflix.spectator.atlas.Publisher
import com.netflix.spectator.atlas.impl.EvalPayload
import com.netflix.spectator.atlas.impl.PublishPayload
Expand All @@ -60,21 +63,23 @@ import scala.util.Using
* URLConnection class built into the JDK. This helps reduce the number of threads
* needed overall.
*/
class AkkaPublisher(config: AggrConfig, implicit val system: ActorSystem) extends Publisher {
class AkkaPublisher(registry: Registry, config: AggrConfig, implicit val system: ActorSystem)
extends Publisher {

import AkkaPublisher._

private implicit val ec: ExecutionContext = system.dispatcher

private val atlasUri = Uri(config.uri())
private val evalUri = Uri(config.evalUri())

private val encodingParallelism = math.max(2, Runtime.getRuntime.availableProcessors() / 2)

private val ec: ExecutionContext =
ThreadPools.fixedSize(registry, "PublishEncoding", encodingParallelism)

private val client = {
val flow = Flow[RequestTuple]
.mapAsync(encodingParallelism) { t =>
Future(t.mkRequest() -> t)
Future(t.mkRequest() -> t)(ec)
}
.via(Http().superPool[RequestTuple]())
.map {
Expand Down Expand Up @@ -168,7 +173,7 @@ object AkkaPublisher {
}

/** Encode publish payload to a byte array. */
private def encode(payload: AnyRef): Array[Byte] = {
private def encode(payload: AnyRef): ByteString = {
val baos = getOrCreateStream
Using.resource(new GzipLevelOutputStream(baos)) { out =>
Using.resource(factory.createGenerator(out)) { gen =>
Expand All @@ -180,7 +185,7 @@ object AkkaPublisher {
}
}
}
baos.toByteArray
ByteString.fromArrayUnsafe(baos.toByteArray)
}

private def encode(gen: JsonGenerator, payload: PublishPayload): Unit = {
Expand Down

0 comments on commit c256123

Please sign in to comment.