Skip to content

Commit

Permalink
Reduce memory usage during Alter execution (#9301)
Browse files Browse the repository at this point in the history
We currently use y.Throttle type to ensure that we do not concurrently
build indexes for many predicates at the same time to avoid opening too
many files on disk. This type allocates large memory initially to
achieve this. By default, on my machine we allocates 2GB of memory
every time Alter is called. Now, Go is slow to release this memory back
to OS increasing the memory usage.

This PR sets the max to 1024 predicates at a time. It would be lower if
Alter request involves fewer predicates in the request
  • Loading branch information
mangalaman93 authored Feb 6, 2025
1 parent e87d3ee commit 6380120
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions worker/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,15 @@ func runSchemaMutation(ctx context.Context, updates []*pb.SchemaUpdate, startTs
var wg sync.WaitGroup
wg.Add(1)
defer wg.Done()
// This throttle allows is used to limit the number of files which are opened simultaneously
// by badger while building indexes for predicates in background.
maxOpenFileLimit, err := x.QueryMaxOpenFiles()
if err != nil {
// Setting to default value on unix systems
maxOpenFileLimit = 1024
}
glog.Infof("Max open files limit: %d", maxOpenFileLimit)

// Badger opens around 8 files for indexing per predicate.
// The throttle limit is set to maxOpenFileLimit/8 to ensure that indexing does not throw
// "Too many open files" error.
throttle := y.NewThrottle(maxOpenFileLimit / 8)
// The throttle limit is set to 1024 to ensure that indexing
// does not throw "Too many open files" error.
maxPredicateAtATime := 1024
if len(updates) < maxPredicateAtATime {
maxPredicateAtATime = len(updates)
}
throttle := y.NewThrottle(maxPredicateAtATime)

buildIndexes := func(update *pb.SchemaUpdate, rebuild posting.IndexRebuild, c *z.Closer) {
// In case background indexing is running, we should call it here again.
Expand Down Expand Up @@ -237,6 +234,7 @@ func runSchemaMutation(ctx context.Context, updates []*pb.SchemaUpdate, startTs

// Start opIndexing task only if schema update needs to build the indexes.
if shouldRebuild && !gr.Node.isRunningTask(opIndexing) {
var err error
closer, err = gr.Node.startTaskAtTs(opIndexing, startTs)
if err != nil {
return err
Expand Down

0 comments on commit 6380120

Please sign in to comment.