From 0987ffb678f834d11860a2a32e7d98a8165c2f12 Mon Sep 17 00:00:00 2001 From: Aditya Maru Date: Wed, 30 Sep 2020 09:45:41 -0400 Subject: [PATCH] backfill: clear bound account after index KV has been written Previously, we were not clearing the bound account associated with the index backfiller, once the index KVs (which it was monitoring memory for) had been written and didn't need to be referenced in the future. This change clears the bound account after each "chunk" is processed by the index backfiller. Since the bound account is only used to track the memory of the index KVs generated when processing a "chunk", we can simply Clear() it instead of doing any fine grained shrinking. Release note: None --- pkg/sql/backfill/backfill.go | 14 ++++++++++++++ pkg/sql/rowexec/indexbackfiller.go | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/sql/backfill/backfill.go b/pkg/sql/backfill/backfill.go index 8e1923e381fd..7486feec00d0 100644 --- a/pkg/sql/backfill/backfill.go +++ b/pkg/sql/backfill/backfill.go @@ -518,6 +518,12 @@ func (ib *IndexBackfiller) Close(ctx context.Context) { } } +// Clear releases the allocations on the IndexBackfiller's bound account, +// prepping it for reuse. +func (ib *IndexBackfiller) Clear(ctx context.Context) { + ib.boundAccount.Clear(ctx) +} + // initCols is a helper to populate column metadata of an IndexBackfiller. It // populates the cols and colIdxMap fields. func (ib *IndexBackfiller) initCols(desc *tabledesc.Immutable) { @@ -628,6 +634,9 @@ func (ib *IndexBackfiller) init( // BuildIndexEntriesChunk reads a chunk of rows from a table using the span sp // provided, and builds all the added indexes. +// The method accounts for the memory used by the index entries for this chunk +// using the memory monitor associated with ib. It is the callers responsibility +// to clear the associated bound account when appropriate. func (ib *IndexBackfiller) BuildIndexEntriesChunk( ctx context.Context, txn *kv.Txn, @@ -789,5 +798,10 @@ func (ib *IndexBackfiller) RunIndexBackfillChunk( return nil, ConvertBackfillError(ctx, tableDesc, batch) } + // After the chunk entries have been written, we must clear the bound account + // tracking the memory usage for the chunk. + entries = nil + ib.Clear(ctx) + return key, nil } diff --git a/pkg/sql/rowexec/indexbackfiller.go b/pkg/sql/rowexec/indexbackfiller.go index d7fcdcc8f4a8..fbbdeeb9fec3 100644 --- a/pkg/sql/rowexec/indexbackfiller.go +++ b/pkg/sql/rowexec/indexbackfiller.go @@ -186,6 +186,13 @@ func (ib *indexBackfiller) runChunk( return nil, ib.wrapDupError(ctx, err) } } + + // After the index KVs have been copied to the underlying BulkAdder, we can + // free the memory which was accounted when building the index entries of the + // current chunk. + entries = nil + ib.Clear(ctx) + if knobs.RunAfterBackfillChunk != nil { if err := ib.adder.Flush(ctx); err != nil { return nil, ib.wrapDupError(ctx, err)