Skip to content

Commit

Permalink
Merge pull request #7151 from planetscale/table-gc-skip-unspecified-s…
Browse files Browse the repository at this point in the history
…tates

Table lifecycle: skip time hint for unspecified states
  • Loading branch information
shlomi-noach authored Jan 4, 2021
2 parents a7c3dfa + 33325cb commit bf57b8c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
33 changes: 26 additions & 7 deletions go/vt/vttablet/tabletserver/gc/tablegc.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,28 @@ func (collector *TableGC) submitTransitionRequest(ctx context.Context, fromState
}()
}

// shouldTransitionTable checks if the given table is a GC table and if it's time to transition it to next state
func (collector *TableGC) shouldTransitionTable(tableName string) (shouldTransition bool, state schema.TableGCState, err error) {
isGCTable, state, t, err := schema.AnalyzeGCTableName(tableName)
if err != nil {
return false, state, err
}
if !isGCTable {
// irrelevant table
return false, state, nil
}
if _, ok := collector.lifecycleStates[state]; ok {
// this state is in our expected lifecycle. Let's check table's time hint:
timeNow := time.Now().UTC()
if timeNow.Before(t) {
// not yet time to operate on this table
return false, state, nil
}
// If the state is not in our expected lifecycle, we ignore the time hint and just move it to the next phase
}
return true, state, nil
}

// checkTables looks for potential GC tables in the MySQL server+schema.
// It lists _vt_% tables, then filters through those which are due-date.
// It then applies the necessary operation per table.
Expand All @@ -350,20 +372,17 @@ func (collector *TableGC) checkTables(ctx context.Context) error {
for _, row := range res.Rows {
tableName := row[0].ToString()

isGCTable, state, t, err := schema.AnalyzeGCTableName(tableName)
shouldTransition, state, err := collector.shouldTransitionTable(tableName)

if err != nil {
log.Errorf("TableGC: error while checking tables: %+v", err)
continue
}
timeNow := time.Now().UTC()
if !isGCTable {
if !shouldTransition {
// irrelevant table
continue
}
if timeNow.Before(t) {
// net yet time to operate on this table
continue
}

log.Infof("TableGC: will operate on table %s", tableName)

if state == schema.HoldTableGCState {
Expand Down
61 changes: 61 additions & 0 deletions go/vt/vttablet/tabletserver/gc/tablegc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,64 @@ func TestNextState(t *testing.T) {
assert.Nil(t, postDrop)
}
}

func TestShouldTransitionTable(t *testing.T) {
tt := []struct {
table string
state schema.TableGCState
shouldTransition bool
isError bool
}{
{
table: "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
state: schema.PurgeTableGCState,
shouldTransition: true,
},
{
table: "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
state: schema.PurgeTableGCState,
shouldTransition: false,
},
{
table: "_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
state: schema.DropTableGCState,
shouldTransition: false,
},
{
table: "_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20090915120410",
state: schema.DropTableGCState,
shouldTransition: true,
},
{
table: "_vt_EVAC_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
state: schema.EvacTableGCState,
shouldTransition: false,
},
{
table: "_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
state: schema.HoldTableGCState,
shouldTransition: true,
},
{
table: "_vt_SOMETHING_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
state: "",
shouldTransition: false,
},
}
lifecycleStates, err := schema.ParseGCLifecycle("purge,evac,drop")
assert.NoError(t, err)
collector := &TableGC{
lifecycleStates: lifecycleStates,
}
for _, ts := range tt {
shouldTransition, state, err := collector.shouldTransitionTable(ts.table)
if ts.isError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, ts.shouldTransition, shouldTransition)
assert.Equal(t, ts.state, state)

}
}
}

0 comments on commit bf57b8c

Please sign in to comment.