From b90537d562067dc1090456218901cfe3f6383246 Mon Sep 17 00:00:00 2001 From: Phil Pearl Date: Wed, 14 Apr 2021 09:53:01 +0100 Subject: [PATCH] fix(bigtable/bttest): make table gc release memory We had a problem where we were unable to run the emulator for extended periods in dev environments without giving it huge amounts of RAM, even if we had quite aggressive GC policies applied to the tables. This fixes that by making GC delete empty rows and columns. --- bigtable/bttest/inmem.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/bigtable/bttest/inmem.go b/bigtable/bttest/inmem.go index 56e6d762ba85..71087b8180ba 100644 --- a/bigtable/bttest/inmem.go +++ b/bigtable/bttest/inmem.go @@ -1242,13 +1242,23 @@ func (t *table) gc() { return } + // It isn't clear whether it's safe to delete within the iterator, so we do + // not + var toDelete []btree.Item t.rows.Ascend(func(i btree.Item) bool { r := i.(*row) r.mu.Lock() r.gc(rules) + if len(r.families) == 0 { + toDelete = append(toDelete, i) + } r.mu.Unlock() return true }) + + for _, i := range toDelete { + t.rows.Delete(i) + } } type byRowKey []*row @@ -1334,7 +1344,15 @@ func (r *row) gc(rules map[string]*btapb.GcRule) { continue } for col, cs := range fam.cells { - r.families[fam.name].cells[col] = applyGC(cs, rule) + cs = applyGC(cs, rule) + if len(cs) == 0 { + delete(fam.cells, col) + } else { + fam.cells[col] = cs + } + } + if len(fam.cells) == 0 { + delete(r.families, fam.name) } } }