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

Adds rideshare memory leak #2641

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions examples/golang-push/rideshare/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ services:
environment:
- REGION=us-east
- PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040
- PARAMETERS_POOL_SIZE=1000
- PARAMETERS_POOL_BUFFER_SIZE_KB=1000
- PARAMETERS_POOL_SIZE=10000
- PARAMETERS_POOL_BUFFER_SIZE_KB=10000
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

Adjusting the sizes here will work when running locally, but in order to get these to reflect in sedemo, you'll need to update them here. There's additional knobs to tune there as well. The memory limit is 64Mi per pod, so using sizes this big would OOM those pods right away.

Of course, if this is just intended to be shown with a local version of the rideshare demo then adjusting these settings will but 👌

build:
context: .

Expand Down
50 changes: 26 additions & 24 deletions examples/golang-push/rideshare/utility/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package utility
import (
"os"
"sync"

"rideshare/rideshare"
)

type workerPool struct {
poolLock *sync.Mutex
pool []chan struct{}
limit int
bufferSize int
poolLock *sync.Mutex
pool []chan struct{}
retainedData [][]byte // Slice to retain data to simulate memory leak.
limit int
}

// Run a function using a pool.
Expand All @@ -30,15 +28,18 @@ func (c *workerPool) Run(fn func()) {
c.poolLock.Lock()
size := len(c.pool)
if c.limit != 0 && size >= c.limit {
// We're at max pool limit, reset the pool.
c.resetWithoutLock()
// We're at max pool limit, release a resource.
last := c.pool[size-1]
last <- struct{}{}
close(last)
c.pool = c.pool[:size-1]
}
c.pool = append(c.pool, stop)
c.poolLock.Unlock()

// Create a goroutine to run the function. It will write to done when the
// work is over, but won't clean up until it receives a signal from stop.
go c.doWork(fn, stop, done)
go cacheVehicleLocations(fn, stop, done, c)

// Block until the worker signals it's done.
<-done
Expand All @@ -50,30 +51,31 @@ func (c *workerPool) Close() {
c.poolLock.Lock()
defer c.poolLock.Unlock()

c.resetWithoutLock()
}

func (c *workerPool) resetWithoutLock() {
for _, c := range c.pool {
c <- struct{}{}
close(c)
}
c.pool = c.pool[:0]
c.pool = nil // Fix here to properly clean up the slice.
c.retainedData = nil // Ensure that we also clear the retained data.
}

func (c *workerPool) doWork(fn func(), stop <-chan struct{}, done chan<- struct{}) {
func cacheVehicleLocations(fn func(), stop <-chan struct{}, done chan<- struct{}, pool *workerPool) {
buf := make([]byte, 0)

// Do work.
fn()

// Simulate the work in fn requiring some data to be added to a buffer.
for i := 0; i < c.bufferSize; i++ {
buf = append(buf, byte(i))
// Increase buffer size to leak more memory.
const mb = 1 << 20
for i := 0; i < 1*mb; i++ {
buf = append(buf, byte(i%256))
}

// Don't let the compiler optimize away the buf.
var _ = buf
// Retain the buffer in the pool to prevent it from being garbage collected.
pool.poolLock.Lock()
pool.retainedData = append(pool.retainedData, buf)
pool.poolLock.Unlock()

// Signal we're done working.
done <- struct{}{}
Expand All @@ -82,11 +84,11 @@ func (c *workerPool) doWork(fn func(), stop <-chan struct{}, done chan<- struct{
<-stop
}

func newPool(c rideshare.Config) *workerPool {
func newPool(n int) *workerPool {
return &workerPool{
poolLock: &sync.Mutex{},
pool: make([]chan struct{}, 0, c.ParametersPoolSize),
limit: c.ParametersPoolSize,
bufferSize: c.ParametersPoolBufferSize,
poolLock: &sync.Mutex{},
pool: make([]chan struct{}, 0, n),
retainedData: make([][]byte, 0), // Initialize the slice to retain data.
limit: n,
}
}
5 changes: 3 additions & 2 deletions examples/golang-push/rideshare/utility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const durationConstant = time.Duration(200 * time.Millisecond)

var pool *workerPool

// InitWorkPool initializes the worker pool and returns a clean up function.
// InitWorkerPool initializes the worker pool and returns a clean up function.
func InitWorkerPool(c rideshare.Config) func() {
pool = newPool(c)
// Use the ParametersPoolSize from the config to initialize the pool.
pool = newPool(c.ParametersPoolSize)
return pool.Close
}

Expand Down
Loading