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

feat(bigtable): Add ignore_warnings flag to SetGcPolicy #9372

Merged
merged 13 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions bigtable/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,23 +547,60 @@ func (ac *AdminClient) TableInfo(ctx context.Context, table string) (*TableInfo,
return ti, nil
}

// SetGCPolicy specifies which cells in a column family should be garbage collected.
// GC executes opportunistically in the background; table reads may return data
// matching the GC policy.
func (ac *AdminClient) SetGCPolicy(ctx context.Context, table, family string, policy GCPolicy) error {
type gcPolicySettings struct {
ignoreWarnings bool
}

// GCPolicyOption is the interface to change GC policy settings
type GCPolicyOption interface {
apply(s *gcPolicySettings)
}

type warnings struct{ ignore bool }

func (w warnings) apply(s *gcPolicySettings) {
s.ignoreWarnings = w.ignore
}
bhshkh marked this conversation as resolved.
Show resolved Hide resolved

// IgnoreWarnings returns a gcPolicyOption that ignores safety checks when modifying the column families
func IgnoreWarnings() GCPolicyOption {
return warnings{ignore: true}
}

func (ac *AdminClient) setGCPolicy(ctx context.Context, table, family string, policy GCPolicy, opts ...GCPolicyOption) error {
ctx = mergeOutgoingMetadata(ctx, ac.md)
prefix := ac.instancePrefix()

s := gcPolicySettings{}
for _, opt := range opts {
if opt != nil {
opt.apply(&s)
}
}
req := &btapb.ModifyColumnFamiliesRequest{
Name: prefix + "/tables/" + table,
Modifications: []*btapb.ModifyColumnFamiliesRequest_Modification{{
Id: family,
Mod: &btapb.ModifyColumnFamiliesRequest_Modification_Update{Update: &btapb.ColumnFamily{GcRule: policy.proto()}},
}},
IgnoreWarnings: s.ignoreWarnings,
}
_, err := ac.tClient.ModifyColumnFamilies(ctx, req)
return err
}

// SetGCPolicy specifies which cells in a column family should be garbage collected.
// GC executes opportunistically in the background; table reads may return data
// matching the GC policy.
func (ac *AdminClient) SetGCPolicy(ctx context.Context, table, family string, policy GCPolicy) error {
return ac.SetGCPolicyWithOptions(ctx, table, family, policy)
}

// SetGCPolicyWithOptions is similar to SetGCPolicy but allows passing options
func (ac *AdminClient) SetGCPolicyWithOptions(ctx context.Context, table, family string, policy GCPolicy, opts ...GCPolicyOption) error {
return ac.setGCPolicy(ctx, table, family, policy, opts...)
}

bhshkh marked this conversation as resolved.
Show resolved Hide resolved
// DropRowRange permanently deletes a row range from the specified table.
func (ac *AdminClient) DropRowRange(ctx context.Context, table, rowKeyPrefix string) error {
ctx = mergeOutgoingMetadata(ctx, ac.md)
Expand Down
53 changes: 53 additions & 0 deletions bigtable/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type mockTableAdminClock struct {
copyBackupReq *btapb.CopyBackupRequest
copyBackupError error

modColumnReq *btapb.ModifyColumnFamiliesRequest

createAuthorizedViewReq *btapb.CreateAuthorizedViewRequest
createAuthorizedViewError error
updateAuthorizedViewReq *btapb.UpdateAuthorizedViewRequest
Expand Down Expand Up @@ -75,6 +77,12 @@ func (c *mockTableAdminClock) CopyBackup(
return nil, c.copyBackupError
}

func (c *mockTableAdminClock) ModifyColumnFamilies(
ctx context.Context, in *btapb.ModifyColumnFamiliesRequest, opts ...grpc.CallOption) (*btapb.Table, error) {
c.modColumnReq = in
return nil, nil
}

func (c *mockTableAdminClock) CreateAuthorizedView(
ctx context.Context, in *btapb.CreateAuthorizedViewRequest, opts ...grpc.CallOption,
) (*longrunning.Operation, error) {
Expand Down Expand Up @@ -306,6 +314,51 @@ func TestTableAdmin_UpdateTableDisableChangeStream(t *testing.T) {
}
}

func TestTableAdmin_SetGcPolicy(t *testing.T) {
for _, test := range []struct {
desc string
opts GCPolicyOption
want bool
}{
{
desc: "IgnoreWarnings: false",
want: false,
},
{
desc: "IgnoreWarnings: true",
opts: IgnoreWarnings(),
want: true,
},
} {

mock := &mockTableAdminClock{}
c := setupTableClient(t, mock)

err := c.SetGCPolicyWithOptions(context.Background(), "My-table", "cf1", NoGcPolicy(), test.opts)
if err != nil {
t.Fatalf("%v: Failed to set GC Policy: %v", test.desc, err)
}

modColumnReq := mock.modColumnReq
if modColumnReq.IgnoreWarnings != test.want {
t.Errorf("%v: IgnoreWarnings got: %v, want: %v", test.desc, modColumnReq.IgnoreWarnings, test.want)
}
}

mock := &mockTableAdminClock{}
c := setupTableClient(t, mock)

err := c.SetGCPolicy(context.Background(), "My-table", "cf1", NoGcPolicy())
if err != nil {
t.Fatalf("SetGCPolicy: Failed to set GC Policy: %v", err)
}

modColumnReq := mock.modColumnReq
if modColumnReq.IgnoreWarnings {
t.Errorf("SetGCPolicy: IgnoreWarnings should be set to false")
}
}

func TestTableAdmin_CreateAuthorizedView_DeletionProtection_Protected(t *testing.T) {
mock := &mockTableAdminClock{}
c := setupTableClient(t, mock)
Expand Down
Loading