Exemple #1
0
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")
}
Exemple #2
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
		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))
	}
}
Exemple #3
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))
	}()
}
Exemple #4
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()
}
Exemple #5
0
func main() {
	logging.SetRotateByHour()
	// logging.SetRotateByDay()

	err := logging.SetOutputByName("example.log")
	checkError(err)

	timer := time.NewTicker(time.Duration(10) * time.Second)
	for {
		select {
		case <-timer.C:
			logging.Debug(time.Now().String())
			logging.Info(time.Now().String())
			logging.Warningf("%s", time.Now().String())
			logging.Errorf("%s", time.Now().String())
		}
	}
}
Exemple #6
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))
	}
}
Exemple #7
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
}