コード例 #1
0
func Init(logPrefix string) {
	if logPrefix != "" {
		logPrefix += " "
	}

	logPrefix += fmt.Sprintf("[%v]", os.Getpid())
	f, err := logfile.Open(*logfileName, *logFrequency, *logMaxSize, *logMaxFiles)
	if err != nil {
		panic(fmt.Sprintf("unable to open logfile %s: %v", *logfileName, err))
	}
	logger := relog.New(f, logPrefix+" ",
		log.Ldate|log.Lmicroseconds|log.Lshortfile, relog.LogNameToLogLevel(*logLevel))
	relog.SetLogger(logger)

	if *gomaxprocs != 0 {
		runtime.GOMAXPROCS(*gomaxprocs)
		relog.Info("set GOMAXPROCS = %v", *gomaxprocs)
	}

	fdLimit := &syscall.Rlimit{*maxOpenFds, *maxOpenFds}
	if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil {
		relog.Fatal("can't Setrlimit %#v: err %v", *fdLimit, err)
	} else {
		relog.Info("set max-open-fds = %v", *maxOpenFds)
	}
}
コード例 #2
0
ファイル: vtocc.go プロジェクト: seacoastboy/vitess
func main() {
	memProfileRate := flag.Int("mem-profile-rate", 512*1024, "profile every n bytes allocated")
	maxOpenFds := flag.Uint64("max-open-fds", 32768, "max open file descriptors")
	configFile := flag.String("config", "", "config file name")
	dbConfigFile := flag.String("dbconfig", "", "db config file name")
	lameDuckPeriod := flag.Float64("lame-duck-period", DefaultLameDuckPeriod,
		"how long to give in-flight transactions to finish")
	rebindDelay := flag.Float64("rebind-delay", DefaultRebindDelay,
		"artificial delay before rebinding a hijacked listener")
	logfileName := flag.String("logfile", "/dev/stderr", "base log file name")
	logFrequency := flag.Int64("logfile.frequency", 0, "rotation frequency in seconds")
	logMaxSize := flag.Int64("logfile.maxsize", 0, "max file size in bytes")
	logMaxFiles := flag.Int64("logfile.maxfiles", 0, "max number of log files")
	queryLog := flag.String("querylog", "", "for testing: log all queries to this file")
	flag.Parse()

	exportBinaryVersion()

	runtime.MemProfileRate = *memProfileRate

	f, err := logfile.Open(*logfileName, *logFrequency, *logMaxSize, *logMaxFiles)
	if err != nil {
		panic(fmt.Sprintf("unable to open logfile %s", *logfileName))
	}
	logger := relog.New(f, "vtocc ",
		log.Ldate|log.Lmicroseconds|log.Lshortfile, relog.DEBUG)
	relog.SetLogger(logger)
	if *queryLog != "" {
		if f, err = os.OpenFile(*queryLog, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644); err == nil {
			ts.QueryLogger = relog.New(f, "", log.Ldate|log.Lmicroseconds, relog.DEBUG)
		}
	}
	unmarshalFile(*configFile, &config)
	unmarshalFile(*dbConfigFile, &dbconfig)
	// work-around for jsonism
	if v, ok := dbconfig["port"].(float64); ok {
		dbconfig["port"] = int(v)
	}

	fdLimit := &syscall.Rlimit{*maxOpenFds, *maxOpenFds}
	if err = syscall.Setrlimit(RLIMIT_NOFILE, fdLimit); err != nil {
		relog.Fatal("can't Setrlimit %#v: err %v", *fdLimit, err)
	} else {
		relog.Info("set max-open-fds = %v", *maxOpenFds)
	}

	snitch.RegisterCommand("reload_schema", "Rescan the schema for new tables", ReloadHandler)
	snitch.Register()

	qm := &OccManager{config, dbconfig}
	rpc.Register(qm)

	ts.StartQueryService(
		config.PoolSize,
		config.TransactionCap,
		config.TransactionTimeout,
		config.MaxResultSize,
		config.QueryCacheSize,
		config.SchemaReloadTime,
		config.QueryTimeout,
		config.IdleTimeout,
	)
	ts.AllowQueries(ts.GenericConnectionCreator(dbconfig), nil)

	rpc.HandleHTTP()
	jsonrpc.ServeHTTP()
	jsonrpc.ServeRPC()
	bsonrpc.ServeHTTP()
	bsonrpc.ServeRPC()

	relog.Info("started vtocc %v", config.Port)

	// we delegate out startup to the micromanagement server so these actions
	// will occur after we have obtained our socket.

	usefulLameDuckPeriod := float64(config.QueryTimeout + 1)
	if usefulLameDuckPeriod > *lameDuckPeriod {
		*lameDuckPeriod = usefulLameDuckPeriod
		relog.Info("readjusted -lame-duck-period to %f", *lameDuckPeriod)
	}
	umgmt.SetLameDuckPeriod(float32(*lameDuckPeriod))
	umgmt.SetRebindDelay(float32(*rebindDelay))
	umgmt.AddStartupCallback(func() {
		umgmt.StartHttpServer(fmt.Sprintf(":%v", config.Port))
	})
	umgmt.AddStartupCallback(func() {
		sighandler.SetSignalHandler(syscall.SIGTERM,
			umgmt.SigTermHandler)
	})
	umgmt.AddCloseCallback(func() {
		ts.DisallowQueries()
	})
	umgmt.AddShutdownCallback(func() error {
		HandleGracefulShutdown()
		return nil
	})

	umgmtSocket := fmt.Sprintf(config.UmgmtSocket, config.Port)
	if umgmtErr := umgmt.ListenAndServe(umgmtSocket); umgmtErr != nil {
		relog.Error("umgmt.ListenAndServe err: %v", umgmtErr)
	}
	relog.Info("done")
}