Exemplo n.º 1
0
func TestRecordResponseTime(t *testing.T) {
	c := stats.NewCounters("test")
	recordResponseTime(c, 1)
	recordResponseTime(c, 5)
	recordResponseTime(c, 10)
	recordResponseTime(c, 50)
	recordResponseTime(c, 200)
	recordResponseTime(c, 1000)
	recordResponseTime(c, 5000)
	recordResponseTime(c, 8000)
	recordResponseTime(c, 10000)
	cnts := c.Counts()
	if cnts["0-5ms"] != 1 {
		t.Fail()
	}
	if cnts["5-10ms"] != 1 {
		t.Fail()
	}
	if cnts["50-200ms"] != 1 {
		t.Fail()
	}
	if cnts["200-1000ms"] != 1 {
		t.Fail()
	}
	if cnts["1000-5000ms"] != 1 {
		t.Fail()
	}
	if cnts["5000-10000ms"] != 2 {
		t.Fail()
	}
	if cnts["10000ms+"] != 1 {
		t.Fail()
	}
}
Exemplo n.º 2
0
func init() {
	flag.IntVar(&optPort, "port", 8080, "port for accessing expvars")
	// internal
	counters = stats.NewCounters("stats")
	rates = stats.NewRates("stats_rates", counters, 10, time.Second)
	stopChan = make(chan int)
	expvars = NewExpvars()
}
Exemplo n.º 3
0
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
}
Exemplo n.º 4
0
func makeServer(configFile string) *Server {
	cfg, err := config.ParseConfigFile(configFile)
	if err != nil {
		log.Error(err.Error())
		return nil
	}

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

	s := &Server{
		configFile:        configFile,
		cfg:               cfg,
		addr:              cfg.Addr,
		user:              cfg.User,
		password:          cfg.Password,
		autoSchamas:       make(map[string]*tabletserver.SchemaInfo),
		taskQ:             make(chan *execTask, 100),
		concurrentLimiter: tokenlimiter.NewTokenLimiter(100),
		counter:           stats.NewCounters("stats"),
		rwlock:            &sync.RWMutex{},
		clients:           make(map[uint32]*Conn),
	}

	f := func(wg *sync.WaitGroup, rs []interface{}, i int, co *mysql.SqlConn, sql string, args []interface{}) {
		r, err := co.Execute(sql, args...)
		if err != nil {
			log.Warning(err)
			rs[i] = err
		} else {
			rs[i] = r
		}

		wg.Done()
	}

	for i := 0; i < 100; i++ {
		go func() {
			for task := range s.taskQ {
				f(task.wg, task.rs, task.idx, task.co, task.sql, task.args)
			}
		}()
	}

	return s
}
Exemplo n.º 5
0
func (s *testProxyRouterSuite) TestRecordResponseTime(c *C) {
	cc := stats.NewCounters("test")
	recordResponseTime(cc, 1)
	recordResponseTime(cc, 5)
	recordResponseTime(cc, 10)
	recordResponseTime(cc, 50)
	recordResponseTime(cc, 200)
	recordResponseTime(cc, 1000)
	recordResponseTime(cc, 5000)
	recordResponseTime(cc, 8000)
	recordResponseTime(cc, 10000)
	cnts := cc.Counts()

	c.Assert(cnts["0-5ms"], Equals, int64(1))
	c.Assert(cnts["5-10ms"], Equals, int64(1))
	c.Assert(cnts["50-200ms"], Equals, int64(1))
	c.Assert(cnts["200-1000ms"], Equals, int64(1))
	c.Assert(cnts["1000-5000ms"], Equals, int64(1))
	c.Assert(cnts["5000-10000ms"], Equals, int64(2))
	c.Assert(cnts["10000ms+"], Equals, int64(1))
}
Exemplo n.º 6
0
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
}
Exemplo n.º 7
0
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
}