func main() { defer exit.Recover() flag.Parse() servenv.Init() if initFakeZK != nil { initFakeZK() } ts := topo.GetServer() defer topo.CloseServers() resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServer") healthCheck = discovery.NewHealthCheck(*connTimeoutTotal, *healthCheckRetryDelay, *healthCheckTimeout, "" /* statsSuffix */) tabletTypes := make([]topodatapb.TabletType, 0, 1) if len(*tabletTypesToWait) != 0 { for _, ttStr := range strings.Split(*tabletTypesToWait, ",") { tt, err := topoproto.ParseTabletType(ttStr) if err != nil { log.Errorf("unknown tablet type: %v", ttStr) continue } tabletTypes = append(tabletTypes, tt) } } vtg := vtgate.Init(context.Background(), healthCheck, ts, resilientSrvTopoServer, *cell, *retryDelay, *retryCount, *connTimeoutTotal, *connTimeoutPerConn, *connLife, tabletTypes, *maxInFlight, *testGateway) servenv.OnRun(func() { addStatusParts(vtg) }) servenv.RunDefault() }
func main() { flag.Parse() args := flag.Args() installSignalHandlers() servenv.Init() defer servenv.Close() ts := topo.GetServer() defer topo.CloseServers() // the logger will be replaced when we start a job wr = wrangler.New(logutil.NewConsoleLogger(), ts, 30*time.Second, 30*time.Second) if len(args) == 0 { // interactive mode, initialize the web UI to chose a command initInteractiveMode() } else { // single command mode, just runs it runCommand(args) } initStatusHandling() servenv.RunDefault() }
func main() { defer exit.Recover() flag.Parse() servenv.Init() ts := topo.GetServer() defer topo.CloseServers() resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServer") healthCheck = discovery.NewHealthCheck(*healthCheckConnTimeout, *healthCheckRetryDelay, *healthCheckTimeout) healthCheck.RegisterStats() tabletTypes := make([]topodatapb.TabletType, 0, 1) if len(*tabletTypesToWait) != 0 { for _, ttStr := range strings.Split(*tabletTypesToWait, ",") { tt, err := topoproto.ParseTabletType(ttStr) if err != nil { log.Errorf("unknown tablet type: %v", ttStr) continue } tabletTypes = append(tabletTypes, tt) } } l2vtg := l2vtgate.Init(healthCheck, ts, resilientSrvTopoServer, *cell, *retryCount, tabletTypes) servenv.OnRun(func() { addStatusParts(l2vtg) }) servenv.RunDefault() }
func main() { flag.Parse() servenv.Init() blm := vtgate.NewBalancerMap(topo.GetServer(), *cell, *portName) vtgate.Init(blm, *tabletProtocol, *retryDelay, *retryCount) servenv.Run(*port) }
func main() { dbConfigsFile, dbCredentialsFile := dbconfigs.RegisterCommonFlags() flag.Parse() relog.Info("started vtaction %v", os.Args) rpc.HandleHTTP() jsonrpc.ServeHTTP() jsonrpc.ServeRPC() bsonrpc.ServeHTTP() bsonrpc.ServeRPC() logFile, err := os.OpenFile(*logFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666) if err != nil { relog.Fatal("Can't open log file: %v", err) } relog.SetOutput(logFile) relog.SetPrefix(fmt.Sprintf("vtaction [%v] ", os.Getpid())) if err := relog.SetLevelByName(*logLevel); err != nil { relog.Fatal("%v", err) } relog.HijackLog(nil) relog.HijackStdio(logFile, logFile) mycnf, mycnfErr := mysqlctl.ReadMycnf(*mycnfFile) if mycnfErr != nil { relog.Fatal("mycnf read failed: %v", mycnfErr) } relog.Debug("mycnf: %v", jscfg.ToJson(mycnf)) dbcfgs, cfErr := dbconfigs.Init(mycnf.SocketFile, *dbConfigsFile, *dbCredentialsFile) if err != nil { relog.Fatal("%s", cfErr) } mysqld := mysqlctl.NewMysqld(mycnf, dbcfgs.Dba, dbcfgs.Repl) topoServer := topo.GetServer() defer topo.CloseServers() actor := tabletmanager.NewTabletActor(mysqld, topoServer) // we delegate out startup to the micromanagement server so these actions // will occur after we have obtained our socket. bindAddr := fmt.Sprintf(":%v", *port) httpServer := &http.Server{Addr: bindAddr} go func() { if err := httpServer.ListenAndServe(); err != nil { relog.Error("httpServer.ListenAndServe err: %v", err) } }() actionErr := actor.HandleAction(*actionNode, *action, *actionGuid, *force) if actionErr != nil { relog.Fatal("action error: %v", actionErr) } relog.Info("finished vtaction %v", os.Args) }
func init() { // Wait until flags are parsed, so we can check which topo server is in use. servenv.OnRun(func() { if zkServer, ok := topo.GetServer().Impl.(*zktopo.Server); ok { HandleExplorer("zk", "/zk/", "zk.html", NewZkExplorer(zkServer.GetZConn())) } }) }
func init() { // Wait until flags are parsed, so we can check which topo server is in use. servenv.OnRun(func() { if etcdServer, ok := topo.GetServer().Impl.(*etcdtopo.Server); ok { vtctld.HandleExplorer("etcd", etcdtopo.NewExplorer(etcdServer)) } }) }
func main() { defer exit.Recover() flag.Parse() servenv.Init() if initFakeZK != nil { initFakeZK() } ts := topo.GetServer() defer topo.CloseServers() var schema *planbuilder.Schema if *schemaFile != "" { var err error if schema, err = planbuilder.LoadFile(*schemaFile); err != nil { log.Error(err) exit.Return(1) } log.Infof("v3 is enabled: loaded schema from file: %v", *schemaFile) } else { ctx := context.Background() schemaJSON, err := ts.GetVSchema(ctx) if err != nil { log.Warningf("Skipping v3 initialization: GetVSchema failed: %v", err) goto startServer } schema, err = planbuilder.NewSchema([]byte(schemaJSON)) if err != nil { log.Warningf("Skipping v3 initialization: GetVSchema failed: %v", err) goto startServer } log.Infof("v3 is enabled: loaded schema from topo") } startServer: resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServer") healthCheck = discovery.NewHealthCheck(*connTimeoutTotal, *healthCheckRetryDelay, *healthCheckTimeout, "" /* statsSuffix */) tabletTypes := make([]topodatapb.TabletType, 0, 1) if len(*tabletTypesToWait) != 0 { for _, ttStr := range strings.Split(*tabletTypesToWait, ",") { tt, err := topoproto.ParseTabletType(ttStr) if err != nil { log.Errorf("unknown tablet type: %v", ttStr) continue } tabletTypes = append(tabletTypes, tt) } } vtg := vtgate.Init(healthCheck, ts, resilientSrvTopoServer, schema, *cell, *retryDelay, *retryCount, *connTimeoutTotal, *connTimeoutPerConn, *connLife, tabletTypes, *maxInFlight, *testGateway) servenv.OnRun(func() { addStatusParts(vtg) }) servenv.RunDefault() }
// NewActionAgent creates a new ActionAgent and registers all the // associated services func NewActionAgent( tabletAlias topo.TabletAlias, dbcfgs *dbconfigs.DBConfigs, mycnf *mysqlctl.Mycnf, port, securePort int, overridesFile string, lockTimeout time.Duration, ) (agent *ActionAgent, err error) { schemaOverrides := loadSchemaOverrides(overridesFile) topoServer := topo.GetServer() mysqld := mysqlctl.NewMysqld("Dba", mycnf, &dbcfgs.Dba, &dbcfgs.Repl) agent = &ActionAgent{ TopoServer: topoServer, TabletAlias: tabletAlias, Mysqld: mysqld, MysqlDaemon: mysqld, DBConfigs: dbcfgs, SchemaOverrides: schemaOverrides, LockTimeout: lockTimeout, done: make(chan struct{}), History: history.New(historyLength), lastHealthMapCount: stats.NewInt("LastHealthMapCount"), changeItems: make(chan tabletChangeItem, 100), } // Start the binlog player services, not playing at start. agent.BinlogPlayerMap = NewBinlogPlayerMap(topoServer, &dbcfgs.Filtered, mysqld) RegisterBinlogPlayerMap(agent.BinlogPlayerMap) // try to figure out the mysql port mysqlPort := mycnf.MysqlPort if mysqlPort == 0 { // we don't know the port, try to get it from mysqld var err error mysqlPort, err = mysqld.GetMysqlPort() if err != nil { log.Warningf("Cannot get current mysql port, will use 0 for now: %v", err) } } if err := agent.Start(mysqlPort, port, securePort); err != nil { return nil, err } // register the RPC services from the agent agent.registerQueryService() // start health check if needed agent.initHeathCheck() return agent, nil }
func main() { flag.Parse() servenv.Init() ts := topo.GetServer() defer topo.CloseServers() topoReader = NewTopoReader(ts) topo.RegisterTopoReader(topoReader) servenv.Run(*port) }
func main() { defer exit.RecoverAll() defer logutil.Flush() flag.Parse() args := flag.Args() if len(args) == 0 { flag.Usage() exit.Return(1) } action := args[0] installSignalHandlers() startMsg := fmt.Sprintf("USER=%v SUDO_USER=%v %v", os.Getenv("USER"), os.Getenv("SUDO_USER"), strings.Join(os.Args, " ")) if syslogger, err := syslog.New(syslog.LOG_INFO, "vtctl "); err == nil { syslogger.Info(startMsg) } else { log.Warningf("cannot connect to syslog: %v", err) } topoServer := topo.GetServer() defer topo.CloseServers() wr := wrangler.New(logutil.NewConsoleLogger(), topoServer, *waitTime, *lockWaitTimeout) actionPath, err := vtctl.RunCommand(wr, args) switch err { case vtctl.ErrUnknownCommand: flag.Usage() exit.Return(1) case nil: // keep going default: log.Errorf("action failed: %v %v", action, err) exit.Return(255) } if actionPath != "" { if *noWaitForAction { fmt.Println(actionPath) } else { err := wr.WaitForCompletion(actionPath) if err != nil { log.Error(err.Error()) exit.Return(255) } else { log.Infof("action completed: %v", actionPath) } } } }
func main() { defer exit.Recover() flag.Parse() servenv.Init() ts := topo.GetServer() defer topo.CloseServers() var schema *planbuilder.Schema if *schemaFile != "" { var err error if schema, err = planbuilder.LoadFile(*schemaFile); err != nil { log.Error(err) exit.Return(1) } log.Infof("v3 is enabled: loaded schema from file: %v", *schemaFile) } else { schemafier, ok := ts.(topo.Schemafier) if !ok { log.Infof("Skipping v3 initialization: topo does not suppurt schemafier interface") goto startServer } ctx := context.Background() schemaJSON, err := schemafier.GetVSchema(ctx) if err != nil { log.Warningf("Skipping v3 initialization: GetVSchema failed: %v", err) goto startServer } schema, err = planbuilder.NewSchema([]byte(schemaJSON)) if err != nil { log.Warningf("Skipping v3 initialization: GetVSchema failed: %v", err) goto startServer } log.Infof("v3 is enabled: loaded schema from topo") } startServer: resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServer") // For the initial phase vtgate is exposing // topoReader api. This will be subsumed by // vtgate once vtgate's client functions become active. topoReader = NewTopoReader(resilientSrvTopoServer) servenv.Register("toporeader", topoReader) vtgate.Init(resilientSrvTopoServer, schema, *cell, *retryDelay, *retryCount, *connTimeoutTotal, *connTimeoutPerConn, *connLife, *maxInFlight) servenv.RunDefault() }
func main() { dbconfigs.RegisterFlags() mysqlctl.RegisterFlags() flag.Parse() servenv.Init() defer servenv.Close() log.Infof("started vtaction %v", os.Args) servenv.ServeRPC() mycnf, mycnfErr := mysqlctl.NewMycnfFromFlags(0) if mycnfErr != nil { log.Fatalf("mycnf read failed: %v", mycnfErr) } log.V(6).Infof("mycnf: %v", jscfg.ToJson(mycnf)) dbcfgs, cfErr := dbconfigs.Init(mycnf.SocketFile) if cfErr != nil { log.Fatalf("%s", cfErr) } mysqld := mysqlctl.NewMysqld("Dba", mycnf, &dbcfgs.Dba, &dbcfgs.Repl) defer mysqld.Close() topoServer := topo.GetServer() defer topo.CloseServers() actor := actor.NewTabletActor(mysqld, mysqld, topoServer, topo.TabletAlias{}) // we delegate out startup to the micromanagement server so these actions // will occur after we have obtained our socket. bindAddr := fmt.Sprintf(":%v", *servenv.Port) httpServer := &http.Server{Addr: bindAddr} go func() { if err := httpServer.ListenAndServe(); err != nil { log.Errorf("httpServer.ListenAndServe err: %v", err) } }() actionErr := actor.HandleAction(*actionNode, *action, *actionGuid, *force) if actionErr != nil { log.Fatalf("action error: %v", actionErr) } log.Infof("finished vtaction %v", os.Args) }
func main() { flag.Parse() servenv.Init() defer servenv.Close() ts = topo.GetServer() defer topo.CloseServers() // Init the vtctld core vtctld.InitVtctld(ts) // Start schema manager service. initSchema() // And run the server. servenv.RunDefault() }
func RegisterShardedDrivers() { // default topo server ts := topo.GetServer() db.Register("vtdb", &sDriver{ts, false}) db.Register("vtdb-streaming", &sDriver{ts, true}) // forced zk topo server zconn := zk.NewMetaConn(false) zkts := zktopo.NewServer(zconn) db.Register("vtdb-zk", &sDriver{zkts, false}) db.Register("vtdb-zk-streaming", &sDriver{zkts, true}) // forced zkocc topo server zkoccconn := zk.NewMetaConn(true) zktsro := zktopo.NewServer(zkoccconn) db.Register("vtdb-zkocc", &sDriver{zktsro, false}) db.Register("vtdb-zkocc-streaming", &sDriver{zktsro, true}) }
func main() { flag.Parse() servenv.Init() ts := topo.GetServer() defer topo.CloseServers() resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServer") // For the initial phase vtgate is exposing // topoReader api. This will be subsumed by // vtgate once vtgate's client functions become active. topoReader = NewTopoReader(resilientSrvTopoServer) servenv.Register("toporeader", topoReader) vtgate.Init(resilientSrvTopoServer, *cell, *retryDelay, *retryCount, *timeout, *maxInFlight) servenv.RunDefault() }
func main() { defer exit.RecoverAll() defer logutil.Flush() flag.Parse() args := flag.Args() if len(args) == 0 { flag.Usage() exit.Return(1) } action := args[0] startMsg := fmt.Sprintf("USER=%v SUDO_USER=%v %v", os.Getenv("USER"), os.Getenv("SUDO_USER"), strings.Join(os.Args, " ")) if syslogger, err := syslog.New(syslog.LOG_INFO, "vtctl "); err == nil { syslogger.Info(startMsg) } else { log.Warningf("cannot connect to syslog: %v", err) } topoServer := topo.GetServer() defer topo.CloseServers() ctx, cancel := context.WithTimeout(context.Background(), *waitTime) wr := wrangler.New(logutil.NewConsoleLogger(), topoServer, tmclient.NewTabletManagerClient(), *lockWaitTimeout) installSignalHandlers(cancel) for _, f := range initFuncs { f() } err := vtctl.RunCommand(ctx, wr, args) cancel() switch err { case vtctl.ErrUnknownCommand: flag.Usage() exit.Return(1) case nil: // keep going default: log.Errorf("action failed: %v %v", action, err) exit.Return(255) } }
func main() { defer exit.Recover() flag.Parse() servenv.Init() if initFakeZK != nil { initFakeZK() } ts := topo.GetServer() defer topo.CloseServers() var schema *planbuilder.Schema if *schemaFile != "" { var err error if schema, err = planbuilder.LoadFile(*schemaFile); err != nil { log.Error(err) exit.Return(1) } log.Infof("v3 is enabled: loaded schema from file: %v", *schemaFile) } else { ctx := context.Background() schemaJSON, err := ts.GetVSchema(ctx) if err != nil { log.Warningf("Skipping v3 initialization: GetVSchema failed: %v", err) goto startServer } schema, err = planbuilder.NewSchema([]byte(schemaJSON)) if err != nil { log.Warningf("Skipping v3 initialization: GetVSchema failed: %v", err) goto startServer } log.Infof("v3 is enabled: loaded schema from topo") } startServer: resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServer") healthCheck = discovery.NewHealthCheck(*connTimeoutTotal, *healthCheckRetryDelay) vtgate.Init(healthCheck, ts, resilientSrvTopoServer, schema, *cell, *retryDelay, *retryCount, *connTimeoutTotal, *connTimeoutPerConn, *connLife, *maxInFlight, *testGateway) servenv.RunDefault() }
func main() { flag.Parse() servenv.Init() // For the initial phase vtgate is exposing // topoReader api. This will be subsumed by // vtgate once vtgate's client functions become active. ts := topo.GetServer() defer topo.CloseServers() rts := vtgate.NewResilientSrvTopoServer(ts) topoReader = NewTopoReader(rts) topo.RegisterTopoReader(topoReader) vtgate.Init(rts, *cell, *retryDelay, *retryCount) log.Infof("vtgate listening to port %v", *port) servenv.Run(*port) }
func main() { flag.Parse() servenv.Init() ts := topo.GetServer() scheduler, err := janitor.New(*keyspace, *shard, ts, wrangler.New(ts, *actionTimeout, *lockTimeout), *sleepTime) if err != nil { log.Fatalf("janitor.New: %v", err) } if len(activeModules)+len(dryRunModules) < 1 { log.Fatal("no modules to run specified") } scheduler.Enable(activeModules) scheduler.EnableDryRun(dryRunModules) go scheduler.Run() servenv.Run() }
func main() { defer exit.Recover() flag.Parse() args := flag.Args() servenv.Init() defer servenv.Close() ts := topo.GetServer() defer topo.CloseServers() wi = worker.NewInstance(ts, *cell, *commandDisplayInterval) wi.InstallSignalHandlers() wi.InitStatusHandling() if len(args) == 0 { // In interactive mode, initialize the web UI to choose a command. wi.InitInteractiveMode() } else { // In single command mode, just run it. worker, done, err := wi.RunCommand(context.Background(), args, nil /*custom wrangler*/, true /*runFromCli*/) if err != nil { log.Error(err) exit.Return(1) } // Run the subsequent, blocking wait asynchronously. go func() { if err := wi.WaitForCommand(worker, done); err != nil { log.Error(err) logutil.Flush() // We cannot use exit.Return() here because we are in a different go routine now. os.Exit(1) } logutil.Flush() os.Exit(0) }() } servenv.RunDefault() }
func main() { flag.Parse() servenv.Init() // For the initial phase vtgate is exposing // topoReader api. This will be subsumed by // vtgate once vtgate's client functions become active. ts := topo.GetServer() defer topo.CloseServers() rts := vtgate.NewResilientSrvTopoServer(ts) stats.Publish("EndpointCount", stats.CountersFunc(rts.HealthyEndpointCount)) stats.Publish("DegradedEndpointCount", stats.CountersFunc(rts.DegradedEndpointCount)) topoReader = NewTopoReader(rts) topo.RegisterTopoReader(topoReader) vtgate.Init(rts, *cell, *retryDelay, *retryCount, *timeout) servenv.Run() }
// NewActionAgent creates a new ActionAgent and registers all the // associated services func NewActionAgent( tabletAlias topo.TabletAlias, dbcfgs *dbconfigs.DBConfigs, mycnf *mysqlctl.Mycnf, port, securePort int, overridesFile string, ) (agent *ActionAgent, err error) { schemaOverrides := loadSchemaOverrides(overridesFile) topoServer := topo.GetServer() mysqld := mysqlctl.NewMysqld(mycnf, &dbcfgs.Dba, &dbcfgs.Repl) agent = &ActionAgent{ TopoServer: topoServer, TabletAlias: tabletAlias, Mysqld: mysqld, DBConfigs: dbcfgs, SchemaOverrides: schemaOverrides, done: make(chan struct{}), History: history.New(historyLength), changeItems: make(chan tabletChangeItem, 100), } // Start the binlog player services, not playing at start. agent.BinlogPlayerMap = NewBinlogPlayerMap(topoServer, &dbcfgs.App.ConnectionParams, mysqld) RegisterBinlogPlayerMap(agent.BinlogPlayerMap) if err := agent.Start(mysqld.Port(), port, securePort); err != nil { return nil, err } // register the RPC services from the agent agent.registerQueryService() // start health check if needed agent.initHeathCheck() return agent, nil }
func main() { flag.Parse() servenv.Init() ts := topo.GetServer() defer topo.CloseServers() resilientSrvTopoServer = vtgate.NewResilientSrvTopoServer(ts, "ResilientSrvTopoServerCounts") labels := []string{"Cell", "Keyspace", "Shard", "DbType"} _ = stats.NewMapCountersFunc("EndpointCount", labels, resilientSrvTopoServer.HealthyEndpointCount) _ = stats.NewMapCountersFunc("DegradedEndpointCount", labels, resilientSrvTopoServer.DegradedEndpointCount) // For the initial phase vtgate is exposing // topoReader api. This will be subsumed by // vtgate once vtgate's client functions become active. topoReader = NewTopoReader(resilientSrvTopoServer) topo.RegisterTopoReader(topoReader) vtgate.Init(resilientSrvTopoServer, *cell, *retryDelay, *retryCount, *timeout) servenv.RunDefault() }
func main() { flag.Parse() args := flag.Args() installSignalHandlers() servenv.Init() defer servenv.Close() ts := topo.GetServer() defer topo.CloseServers() wr := wrangler.New(ts, 30*time.Second, 30*time.Second) if len(args) == 0 { // interactive mode, initialize the web UI to chose a command initInteractiveMode(wr) } else { runCommand(wr, args) } initStatusHandling() servenv.RunDefault() }
func main() { defer exit.Recover() flag.Parse() servenv.Init() ts := topo.GetServer() scheduler, err := janitor.New(*keyspace, *shard, ts, wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()), *sleepTime) if err != nil { log.Errorf("janitor.New: %v", err) exit.Return(1) } if len(activeModules)+len(dryRunModules) < 1 { log.Error("no modules to run specified") exit.Return(1) } scheduler.Enable(activeModules) scheduler.EnableDryRun(dryRunModules) go scheduler.Run() servenv.RunDefault() }
// NewActionAgent creates a new ActionAgent and registers all the // associated services. // // batchCtx is the context that the agent will use for any background tasks // it spawns. func NewActionAgent( batchCtx context.Context, mysqld mysqlctl.MysqlDaemon, queryServiceControl tabletserver.QueryServiceControl, tabletAlias topo.TabletAlias, dbcfgs *dbconfigs.DBConfigs, mycnf *mysqlctl.Mycnf, port, securePort, gRPCPort int, overridesFile string, lockTimeout time.Duration, ) (agent *ActionAgent, err error) { schemaOverrides := loadSchemaOverrides(overridesFile) topoServer := topo.GetServer() agent = &ActionAgent{ QueryServiceControl: queryServiceControl, HealthReporter: health.DefaultAggregator, batchCtx: batchCtx, TopoServer: topoServer, TabletAlias: tabletAlias, MysqlDaemon: mysqld, DBConfigs: dbcfgs, SchemaOverrides: schemaOverrides, LockTimeout: lockTimeout, History: history.New(historyLength), lastHealthMapCount: stats.NewInt("LastHealthMapCount"), _healthy: fmt.Errorf("healthcheck not run yet"), healthStreamMap: make(map[int]chan<- *actionnode.HealthStreamReply), } // try to initialize the tablet if we have to if err := agent.InitTablet(port, securePort, gRPCPort); err != nil { return nil, fmt.Errorf("agent.InitTablet failed: %v", err) } // Publish and set the TargetTabletType. Not a global var // since it should never be changed. statsTabletType := stats.NewString("TargetTabletType") statsTabletType.Set(*targetTabletType) // Start the binlog player services, not playing at start. agent.BinlogPlayerMap = NewBinlogPlayerMap(topoServer, &dbcfgs.Filtered, mysqld) RegisterBinlogPlayerMap(agent.BinlogPlayerMap) // try to figure out the mysql port mysqlPort := mycnf.MysqlPort if mysqlPort == 0 { // we don't know the port, try to get it from mysqld var err error mysqlPort, err = mysqld.GetMysqlPort() if err != nil { log.Warningf("Cannot get current mysql port, will use 0 for now: %v", err) } } if err := agent.Start(batchCtx, mysqlPort, port, securePort, gRPCPort); err != nil { return nil, err } // register the RPC services from the agent agent.registerQueryService() // two cases then: // - restoreFromBackup is set: we restore, then initHealthCheck, all // in the background // - restoreFromBackup is not set: we initHealthCheck right away if *restoreFromBackup { go func() { // restoreFromBackup wil just be a regular action // (same as if it was triggered remotely) if err := agent.RestoreFromBackup(batchCtx); err != nil { println(fmt.Sprintf("RestoreFromBackup failed: %v", err)) log.Fatalf("RestoreFromBackup failed: %v", err) } // after the restore is done, start health check agent.initHeathCheck() }() } else { // synchronously start health check if needed agent.initHeathCheck() } return agent, nil }
// InitAgent initializes the agent within vttablet. func InitAgent( tabletAlias topo.TabletAlias, dbcfgs dbconfigs.DBConfigs, mycnf *mysqlctl.Mycnf, dbConfigsFile, dbCredentialsFile string, port, securePort int, mycnfFile, overridesFile string) (err error) { schemaOverrides := loadSchemaOverrides(overridesFile) topoServer := topo.GetServer() // Start the binlog server service, disabled at start. binlogServer = mysqlctl.NewBinlogServer(mycnf) mysqlctl.RegisterBinlogServerService(binlogServer) // Start the binlog player services, not playing at start. binlogPlayerMap = NewBinlogPlayerMap(topoServer, &dbcfgs.Dba) RegisterBinlogPlayerMap(binlogPlayerMap) // Compute the bind addresses bindAddr := fmt.Sprintf(":%v", port) secureAddr := "" if securePort != 0 { secureAddr = fmt.Sprintf(":%v", securePort) } exportedType := expvar.NewString("tablet-type") // Action agent listens to changes in zookeeper and makes // modifications to this tablet. agent, err = tm.NewActionAgent(topoServer, tabletAlias, mycnfFile, dbConfigsFile, dbCredentialsFile) if err != nil { return err } agent.AddChangeCallback(func(oldTablet, newTablet topo.Tablet) { if newTablet.IsServingType() { if dbcfgs.App.Dbname == "" { dbcfgs.App.Dbname = newTablet.DbName() } dbcfgs.App.KeyRange = newTablet.KeyRange dbcfgs.App.Keyspace = newTablet.Keyspace dbcfgs.App.Shard = newTablet.Shard // Transitioning from replica to master, first disconnect // existing connections. "false" indicateds that clients must // re-resolve their endpoint before reconnecting. if newTablet.Type == topo.TYPE_MASTER && oldTablet.Type != topo.TYPE_MASTER { ts.DisallowQueries(false) } qrs := ts.LoadCustomRules() if dbcfgs.App.KeyRange.IsPartial() { qr := ts.NewQueryRule("enforce keyspace_id range", "keyspace_id_not_in_range", ts.QR_FAIL_QUERY) qr.AddPlanCond(sqlparser.PLAN_INSERT_PK) err = qr.AddBindVarCond("keyspace_id", true, true, ts.QR_NOTIN, dbcfgs.App.KeyRange) if err != nil { log.Warningf("Unable to add keyspace rule: %v", err) } else { qrs.Add(qr) } } ts.AllowQueries(dbcfgs.App, schemaOverrides, qrs) mysqlctl.EnableUpdateStreamService(string(newTablet.Type), dbcfgs) if newTablet.Type != topo.TYPE_MASTER { ts.StartRowCacheInvalidation() } } else { ts.DisallowQueries(false) ts.StopRowCacheInvalidation() mysqlctl.DisableUpdateStreamService() } exportedType.Set(string(newTablet.Type)) // BinlogServer is only enabled for replicas if newTablet.Type == topo.TYPE_REPLICA { if !mysqlctl.IsBinlogServerEnabled(binlogServer) { mysqlctl.EnableBinlogServerService(binlogServer, dbcfgs.App.Dbname) } } else { if mysqlctl.IsBinlogServerEnabled(binlogServer) { mysqlctl.DisableBinlogServerService(binlogServer) } } // See if we need to start or stop any binlog player if newTablet.Type == topo.TYPE_MASTER { binlogPlayerMap.RefreshMap(newTablet) } else { binlogPlayerMap.StopAllPlayers() } }) mysqld := mysqlctl.NewMysqld(mycnf, dbcfgs.Dba, dbcfgs.Repl) if err := agent.Start(bindAddr, secureAddr, mysqld.Addr()); err != nil { return err } // register the RPC services from the agent agent.RegisterQueryService(mysqld) return nil }
// NewActionAgent creates a new ActionAgent and registers all the // associated services. // // batchCtx is the context that the agent will use for any background tasks // it spawns. func NewActionAgent( batchCtx context.Context, mysqld mysqlctl.MysqlDaemon, queryServiceControl tabletserver.Controller, tabletAlias *topodatapb.TabletAlias, dbcfgs dbconfigs.DBConfigs, mycnf *mysqlctl.Mycnf, port, gRPCPort int32, overridesFile string, ) (agent *ActionAgent, err error) { schemaOverrides := loadSchemaOverrides(overridesFile) topoServer := topo.GetServer() agent = &ActionAgent{ QueryServiceControl: queryServiceControl, HealthReporter: health.DefaultAggregator, batchCtx: batchCtx, TopoServer: topoServer, TabletAlias: tabletAlias, MysqlDaemon: mysqld, DBConfigs: dbcfgs, SchemaOverrides: schemaOverrides, History: history.New(historyLength), lastHealthMapCount: stats.NewInt("LastHealthMapCount"), _healthy: fmt.Errorf("healthcheck not run yet"), } agent.registerQueryRuleSources() // try to initialize the tablet if we have to if err := agent.InitTablet(port, gRPCPort); err != nil { return nil, fmt.Errorf("agent.InitTablet failed: %v", err) } // Publish and set the TargetTabletType. Not a global var // since it should never be changed. statsTabletType := stats.NewString("TargetTabletType") statsTabletType.Set(*targetTabletType) // Create the TabletType stats agent.exportStats = true agent.statsTabletType = stats.NewString("TabletType") // Start the binlog player services, not playing at start. agent.BinlogPlayerMap = NewBinlogPlayerMap(topoServer, mysqld, func() binlogplayer.VtClient { return binlogplayer.NewDbClient(&agent.DBConfigs.Filtered) }) // Stop all binlog players upon entering lameduck. servenv.OnTerm(agent.BinlogPlayerMap.StopAllPlayersAndReset) RegisterBinlogPlayerMap(agent.BinlogPlayerMap) // try to figure out the mysql port mysqlPort := mycnf.MysqlPort if mysqlPort == 0 { // we don't know the port, try to get it from mysqld var err error mysqlPort, err = mysqld.GetMysqlPort() if err != nil { log.Warningf("Cannot get current mysql port, will use 0 for now: %v", err) } } // Start will get the tablet info, and update our state from it if err := agent.Start(batchCtx, int32(mysqlPort), port, gRPCPort, true); err != nil { return nil, err } // register the RPC services from the agent agent.registerQueryService() // two cases then: // - restoreFromBackup is set: we restore, then initHealthCheck, all // in the background // - restoreFromBackup is not set: we initHealthCheck right away if *restoreFromBackup { go func() { // restoreFromBackup wil just be a regular action // (same as if it was triggered remotely) if err := agent.RestoreFromBackup(batchCtx); err != nil { println(fmt.Sprintf("RestoreFromBackup failed: %v", err)) log.Fatalf("RestoreFromBackup failed: %v", err) } // after the restore is done, start health check agent.initHealthCheck() }() } else { // synchronously start health check if needed agent.initHealthCheck() } return agent, nil }
func main() { flag.Parse() servenv.Init() defer servenv.Close() templateLoader = NewTemplateLoader(*templateDir, dummyTemplate, *debug) ts = topo.GetServer() defer topo.CloseServers() wr := wrangler.New(logutil.NewConsoleLogger(), ts, 30*time.Second, 30*time.Second) actionRepo = NewActionRepository(ts) // keyspace actions actionRepo.RegisterKeyspaceAction("ValidateKeyspace", func(wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) { return "", wr.ValidateKeyspace(keyspace, false) }) actionRepo.RegisterKeyspaceAction("ValidateSchemaKeyspace", func(wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) { return "", wr.ValidateSchemaKeyspace(keyspace, nil, false) }) actionRepo.RegisterKeyspaceAction("ValidateVersionKeyspace", func(wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) { return "", wr.ValidateVersionKeyspace(keyspace) }) actionRepo.RegisterKeyspaceAction("ValidatePermissionsKeyspace", func(wr *wrangler.Wrangler, keyspace string, r *http.Request) (string, error) { return "", wr.ValidatePermissionsKeyspace(keyspace) }) // shard actions actionRepo.RegisterShardAction("ValidateShard", func(wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) { return "", wr.ValidateShard(keyspace, shard, false) }) actionRepo.RegisterShardAction("ValidateSchemaShard", func(wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) { return "", wr.ValidateSchemaShard(keyspace, shard, nil, false) }) actionRepo.RegisterShardAction("ValidateVersionShard", func(wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) { return "", wr.ValidateVersionShard(keyspace, shard) }) actionRepo.RegisterShardAction("ValidatePermissionsShard", func(wr *wrangler.Wrangler, keyspace, shard string, r *http.Request) (string, error) { return "", wr.ValidatePermissionsShard(keyspace, shard) }) // tablet actions actionRepo.RegisterTabletAction("RpcPing", "", func(wr *wrangler.Wrangler, tabletAlias topo.TabletAlias, r *http.Request) (string, error) { return "", wr.ActionInitiator().RpcPing(tabletAlias, 10*time.Second) }) actionRepo.RegisterTabletAction("ScrapTablet", acl.ADMIN, func(wr *wrangler.Wrangler, tabletAlias topo.TabletAlias, r *http.Request) (string, error) { // refuse to scrap tablets that are not spare ti, err := wr.TopoServer().GetTablet(tabletAlias) if err != nil { return "", err } if ti.Type != topo.TYPE_SPARE { return "", fmt.Errorf("Can only scrap spare tablets") } actionPath, err := wr.Scrap(tabletAlias, false, false) if err != nil { return "", err } return "", wr.WaitForCompletion(actionPath) }) actionRepo.RegisterTabletAction("ScrapTabletForce", acl.ADMIN, func(wr *wrangler.Wrangler, tabletAlias topo.TabletAlias, r *http.Request) (string, error) { // refuse to scrap tablets that are not spare ti, err := wr.TopoServer().GetTablet(tabletAlias) if err != nil { return "", err } if ti.Type != topo.TYPE_SPARE { return "", fmt.Errorf("Can only scrap spare tablets") } _, err = wr.Scrap(tabletAlias, true, false) return "", err }) actionRepo.RegisterTabletAction("DeleteTablet", acl.ADMIN, func(wr *wrangler.Wrangler, tabletAlias topo.TabletAlias, r *http.Request) (string, error) { return "", wr.DeleteTablet(tabletAlias) }) // toplevel index http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { templateLoader.ServeTemplate("index.html", indexContent, w, r) }) // keyspace actions http.HandleFunc("/keyspace_actions", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { httpError(w, "cannot parse form: %s", err) return } action := r.FormValue("action") if action == "" { http.Error(w, "no action provided", http.StatusBadRequest) return } keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "no keyspace provided", http.StatusBadRequest) return } result := actionRepo.ApplyKeyspaceAction(action, keyspace, r) templateLoader.ServeTemplate("action.html", result, w, r) }) // shard actions http.HandleFunc("/shard_actions", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { httpError(w, "cannot parse form: %s", err) return } action := r.FormValue("action") if action == "" { http.Error(w, "no action provided", http.StatusBadRequest) return } keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "no keyspace provided", http.StatusBadRequest) return } shard := r.FormValue("shard") if shard == "" { http.Error(w, "no shard provided", http.StatusBadRequest) return } result := actionRepo.ApplyShardAction(action, keyspace, shard, r) templateLoader.ServeTemplate("action.html", result, w, r) }) // tablet actions http.HandleFunc("/tablet_actions", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { httpError(w, "cannot parse form: %s", err) return } action := r.FormValue("action") if action == "" { http.Error(w, "no action provided", http.StatusBadRequest) return } alias := r.FormValue("alias") if alias == "" { http.Error(w, "no alias provided", http.StatusBadRequest) return } tabletAlias, err := topo.ParseTabletAliasString(alias) if err != nil { http.Error(w, "bad alias provided", http.StatusBadRequest) return } result := actionRepo.ApplyTabletAction(action, tabletAlias, r) templateLoader.ServeTemplate("action.html", result, w, r) }) // topology server http.HandleFunc("/dbtopo", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { httpError(w, "cannot parse form: %s", err) return } result := DbTopologyResult{} topology, err := topotools.DbTopology(wr.TopoServer()) if err != nil { result.Error = err.Error() } else { result.Topology = topology } templateLoader.ServeTemplate("dbtopo.html", result, w, r) }) // serving graph http.HandleFunc("/serving_graph/", func(w http.ResponseWriter, r *http.Request) { parts := strings.Split(r.URL.Path, "/") cell := parts[len(parts)-1] if cell == "" { cells, err := ts.GetKnownCells() if err != nil { httpError(w, "cannot get known cells: %v", err) return } else { templateLoader.ServeTemplate("serving_graph_cells.html", cells, w, r) } return } servingGraph := topotools.DbServingGraph(wr.TopoServer(), cell) templateLoader.ServeTemplate("serving_graph.html", servingGraph, w, r) }) // redirects for explorers http.HandleFunc("/explorers/redirect", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { httpError(w, "cannot parse form: %s", err) return } var explorer Explorer switch len(explorers) { case 0: http.Error(w, "no explorer configured", http.StatusInternalServerError) return case 1: for _, ex := range explorers { explorer = ex } default: explorerName := r.FormValue("explorer") var ok bool explorer, ok = explorers[explorerName] if !ok { http.Error(w, "bad explorer name", http.StatusBadRequest) return } } var target string switch r.FormValue("type") { case "keyspace": keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "keyspace is obligatory for this redirect", http.StatusBadRequest) return } target = explorer.GetKeyspacePath(keyspace) case "shard": keyspace, shard := r.FormValue("keyspace"), r.FormValue("shard") if keyspace == "" || shard == "" { http.Error(w, "keyspace and shard are obligatory for this redirect", http.StatusBadRequest) return } target = explorer.GetShardPath(keyspace, shard) case "srv_keyspace": cell := r.FormValue("cell") if cell == "" { http.Error(w, "cell is obligatory for this redirect", http.StatusBadRequest) return } keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "keyspace is obligatory for this redirect", http.StatusBadRequest) return } target = explorer.GetSrvKeyspacePath(cell, keyspace) case "srv_shard": cell := r.FormValue("cell") if cell == "" { http.Error(w, "cell is obligatory for this redirect", http.StatusBadRequest) return } keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "keyspace is obligatory for this redirect", http.StatusBadRequest) return } shard := r.FormValue("shard") if shard == "" { http.Error(w, "shard is obligatory for this redirect", http.StatusBadRequest) return } target = explorer.GetSrvShardPath(cell, keyspace, shard) case "srv_type": cell := r.FormValue("cell") if cell == "" { http.Error(w, "cell is obligatory for this redirect", http.StatusBadRequest) return } keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "keyspace is obligatory for this redirect", http.StatusBadRequest) return } shard := r.FormValue("shard") if shard == "" { http.Error(w, "shard is obligatory for this redirect", http.StatusBadRequest) return } tabletType := r.FormValue("tablet_type") if tabletType == "" { http.Error(w, "tablet_type is obligatory for this redirect", http.StatusBadRequest) return } target = explorer.GetSrvTypePath(cell, keyspace, shard, topo.TabletType(tabletType)) case "tablet": aliasName := r.FormValue("alias") if aliasName == "" { http.Error(w, "keyspace is obligatory for this redirect", http.StatusBadRequest) return } alias, err := topo.ParseTabletAliasString(aliasName) if err != nil { http.Error(w, "bad tablet alias", http.StatusBadRequest) return } target = explorer.GetTabletPath(alias) case "replication": cell := r.FormValue("cell") if cell == "" { http.Error(w, "cell is obligatory for this redirect", http.StatusBadRequest) return } keyspace := r.FormValue("keyspace") if keyspace == "" { http.Error(w, "keyspace is obligatory for this redirect", http.StatusBadRequest) return } shard := r.FormValue("shard") if shard == "" { http.Error(w, "shard is obligatory for this redirect", http.StatusBadRequest) return } target = explorer.GetReplicationSlaves(cell, keyspace, shard) default: http.Error(w, "bad redirect type", http.StatusBadRequest) return } http.Redirect(w, r, target, http.StatusFound) }) servenv.RunDefault() }