Skip to content

Commit

Permalink
finagle+scrooge: Pass a factory to create TReusableBuffer as the para…
Browse files Browse the repository at this point in the history
…meter of a finagle client

Problem

Currently every instance of finagle client holds its own TReusableBuffer. The purpose of this object
is to hold an instance of temporary buffer for every io thread. A typical size of the buffer is around
16k. This means that if, for example, there are 128 i/o threads then one client uses at least two megabytes of heap.
If the app creates multiple instances of the client then the memory allocated for these buffers is just wasted
because the thread can use only one buffer at any given time and the number of threads is limited.

Solution
Pass a shared instance of TReusableBuffer to the client's ctor.

Result
Significantly decreased memory usage

JIRA Issues: GRAPH-9650

Differential Revision: https://phabricator.twitter.biz/D378466
  • Loading branch information
mbezoyan authored and jenkins committed Oct 4, 2019
1 parent 706a93d commit 42592b3
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]>
this.service = service;
this.protocolFactory = clientParam.restrictedProtocolFactory();
this.responseClassifier = clientParam.responseClassifier();
this.tlReusableBuffer = new TReusableBuffer(512, clientParam.maxThriftBufferSize());
this.tlReusableBuffer = clientParam.createThriftReusableBuffer();
}

public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]> service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]>
this.service = service;
this.protocolFactory = clientParam.restrictedProtocolFactory();
this.responseClassifier = clientParam.responseClassifier();
this.tlReusableBuffer = new TReusableBuffer(512, clientParam.maxThriftBufferSize());
this.tlReusableBuffer = clientParam.createThriftReusableBuffer();
}

public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]> service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ class GoldService$FinagleClient(
override def asClosable: _root_.com.twitter.util.Closable = service

private[this] def protocolFactory: TProtocolFactory = clientParam.restrictedProtocolFactory
private[this] def maxReusableBufferSize: Int = clientParam.maxThriftBufferSize

private[this] val tlReusableBuffer: _root_.com.twitter.scrooge.TReusableBuffer =
_root_.com.twitter.scrooge.TReusableBuffer(maxThriftBufferSize = maxReusableBufferSize)
clientParam.createThriftReusableBuffer()

protected def encodeRequest(name: String, args: _root_.com.twitter.scrooge.ThriftStruct): ThriftClientRequest = {
val memoryBuffer = tlReusableBuffer.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,8 @@ object GoldService extends _root_.com.twitter.finagle.thrift.GeneratedThriftServ
private[this] def responseClassifier: ctfs.ResponseClassifier = serverParam.responseClassifier
private[this] def stats: StatsReceiver = serverParam.serverStats
private[this] def perEndpointStats: Boolean = serverParam.perEndpointStats && !stats.isNull
private[this] def maxReusableBufferSize: Int = serverParam.maxThriftBufferSize

private[this] val tlReusableBuffer: TReusableBuffer = TReusableBuffer(maxThriftBufferSize = maxReusableBufferSize)
private[this] val tlReusableBuffer: TReusableBuffer = TReusableBuffer(maxThriftBufferSize = serverParam.maxThriftBufferSize)

private[thriftscala] def exception(name: String, seqid: Int, code: Int, message: String): Buf = {
val x = new TApplicationException(code, message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,8 @@ object PlatinumService extends _root_.com.twitter.finagle.thrift.GeneratedThrift
private[this] def responseClassifier: ctfs.ResponseClassifier = serverParam.responseClassifier
private[this] def stats: StatsReceiver = serverParam.serverStats
private[this] def perEndpointStats: Boolean = serverParam.perEndpointStats && !stats.isNull
private[this] def maxReusableBufferSize: Int = serverParam.maxThriftBufferSize

private[this] val tlReusableBuffer: TReusableBuffer = TReusableBuffer(maxThriftBufferSize = maxReusableBufferSize)
private[this] val tlReusableBuffer: TReusableBuffer = TReusableBuffer(maxThriftBufferSize = serverParam.maxThriftBufferSize)

private[thriftscala] def exception(name: String, seqid: Int, code: Int, message: String): Buf = {
val x = new TApplicationException(code, message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public class {{name}} {
this.service = service;
this.protocolFactory = clientParam.restrictedProtocolFactory();
{{^is_oneway}}this.responseClassifier = clientParam.responseClassifier();{{/is_oneway}}
this.tlReusableBuffer = new TReusableBuffer(512, clientParam.maxThriftBufferSize());
this.tlReusableBuffer = clientParam.createThriftReusableBuffer();
}

public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]> service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ class {{ServiceName}}$FinagleClient(
{{/withAsClosable}}

private[this] def protocolFactory: TProtocolFactory = clientParam.restrictedProtocolFactory
private[this] def maxReusableBufferSize: Int = clientParam.maxThriftBufferSize

private[this] val tlReusableBuffer: _root_.com.twitter.scrooge.TReusableBuffer =
_root_.com.twitter.scrooge.TReusableBuffer(maxThriftBufferSize = maxReusableBufferSize)
clientParam.createThriftReusableBuffer()

protected def encodeRequest(name: String, args: _root_.com.twitter.scrooge.ThriftStruct): ThriftClientRequest = {
val memoryBuffer = tlReusableBuffer.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,8 @@ object {{ServiceName}} {{#withFinagle}}extends _root_.com.twitter.finagle.thrift
private[this] def responseClassifier: ctfs.ResponseClassifier = serverParam.responseClassifier
private[this] def stats: StatsReceiver = serverParam.serverStats
private[this] def perEndpointStats: Boolean = serverParam.perEndpointStats && !stats.isNull
private[this] def maxReusableBufferSize: Int = serverParam.maxThriftBufferSize
private[this] val tlReusableBuffer: TReusableBuffer = TReusableBuffer(maxThriftBufferSize = maxReusableBufferSize)
private[this] val tlReusableBuffer: TReusableBuffer = TReusableBuffer(maxThriftBufferSize = serverParam.maxThriftBufferSize)
private[{{packageName}}] def exception(name: String, seqid: Int, code: Int, message: String): Buf = {
val x = new TApplicationException(code, message)
Expand Down

0 comments on commit 42592b3

Please sign in to comment.