Skip to content

Commit

Permalink
feat(bigtable): add GC policy to FamilyInfo. (#6234)
Browse files Browse the repository at this point in the history
* Add GC policy proto to FamilyInfo.

* Add PolicyType and expose GCPolicyObject.

* Add PolicyType alias to int.

* Add new TypedGCPolicy to prevent breaking changes

* Remove unused methods

* Address PR comments.

* Change variable name to Unspecified instead of Unknown.

* Fix go fmt.

* Address build failure.

* Add FullGCPolicy to admin.go.

Co-authored-by: Eric Schmidt <erschmid@google.com>
  • Loading branch information
hoangpham95 and telpirion authored Jun 22, 2022
1 parent 1b39bf4 commit eb0540d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
11 changes: 8 additions & 3 deletions bigtable/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ type TableInfo struct {

// FamilyInfo represents information about a column family.
type FamilyInfo struct {
Name string
GCPolicy string
Name string
GCPolicy string
FullGCPolicy GCPolicy
}

func (ac *AdminClient) getTable(ctx context.Context, table string, view btapb.Table_View) (*btapb.Table, error) {
Expand Down Expand Up @@ -347,7 +348,11 @@ func (ac *AdminClient) TableInfo(ctx context.Context, table string) (*TableInfo,
ti := &TableInfo{}
for name, fam := range res.ColumnFamilies {
ti.Families = append(ti.Families, name)
ti.FamilyInfos = append(ti.FamilyInfos, FamilyInfo{Name: name, GCPolicy: GCRuleToString(fam.GcRule)})
ti.FamilyInfos = append(ti.FamilyInfos, FamilyInfo{
Name: name,
GCPolicy: GCRuleToString(fam.GcRule),
FullGCPolicy: gcRuleToPolicy(fam.GcRule),
})
}
return ti, nil
}
Expand Down
44 changes: 44 additions & 0 deletions bigtable/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ import (
bttdpb "google.golang.org/genproto/googleapis/bigtable/admin/v2"
)

type policyType int

const (
policyTypeUnion policyType = iota
policyTypeIntersection
)

// A GCPolicy represents a rule that determines which cells are eligible for garbage collection.
type GCPolicy interface {
String() string
Expand Down Expand Up @@ -158,10 +165,47 @@ func GCRuleToString(rule *bttdpb.GcRule) string {
}
}

func gcRuleToPolicy(rule *bttdpb.GcRule) GCPolicy {
if rule == nil {
return NoGcPolicy()
}
switch r := rule.Rule.(type) {
case *bttdpb.GcRule_Intersection_:
return compoundRuleToPolicy(r.Intersection.Rules, policyTypeIntersection)
case *bttdpb.GcRule_Union_:
return compoundRuleToPolicy(r.Union.Rules, policyTypeUnion)
case *bttdpb.GcRule_MaxAge:
return MaxAgePolicy(time.Duration(r.MaxAge.Seconds) * time.Second)
case *bttdpb.GcRule_MaxNumVersions:
return MaxVersionsPolicy(int(r.MaxNumVersions))
default:
return NoGcPolicy()
}
}

func joinRules(rules []*bttdpb.GcRule, sep string) string {
var chunks []string
for _, r := range rules {
chunks = append(chunks, GCRuleToString(r))
}
return "(" + strings.Join(chunks, sep) + ")"
}

func compoundRuleToPolicy(rules []*bttdpb.GcRule, mode policyType) GCPolicy {
sub := []GCPolicy{}
for _, r := range rules {
p := gcRuleToPolicy(r)
if p.String() != "" {
sub = append(sub, gcRuleToPolicy(r))
}
}

switch mode {
case policyTypeUnion:
return unionPolicy{sub: sub}
case policyTypeIntersection:
return intersectionPolicy{sub: sub}
default:
return NoGcPolicy()
}
}
23 changes: 23 additions & 0 deletions bigtable/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,26 @@ func TestGcRuleToString(t *testing.T) {
}
}
}

func TestGCRuleToPolicy(t *testing.T) {
var tc = []struct {
proto *bttdpb.GcRule
want string
}{
{MaxAgePolicy(72 * time.Hour).proto(), "age() > 3d"},
{MaxVersionsPolicy(5).proto(), "versions() > 5"},
{IntersectionPolicy(MaxAgePolicy(5*time.Hour), MaxVersionsPolicy(4)).proto(),
"(age() > 5h && versions() > 4)"},
{UnionPolicy(MaxAgePolicy(5*time.Hour), MaxVersionsPolicy(4)).proto(),
"(age() > 5h || versions() > 4)"},
{IntersectionPolicy(UnionPolicy(MaxAgePolicy(5*time.Hour), MaxVersionsPolicy(4)), MaxVersionsPolicy(8)).proto(),
"((age() > 5h || versions() > 4) && versions() > 8)"},
}

for _, test := range tc {
got := gcRuleToPolicy(test.proto)
if got.String() != test.want {
t.Errorf("got gc rule %v, want: %v", got, test.want)
}
}
}

0 comments on commit eb0540d

Please sign in to comment.