Skip to content

Commit 2095028

Browse files
authored
fix(bigtable/bttest): fix ModifyColumnFamilies to purge data (#4096)
- Adds TestModifyColumnFamilies to test that it works Tested vs. real bigtable.
1 parent fc6f86e commit 2095028

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

bigtable/bttest/inmem.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ func (s *server) ModifyColumnFamilies(ctx context.Context, req *btapb.ModifyColu
228228
return nil, fmt.Errorf("can't delete unknown family %q", mod.Id)
229229
}
230230
delete(tbl.families, mod.Id)
231+
232+
// Purge all data for this column family
233+
tbl.rows.Ascend(func(i btree.Item) bool {
234+
r := i.(*row)
235+
r.mu.Lock()
236+
defer r.mu.Unlock()
237+
delete(r.families, mod.Id)
238+
return true
239+
})
231240
} else if modify := mod.GetUpdate(); modify != nil {
232241
if _, ok := tbl.families[mod.Id]; !ok {
233242
return nil, fmt.Errorf("no such family %q", mod.Id)

bigtable/bttest/inmem_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,74 @@ func TestTableRowsConcurrent(t *testing.T) {
345345
}
346346
}
347347

348+
func TestModifyColumnFamilies(t *testing.T) {
349+
s := &server{
350+
tables: make(map[string]*table),
351+
}
352+
ctx := context.Background()
353+
tblInfo, err := populateTable(ctx, s)
354+
if err != nil {
355+
t.Fatal(err)
356+
}
357+
358+
readRows := func(expectChunks, expectCols, expectFams int) {
359+
t.Helper()
360+
mock := &MockReadRowsServer{}
361+
req := &btpb.ReadRowsRequest{TableName: tblInfo.Name}
362+
if err := s.ReadRows(req, mock); err != nil {
363+
t.Fatalf("ReadRows error: %v", err)
364+
}
365+
cols := map[string]bool{}
366+
fams := map[string]bool{}
367+
chunks := 0
368+
for _, r := range mock.responses {
369+
for _, c := range r.Chunks {
370+
chunks++
371+
colName := c.FamilyName.Value + "." + string(c.Qualifier.Value)
372+
cols[colName] = true
373+
fams[c.FamilyName.Value] = true
374+
}
375+
}
376+
if got, want := len(fams), expectFams; got != want {
377+
t.Errorf("col count: got %d, want %d", got, want)
378+
}
379+
if got, want := len(cols), expectCols; got != want {
380+
t.Errorf("col count: got %d, want %d", got, want)
381+
}
382+
if got, want := chunks, expectChunks; got != want {
383+
t.Errorf("chunk count: got %d, want %d", got, want)
384+
}
385+
}
386+
387+
readRows(27, 9, 3)
388+
389+
// Now drop the middle column.
390+
if _, err := s.ModifyColumnFamilies(ctx, &btapb.ModifyColumnFamiliesRequest{
391+
Name: tblInfo.Name,
392+
Modifications: []*btapb.ModifyColumnFamiliesRequest_Modification{{
393+
Id: "cf1",
394+
Mod: &btapb.ModifyColumnFamiliesRequest_Modification_Drop{Drop: true},
395+
}},
396+
}); err != nil {
397+
t.Fatalf("ModifyColumnFamilies error: %v", err)
398+
}
399+
400+
readRows(18, 6, 2)
401+
402+
// adding the column back should not re-create the data.
403+
if _, err := s.ModifyColumnFamilies(ctx, &btapb.ModifyColumnFamiliesRequest{
404+
Name: tblInfo.Name,
405+
Modifications: []*btapb.ModifyColumnFamiliesRequest_Modification{{
406+
Id: "cf1",
407+
Mod: &btapb.ModifyColumnFamiliesRequest_Modification_Create{Create: &btapb.ColumnFamily{}},
408+
}},
409+
}); err != nil {
410+
t.Fatalf("ModifyColumnFamilies error: %v", err)
411+
}
412+
413+
readRows(18, 6, 2)
414+
}
415+
348416
func TestDropRowRange(t *testing.T) {
349417
s := &server{
350418
tables: make(map[string]*table),

0 commit comments

Comments
 (0)