Exemplo n.º 1
0
Arquivo: main.go Projeto: banyue/codis
func main() {
	fmt.Print(banner)
	log.SetLevelByString("info")

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

	// set config file
	if args["-c"] != nil {
		configFile = args["-c"].(string)
	}

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

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

	// set cpu
	if args["--cpu"] != nil {
		cpus, err = strconv.Atoi(args["--cpu"].(string))
		if err != nil {
			log.Fatal(err)
		}
	}

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

	// set http addr
	if args["--http-addr"] != nil {
		httpAddr = args["--http-addr"].(string)
	}

	wd, _ := os.Getwd()
	log.Info("wd:", wd)
	log.CrashLog(wd + ".dump")

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

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)
	log.Info("running on ", addr)
	conf, err := router.LoadConf(configFile)
	if err != nil {
		log.Fatal(err)
	}
	s := router.NewServer(addr, httpAddr, conf)
	s.Run()
	log.Warning("exit")
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: temco/codis
func main() {
	log.SetLevelByString("info")

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	go func() {
		<-c
		if createdDashboardNode {
			releaseDashboardNode()
		}
		Fatal("ctrl-c or SIGTERM found, exit")
	}()

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

	// 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 {
			log.Warning("load config file error")
			Fatal(err)
		}
	} else {
		config, err = utils.InitConfig()
		if err != nil {
			log.Warning("load config file error")
			Fatal(err)
		}
	}

	// load global vars
	globalEnv = env.LoadCodisEnv(config)

	// set output log file
	if args["-L"] != nil {
		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)

	go http.ListenAndServe(":10086", nil)
	err = runCommand(cmd, cmdArgs)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}
}
Exemplo n.º 3
0
func LogVerbose(v string) {
	switch v {
	case "info":
		log.SetLevelByString("info")
	case "debug":
		log.SetLevelByString("debug")
	case "warning":
		log.SetLevelByString("warning")
	case "fatal":
		log.SetLevelByString("fatal")
	}
}
Exemplo n.º 4
0
func (s *Server) resetSchemaInfo() error {
	for _, c := range s.clients {
		if len(c.txConns) > 0 {
			return errors.Errorf("transaction exist")
		}
	}

	cfg, err := config.ParseConfigFile(s.configFile)
	if err != nil {
		log.Error(err)
		return err
	}

	s.cleanup()
	s.autoSchamas = make(map[string]*tabletserver.SchemaInfo)
	for _, n := range s.shards {
		n.Close()
	}

	s.shards = nil
	s.schemas = nil

	log.Warningf("%#v", cfg)

	log.SetLevelByString(cfg.LogLevel)

	s.cfg = cfg
	return s.loadSchemaInfo()
}
Exemplo n.º 5
0
func handleSetLogLevel(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	level := r.Form.Get("level")
	log.SetLevelByString(level)
	log.Info("set log level to ", level)
	w.Header().Set("Content-Type", "text/html")
	w.Write([]byte("OK"))
}
Exemplo n.º 6
0
func (pc *ProxyConfig) apply() {
	log.SetLevelByString(pc.logLevel)

	if pc.logFile != "" {
		err := log.SetOutputByName(pc.logFile)
		if err != nil {
			log.Fatalf("ProxyConfig SetOutputByName %s failed %s ", pc.logFile, err.Error())
		}
		log.SetRotateByDay()
	}

	if pc.name == "" {
		log.Fatal("ProxyConfig name must not empty")
	}

	if pc.port == 0 {
		log.Fatal("ProxyConfig port  must not 0")
	}

	if pc.cpu > runtime.NumCPU() {
		log.Warningf("ProxyConfig cpu  %d exceed %d, adjust to %d ", pc.cpu, runtime.NumCPU(), runtime.NumCPU())
		pc.cpu = runtime.NumCPU()
	}

	if pc.maxConn > 10000 {
		log.Warningf("ProxyConfig maxconn %d exceed 10000, adjust to 10000", pc.maxConn)
		pc.maxConn = 10000
	}

	runtime.GOMAXPROCS(pc.cpu)

	if pc.poolSize <= 0 || pc.poolSize > 30 {
		log.Warning("ProxyConfig poolSize %d , adjust to 10 ", pc.poolSize)
		pc.poolSize = 10
	}

	if pc.cpuFile != "" {
		f, err := os.Create(pc.cpuFile)
		if err != nil {
			log.Fatal(err)
		}
		log.Warning("Archer start CPUProfile ", pc.cpuFile)
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if pc.memFile != "" {
		f, err := os.Create(pc.memFile)
		if err == nil {
			log.Warning("Archer start HeapProfile ", pc.memFile)
			pprof.WriteHeapProfile(f)
		}
	}

	go func() {
		log.Warning(http.ListenAndServe(":6061", nil))
	}()
}
Exemplo n.º 7
0
func main() {
	flag.Parse()
	log.SetLevelByString(*logLevel)

	for {
		groups, err := GetServerGroups()
		if err != nil {
			log.Error(errors.ErrorStack(err))
			return
		}

		CheckAliveAndPromote(groups)
		time.Sleep(3 * time.Second)
	}
}
Exemplo n.º 8
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()

	if len(*configFile) == 0 {
		log.Error("must use a config file")
		return
	}

	cfg, err := config.ParseConfigFile(*configFile)
	if err != nil {
		log.Error(err.Error())
		return
	}

	log.SetLevelByString(cfg.LogLevel)

	log.CrashLog("./cm-proxy.dump")

	var svr *proxy.Server
	svr, err = proxy.NewServer(*configFile)
	if err != nil {
		log.Error(err.Error())
		return
	}

	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 svr.Run()

	http.HandleFunc("/api/reload", svr.HandleReload)
	//todo: using configuration
	http.ListenAndServe(":8888", nil)
}
Exemplo n.º 9
0
func main() {
	autoflags.Define(&config)
	flag.Parse()
	log.SetLevelByString(config.LogLevel)
	// to avoid pprof being optimized by gofmt
	log.Debug(pprof.Handler("profile"))
	if len(config.LogFile) != 0 {
		log.SetOutputByName(config.LogFile)
		log.SetRotateByDay()
	}
	if config.LogEveryN <= 0 {
		proxy.LogEveryN = 1
	} else {
		proxy.LogEveryN = config.LogEveryN
	}
	log.Infof("%#v", config)
	sigChan := make(chan os.Signal)
	signal.Notify(sigChan, os.Interrupt, os.Kill)

	log.Infof("pid %d", os.Getpid())
	if len(config.DebugAddr) != 0 {
		http.HandleFunc("/setloglevel", handleSetLogLevel)
		go func() {
			log.Fatal(http.ListenAndServe(config.DebugAddr, nil))
		}()
		log.Infof("debug service listens on %s", config.DebugAddr)
	}

	// shuffle startup nodes
	startupNodes := strings.Split(config.StartupNodes, ",")
	indexes := rand.Perm(len(startupNodes))
	for i, startupNode := range startupNodes {
		startupNodes[i] = startupNodes[indexes[i]]
		startupNodes[indexes[i]] = startupNode
	}
	connPool := proxy.NewConnPool(config.BackendIdleConnections, config.ConnectTimeout, config.ReadPrefer != proxy.READ_PREFER_MASTER)
	dispatcher := proxy.NewDispatcher(startupNodes, config.SlotsReloadInterval, connPool, config.ReadPrefer)
	if err := dispatcher.InitSlotTable(); err != nil {
		log.Fatal(err)
	}
	proxy := proxy.NewProxy(config.Addr, dispatcher, connPool)
	go proxy.Run()
	sig := <-sigChan
	log.Infof("terminated by %#v", sig)
	proxy.Exit()
}
Exemplo n.º 10
0
func main() {
	log.SetLevelByString("info")

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	go func() {
		<-c
		Fatal("ctrl-c or SIGTERM found, exit")
	}()

	//	productName, _ = config.ReadString("product", "test")
	args, err := docopt.Parse(usage, nil, true, "codis config v0.1", true)
	if err != nil {
		log.Error(err)
	}

	// set config file
	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)
		}
	}

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

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

	productName, _ = config.ReadString("product", "test")
	zkAddr, _ = config.ReadString("zk", "localhost:2181")
	zkConn, _ = zkhelper.ConnectToZk(zkAddr)
	zkLock = utils.GetZkLock(zkConn, productName)

	broker, _ = config.ReadString("broker", "ledisdb")
	slot_num, _ = config.ReadInt("slot_num", 16)

	log.Debugf("product: %s", productName)
	log.Debugf("zk: %s", zkAddr)
	log.Debugf("broker: %s", broker)

	if err := registerConfigNode(); err != nil {
		log.Fatal(errors.ErrorStack(err))
	}
	defer unRegisterConfigNode()

	if err := removeOrphanLocks(); err != nil {
		log.Fatal(errors.ErrorStack(err))
	}

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

	go http.ListenAndServe(":10086", nil)
	err = runCommand(cmd, cmdArgs)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}
}
Exemplo n.º 11
0
func init() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	log.SetLevelByString("info")
}
Exemplo n.º 12
0
func handleSetLogLevel(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	level := r.Form.Get("level")
	log.SetLevelByString(level)
	log.Info("set log level to", level)
}
Exemplo n.º 13
0
func NewProxyConfig(filename string) *ProxyConfig {
	c, err := config.NewConfig("ini", filename)
	if err != nil {
		log.Fatal("read config file failed ", err)
	}

	loglevel := c.DefaultString("log::loglevel", "info")
	log.SetLevelByString(loglevel)

	logfile := c.DefaultString("log::logfile", "")
	if logfile != "" {
		err := log.SetOutputByName(logfile)
		if err != nil {
			log.Fatal("Set log Output failed ", err)
		}
		log.Info("Set log Output to file ", logfile)
		log.SetRotateByDay()
	}

	cpus := c.DefaultInt("proxy::cpus", 4)
	log.Info("set runtime GOMAXPROCS to ", cpus)
	runtime.GOMAXPROCS(cpus)

	pc := &ProxyConfig{
		Id:              c.DefaultString("product::id", ""),
		Name:            c.DefaultString("product::name", ""),
		Port:            c.DefaultString("proxy::port", ""),
		SlaveOk:         c.DefaultBool("proxy::slaveok", false),
		IdleTime:        c.DefaultInt64("proxy::idletime", 300),
		MaxConn:         c.DefaultInt64("proxy::maxconn", 60000),
		Statsd:          c.DefaultString("proxy::statsd", ""),
		Zk:              c.DefaultString("zk::zk", ""),
		ZkPath:          c.DefaultString("zk::zkpath", ""),
		MulOpParallel:   c.DefaultInt("proxy::mulparallel", 10),
		PoolSizePerNode: c.DefaultInt("proxy::poolsizepernode", 30),
		StatsdPrefix:    c.DefaultString("proxy::prefix", "redis.proxy."),
		FileName:        filename,
	}

	pc.Config = c

	nodes := c.DefaultString("proxy::nodes", "")
	if nodes == "" {
		log.Fatal("proxy nodes must not empty ")
	}
	pc.Nodes = strings.Split(nodes, ",")

	if pc.Id == "" || pc.Name == "" || pc.Port == "" {
		log.Fatal("id name or port must not empty")
	}

	if pc.PoolSizePerNode < MinPoolSizePerNode || pc.PoolSizePerNode > MaxPoolSizePerNode {
		log.Info("Adjust PoolSizePerNode to 30")
		pc.PoolSizePerNode = 30
	}

	if pc.MulOpParallel < MinMulOpParallel || pc.MulOpParallel > MaxMulOpParallel {
		log.Info("Adjust MulOpParallel to 10")
		pc.MulOpParallel = 10
	}
	if pc.MaxConn < MinMaxConn || pc.MaxConn > MaxMaxConn {
		log.Info("Adjust MaxConn to 60000")
		pc.MaxConn = 60000
	}

	if pc.IdleTime < MinIdleTime || pc.IdleTime > MaxIdleTime {
		log.Info("Adjust MaxConn to 300")
		pc.IdleTime = 300
	}

	fcpu := c.DefaultString("debug::cpufile", "")
	if fcpu != "" {
		f, err := os.Create(fcpu)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	fmem := c.DefaultString("debug::memfile", "")
	if fmem != "" {
		f, err := os.Create(fmem)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
	}
	return pc
}
Exemplo n.º 14
0
// setbyname will set name by value
// return old value of name
func (s *Session) proxyConfigSetByName(name string, value string) []byte {
	var reply []byte

	switch name {
	case "loglevel":
		v := strings.ToLower(value)
		if v != "info" || v != "warning" || v != "debug" {
			reply = []byte("-loglevel must be info warning or debug\r\n")
			return reply
		}
		reply = s.proxyConfigGetByName(name)
		log.SetLevelByString(v)
	case "idletime":
		v, err := strconv.Atoi(value)
		if err != nil {
			reply = []byte("-unavailable idletime\r\n")
			return reply
		}
		if v < MinIdleTime || v > MaxIdleTime {
			reply = []byte("-unavailable idletime, must between 10 ~ 300\r\n")
			return reply
		}
		reply = s.proxyConfigGetByName("idletime")
		s.Proxy.Conf.IdleTime = int64(v)
	case "slaveok":
		v, err := strconv.Atoi(value)
		if err != nil {
			reply = []byte("-unavailable slaveok,must 0 or 1\r\n")
			return reply
		}
		if v != 0 || v != 1 {
			reply = []byte("-unavailable slaveok,must 0 or 1\r\n")
			return reply
		}
		reply = s.proxyConfigGetByName("slaveok")
		if v == 1 {
			s.Proxy.Conf.SlaveOk = true
		} else {
			s.Proxy.Conf.SlaveOk = false
		}
	case "mulparallel":
		v, err := strconv.Atoi(value)
		if err != nil {
			reply = []byte("-unavailable mulparallel\r\n")
			return reply
		}
		if v < MinMulOpParallel || v > MaxMulOpParallel {
			reply = []byte("-unavailable mulparallel, must between 5 ~ 100\r\n")
			return reply
		}
		reply = s.proxyConfigGetByName("mulparallel")
		s.Proxy.Conf.MulOpParallel = v
	case "statsd":
		reply = s.proxyConfigGetByName("statsd")
		s.Proxy.Conf.Statsd = value
	case "maxconn":
		v, err := strconv.Atoi(value)
		if err != nil {
			reply = []byte("-unavailable maxconn\r\n")
			return reply
		}
		if v < MinMaxConn || v > MaxMaxConn {
			reply = []byte("-unavailable maxconn, must between 100 ~ 60000\r\n")
			return reply
		}
		reply = s.proxyConfigGetByName("maxconn")
		s.Proxy.Conf.MaxConn = int64(v)
	default:
		reply = []byte("-wrong proxy config name\r\n")
	}
	return reply
}