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

Vtgate: pass 'SHOW VITESS_MIGRATIONS' to tablet's query executor #13726

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions go/vt/vtgate/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2569,8 +2569,8 @@ func TestExecutorShowVitessMigrations(t *testing.T) {
ctx := context.Background()
_, err := executor.Execute(ctx, nil, "", session, showQuery, nil)
require.NoError(t, err)
assert.Contains(t, sbc1.StringQueries(), "SELECT * FROM _vt.schema_migrations")
assert.Contains(t, sbc2.StringQueries(), "SELECT * FROM _vt.schema_migrations")
assert.Contains(t, sbc1.StringQueries(), "show vitess_migrations")
assert.Contains(t, sbc2.StringQueries(), "show vitess_migrations")
}

func TestExecutorDescHash(t *testing.T) {
Expand Down
30 changes: 6 additions & 24 deletions go/vt/vtgate/planbuilder/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import (
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sidecardb"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
Expand Down Expand Up @@ -98,7 +96,7 @@ func buildShowBasicPlan(show *sqlparser.ShowBasic, vschema plancontext.VSchema)
case sqlparser.StatusGlobal, sqlparser.StatusSession:
return buildSendAnywherePlan(show, vschema)
case sqlparser.VitessMigrations:
return buildShowVMigrationsPlan(show, vschema)
return buildShowVitessMigrationsPlan(show, vschema)
case sqlparser.VGtidExecGlobal:
return buildShowVGtidPlan(show, vschema)
case sqlparser.GtidExecGlobal:
Expand Down Expand Up @@ -243,10 +241,9 @@ func buildDBPlan(show *sqlparser.ShowBasic, vschema plancontext.VSchema) (engine
return engine.NewRowsPrimitive(rows, buildVarCharFields("Database")), nil
}

// buildShowVMigrationsPlan serves `SHOW VITESS_MIGRATIONS ...` queries.
// It invokes queries on the sidecar database's schema_migrations table
// on all PRIMARY tablets in the keyspace's shards.
func buildShowVMigrationsPlan(show *sqlparser.ShowBasic, vschema plancontext.VSchema) (engine.Primitive, error) {
// buildShowVitessMigrationsPlan serves `SHOW VITESS_MIGRATIONS ...` queries.
// It sends down the SHOW command to the PRIMARY shard tablets (on all shards)
func buildShowVitessMigrationsPlan(show *sqlparser.ShowBasic, vschema plancontext.VSchema) (engine.Primitive, error) {
dest, ks, tabletType, err := vschema.TargetDestination(show.DbName.String())
if err != nil {
return nil, err
Expand All @@ -263,26 +260,11 @@ func buildShowVMigrationsPlan(show *sqlparser.ShowBasic, vschema plancontext.VSc
dest = key.DestinationAllShards{}
}

sidecarDBID, err := sidecardb.GetIdentifierForKeyspace(ks.Name)
if err != nil {
log.Errorf("Failed to read sidecar database identifier for keyspace %q from the cache: %v", ks.Name, err)
return nil, vterrors.VT14005(ks.Name)
}

sql := sqlparser.BuildParsedQuery("SELECT * FROM %s.schema_migrations", sidecarDBID).Query

if show.Filter != nil {
if show.Filter.Filter != nil {
sql += fmt.Sprintf(" where %s", sqlparser.String(show.Filter.Filter))
} else if show.Filter.Like != "" {
lit := sqlparser.String(sqlparser.NewStrLiteral(show.Filter.Like))
sql += fmt.Sprintf(" where migration_uuid LIKE %s OR migration_context LIKE %s OR migration_status LIKE %s", lit, lit, lit)
}
}
return &engine.Send{
Keyspace: ks,
TargetDestination: dest,
Query: sql,
Query: sqlparser.String(show),
IsDML: false,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/testdata/show_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@
"Sharded": true
},
"TargetDestination": "AllShards()",
"Query": "SELECT * FROM _vt.schema_migrations where migration_uuid LIKE '%format' OR migration_context LIKE '%format' OR migration_status LIKE '%format'"
"Query": "show vitess_migrations from `user` like '%format'"
}
}
},
Expand All @@ -552,7 +552,7 @@
"Sharded": true
},
"TargetDestination": "AllShards()",
"Query": "SELECT * FROM _vt.schema_migrations where id = 5"
"Query": "show vitess_migrations from `user` where id = 5"
}
}
},
Expand Down