Пример #1
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	zknsDomain := flag.String("zkns-domain", "", "The naming hierarchy portion to serve")
	zknsRoot := flag.String("zkns-root", "", "The root path from which to resolve")
	bindAddr := flag.String("bind-addr", ":31981", "Bind the debug http server")
	flag.Parse()

	if *bindAddr != "" {
		go func() {
			err := http.ListenAndServe(*bindAddr, nil)
			if err != nil {
				log.Errorf("ListenAndServe: %s", err)
				exit.Return(1)
			}
		}()
	}

	zconn := zk.NewMetaConn()
	fqdn := netutil.FullyQualifiedHostnameOrPanic()
	zr1 := newZknsResolver(zconn, fqdn, *zknsDomain, *zknsRoot)
	pd := &pdns{zr1}
	pd.Serve(os.Stdin, os.Stdout)
	os.Stdout.Close()
}
Пример #2
0
func main() {
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	zkConfig := zkctl.MakeZkConfigFromString(*zkCfg, uint32(*myId))
	zkd := zkctl.NewZkd(zkConfig)

	action := flag.Arg(0)
	var err error
	switch action {
	case "init":
		err = zkd.Init()
	case "shutdown":
		err = zkd.Shutdown()
	case "start":
		err = zkd.Start()
	case "teardown":
		err = zkd.Teardown()
	default:
		log.Fatalf("invalid action: %v", action)
	}
	if err != nil {
		log.Fatalf("failed %v: %v", action, err)
	}
}
Пример #3
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()

	zkConfig := zkctl.MakeZkConfigFromString(*zkCfg, uint32(*myId))
	zkd := zkctl.NewZkd(zkConfig)

	if zkd.Inited() {
		log.Infof("already initialized, starting without init...")
		if err := zkd.Start(); err != nil {
			log.Errorf("failed start: %v", err)
			exit.Return(255)
		}
	} else {
		log.Infof("initializing...")
		if err := zkd.Init(); err != nil {
			log.Errorf("failed init: %v", err)
			exit.Return(255)
		}
	}

	log.Infof("waiting for signal or server shutdown...")
	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	select {
	case <-zkd.Done():
		log.Infof("server shut down on its own")
	case <-sig:
		log.Infof("signal received, shutting down server")
		zkd.Shutdown()
	}
}
Пример #4
0
func main() {
	defer exit.RecoverAll()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()
	if len(args) != 0 {
		flag.Usage()
		os.Exit(1)
	}

	if *fromTopo == "" || *toTopo == "" {
		log.Errorf("Need both from and to topo")
		exit.Return(1)
	}

	ctx := context.Background()
	fromTS := topo.GetServerByName(*fromTopo)
	toTS := topo.GetServerByName(*toTopo)

	if *doKeyspaces {
		helpers.CopyKeyspaces(ctx, fromTS.Impl, toTS.Impl)
	}
	if *doShards {
		helpers.CopyShards(ctx, fromTS.Impl, toTS.Impl, *deleteKeyspaceShards)
	}
	if *doShardReplications {
		helpers.CopyShardReplications(ctx, fromTS.Impl, toTS.Impl)
	}
	if *doTablets {
		helpers.CopyTablets(ctx, fromTS.Impl, toTS.Impl)
	}
}
Пример #5
0
func main() {
	defer exit.RecoverAll()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()
	if len(args) != 0 {
		flag.Usage()
		os.Exit(1)
	}

	if *fromTopo == "" || *toTopo == "" {
		log.Fatalf("Need both from and to topo")
	}

	fromTS := topo.GetServerByName(*fromTopo)
	toTS := topo.GetServerByName(*toTopo)

	if *doKeyspaces {
		helpers.CopyKeyspaces(fromTS, toTS)
	}
	if *doShards {
		helpers.CopyShards(fromTS, toTS, *deleteKeyspaceShards)
	}
	if *doShardReplications {
		helpers.CopyShardReplications(fromTS, toTS)
	}
	if *doTablets {
		helpers.CopyTablets(fromTS, toTS)
	}
}
Пример #6
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %v:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, doc)
	}
	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	if *zkAddrs != "" {
		var err error
		zconn, _, err = zk.DialZkTimeout(*zkAddrs, 5*time.Second, 10*time.Second)
		if err != nil {
			log.Errorf("zk connect failed: %v", err.Error())
			exit.Return(1)
		}
	}

	cmdName := args[0]
	args = args[1:]
	if cmd, ok := cmdMap[cmdName]; ok {
		subFlags := flag.NewFlagSet(cmdName, flag.ExitOnError)
		if err := cmd(subFlags, args); err != nil {
			log.Error(err)
			exit.Return(1)
		}
	}
}
Пример #7
0
func init() {
	onInit(func() {
		http.HandleFunc("/debug/flushlogs", func(w http.ResponseWriter, r *http.Request) {
			logutil.Flush()
			fmt.Fprint(w, "flushed")
		})
	})
}
Пример #8
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	dbconfigs.RegisterFlags()
	flag.Parse()

	mycnf := mysqlctl.NewMycnf(uint32(*tabletUid), *mysqlPort)
	if *mysqlSocket != "" {
		mycnf.SocketFile = *mysqlSocket
	}

	dbcfgs, err := dbconfigs.Init(mycnf.SocketFile)
	if err != nil {
		log.Errorf("%v", err)
		exit.Return(255)
	}
	mysqld = mysqlctl.NewMysqld("Dba", mycnf, &dbcfgs.Dba, &dbcfgs.Repl)

	// Register OnTerm handler before mysqld starts, so we get notified if mysqld
	// dies on its own without us (or our RPC client) telling it to.
	mysqldTerminated := make(chan struct{})
	mysqld.OnTerm(func() {
		close(mysqldTerminated)
	})

	// Start or Init mysqld as needed.
	if _, err = os.Stat(mycnf.DataDir); os.IsNotExist(err) {
		log.Infof("mysql data dir (%s) doesn't exist, initializing", mycnf.DataDir)
		mysqld.Init(*waitTime, *bootstrapArchive, *skipSchema)
	} else {
		log.Infof("mysql data dir (%s) already exists, starting without init", mycnf.DataDir)
		mysqld.Start(*waitTime)
	}

	servenv.Init()
	defer servenv.Close()

	// Take mysqld down with us on SIGTERM before entering lame duck.
	servenv.OnTerm(func() {
		log.Infof("mysqlctl received SIGTERM, shutting down mysqld first")
		mysqld.Shutdown(false, 0)
	})

	// Start RPC server and wait for SIGTERM.
	mysqlctldTerminated := make(chan struct{})
	go func() {
		servenv.RunDefault()
		close(mysqlctldTerminated)
	}()

	select {
	case <-mysqldTerminated:
		log.Infof("mysqld shut down on its own, exiting mysqlctld")
	case <-mysqlctldTerminated:
		log.Infof("mysqlctld shut down gracefully")
	}
}
Пример #9
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Error(err)
			exit.Return(1)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	ctx := context.Background()
	if *mode == "getSrvKeyspaceNames" {
		rpcClient := connect()
		if len(args) == 1 {
			getSrvKeyspaceNames(ctx, rpcClient, args[0], true)
		} else {
			log.Errorf("getSrvKeyspaceNames only takes one argument")
			exit.Return(1)
		}

	} else if *mode == "getSrvKeyspace" {
		rpcClient := connect()
		if len(args) == 2 {
			getSrvKeyspace(ctx, rpcClient, args[0], args[1], true)
		} else {
			log.Errorf("getSrvKeyspace only takes two arguments")
			exit.Return(1)
		}

	} else if *mode == "getEndPoints" {
		rpcClient := connect()
		if len(args) == 4 {
			getEndPoints(ctx, rpcClient, args[0], args[1], args[2], args[3], true)
		} else {
			log.Errorf("getEndPoints only takes four arguments")
			exit.Return(1)
		}

	} else if *mode == "qps" {
		qps(ctx, args[0], args[1:])

	} else {
		flag.Usage()
		log.Errorf("Invalid mode: %v", *mode)
		exit.Return(1)
	}
}
Пример #10
0
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()
}
Пример #11
0
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)
			}
		}
	}
}
Пример #12
0
func main() {
	defer logutil.Flush()

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [global parameters] command [command parameters]\n", os.Args[0])

		fmt.Fprintf(os.Stderr, "\nThe global optional parameters are:\n")
		flag.PrintDefaults()

		fmt.Fprintf(os.Stderr, "\nThe commands are listed below. Use '%s <command> -h' for more help.\n\n", os.Args[0])
		for _, cmd := range commands {
			fmt.Fprintf(os.Stderr, "  %s", cmd.name)
			if cmd.params != "" {
				fmt.Fprintf(os.Stderr, " %s", cmd.params)
			}
			fmt.Fprintf(os.Stderr, "\n")
		}
		fmt.Fprintf(os.Stderr, "\n")
	}
	dbconfigs.RegisterFlags()
	flag.Parse()

	tabletAddr = fmt.Sprintf("%v:%v", "localhost", *port)
	mycnf := mysqlctl.NewMycnf(uint32(*tabletUid), *mysqlPort)

	if *mysqlSocket != "" {
		mycnf.SocketFile = *mysqlSocket
	}

	dbcfgs, err := dbconfigs.Init(mycnf.SocketFile)
	if err != nil {
		log.Fatalf("%v", err)
	}
	mysqld := mysqlctl.NewMysqld("Dba", mycnf, &dbcfgs.Dba, &dbcfgs.Repl)
	defer mysqld.Close()

	action := flag.Arg(0)
	for _, cmd := range commands {
		if cmd.name == action {
			subFlags := flag.NewFlagSet(action, flag.ExitOnError)
			subFlags.Usage = func() {
				fmt.Fprintf(os.Stderr, "Usage: %s %s %s\n\n", os.Args[0], cmd.name, cmd.params)
				fmt.Fprintf(os.Stderr, "%s\n\n", cmd.help)
				subFlags.PrintDefaults()
			}

			cmd.method(mysqld, subFlags, flag.Args()[1:])
			return
		}
	}
	log.Fatalf("invalid action: %v", action)
}
Пример #13
0
func main() {
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	zkConfig := zkctl.MakeZkConfigFromString(*zkCfg, uint32(*myId))
	zkd := zkctl.NewZkd(zkConfig)

	action := flag.Arg(0)
	var err error
	var waitForSignal bool
	switch action {
	case "init":
		err = zkd.Init()
		waitForSignal = *follow
	case "shutdown":
		err = zkd.Shutdown()
	case "start":
		err = zkd.Start()
		waitForSignal = *follow
	case "teardown":
		err = zkd.Teardown()
	default:
		log.Fatalf("invalid action: %v", action)
	}
	if err != nil {
		log.Fatalf("failed %v: %v", action, err)
	}

	if waitForSignal {
		log.Infof("waiting for signal or server shutdown...")
		sig := make(chan os.Signal)
		signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
		select {
		case <-zkd.Done():
			log.Infof("server shut down on its own")
		case <-sig:
			log.Infof("signal received, shutting down server")
			zkd.Shutdown()
		}
	}
}
Пример #14
0
func main() {
	defer logutil.Flush()

	dbconfigs.RegisterFlags()
	flag.Parse()

	dbcfgs, err := dbconfigs.Init(*mysqlSocketFile)
	if err != nil {
		log.Fatalf("Failed to init dbconfigs: %v", err)
	}

	pc := primecache.NewPrimeCache(dbcfgs, *relayLogsPath)
	pc.WorkerCount = *workerCount
	pc.SleepDuration = *sleepDuration

	pc.Loop()
}
Пример #15
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [global parameters] command [command parameters]\n", os.Args[0])

		fmt.Fprintf(os.Stderr, "\nThe global optional parameters are:\n")
		flag.PrintDefaults()

		fmt.Fprintf(os.Stderr, "\nThe commands are listed below. Use '%s <command> -h' for more help.\n\n", os.Args[0])
		for _, cmd := range commands {
			fmt.Fprintf(os.Stderr, "  %s", cmd.name)
			if cmd.params != "" {
				fmt.Fprintf(os.Stderr, " %s", cmd.params)
			}
			fmt.Fprintf(os.Stderr, "\n")
		}
		fmt.Fprintf(os.Stderr, "\n")
	}

	dbconfigs.RegisterFlags(dbconfigFlags)
	flag.Parse()

	tabletAddr = netutil.JoinHostPort("localhost", int32(*port))

	action := flag.Arg(0)
	for _, cmd := range commands {
		if cmd.name == action {
			subFlags := flag.NewFlagSet(action, flag.ExitOnError)
			subFlags.Usage = func() {
				fmt.Fprintf(os.Stderr, "Usage: %s %s %s\n\n", os.Args[0], cmd.name, cmd.params)
				fmt.Fprintf(os.Stderr, "%s\n\n", cmd.help)
				subFlags.PrintDefaults()
			}

			if err := cmd.method(subFlags, flag.Args()[1:]); err != nil {
				log.Error(err)
				exit.Return(1)
			}
			return
		}
	}
	log.Errorf("invalid action: %v", action)
	exit.Return(1)
}
Пример #16
0
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)
	}
}
Пример #17
0
func main() {
	defer logutil.Flush()
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %v:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, doc)
	}
	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	if *zkAddrs != "" {
		if *zkoccAddr != "" {
			log.Fatalf("zk.addrs and zk.zkocc-addr are mutually exclusive")
		}
		var err error
		zconn, _, err = zk.DialZkTimeout(*zkAddrs, 5*time.Second, 10*time.Second)
		if err != nil {
			log.Fatalf("zk connect failed: %v", err.Error())
		}
	}

	if *zkoccAddr != "" {
		var err error
		zconn, err = zk.DialZkocc(*zkoccAddr, 5*time.Second)
		if err != nil {
			log.Fatalf("zkocc connect failed: %v", err.Error())
		}
	}

	cmdName := args[0]
	args = args[1:]
	if cmd, ok := cmdMap[cmdName]; ok {
		subFlags := flag.NewFlagSet(cmdName, flag.ExitOnError)
		cmd(subFlags, args)
	}
}
Пример #18
0
func TestError(t *testing.T) {
	env := setup("echo ERROR expected error $* 1>&2", 1)
	defer cleanup(env)

	mbl := &MysqlBinlog{}
	out, err := mbl.Launch("db", "name", 10)
	if err != nil {
		panic(err)
	}
	ioutil.ReadAll(out)
	mbl.Wait()
	logutil.Flush()
	warnbytes, err := ioutil.ReadFile(path.Join(os.TempDir(), "binlog.test.INFO"))
	if err != nil {
		t.Error(err)
	}
	got := string(warnbytes)
	want := "ERROR expected error --database=db --start-position=10 name"
	if !strings.Contains(got, want) {
		t.Errorf("want '%s' in '%s'", want, got)
	}
}
Пример #19
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flags := dbconfigs.AppConfig | dbconfigs.DbaConfig |
		dbconfigs.FilteredConfig | dbconfigs.ReplConfig
	dbconfigs.RegisterFlags(flags)
	flag.Parse()

	mycnf := mysqlctl.NewMycnf(uint32(*tabletUID), int32(*mysqlPort))
	if *mysqlSocket != "" {
		mycnf.SocketFile = *mysqlSocket
	}

	dbcfgs, err := dbconfigs.Init(mycnf.SocketFile, flags)
	if err != nil {
		log.Errorf("%v", err)
		exit.Return(255)
	}
	mysqld = mysqlctl.NewMysqld("Dba", "App", mycnf, &dbcfgs.Dba, &dbcfgs.App.ConnParams, &dbcfgs.Repl)

	// Register OnTerm handler before mysqld starts, so we get notified if mysqld
	// dies on its own without us (or our RPC client) telling it to.
	mysqldTerminated := make(chan struct{})
	mysqld.OnTerm(func() {
		close(mysqldTerminated)
	})

	// Start or Init mysqld as needed.
	ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
	if _, err = os.Stat(mycnf.DataDir); os.IsNotExist(err) {
		log.Infof("mysql data dir (%s) doesn't exist, initializing", mycnf.DataDir)
		mysqld.Init(ctx, *initDBSQLFile)
	} else {
		log.Infof("mysql data dir (%s) already exists, starting without init", mycnf.DataDir)
		mysqld.Start(ctx)
	}
	cancel()

	servenv.Init()
	defer servenv.Close()

	// Take mysqld down with us on SIGTERM before entering lame duck.
	servenv.OnTerm(func() {
		log.Infof("mysqlctl received SIGTERM, shutting down mysqld first")
		ctx := context.Background()
		mysqld.Shutdown(ctx, false)
	})

	// Start RPC server and wait for SIGTERM.
	mysqlctldTerminated := make(chan struct{})
	go func() {
		servenv.RunDefault()
		close(mysqlctldTerminated)
	}()

	select {
	case <-mysqldTerminated:
		log.Infof("mysqld shut down on its own, exiting mysqlctld")
	case <-mysqlctldTerminated:
		log.Infof("mysqlctld shut down gracefully")
	}
}
Пример #20
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	connStr := fmt.Sprintf(`{"address": "%s", "tablet_type": "%s", "streaming": %v, "timeout": %d}`, *server, *tabletType, *streaming, int64(30*(*timeout)))
	db, err := sql.Open("vitess", connStr)
	if err != nil {
		log.Errorf("client error: %v", err)
		exit.Return(1)
	}

	log.Infof("Sending the query...")
	now := time.Now()

	// handle dml
	if isDml(args[0]) {
		tx, err := db.Begin()
		if err != nil {
			log.Errorf("begin failed: %v", err)
			exit.Return(1)
		}

		result, err := db.Exec(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("exec failed: %v", err)
			exit.Return(1)
		}

		err = tx.Commit()
		if err != nil {
			log.Errorf("commit failed: %v", err)
			exit.Return(1)
		}

		rowsAffected, err := result.RowsAffected()
		lastInsertId, err := result.LastInsertId()
		log.Infof("Total time: %v / Row affected: %v / Last Insert Id: %v", time.Now().Sub(now), rowsAffected, lastInsertId)
	} else {

		// launch the query
		rows, err := db.Query(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		defer rows.Close()

		// print the headers
		cols, err := rows.Columns()
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		line := "Index"
		for _, field := range cols {
			line += "\t" + field
		}
		fmt.Printf("%s\n", line)

		// get the rows
		rowIndex := 0
		for rows.Next() {
			row := make([]interface{}, len(cols))
			for i := range row {
				var col string
				row[i] = &col
			}
			if err := rows.Scan(row...); err != nil {
				log.Errorf("client error: %v", err)
				exit.Return(1)
			}

			// print the line
			line := fmt.Sprintf("%d", rowIndex)
			for _, value := range row {
				line += fmt.Sprintf("\t%v", *(value.(*string)))
			}
			fmt.Printf("%s\n", line)
			rowIndex++
		}
		if err := rows.Err(); err != nil {
			log.Errorf("Error %v\n", err)
			exit.Return(1)
		}
		log.Infof("Total time: %v / Row count: %v", time.Now().Sub(now), rowIndex)
	}
}
Пример #21
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	dbconfigFlags := dbconfigs.AppConfig | dbconfigs.DbaConfig |
		dbconfigs.FilteredConfig | dbconfigs.ReplConfig
	dbconfigs.RegisterFlags(dbconfigFlags)
	flag.Parse()

	// We'll register this OnTerm handler before mysqld starts, so we get notified
	// if mysqld dies on its own without us (or our RPC client) telling it to.
	mysqldTerminated := make(chan struct{})
	onTermFunc := func() {
		close(mysqldTerminated)
	}

	// Start or Init mysqld as needed.
	ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
	tabletDir := mysqlctl.TabletDir(uint32(*tabletUID))
	if _, statErr := os.Stat(tabletDir); os.IsNotExist(statErr) {
		// Generate my.cnf from scratch and use it to find mysqld.
		log.Infof("tablet dir (%s) doesn't exist, initializing", tabletDir)

		var err error
		mysqld, err = mysqlctl.CreateMysqld(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort), dbconfigFlags)
		if err != nil {
			log.Errorf("failed to initialize mysql config: %v", err)
			exit.Return(1)
		}
		mysqld.OnTerm(onTermFunc)

		if err := mysqld.Init(ctx, *initDBSQLFile); err != nil {
			log.Errorf("failed to initialize mysql data dir and start mysqld: %v", err)
			exit.Return(1)
		}
	} else {
		// There ought to be an existing my.cnf, so use it to find mysqld.
		log.Infof("tablet dir (%s) already exists, starting without init", tabletDir)

		var err error
		mysqld, err = mysqlctl.OpenMysqld(uint32(*tabletUID), dbconfigFlags)
		if err != nil {
			log.Errorf("failed to find mysql config: %v", err)
			exit.Return(1)
		}
		mysqld.OnTerm(onTermFunc)

		if err := mysqld.Start(ctx); err != nil {
			log.Errorf("failed to start mysqld: %v", err)
			exit.Return(1)
		}
	}
	cancel()

	servenv.Init()
	defer servenv.Close()

	// Take mysqld down with us on SIGTERM before entering lame duck.
	servenv.OnTerm(func() {
		log.Infof("mysqlctl received SIGTERM, shutting down mysqld first")
		ctx := context.Background()
		mysqld.Shutdown(ctx, false)
	})

	// Start RPC server and wait for SIGTERM.
	mysqlctldTerminated := make(chan struct{})
	go func() {
		servenv.RunDefault()
		close(mysqlctldTerminated)
	}()

	select {
	case <-mysqldTerminated:
		log.Infof("mysqld shut down on its own, exiting mysqlctld")
	case <-mysqlctldTerminated:
		log.Infof("mysqlctld shut down gracefully")
	}
}
Пример #22
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	c := vitessdriver.Configuration{
		Protocol:   *vtgateconn.VtgateProtocol,
		Address:    *server,
		Keyspace:   *keyspace,
		Shard:      *shard,
		TabletType: *tabletType,
		Timeout:    *timeout,
		Streaming:  *streaming,
	}
	db, err := vitessdriver.OpenWithConfiguration(c)
	if err != nil {
		log.Errorf("client error: %v", err)
		exit.Return(1)
	}

	log.Infof("Sending the query...")
	startTime := time.Now()

	// handle dml
	if isDml(args[0]) {
		tx, err := db.Begin()
		if err != nil {
			log.Errorf("begin failed: %v", err)
			exit.Return(1)
		}

		result, err := db.Exec(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("exec failed: %v", err)
			exit.Return(1)
		}

		err = tx.Commit()
		if err != nil {
			log.Errorf("commit failed: %v", err)
			exit.Return(1)
		}

		rowsAffected, err := result.RowsAffected()
		lastInsertID, err := result.LastInsertId()
		log.Infof("Total time: %v / Row affected: %v / Last Insert Id: %v", time.Since(startTime), rowsAffected, lastInsertID)
	} else {
		// launch the query
		rows, err := db.Query(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		defer rows.Close()

		// get the headers
		var qr results
		cols, err := rows.Columns()
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		qr.Fields = cols

		// get the rows
		for rows.Next() {
			row := make([]interface{}, len(cols))
			for i := range row {
				var col string
				row[i] = &col
			}
			if err := rows.Scan(row...); err != nil {
				log.Errorf("client error: %v", err)
				exit.Return(1)
			}

			// unpack []*string into []string
			vals := make([]string, 0, len(row))
			for _, value := range row {
				vals = append(vals, *(value.(*string)))
			}
			qr.Rows = append(qr.Rows, vals)
		}
		if err := rows.Err(); err != nil {
			log.Errorf("Error %v\n", err)
			exit.Return(1)
		}

		if *jsonOutput {
			data, err := json.MarshalIndent(qr, "", "  ")
			if err != nil {
				log.Errorf("cannot marshal data: %v", err)
				exit.Return(1)
			}
			fmt.Print(string(data))
		} else {
			printTable(qr, time.Since(startTime))
		}
	}
}
Пример #23
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		exit.Return(1)
	}

	c := vitessdriver.Configuration{
		Protocol:   *vtgateconn.VtgateProtocol,
		Address:    *server,
		Keyspace:   *keyspace,
		Shard:      *shard,
		TabletType: *tabletType,
		Timeout:    *timeout,
		Streaming:  *streaming,
	}
	db, err := vitessdriver.OpenWithConfiguration(c)
	if err != nil {
		log.Errorf("client error: %v", err)
		exit.Return(1)
	}

	log.Infof("Sending the query...")
	now := time.Now()

	// handle dml
	if isDml(args[0]) {
		tx, err := db.Begin()
		if err != nil {
			log.Errorf("begin failed: %v", err)
			exit.Return(1)
		}

		result, err := db.Exec(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("exec failed: %v", err)
			exit.Return(1)
		}

		err = tx.Commit()
		if err != nil {
			log.Errorf("commit failed: %v", err)
			exit.Return(1)
		}

		rowsAffected, err := result.RowsAffected()
		lastInsertID, err := result.LastInsertId()
		log.Infof("Total time: %v / Row affected: %v / Last Insert Id: %v", time.Now().Sub(now), rowsAffected, lastInsertID)
	} else {

		// launch the query
		rows, err := db.Query(args[0], []interface{}(*bindVariables)...)
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		defer rows.Close()

		// print the headers
		cols, err := rows.Columns()
		if err != nil {
			log.Errorf("client error: %v", err)
			exit.Return(1)
		}
		line := "Index"
		for _, field := range cols {
			line += "\t" + field
		}
		fmt.Printf("%s\n", line)

		// get the rows
		rowIndex := 0
		for rows.Next() {
			row := make([]interface{}, len(cols))
			for i := range row {
				var col string
				row[i] = &col
			}
			if err := rows.Scan(row...); err != nil {
				log.Errorf("client error: %v", err)
				exit.Return(1)
			}

			// print the line
			line := fmt.Sprintf("%d", rowIndex)
			for _, value := range row {
				line += fmt.Sprintf("\t%v", *(value.(*string)))
			}
			fmt.Printf("%s\n", line)
			rowIndex++
		}
		if err := rows.Err(); err != nil {
			log.Errorf("Error %v\n", err)
			exit.Return(1)
		}
		log.Infof("Total time: %v / Row count: %v", time.Now().Sub(now), rowIndex)
	}
}
Пример #24
0
func main() {
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *mode == "get" {
		rpcClient := connect()
		if len(args) == 1 {
			get(rpcClient, args[0], true)
		} else {
			getv(rpcClient, args, true)
		}

	} else if *mode == "children" {
		rpcClient := connect()
		children(rpcClient, args, true)

	} else if *mode == "getSrvKeyspaceNames" {
		rpcClient := connect()
		if len(args) == 1 {
			getSrvKeyspaceNames(rpcClient, args[0], true)
		} else {
			log.Fatalf("getSrvKeyspaceNames only takes one argument")
		}

	} else if *mode == "getSrvKeyspace" {
		rpcClient := connect()
		if len(args) == 2 {
			getSrvKeyspace(rpcClient, args[0], args[1], true)
		} else {
			log.Fatalf("getSrvKeyspace only takes two arguments")
		}

	} else if *mode == "getEndPoints" {
		rpcClient := connect()
		if len(args) == 4 {
			getEndPoints(rpcClient, args[0], args[1], args[2], args[3], true)
		} else {
			log.Fatalf("getEndPoints only takes four arguments")
		}

	} else if *mode == "qps" {
		qps(args)

	} else if *mode == "qps2" {
		qps2(args[0], args[1:])

	} else {
		flag.Usage()
		log.Fatalf("Invalid mode: %v", *mode)
	}
}
Пример #25
0
func main() {
	defer exit.Recover()
	defer logutil.Flush()

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [global parameters] command [command parameters]\n", os.Args[0])

		fmt.Fprintf(os.Stderr, "\nThe global optional parameters are:\n")
		flag.PrintDefaults()

		fmt.Fprintf(os.Stderr, "\nThe commands are listed below. Use '%s <command> -h' for more help.\n\n", os.Args[0])
		for _, cmd := range commands {
			fmt.Fprintf(os.Stderr, "  %s", cmd.name)
			if cmd.params != "" {
				fmt.Fprintf(os.Stderr, " %s", cmd.params)
			}
			fmt.Fprintf(os.Stderr, "\n")
		}
		fmt.Fprintf(os.Stderr, "\n")
	}

	flags := dbconfigs.AppConfig | dbconfigs.DbaConfig |
		dbconfigs.FilteredConfig | dbconfigs.ReplConfig
	dbconfigs.RegisterFlags(flags)
	flag.Parse()

	tabletAddr = netutil.JoinHostPort("localhost", int32(*port))
	mycnf := mysqlctl.NewMycnf(uint32(*tabletUID), int32(*mysqlPort))

	if *mysqlSocket != "" {
		mycnf.SocketFile = *mysqlSocket
	}

	dbcfgs, err := dbconfigs.Init(mycnf.SocketFile, flags)
	if err != nil {
		log.Errorf("%v", err)
		exit.Return(1)
	}
	mysqld := mysqlctl.NewMysqld("Dba", "App", mycnf, &dbcfgs.Dba, &dbcfgs.App.ConnParams, &dbcfgs.Repl)
	defer mysqld.Close()

	action := flag.Arg(0)
	for _, cmd := range commands {
		if cmd.name == action {
			subFlags := flag.NewFlagSet(action, flag.ExitOnError)
			subFlags.Usage = func() {
				fmt.Fprintf(os.Stderr, "Usage: %s %s %s\n\n", os.Args[0], cmd.name, cmd.params)
				fmt.Fprintf(os.Stderr, "%s\n\n", cmd.help)
				subFlags.PrintDefaults()
			}

			if err := cmd.method(mysqld, subFlags, flag.Args()[1:]); err != nil {
				log.Error(err)
				exit.Return(1)
			}
			return
		}
	}
	log.Errorf("invalid action: %v", action)
	exit.Return(1)
}
Пример #26
0
func main() {
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()

	if len(args) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	client2.RegisterShardedDrivers()
	conn, err := db.Open(*driver, *server)
	if err != nil {
		log.Fatalf("client error: %v", err)
	}

	log.Infof("Sending the query...")
	now := time.Now()

	// handle dml
	if isDml(args[0]) {
		t, err := conn.Begin()
		if err != nil {
			log.Fatalf("begin failed: %v", err)
		}

		r, err := conn.Exec(args[0], bindvars)
		if err != nil {
			log.Fatalf("exec failed: %v", err)
		}

		err = t.Commit()
		if err != nil {
			log.Fatalf("commit failed: %v", err)
		}

		n, err := r.RowsAffected()
		log.Infof("Total time: %v / Row affected: %v", time.Now().Sub(now), n)
	} else {

		// launch the query
		r, err := conn.Exec(args[0], bindvars)
		if err != nil {
			log.Fatalf("client error: %v", err)
		}

		// get the headers
		cols := r.Columns()
		if err != nil {
			log.Fatalf("client error: %v", err)
		}

		// print the header
		if *verbose {
			line := "Index"
			for _, field := range cols {
				line += "\t" + field
			}
			log.Infof(line)
		}

		// get the rows
		rowIndex := 0
		for row := r.Next(); row != nil; row = r.Next() {
			// print the line if needed
			if *verbose {
				line := fmt.Sprintf("%d", rowIndex)
				for _, value := range row {
					if value != nil {
						switch value.(type) {
						case []byte:
							line += fmt.Sprintf("\t%s", value)
						default:
							line += fmt.Sprintf("\t%v", value)
						}
					} else {
						line += "\t"
					}
				}
				log.Infof(line)
			}
			rowIndex++
		}
		if err := r.Err(); err != nil {
			log.Fatalf("Error %v\n", err)
		}
		log.Infof("Total time: %v / Row count: %v", time.Now().Sub(now), rowIndex)
	}
}