Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

[buffered encoder pool] Add maximum capacity check to buffers returned to pool #76

Conversation

jeromefroe
Copy link
Contributor

@jeromefroe jeromefroe commented Aug 28, 2017

This PR adds a maximum capacity check to the buffered encoder pool. When set, buffers with a capacity greater than the maximum capacity will not be returned to the pool. This PR is to address memory growth in the proxycollectors when they receive large timer batches, write them into a single buffer, and return that buffer to the pool.

cc @xichen2020

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.0e-05%) to 88.896% when pulling c023826 on jeromefroe/buffered-encoder/provide-option-to-not-return-large-buffers-to-pool into 5d71396 on master.

@@ -23,12 +23,16 @@ package msgpack
import "github.com/m3db/m3x/pool"

type bufferedEncoderPool struct {
pool pool.ObjectPool
maxBufferCapacity int
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps just maxCapacity?

@@ -42,5 +46,8 @@ func (p *bufferedEncoderPool) Get() BufferedEncoder {
}

func (p *bufferedEncoderPool) Put(encoder BufferedEncoder) {
if p.maxBufferCapacity != 0 && cap(encoder.Buffer().Bytes()) > p.maxBufferCapacity {
Copy link
Contributor

@xichen2020 xichen2020 Aug 28, 2017

Choose a reason for hiding this comment

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

Should probably do encoder.Buffer().Cap() here? .Bytes() returns the a slice whose length is the same as the number of unread bytes so that's probably not what we want.

Also nit: no need to special check p.maxBufferCapacity != 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, ok, was not aware of that distinction, will update.

Initially, I had thought of using 0 to indicate no max capacity, will just change the default to be max int so we don't have to make this check.

// Retrieve an encoder and assert it's a different encoder since
// the previous one exceeded the maximum capacity of the pool.
encoder = p.Get()
fmt.Println(encoder.Buffer().Len())
Copy link
Contributor

Choose a reason for hiding this comment

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

Debugging only?

// the previous one exceeded the maximum capacity of the pool.
encoder = p.Get()
fmt.Println(encoder.Buffer().Len())
require.Equal(t, 0, encoder.Buffer().Len())
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably better to assert encoder.Buffer().Cap() here?

@@ -42,6 +47,39 @@ const (
defaultAggregatedReaderBufferSize = 1440
)

type bufferedEncoderPoolOptions struct {
maxBufferCapacity int
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: just maxCapacity?

// BufferedEncoderPoolOptions provides options for buffered encoder pools.
type BufferedEncoderPoolOptions interface {
// SetMaxBufferCapacity sets the maximum buffer capacity.
SetMaxBufferCapacity(value int) BufferedEncoderPoolOptions
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: just SetMaxCapacity?

SetMaxBufferCapacity(value int) BufferedEncoderPoolOptions

// MaxBufferCapacity returns the maximum buffer capacity.
MaxBufferCapacity() int
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: just MaxCapacity?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 88.916% when pulling c373cff on jeromefroe/buffered-encoder/provide-option-to-not-return-large-buffers-to-pool into 5d71396 on master.

@@ -23,15 +23,15 @@ package msgpack
import "github.com/m3db/m3x/pool"

type bufferedEncoderPool struct {
maxBufferCapacity int
pool pool.ObjectPool
maxCapacity int64
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: probably int is sufficient, given that that's the return type of cap.

@@ -21,12 +21,13 @@
package msgpack

import xpool "github.com/m3db/m3x/pool"
import "math"
Copy link
Contributor

Choose a reason for hiding this comment

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

Group them into an import block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why VS Code did it like that lol

@@ -48,26 +49,26 @@ const (
)

type bufferedEncoderPoolOptions struct {
maxBufferCapacity int
poolOpts xpool.ObjectPoolOptions
maxCapacity int64
Copy link
Contributor

Choose a reason for hiding this comment

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

Can just be an int.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 88.936% when pulling 7aa4c18 on jeromefroe/buffered-encoder/provide-option-to-not-return-large-buffers-to-pool into 5d71396 on master.

Copy link
Contributor

@xichen2020 xichen2020 left a comment

Choose a reason for hiding this comment

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

LGTM w/ a nit


const (
// The maximum capacity of buffers that can be returned to the buffered
// encoder pool. A value of 0 indicates no maximum so all buffered
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the A value of 0... part?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 88.956% when pulling ebe3d2a on jeromefroe/buffered-encoder/provide-option-to-not-return-large-buffers-to-pool into 5d71396 on master.

@jeromefroe jeromefroe merged commit b39083e into master Aug 28, 2017
@jeromefroe jeromefroe deleted the jeromefroe/buffered-encoder/provide-option-to-not-return-large-buffers-to-pool branch August 28, 2017 14:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants