Skip to content

Commit 0e6413d

Browse files
feat(bigtable): Add ignore_warnings flag to SetGcPolicy (#9372)
* feat(bigtable): Add ignore_warnings flag to SetGcPolicy * feat(bigtable): Add GcPolicyOptions * refactor(bigtable): Change options struct to interface * refactor(bigtable): Add nil checks and rename --------- Co-authored-by: Baha Aiman <bahaaiman@google.com>
1 parent 2d34bf3 commit 0e6413d

2 files changed

Lines changed: 94 additions & 4 deletions

File tree

bigtable/admin.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,23 +659,60 @@ func (ac *AdminClient) TableInfo(ctx context.Context, table string) (*TableInfo,
659659
return ti, nil
660660
}
661661

662-
// SetGCPolicy specifies which cells in a column family should be garbage collected.
663-
// GC executes opportunistically in the background; table reads may return data
664-
// matching the GC policy.
665-
func (ac *AdminClient) SetGCPolicy(ctx context.Context, table, family string, policy GCPolicy) error {
662+
type gcPolicySettings struct {
663+
ignoreWarnings bool
664+
}
665+
666+
// GCPolicyOption is the interface to change GC policy settings
667+
type GCPolicyOption interface {
668+
apply(s *gcPolicySettings)
669+
}
670+
671+
type ignoreWarnings bool
672+
673+
func (w ignoreWarnings) apply(s *gcPolicySettings) {
674+
s.ignoreWarnings = bool(w)
675+
}
676+
677+
// IgnoreWarnings returns a gcPolicyOption that ignores safety checks when modifying the column families
678+
func IgnoreWarnings() GCPolicyOption {
679+
return ignoreWarnings(true)
680+
}
681+
682+
func (ac *AdminClient) setGCPolicy(ctx context.Context, table, family string, policy GCPolicy, opts ...GCPolicyOption) error {
666683
ctx = mergeOutgoingMetadata(ctx, ac.md)
667684
prefix := ac.instancePrefix()
685+
686+
s := gcPolicySettings{}
687+
for _, opt := range opts {
688+
if opt != nil {
689+
opt.apply(&s)
690+
}
691+
}
668692
req := &btapb.ModifyColumnFamiliesRequest{
669693
Name: prefix + "/tables/" + table,
670694
Modifications: []*btapb.ModifyColumnFamiliesRequest_Modification{{
671695
Id: family,
672696
Mod: &btapb.ModifyColumnFamiliesRequest_Modification_Update{Update: &btapb.ColumnFamily{GcRule: policy.proto()}},
673697
}},
698+
IgnoreWarnings: s.ignoreWarnings,
674699
}
675700
_, err := ac.tClient.ModifyColumnFamilies(ctx, req)
676701
return err
677702
}
678703

704+
// SetGCPolicy specifies which cells in a column family should be garbage collected.
705+
// GC executes opportunistically in the background; table reads may return data
706+
// matching the GC policy.
707+
func (ac *AdminClient) SetGCPolicy(ctx context.Context, table, family string, policy GCPolicy) error {
708+
return ac.SetGCPolicyWithOptions(ctx, table, family, policy)
709+
}
710+
711+
// SetGCPolicyWithOptions is similar to SetGCPolicy but allows passing options
712+
func (ac *AdminClient) SetGCPolicyWithOptions(ctx context.Context, table, family string, policy GCPolicy, opts ...GCPolicyOption) error {
713+
return ac.setGCPolicy(ctx, table, family, policy, opts...)
714+
}
715+
679716
// DropRowRange permanently deletes a row range from the specified table.
680717
func (ac *AdminClient) DropRowRange(ctx context.Context, table, rowKeyPrefix string) error {
681718
ctx = mergeOutgoingMetadata(ctx, ac.md)

bigtable/admin_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type mockTableAdminClock struct {
4343
copyBackupReq *btapb.CopyBackupRequest
4444
copyBackupError error
4545

46+
modColumnReq *btapb.ModifyColumnFamiliesRequest
47+
4648
createAuthorizedViewReq *btapb.CreateAuthorizedViewRequest
4749
createAuthorizedViewError error
4850
updateAuthorizedViewReq *btapb.UpdateAuthorizedViewRequest
@@ -76,6 +78,12 @@ func (c *mockTableAdminClock) CopyBackup(
7678
return nil, c.copyBackupError
7779
}
7880

81+
func (c *mockTableAdminClock) ModifyColumnFamilies(
82+
ctx context.Context, in *btapb.ModifyColumnFamiliesRequest, opts ...grpc.CallOption) (*btapb.Table, error) {
83+
c.modColumnReq = in
84+
return nil, nil
85+
}
86+
7987
func (c *mockTableAdminClock) CreateAuthorizedView(
8088
ctx context.Context, in *btapb.CreateAuthorizedViewRequest, opts ...grpc.CallOption,
8189
) (*longrunning.Operation, error) {
@@ -357,6 +365,51 @@ func TestTableAdmin_UpdateTableDisableChangeStream(t *testing.T) {
357365
}
358366
}
359367

368+
func TestTableAdmin_SetGcPolicy(t *testing.T) {
369+
for _, test := range []struct {
370+
desc string
371+
opts GCPolicyOption
372+
want bool
373+
}{
374+
{
375+
desc: "IgnoreWarnings: false",
376+
want: false,
377+
},
378+
{
379+
desc: "IgnoreWarnings: true",
380+
opts: IgnoreWarnings(),
381+
want: true,
382+
},
383+
} {
384+
385+
mock := &mockTableAdminClock{}
386+
c := setupTableClient(t, mock)
387+
388+
err := c.SetGCPolicyWithOptions(context.Background(), "My-table", "cf1", NoGcPolicy(), test.opts)
389+
if err != nil {
390+
t.Fatalf("%v: Failed to set GC Policy: %v", test.desc, err)
391+
}
392+
393+
modColumnReq := mock.modColumnReq
394+
if modColumnReq.IgnoreWarnings != test.want {
395+
t.Errorf("%v: IgnoreWarnings got: %v, want: %v", test.desc, modColumnReq.IgnoreWarnings, test.want)
396+
}
397+
}
398+
399+
mock := &mockTableAdminClock{}
400+
c := setupTableClient(t, mock)
401+
402+
err := c.SetGCPolicy(context.Background(), "My-table", "cf1", NoGcPolicy())
403+
if err != nil {
404+
t.Fatalf("SetGCPolicy: Failed to set GC Policy: %v", err)
405+
}
406+
407+
modColumnReq := mock.modColumnReq
408+
if modColumnReq.IgnoreWarnings {
409+
t.Errorf("SetGCPolicy: IgnoreWarnings should be set to false")
410+
}
411+
}
412+
360413
func TestTableAdmin_CreateAuthorizedView_DeletionProtection_Protected(t *testing.T) {
361414
mock := &mockTableAdminClock{}
362415
c := setupTableClient(t, mock)

0 commit comments

Comments
 (0)