Skip to content

Commit

Permalink
Backport: VReplication: Fix VDiff2 DeleteByUUID Query (vitessio#13255)
Browse files Browse the repository at this point in the history
Omitted changes to go/vt/vttablet/tabletmanager/vdiff/action_test.go
as they would require pulling in several additional refactoring PRs

Signed-off-by: Brendan Dougherty <[email protected]>
  • Loading branch information
mattlord authored and brendar committed Jun 21, 2023
1 parent 14548d2 commit 2cdd01c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
42 changes: 37 additions & 5 deletions go/test/endtoend/vreplication/vdiff2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

type testCase struct {
Expand Down Expand Up @@ -232,16 +233,47 @@ func testCLIErrors(t *testing.T, ksWorkflow, cells string) {

func testDelete(t *testing.T, ksWorkflow, cells string) {
t.Run("Delete", func(t *testing.T) {
// test show verbose too as a side effect
// Let's be sure that we have at least 3 unique VDiffs.
// We have one record in the SHOW output per VDiff, per
// shard. So we want to get a count of the unique VDiffs
// by UUID.
uuidCount := func(uuids []gjson.Result) int64 {
seen := make(map[string]struct{})
for _, uuid := range uuids {
seen[uuid.String()] = struct{}{}
}
return int64(len(seen))
}
_, output := performVDiff2Action(t, ksWorkflow, cells, "show", "all", false)
initialVDiffCount := uuidCount(gjson.Get(output, "#.UUID").Array())
for ; initialVDiffCount < 3; initialVDiffCount++ {
_, _ = performVDiff2Action(t, ksWorkflow, cells, "create", "", false)
}

// Now let's confirm that we have at least 3 unique VDiffs.
_, output = performVDiff2Action(t, ksWorkflow, cells, "show", "all", false)
require.GreaterOrEqual(t, uuidCount(gjson.Get(output, "#.UUID").Array()), int64(3))
// And that our initial count is what we expect.
require.Equal(t, initialVDiffCount, uuidCount(gjson.Get(output, "#.UUID").Array()))

// Test show last with verbose too as a side effect.
uuid, output := performVDiff2Action(t, ksWorkflow, cells, "show", "last", false, "--verbose")
// only present with --verbose
// The TableSummary is only present with --verbose.
require.Contains(t, output, `"TableSummary":`)

// Now let's delete one of the VDiffs.
_, output = performVDiff2Action(t, ksWorkflow, cells, "delete", uuid, false)
require.Contains(t, output, `"Status": "completed"`)
require.Equal(t, "completed", gjson.Get(output, "Status").String())
// And confirm that our unique VDiff count has only decreased by one.
_, output = performVDiff2Action(t, ksWorkflow, cells, "show", "all", false)
require.Equal(t, initialVDiffCount-1, uuidCount(gjson.Get(output, "#.UUID").Array()))

// Now let's delete all of them.
_, output = performVDiff2Action(t, ksWorkflow, cells, "delete", "all", false)
require.Contains(t, output, `"Status": "completed"`)
require.Equal(t, "completed", gjson.Get(output, "Status").String())
// And finally confirm that we have no more VDiffs.
_, output = performVDiff2Action(t, ksWorkflow, cells, "show", "all", false)
require.Equal(t, "[]\n", output)
require.Equal(t, int64(0), gjson.Get(output, "#").Int())
})
}

Expand Down
27 changes: 13 additions & 14 deletions go/test/endtoend/vreplication/vdiff_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"testing"
"time"

"github.com/buger/jsonparser"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"

"vitess.io/vitess/go/sqlescape"
"vitess.io/vitess/go/sqltypes"
Expand Down Expand Up @@ -174,7 +174,7 @@ func performVDiff2Action(t *testing.T, ksWorkflow, cells, action, actionArg stri
log.Infof("vdiff2 output: %+v (err: %+v)", output, err)
if !expectError {
require.Nil(t, err)
uuid, err = jsonparser.GetString([]byte(output), "UUID")
uuid = gjson.Get(output, "UUID").String()
if action != "delete" && !(action == "show" && actionArg == "all") { // a UUID is not required
require.NoError(t, err)
require.NotEmpty(t, uuid)
Expand All @@ -193,19 +193,18 @@ type vdiffInfo struct {
Progress vdiff2.ProgressReport
}

func getVDiffInfo(jsonStr string) *vdiffInfo {
func getVDiffInfo(json string) *vdiffInfo {
var info vdiffInfo
json := []byte(jsonStr)
info.Workflow, _ = jsonparser.GetString(json, "Workflow")
info.Keyspace, _ = jsonparser.GetString(json, "Keyspace")
info.State, _ = jsonparser.GetString(json, "State")
info.Shards, _ = jsonparser.GetString(json, "Shards")
info.RowsCompared, _ = jsonparser.GetInt(json, "RowsCompared")
info.StartedAt, _ = jsonparser.GetString(json, "StartedAt")
info.CompletedAt, _ = jsonparser.GetString(json, "CompletedAt")
info.HasMismatch, _ = jsonparser.GetBoolean(json, "HasMismatch")
info.Progress.Percentage, _ = jsonparser.GetFloat(json, "Progress", "Percentage")
info.Progress.ETA, _ = jsonparser.GetString(json, "Progress", "ETA")
info.Workflow = gjson.Get(json, "Workflow").String()
info.Keyspace = gjson.Get(json, "Keyspace").String()
info.State = gjson.Get(json, "State").String()
info.Shards = gjson.Get(json, "Shards").String()
info.RowsCompared = gjson.Get(json, "RowsCompared").Int()
info.StartedAt = gjson.Get(json, "StartedAt").String()
info.CompletedAt = gjson.Get(json, "CompletedAt").String()
info.HasMismatch = gjson.Get(json, "HasMismatch").Bool()
info.Progress.Percentage = gjson.Get(json, "Progress.Percentage").Float()
info.Progress.ETA = gjson.Get(json, "Progress.ETA").String()
return &info
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vttablet/tabletmanager/vdiff/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const (
left join _vt.vdiff_log as vdl on (vd.id = vdl.vdiff_id)
where vd.keyspace = %s and vd.workflow = %s`
sqlDeleteVDiffByUUID = `delete from vd, vdt using _vt.vdiff as vd left join _vt.vdiff_table as vdt on (vd.id = vdt.vdiff_id)
and vd.vdiff_uuid = %s`
where vd.vdiff_uuid = %s`
sqlVDiffSummary = `select vd.state as vdiff_state, vd.last_error as last_error, vdt.table_name as table_name,
vd.vdiff_uuid as 'uuid', vdt.state as table_state, vdt.table_rows as table_rows,
vd.started_at as started_at, vdt.table_rows as table_rows, vdt.rows_compared as rows_compared,
Expand Down

0 comments on commit 2cdd01c

Please sign in to comment.