Skip to content

Commit

Permalink
Add -init_populate_metadata to vttablet (#4115)
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Saponara <[email protected]>
  • Loading branch information
adsr committed Nov 29, 2018
1 parent 9d0594c commit 85847a7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
6 changes: 3 additions & 3 deletions go/vt/mysqlctl/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func Restore(
}
if !ok {
logger.Infof("Auto-restore is enabled, but mysqld already contains data. Assuming vttablet was just restarted.")
if err = populateMetadataTables(mysqld, localMetadata); err == nil {
if err = PopulateMetadataTables(mysqld, localMetadata); err == nil {
err = ErrExistingDB
}
return mysql.Position{}, err
Expand All @@ -820,7 +820,7 @@ func Restore(
if len(bhs) == 0 {
// There are no backups (not even broken/incomplete ones).
logger.Errorf("No backup to restore on BackupStorage for directory %v. Starting up empty.", dir)
if err = populateMetadataTables(mysqld, localMetadata); err == nil {
if err = PopulateMetadataTables(mysqld, localMetadata); err == nil {
err = ErrNoBackup
}
return mysql.Position{}, err
Expand Down Expand Up @@ -901,7 +901,7 @@ func Restore(
// Populate local_metadata before starting without --skip-networking,
// so it's there before we start announcing ourselves.
logger.Infof("Restore: populating local_metadata")
err = populateMetadataTables(mysqld, localMetadata)
err = PopulateMetadataTables(mysqld, localMetadata)
if err != nil {
return mysql.Position{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions go/vt/mysqlctl/metadata_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const sqlCreateShardMetadataTable = `CREATE TABLE IF NOT EXISTS _vt.shard_metada
PRIMARY KEY (name)
) ENGINE=InnoDB`

// populateMetadataTables creates and fills the _vt.local_metadata table and
// PopulateMetadataTables creates and fills the _vt.local_metadata table and
// creates _vt.shard_metadata table. _vt.local_metadata table is
// a per-tablet table that is never replicated. This allows queries
// against local_metadata to return different values on different tablets,
Expand All @@ -46,7 +46,7 @@ const sqlCreateShardMetadataTable = `CREATE TABLE IF NOT EXISTS _vt.shard_metada
// created here to make it easier to create it on databases that were running
// old version of Vitess, or databases that are getting converted to run under
// Vitess.
func populateMetadataTables(mysqld MysqlDaemon, localMetadata map[string]string) error {
func PopulateMetadataTables(mysqld MysqlDaemon, localMetadata map[string]string) error {
log.Infof("Populating _vt.local_metadata table...")

// Get a non-pooled DBA connection.
Expand Down
27 changes: 19 additions & 8 deletions go/vt/vttablet/tabletmanager/init_tablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ import (
"vitess.io/vitess/go/flagutil"
"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)

var (
initDbNameOverride = flag.String("init_db_name_override", "", "(init parameter) override the name of the db used by vttablet")
initKeyspace = flag.String("init_keyspace", "", "(init parameter) keyspace to use for this tablet")
initShard = flag.String("init_shard", "", "(init parameter) shard to use for this tablet")
initTags flagutil.StringMapValue
initTabletType = flag.String("init_tablet_type", "", "(init parameter) the tablet type to use for this tablet.")
initTimeout = flag.Duration("init_timeout", 1*time.Minute, "(init parameter) timeout to use for the init phase.")
initDbNameOverride = flag.String("init_db_name_override", "", "(init parameter) override the name of the db used by vttablet")
initKeyspace = flag.String("init_keyspace", "", "(init parameter) keyspace to use for this tablet")
initShard = flag.String("init_shard", "", "(init parameter) shard to use for this tablet")
initTags flagutil.StringMapValue
initTabletType = flag.String("init_tablet_type", "", "(init parameter) the tablet type to use for this tablet.")
initTimeout = flag.Duration("init_timeout", 1*time.Minute, "(init parameter) timeout to use for the init phase.")
initPopulateMetadata = flag.Bool("init_populate_metadata", false, "(init parameter) populate metadata tables")
)

func init() {
Expand Down Expand Up @@ -206,5 +207,15 @@ func (agent *ActionAgent) InitTablet(port, gRPCPort int32) error {
return vterrors.Wrap(err, "CreateTablet failed")
}

// optionally populate metadata records
if *initPopulateMetadata {
agent.setTablet(tablet)
localMetadata := agent.getLocalMetadataValues(tablet.Type)
err := mysqlctl.PopulateMetadataTables(agent.MysqlDaemon, localMetadata)
if err != nil {
return vterrors.Wrap(err, "failed to -init_populate_metadata")
}
}

return nil
}
18 changes: 17 additions & 1 deletion go/vt/vttablet/tabletmanager/init_tablet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/golang/protobuf/proto"

"vitess.io/vitess/go/history"
"vitess.io/vitess/go/mysql/fakesqldb"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/mysqlctl/fakemysqldaemon"
"vitess.io/vitess/go/vt/topo"
Expand Down Expand Up @@ -169,11 +171,24 @@ func TestInitTablet(t *testing.T) {
Cell: "cell1",
Uid: 1,
}
db := fakesqldb.New(t)
defer db.Close()
db.AddQueryPattern(`(SET|CREATE|BEGIN|INSERT|COMMIT)\b.*`, &sqltypes.Result{})
/*
db.AddQuery("SET @@session.sql_log_bin = 0", &sqltypes.Result{})
db.AddQuery("CREATE DATABASE IF NOT EXISTS _vt", &sqltypes.Result{})
db.AddQueryPattern(`CREATE TABLE IF NOT EXISTS _vt\.local_metadata.*`, &sqltypes.Result{})
db.AddQueryPattern(`CREATE TABLE IF NOT EXISTS _vt\.shard_metadata.*`, &sqltypes.Result{})
db.AddQuery("BEGIN", &sqltypes.Result{})
db.AddQueryPattern(`INSERT INTO _vt.local_metadata.*`, &sqltypes.Result{})
db.AddQueryPattern(`INSERT INTO _vt.shard_metadata.*`, &sqltypes.Result{})
db.AddQuery("COMMIT", &sqltypes.Result{})
*/

// start with a tablet record that doesn't exist
port := int32(1234)
gRPCPort := int32(3456)
mysqlDaemon := fakemysqldaemon.NewFakeMysqlDaemon(nil)
mysqlDaemon := fakemysqldaemon.NewFakeMysqlDaemon(db)
agent := &ActionAgent{
TopoServer: ts,
TabletAlias: tabletAlias,
Expand All @@ -194,6 +209,7 @@ func TestInitTablet(t *testing.T) {
*initKeyspace = "test_keyspace"
*initShard = "-C0"
*initTabletType = "replica"
*initPopulateMetadata = true
tabletAlias = &topodatapb.TabletAlias{
Cell: "cell1",
Uid: 2,
Expand Down

0 comments on commit 85847a7

Please sign in to comment.