Skip to content

Commit 3f3df87

Browse files
committed
RONDB-969: Change RDRS to prevent excessive malloc ops. Simple peak-tracking i.e. keep the max buffers allocated for peak load
1 parent 4002ead commit 3f3df87

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

storage/ndb/rest-server/rest-api-server/internal/dal/heap/heap.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
33
* This file is part of the RonDB REST API Server
4-
* Copyright (c) 2023 Hopsworks AB
4+
* Copyright (c) 2025 Hopsworks AB
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
@@ -37,7 +37,6 @@ type Heap struct {
3737
buffers []*NativeBuffer
3838
buffersStats MemoryStats
3939
mutex *sync.Mutex
40-
minAllocatedBuffers int64
4140
allocatedBuffers int64
4241
maxAllocatedBuffers int64
4342
}
@@ -68,7 +67,6 @@ func New() (heap *Heap, releaseBuffers func(), err error) {
6867
heap = &Heap{
6968
buffers: []*NativeBuffer{},
7069
mutex: &sync.Mutex{},
71-
minAllocatedBuffers: preAllocatedBuffers,
7270
allocatedBuffers: preAllocatedBuffers,
7371
maxAllocatedBuffers: maxAllocatedBuffers,
7472
buffersStats: MemoryStats{
@@ -101,9 +99,10 @@ func (heap *Heap) releaseAllBuffers() {
10199
heap.mutex.Lock()
102100
defer heap.mutex.Unlock()
103101

104-
if heap.buffersStats.BuffersCount != int64(len(heap.buffers)) {
102+
stats := heap.getNativeBuffersStatsInt()
103+
if stats.BuffersCount != int64(len(heap.buffers)) {
105104
log.Warnf("Shutting down heap. Number of free buffers do not match. Expecting: %d, Got: %d.",
106-
heap.buffersStats.BuffersCount, int64(len(heap.buffers)))
105+
stats.BuffersCount, int64(len(heap.buffers)))
107106
}
108107

109108
for _, buffer := range heap.buffers {
@@ -136,21 +135,19 @@ func (heap *Heap) GetBuffer() (buff *NativeBuffer, returnBuff func(), err error)
136135
func (heap *Heap) returnBuffer(buffer *NativeBuffer) {
137136
heap.mutex.Lock()
138137
defer heap.mutex.Unlock()
139-
140-
if int64(len(heap.buffers)) < heap.minAllocatedBuffers {
141-
heap.buffers = append(heap.buffers, buffer)
142-
} else {
143-
C.free(buffer.Buffer)
144-
heap.allocatedBuffers--
145-
heap.buffersStats.DeallocationsCount++
146-
}
138+
heap.buffers = append(heap.buffers, buffer)
147139
}
148140

149141
func (heap *Heap) GetNativeBuffersStats() MemoryStats {
150142
heap.mutex.Lock()
151143
defer heap.mutex.Unlock()
144+
return heap.getNativeBuffersStatsInt()
145+
}
152146

153-
// Only (De)AllocationsCount are updated continuously. Update the others now.
147+
/*
148+
* with out locks
149+
*/
150+
func (heap *Heap) getNativeBuffersStatsInt() MemoryStats {
154151
heap.buffersStats.BuffersCount = heap.allocatedBuffers
155152
heap.buffersStats.FreeBuffers = int64(len(heap.buffers))
156153
return heap.buffersStats

storage/ndb/rest-server/rest-api-server/internal/dal/heap/heap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestHeap(t *testing.T) {
8484
// Note that we now have more FreeBuffers than initially
8585
// TODO: Figure out whether we want this to happen
8686
stats = heap.GetNativeBuffersStats()
87-
if stats.FreeBuffers != int64(conf.Internal.PreAllocatedBuffers) {
87+
if stats.FreeBuffers != allocations {
8888
t.Fatalf("Number of free buffers does not match. Expecting: %d, Got: %d",
8989
allocations, stats.FreeBuffers)
9090
}

0 commit comments

Comments
 (0)