Skip to content

Commit

Permalink
DDL strategy: support --retain-artifacts flag
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach committed Sep 19, 2023
1 parent 24a90a2 commit ad4a4c6
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
37 changes: 36 additions & 1 deletion go/vt/schema/ddl_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
var (
strategyParserRegexp = regexp.MustCompile(`^([\S]+)\s+(.*)$`)
cutOverThresholdFlagRegexp = regexp.MustCompile(fmt.Sprintf(`^[-]{1,2}%s=(.*?)$`, cutOverThresholdFlag))
retainArtifactsFlagRegexp = regexp.MustCompile(fmt.Sprintf(`^[-]{1,2}%s=(.*?)$`, retainArtifactsFlag))
)

const (
Expand All @@ -43,6 +44,7 @@ const (
preferInstantDDL = "prefer-instant-ddl"
fastRangeRotationFlag = "fast-range-rotation"
cutOverThresholdFlag = "cut-over-threshold"
retainArtifactsFlag = "retain-artifacts"
vreplicationTestSuite = "vreplication-test-suite"
allowForeignKeysFlag = "unsafe-allow-foreign-keys"
analyzeTableFlag = "analyze-table"
Expand Down Expand Up @@ -110,6 +112,9 @@ func ParseDDLStrategy(strategyVariable string) (*DDLStrategySetting, error) {
if _, err := setting.CutOverThreshold(); err != nil {
return nil, err
}
if _, err := setting.RetainArtifactsDuration(); err != nil {
return nil, err
}
return setting, nil
}

Expand Down Expand Up @@ -194,7 +199,16 @@ func isCutOverThresholdFlag(opt string) (string, bool) {
return submatch[1], true
}

// CutOverThreshold returns a list of shards specified in '--shards=...', or an empty slice if unspecified
// isRetainArtifactsFlag returns true when given option denotes a `--retain-artifacts=[...]` flag
func isRetainArtifactsFlag(opt string) (string, bool) {
submatch := retainArtifactsFlagRegexp.FindStringSubmatch(opt)
if len(submatch) == 0 {
return "", false
}
return submatch[1], true
}

// CutOverThreshold returns a the duration threshold indicated by --cut-over-threshold
func (setting *DDLStrategySetting) CutOverThreshold() (d time.Duration, err error) {
// We do some ugly manual parsing of --cut-over-threshold value
opts, _ := shlex.Split(setting.Options)
Expand All @@ -212,6 +226,24 @@ func (setting *DDLStrategySetting) CutOverThreshold() (d time.Duration, err erro
return d, err
}

// RetainArtifactsDuration returns a the duration indicated by --retain-artifacts
func (setting *DDLStrategySetting) RetainArtifactsDuration() (d time.Duration, err error) {
// We do some ugly manual parsing of --retain-artifacts
opts, _ := shlex.Split(setting.Options)
for _, opt := range opts {
if val, isRetainArtifacts := isRetainArtifactsFlag(opt); isRetainArtifacts {
// value is possibly quoted
if s, err := strconv.Unquote(val); err == nil {
val = s
}
if val != "" {
d, err = time.ParseDuration(val)
}
}
}
return d, err
}

// IsVreplicationTestSuite checks if strategy options include --vreplicatoin-test-suite
func (setting *DDLStrategySetting) IsVreplicationTestSuite() bool {
return setting.hasFlag(vreplicationTestSuite)
Expand All @@ -235,6 +267,9 @@ func (setting *DDLStrategySetting) RuntimeOptions() []string {
if _, ok := isCutOverThresholdFlag(opt); ok {
continue
}
if _, ok := isRetainArtifactsFlag(opt); ok {
continue
}
switch {
case isFlag(opt, declarativeFlag):
case isFlag(opt, skipTopoFlag):
Expand Down
77 changes: 76 additions & 1 deletion go/vt/schema/ddl_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestIsCutOverThresholdFlag(t *testing.T) {
setting, err := ParseDDLStrategy("online " + ts.s)
assert.NoError(t, err)

val, isCutOver := isCutOverThresholdFlag((ts.s))
val, isCutOver := isCutOverThresholdFlag(ts.s)
assert.Equal(t, ts.expect, isCutOver)
assert.Equal(t, ts.val, val)

Expand All @@ -102,6 +102,69 @@ func TestIsCutOverThresholdFlag(t *testing.T) {
}
}

func TestIsExpireArtifactsFlag(t *testing.T) {
tt := []struct {
s string
expect bool
val string
d time.Duration
}{
{
s: "something",
},
{
s: "-retain-artifacts",
},
{
s: "--retain-artifacts",
},
{
s: "--retain-artifacts=",
expect: true,
},
{
s: "--retain-artifacts=0",
expect: true,
val: "0",
d: 0,
},
{
s: "-retain-artifacts=0",
expect: true,
val: "0",
d: 0,
},
{
s: "--retain-artifacts=1m",
expect: true,
val: "1m",
d: time.Minute,
},
{
s: `--retain-artifacts="1m"`,
expect: true,
val: `"1m"`,
d: time.Minute,
},
}
for _, ts := range tt {
t.Run(ts.s, func(t *testing.T) {
setting, err := ParseDDLStrategy("online " + ts.s)
assert.NoError(t, err)

val, isRetainArtifacts := isRetainArtifactsFlag(ts.s)
assert.Equal(t, ts.expect, isRetainArtifacts)
assert.Equal(t, ts.val, val)

if ts.expect {
d, err := setting.RetainArtifactsDuration()
assert.NoError(t, err)
assert.Equal(t, ts.d, d)
}
})
}
}

func TestParseDDLStrategy(t *testing.T) {
tt := []struct {
strategyVariable string
Expand All @@ -118,6 +181,7 @@ func TestParseDDLStrategy(t *testing.T) {
allowForeignKeys bool
analyzeTable bool
cutOverThreshold time.Duration
expireArtifacts time.Duration
runtimeOptions string
err error
}{
Expand Down Expand Up @@ -239,6 +303,13 @@ func TestParseDDLStrategy(t *testing.T) {
runtimeOptions: "",
cutOverThreshold: 5 * time.Minute,
},
{
strategyVariable: "vitess --retain-artifacts=4m",
strategy: DDLStrategyVitess,
options: "--retain-artifacts=4m",
runtimeOptions: "",
expireArtifacts: 4 * time.Minute,
},
{
strategyVariable: "vitess --analyze-table",
strategy: DDLStrategyVitess,
Expand Down Expand Up @@ -282,4 +353,8 @@ func TestParseDDLStrategy(t *testing.T) {
_, err := ParseDDLStrategy("online --cut-over-threshold=3")
assert.Error(t, err)
}
{
_, err := ParseDDLStrategy("online --retain-artifacts=3")
assert.Error(t, err)
}
}

0 comments on commit ad4a4c6

Please sign in to comment.