예제 #1
0
파일: router.go 프로젝트: jameswei/xcodis
func NewServer(addr string, debugVarAddr string, conf *Conf) *Server {
	log.Infof("%+v", conf)
	s := &Server{
		evtbus:            make(chan interface{}, 100),
		top:               topo.NewTopo(conf.productName, conf.zkAddr, conf.f),
		net_timeout:       conf.net_timeout,
		counter:           stats.NewCounters("router"),
		lastActionSeq:     -1,
		startAt:           time.Now(),
		addr:              addr,
		concurrentLimiter: tokenlimiter.NewTokenLimiter(100),
		moper:             NewMultiOperator(addr),
		pools:             cachepool.NewCachePool(),
	}

	s.broker = conf.broker

	slot_num = conf.slot_num

	s.mu.Lock()
	s.pi.Id = conf.proxyId
	s.pi.State = models.PROXY_STATE_OFFLINE
	hname, err := os.Hostname()
	if err != nil {
		log.Fatal("get host name failed", err)
	}
	s.pi.Addr = hname + ":" + strings.Split(addr, ":")[1]
	s.pi.DebugVarAddr = hname + ":" + strings.Split(debugVarAddr, ":")[1]
	log.Infof("proxy_info:%+v", s.pi)
	s.mu.Unlock()
	//todo:fill more field

	stats.Publish("evtbus", stats.StringFunc(func() string {
		return strconv.Itoa(len(s.evtbus))
	}))
	stats.Publish("startAt", stats.StringFunc(func() string {
		return s.startAt.String()
	}))

	s.RegisterAndWait()

	_, err = s.top.WatchChildren(models.GetWatchActionPath(conf.productName), s.evtbus)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}

	s.FillSlots()

	//start event handler
	go s.handleTopoEvent()

	log.Info("proxy start ok")

	return s
}
예제 #2
0
파일: router.go 프로젝트: ZuoGuocai/codis
func NewServer(addr string, debugVarAddr string, conf *Conf) *Server {
	log.Infof("start with configuration: %+v", conf)
	s := &Server{
		conf:          conf,
		evtbus:        make(chan interface{}, 1000),
		top:           topo.NewTopo(conf.productName, conf.zkAddr, conf.f, conf.provider),
		counter:       stats.NewCounters("router"),
		lastActionSeq: -1,
		startAt:       time.Now(),
		addr:          addr,
		moper:         NewMultiOperator(addr),
		reqCh:         make(chan *PipelineRequest, 1000),
		pools:         cachepool.NewCachePool(),
		pipeConns:     make(map[string]*taskRunner),
		bufferedReq:   list.New(),
	}

	s.pi.Id = conf.proxyId
	s.pi.State = models.PROXY_STATE_OFFLINE
	host := strings.Split(addr, ":")[0]
	debugHost := strings.Split(debugVarAddr, ":")[0]
	hname, err := os.Hostname()
	if err != nil {
		log.Fatal("get host name failed", err)
	}
	if host == "0.0.0.0" || strings.HasPrefix(host, "127.0.0.") {
		host = hname
	}
	if debugHost == "0.0.0.0" || strings.HasPrefix(debugHost, "127.0.0.") {
		debugHost = hname
	}
	s.pi.Addr = host + ":" + strings.Split(addr, ":")[1]
	s.pi.DebugVarAddr = debugHost + ":" + strings.Split(debugVarAddr, ":")[1]
	s.pi.Pid = os.Getpid()
	s.pi.StartAt = time.Now().String()

	log.Infof("proxy_info:%+v", s.pi)

	stats.Publish("evtbus", stats.StringFunc(func() string {
		return strconv.Itoa(len(s.evtbus))
	}))
	stats.Publish("startAt", stats.StringFunc(func() string {
		return s.startAt.String()
	}))

	s.RegisterAndWait()

	_, err = s.top.WatchChildren(models.GetWatchActionPath(conf.productName), s.evtbus)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}

	s.FillSlots()

	//start event handler
	go s.handleTopoEvent()

	log.Info("proxy start ok")

	return s
}
예제 #3
0
파일: router.go 프로젝트: GavinHwa/reborn
func NewServer(conf *Conf) *Server {
	log.Infof("start with configuration: %+v", conf)

	f := func(addr string) (*redisconn.Conn, error) {
		return newRedisConn(addr, conf.NetTimeout, RedisConnReaderSize, RedisConnWiterSize, conf.StoreAuth)
	}

	s := &Server{
		conf:          conf,
		evtbus:        make(chan interface{}, EventBusNum),
		top:           topo.NewTopo(conf.ProductName, conf.CoordinatorAddr, conf.f, conf.Coordinator),
		counter:       stats.NewCounters("router"),
		lastActionSeq: -1,
		startAt:       time.Now(),
		moper:         newMultiOperator(conf.Addr, conf.ProxyAuth),
		reqCh:         make(chan *PipelineRequest, PipelineRequestNum),
		pools:         redisconn.NewPools(PoolCapability, f),
		pipeConns:     make(map[string]*taskRunner),
		bufferedReq:   list.New(),
	}

	s.pi.ID = conf.ProxyID
	s.pi.State = models.PROXY_STATE_OFFLINE

	addr := conf.Addr
	addrs := strings.Split(addr, ":")
	if len(addrs) != 2 {
		log.Fatalf("bad addr %s", addr)
	}

	hname, err := os.Hostname()
	if err != nil {
		log.Fatal("get host name failed", err)
	}

	s.pi.Addr = hname + ":" + addrs[1]

	debugVarAddr := conf.HTTPAddr
	debugVarAddrs := strings.Split(debugVarAddr, ":")
	if len(debugVarAddrs) != 2 {
		log.Fatalf("bad debugVarAddr %s", debugVarAddr)
	}
	s.pi.DebugVarAddr = hname + ":" + debugVarAddrs[1]

	s.pi.Pid = os.Getpid()
	s.pi.StartAt = time.Now().String()

	log.Infof("proxy_info:%+v", s.pi)

	stats.Publish("evtbus", stats.StringFunc(func() string {
		return strconv.Itoa(len(s.evtbus))
	}))
	stats.Publish("startAt", stats.StringFunc(func() string {
		return s.startAt.String()
	}))

	s.RegisterAndWait(true)
	s.registerSignal()

	_, err = s.top.WatchChildren(models.GetWatchActionPath(conf.ProductName), s.evtbus)
	if err != nil {
		log.Fatal(errors.ErrorStack(err))
	}

	s.FillSlots()

	// start event handler
	go s.handleTopoEvent()
	go s.dumpCounter()

	log.Info("proxy start ok")

	return s
}
예제 #4
0
func NewServer(addr string, debugVarAddr string, conf *Config) (*Server, error) {
	log.Infof("start proxy with config: %+v", conf)
	s := &Server{
		evtbus:        make(chan interface{}, 1000),
		conf:          conf,
		topo:          topology.NewTopo(conf.productName, conf.zkAddr, conf.fact, conf.provider),
		pool:          make(map[string]*SharedBackendConn),
		lastActionSeq: -1,
	}
	for i := 0; i < MaxSlotNum; i++ {
		s.slots[i] = &Slot{Id: i}
	}

	proxyHost := strings.Split(addr, ":")[0]
	debugHost := strings.Split(debugVarAddr, ":")[0]

	hostname, err := os.Hostname()
	if err != nil {
		log.PanicErrorf(err, "get host name failed")
	}
	if proxyHost == "0.0.0.0" || strings.HasPrefix(proxyHost, "127.0.0.") {
		proxyHost = hostname
	}
	if debugHost == "0.0.0.0" || strings.HasPrefix(debugHost, "127.0.0.") {
		debugHost = hostname
	}

	s.info.Id = conf.proxyId
	s.info.State = models.PROXY_STATE_OFFLINE
	s.info.Addr = proxyHost + ":" + strings.Split(addr, ":")[1]
	s.info.DebugVarAddr = debugHost + ":" + strings.Split(debugVarAddr, ":")[1]
	s.info.Pid = os.Getpid()
	s.info.StartAt = time.Now().String()

	log.Infof("proxy info = %+v", s.info)

	if l, err := net.Listen(conf.proto, addr); err != nil {
		return nil, errors.Trace(err)
	} else {
		s.Listener = l
	}

	stats.Publish("evtbus", stats.StringFunc(func() string {
		return strconv.Itoa(len(s.evtbus))
	}))

	stats.PublishJSONFunc("router", func() string {
		var m = make(map[string]interface{})
		m["ops"] = cmdstats.requests.Get()
		m["cmds"] = getAllOpStats()
		m["info"] = s.info
		m["build"] = map[string]interface{}{
			"version": utils.Version,
			"compile": utils.Compile,
		}
		b, _ := json.Marshal(m)
		return string(b)
	})

	s.RegisterAndWait()

	_, err = s.topo.WatchChildren(models.GetWatchActionPath(conf.productName), s.evtbus)
	if err != nil {
		log.PanicErrorf(err, "watch children failed")
	}

	for i := 0; i < MaxSlotNum; i++ {
		s.fillSlot(i, false)
	}

	go s.handleTopoEvent()

	log.Info("proxy start ok")

	return s, nil
}