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

fix(bigtable/bttest): make table gc release memory #3930

Merged
merged 14 commits into from
Feb 15, 2024
40 changes: 38 additions & 2 deletions bigtable/bttest/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,27 @@ func (t *table) mutableRow(key string) *row {
}

func (t *table) gc() {
toDelete := t.gcReadOnly()
if len(toDelete) == 0 {
return
}

// We delete rows that no longer have any cells
t.mu.Lock()
defer t.mu.Unlock()
for _, i := range toDelete {
r := i.(*row)
// Make sure the row still has no cells. We've not been holding a lock
// so it could have changed since we checked it.
r.mu.Lock()
if len(r.families) == 0 {
t.rows.Delete(i)
}
r.mu.Unlock()
}
}

func (t *table) gcReadOnly() (toDelete []btree.Item) {
// This method doesn't add or remove rows, so we only need a read lock for the table.
t.mu.RLock()
defer t.mu.RUnlock()
Expand All @@ -1239,16 +1260,23 @@ func (t *table) gc() {
}
}
if len(rules) == 0 {
return
return nil
}

// It isn't clear whether it's safe to delete within the iterator, so we do
// not
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
})

return toDelete
}

type byRowKey []*row
Expand Down Expand Up @@ -1334,7 +1362,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)
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
} else {
fam.cells[col] = cs
}
}
if len(fam.cells) == 0 {
delete(r.families, fam.name)
}
}
}
Expand Down