コード例 #1
0
ファイル: main.go プロジェクト: CowLeo/qdb
func main() {
	usage := `
Usage:
    qdb-server [options]

Options:
    -L logfile                        log file path, if empty, use stdout
    -n N, --ncpu=N                    set runtime.GOMAXPROCS to N
    -c CONF, --config=CONF            specify the config file
    --repair                          repair database
    --dbtype=TYPE                     dtabase type, like rocksdb, leveldb, goleveldb	
    --dbpath=PATH                     database store path						
    --addr=ADDR                       service listening address	
    --auth=AUTH                       service auth
    --pidfile=FILE                    service pid file 
    --conn_timeout=N                  connection timeout after N seconds
    --dump_path=PATH                  path saving snapshot rdb file
    --sync_file_path=PATH             path saving replication syncing data
    --sync_file_size=SIZE             maximum file(bytes) size for replication syncing 
    --sync_buff_size=SIZE             maximum memory buffer size(bytes) for replication syncing
    --repl_backlog_file_path=PATH     path saving replication backlog data, if empty, use memory instead
    --repl_backlog_size=SIZE          maximum backlog size(bytes)
    --repl_ping_slave_period=N        Master pings slave in an interval(seconds) when replication
    --master_auth=MASTERAUTH          Master auth for replication
`
	d, err := docopt.Parse(usage, nil, true, "", false)
	if err != nil {
		log.Fatalf("parse arguments failed - %s", err)
	}

	if s, ok := d["-L"].(string); ok && len(s) > 0 {
		log.SetHighlighting(false)
		err = log.SetOutputByName(s)
		if err != nil {
			log.Fatalf("set log name failed - %s", err)
		}
	}

	if s, ok := d["--ncpu"].(string); ok && len(s) != 0 {
		if n, err := strconv.ParseInt(s, 10, 64); err != nil {
			log.Fatalf("parse --ncpu failed - %s", err)
		} else if n <= 0 || n > 64 {
			log.Fatalf("parse --ncpu = %d, only accept [1,64]", n)
		} else {
			runtime.GOMAXPROCS(int(n))
		}
	}

	args.config, _ = d["--config"].(string)
	args.repair, _ = d["--repair"].(bool)

	conf := &Config{
		DBType:    "goleveldb",
		DBPath:    "./var/testdb-goleveldb",
		LevelDB:   leveldb.NewDefaultConfig(),
		RocksDB:   rocksdb.NewDefaultConfig(),
		GoLevelDB: goleveldb.NewDefaultConfig(),
		Service:   service.NewDefaultConfig(),
	}

	if args.config != "" {
		if err := conf.LoadFromFile(args.config); err != nil {
			log.Fatalf("load config failed - %s", err)
		}
	}

	setStringFromOpt(&conf.DBType, d, "--dbtype")
	setStringFromOpt(&conf.DBPath, d, "--dbpath")

	setStringFromOpt(&conf.Service.Listen, d, "--addr")
	setStringFromOpt(&conf.Service.PidFile, d, "--pidfile")
	setStringFromOpt(&conf.Service.Auth, d, "--auth")
	setIntFromOpt(&conf.Service.ConnTimeout, d, "--conn_timeout")
	setStringFromOpt(&conf.Service.DumpPath, d, "--dump_path")
	setStringFromOpt(&conf.Service.SyncFilePath, d, "--sync_file_path")
	setIntFromOpt(&conf.Service.SyncFileSize, d, "--sync_file_size")
	setIntFromOpt(&conf.Service.SyncBuffSize, d, "--sync_buff_size")
	setStringFromOpt(&conf.Service.ReplBacklogFilePath, d, "--repl_backlog_file_path")
	setIntFromOpt(&conf.Service.ReplBacklogSize, d, "--repl_backlog_size")
	setIntFromOpt(&conf.Service.ReplPingSlavePeriod, d, "--repl_ping_slave_period")
	setStringFromOpt(&conf.Service.MasterAuth, d, "--master_auth")

	log.Infof("load config\n%s\n\n", conf)

	var db engine.Database
	var dbConf interface{}
	switch t := strings.ToLower(conf.DBType); t {
	default:
		log.Fatalf("unknown db type = '%s'", conf.DBType)
	case "leveldb":
		dbConf = conf.LevelDB
	case "rocksdb":
		dbConf = conf.RocksDB
	case "goleveldb":
		dbConf = conf.GoLevelDB
	}

	db, err = engine.Open(conf.DBType, conf.DBPath, dbConf, args.repair)

	if err != nil {
		log.Fatalf("open database failed - %s", err)
	}

	dbStore := store.New(db)

	if args.repair {
		return
	}

	server, err := service.NewServer(conf.Service, dbStore)
	if err != nil {
		log.Fatalf("create server failed - %s", err)
	}

	// create pid file
	createPidFile(conf.Service.PidFile)

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, os.Interrupt, os.Kill)

	go func(s *service.Server) {
		for _ = range c {
			log.Infof("interrupt and shutdown")

			// close server
			s.Close()

			// shutdown gracefully, remove pidfile
			os.Remove(conf.Service.PidFile)

			os.Exit(0)
		}
	}(server)

	if err := server.Serve(); err != nil {
		log.Errorf("service failed - %s", err)
	}

	server.Close()
}
コード例 #2
0
ファイル: main.go プロジェクト: pingcap/tidb
func main() {
	tidb.RegisterLocalStore("boltdb", boltdb.Driver{})
	tidb.RegisterStore("tikv", tikv.Driver{})

	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()
	if *version {
		printer.PrintRawTiDBInfo()
		os.Exit(0)
	}

	leaseDuration := parseLease()
	tidb.SetSchemaLease(leaseDuration)

	cfg := &server.Config{
		Addr:         fmt.Sprintf("%s:%s", *host, *port),
		LogLevel:     *logLevel,
		StatusAddr:   fmt.Sprintf(":%s", *statusPort),
		Socket:       *socket,
		ReportStatus: *reportStatus,
	}

	// set log options
	if len(*logFile) > 0 {
		err := log.SetOutputByName(*logFile)
		if err != nil {
			log.Fatal(errors.ErrorStack(err))
		}
		log.SetRotateByDay()
		log.SetHighlighting(false)
	}

	if joinCon != nil && *joinCon > 0 {
		plan.JoinConcurrency = *joinCon
	}
	plan.AllowCartesianProduct = *crossJoin
	// Call this before setting log level to make sure that TiDB info could be printed.
	printer.PrintTiDBInfo()
	log.SetLevelByString(cfg.LogLevel)

	store := createStore()

	if *enablePS {
		perfschema.EnablePerfSchema()
	}
	if *binlogSocket != "" {
		createBinlogClient()
	}

	// Create a session to load information schema.
	se, err := tidb.CreateSession(store)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}
	se.Close()

	var driver server.IDriver
	driver = server.NewTiDBDriver(store)
	var svr *server.Server
	svr, err = server.NewServer(cfg, driver)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}

	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)

	go func() {
		sig := <-sc
		log.Infof("Got signal [%d] to exit.", sig)
		svr.Close()
		os.Exit(0)
	}()

	go systimemon.StartMonitor(time.Now, func() {
		log.Error("error: system time jump backward")
	})

	pushMetric(*metricsAddr, time.Duration(*metricsInterval)*time.Second)

	log.Error(svr.Run())
}
コード例 #3
0
ファイル: main.go プロジェクト: vebin/reborn
func main() {
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn agent v0.1", true)
	if err != nil {
		log.Fatal(err)
	}

	setStringFromOpt(&configFile, args, "-c")
	resetAbsPath(&configFile)

	cfg, err := utils.InitConfigFromFile(configFile)
	if err != nil {
		fatal(err)
	}

	globalEnv = env.LoadRebornEnv(cfg)
	globalConn, err = globalEnv.NewCoordConn()
	if err != nil {
		fatal(err)
	}

	setStringFromOpt(&addr, args, "--http-addr")

	agentID = genProcID()

	if err := addAgent(&agentInfo{
		ID:   agentID,
		Addr: addr,
		PID:  os.Getpid(),
	}); err != nil {
		fatal(err)
	}

	setStringFromOpt(&qdbConfigFile, args, "--qdb-config")
	resetAbsPath(&qdbConfigFile)

	setStringFromOpt(&redisConfigFile, args, "--redis-config")
	resetAbsPath(&redisConfigFile)

	if v := getStringArg(args, "--exec-path"); len(v) > 0 {
		path := os.ExpandEnv(fmt.Sprintf("${PATH}:%s", v))
		os.Setenv("PATH", path)
	}

	// set data dir
	setStringFromOpt(&dataDir, args, "--data-dir")
	resetAbsPath(&dataDir)

	os.MkdirAll(baseProcDataDir(), 0755)

	// set app log dir
	setStringFromOpt(&logDir, args, "--log-dir")
	resetAbsPath(&logDir)

	os.MkdirAll(baseProcLogDir(), 0755)

	// set app trash log dir
	os.MkdirAll(baseTrashLogDir(), 0755)

	// set output log file
	if v := getStringArg(args, "-L"); len(v) > 0 {
		log.SetHighlighting(false)
		log.SetOutputByName(v)
	}

	// set log level
	if v := getStringArg(args, "--log-level"); len(v) > 0 {
		log.SetLevelByString(v)
	}

	// set cpu
	if v := getStringArg(args, "--cpu"); len(v) > 0 {
		cpus, err = strconv.Atoi(v)
		if err != nil {
			fatal(err)
		}
	}

	runtime.GOMAXPROCS(cpus)

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, os.Interrupt, os.Kill)
	go func() {
		<-c

		fatal("ctrl-c or SIGTERM found, exit")
	}()

	if args["--ha"].(bool) {
		setIntArgFromOpt(&haMaxRetryNum, args, "--ha-max-retry-num")
		setIntArgFromOpt(&haRetryDelay, args, "--ha-retry-delay")

		go startHA()
	}

	err = loadSavedProcs()
	if err != nil {
		log.Fatalf("restart agent using last saved processes err: %v", err)
	}

	clearProcFiles()

	go runCheckProcs()

	log.Infof("listening %s", addr)
	runHTTPServer()
}
コード例 #4
0
ファイル: main.go プロジェクト: vebin/reborn
func main() {
	fmt.Print(banner)
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn proxy v0.1", true)
	if err != nil {
		log.Error(err)
	}

	// set config file
	setStringFromOpt(&configFile, args, "-c")

	// set output log file
	if v := args["-L"]; v != nil {
		log.SetHighlighting(false)
		log.SetOutputByName(v.(string))
	}

	// set log level
	if v := args["--log-level"]; v != nil {
		log.SetLevelByString(v.(string))
	}

	// set cpu
	setIntArgFromOpt(&cpus, args, "--cpu")

	// set addr
	setStringFromOpt(&addr, args, "--addr")

	// set proto
	setStringFromOpt(&proto, args, "--proto")

	// set http addr
	setStringFromOpt(&httpAddr, args, "--http-addr")

	// set proxy id
	setStringFromOpt(&proxyID, args, "--id")
	if len(proxyID) == 0 {
		log.Fatalf("invalid empty proxy id")
	}

	// set log dump path
	dumppath := utils.GetExecutorPath()
	setStringFromOpt(&dumppath, args, "--dump-path")

	log.Info("dump file path:", dumppath)
	log.CrashLog(path.Join(dumppath, "reborn-proxy.dump"))

	// set pidfile
	setStringFromOpt(&pidfile, args, "--pidfile")

	// set proxy auth
	setStringFromOpt(&proxyAuth, args, "--proxy-auth")

	// set net time
	setIntArgFromOpt(&netTimeout, args, "--net-timeout")

	router.CheckUlimit(1024)
	runtime.GOMAXPROCS(cpus)

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)

	conf, err := router.LoadConf(configFile)
	if err != nil {
		log.Fatal(err)
	}

	conf.Addr = addr
	conf.HTTPAddr = httpAddr
	conf.ProxyID = proxyID
	conf.PidFile = pidfile
	conf.NetTimeout = netTimeout
	conf.Proto = proto
	conf.ProxyAuth = proxyAuth

	if err := utils.CreatePidFile(conf.PidFile); err != nil {
		log.Fatal(err)
	}

	log.Info("running on ", addr)

	s := router.NewServer(conf)
	s.Run()
	log.Warning("exit")
}
コード例 #5
0
func main() {
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn config v0.1", true)
	if err != nil {
		log.Error(err)
	}

	if v := args["--pidfile"]; v != nil {
		pidFile = v.(string)
	}

	// set config file
	var configFile string
	var config *cfg.Cfg
	if args["-c"] != nil {
		configFile = args["-c"].(string)
		config, err = utils.InitConfigFromFile(configFile)
		if err != nil {
			Fatal(err)
		}
	} else {
		config, err = utils.InitConfig()
		if err != nil {
			Fatal(err)
		}
	}

	// load global vars
	globalEnv = env.LoadRebornEnv(config)
	globalConn = CreateCoordConn()

	// set output log file
	if args["-L"] != nil {
		log.SetHighlighting(false)
		log.SetOutputByName(args["-L"].(string))
	}

	// set log level
	if args["--log-level"] != nil {
		log.SetLevelByString(args["--log-level"].(string))
	}

	cmd := args["<command>"].(string)
	cmdArgs := args["<args>"].([]string)

	httpAddr := ":10086"
	if v := args["--http-addr"]; v != nil {
		httpAddr = v.(string)
	}

	go http.ListenAndServe(httpAddr, nil)

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, os.Interrupt, os.Kill)
	go func() {
		<-c

		Fatal("ctrl-c or SIGTERM found, exit")
	}()

	utils.CreatePidFile(pidFile)

	err = runCommand(cmd, cmdArgs)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}
}