예제 #1
0
파일: util_test.go 프로젝트: pingcap/tso
func (s *testUtilSuite) TestTimestamp(c *C) {
	conn, err := zkhelper.ConnectToZkWithTimeout(*testZKAddr, time.Second)
	c.Assert(err, IsNil)
	defer conn.Close()

	tbl := []int64{
		1, 100, 1000,
	}

	for _, t := range tbl {
		err = saveTimestamp(conn, s.rootPath, t)
		c.Assert(err, IsNil)

		n, err := loadTimestamp(conn, s.rootPath)
		c.Assert(err, IsNil)
		c.Assert(n, Equals, t)
	}

	// test error
	_, err = loadTimestamp(conn, "error_root_path")
	c.Assert(err, NotNil)

	_, err = conn.Set(getTimestampPath(s.rootPath), []byte{}, -1)
	c.Assert(err, IsNil)

	_, err = loadTimestamp(conn, s.rootPath)
	c.Assert(err, NotNil)
}
예제 #2
0
파일: server.go 프로젝트: pingcap/tso
// NewTimestampOracle creates a tso server with special config.
func NewTimestampOracle(cfg *Config) (*TimestampOracle, error) {
	if cfg.SaveInterval <= 0 {
		cfg.SaveInterval = defaultSaveInterval
	}

	tso := &TimestampOracle{
		cfg:      cfg,
		isLeader: 0,
	}

	var err error
	tso.listener, err = net.Listen("tcp", cfg.Addr)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if len(cfg.ZKAddr) == 0 {
		// no zookeeper, use a fake zk conn instead
		tso.zkConn = zkhelper.NewConn()
	} else {
		tso.zkConn, err = zkhelper.ConnectToZkWithTimeout(cfg.ZKAddr, zkTimeout)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	tso.conns = map[net.Conn]struct{}{}

	return tso, nil
}
예제 #3
0
파일: util_test.go 프로젝트: pingcap/tso
func (s *testUtilSuite) SetUpSuite(c *C) {
	conn, err := zkhelper.ConnectToZkWithTimeout(*testZKAddr, time.Second)
	c.Assert(err, IsNil)
	s.zkConn = conn

	s.rootPath = "/zk/tso_util_test"

}
예제 #4
0
파일: util_test.go 프로젝트: pingcap/tso
func (s *testUtilSuite) SetUpSuite(c *C) {
	conn, err := zkhelper.ConnectToZkWithTimeout(*testZKAddr, time.Second)
	c.Assert(err, IsNil)
	s.zkConn = conn

	s.rootPath = "/zk/tso_util_test"

	_, err = zkhelper.CreateRecursive(s.zkConn, s.rootPath, "", 0, zk.WorldACL(zkhelper.PERM_DIRECTORY))
	c.Assert(err, IsNil)
}
예제 #5
0
파일: server_test.go 프로젝트: pingcap/tso
func (s *testServerSuite) SetUpSuite(c *C) {
	conn, err := zkhelper.ConnectToZkWithTimeout(*testZKAddr, time.Second)
	c.Assert(err, IsNil)
	s.zkConn = conn

	cfg := &Config{
		Addr:         "127.0.0.1:0",
		ZKAddr:       *testZKAddr,
		RootPath:     "/zk/tso_test",
		SaveInterval: 100,
	}

	s.tso = testStartServer(c, cfg)
}
예제 #6
0
파일: util_test.go 프로젝트: pingcap/tso
func (s *testUtilSuite) TestLeader(c *C) {
	conn, err := zkhelper.ConnectToZkWithTimeout(*testZKAddr, time.Second)
	c.Assert(err, IsNil)
	defer conn.Close()

	leaderPath := getLeaderPath(s.rootPath)
	conn.Delete(leaderPath, -1)

	_, err = GetLeader(conn, s.rootPath)
	c.Assert(err, NotNil)

	_, _, err = GetWatchLeader(conn, s.rootPath)
	c.Assert(err, NotNil)

	_, err = zkhelper.CreateRecursive(conn, leaderPath, "", 0, zk.WorldACL(zkhelper.PERM_FILE))
	c.Assert(err, IsNil)

	_, err = GetLeader(conn, s.rootPath)
	c.Assert(err, NotNil)

	_, _, err = GetWatchLeader(conn, s.rootPath)
	c.Assert(err, NotNil)

	addr := "127.0.0.1:1234"
	m := map[string]interface{}{
		"Addr": addr,
	}

	data, err := json.Marshal(m)
	c.Assert(err, IsNil)

	_, err = conn.Set(leaderPath, data, -1)
	c.Assert(err, IsNil)

	v, err := GetLeader(conn, s.rootPath)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, addr)

	v, _, err = GetWatchLeader(conn, s.rootPath)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, addr)
}
예제 #7
0
파일: servers_test.go 프로젝트: pingcap/tso
func (s *testMultiServerSuite) SetUpSuite(c *C) {
	conn, err := zkhelper.ConnectToZkWithTimeout(*testZKAddr, time.Second)
	c.Assert(err, IsNil)
	s.zkConn = conn

	s.rootPath = "/zk/tso_multi_test"

	n := 3
	s.servers = make([]*TimestampOracle, 0, n)
	for i := 0; i < n; i++ {
		cfg := &Config{
			// use 127.0.0.1:0 to listen a unique port.
			Addr:         "127.0.0.1:0",
			ZKAddr:       *testZKAddr,
			RootPath:     s.rootPath,
			SaveInterval: 100,
		}
		tso := testStartServer(c, cfg)
		s.servers = append(s.servers, tso)
	}
}
예제 #8
0
파일: client.go 프로젝트: pingcap/tso
func (c *Client) watchLeader() {
	var (
		conn zkhelper.Conn
		err  error
	)

	for {
		conn, err = zkhelper.ConnectToZkWithTimeout(c.conf.ZKAddr, time.Second)
		if err != nil {
			log.Errorf("connect zk err %v, retry later", err)
			time.Sleep(3 * time.Second)
			continue
		}
		break
	}

	defer conn.Close()

	var lastAddr string

	for {
		addr, watcher, err := util.GetWatchLeader(conn, c.conf.RootPath)
		if err != nil {
			log.Errorf("get tso leader err %v, retry later", err)
			time.Sleep(3 * time.Second)
			continue
		}

		if lastAddr != addr {
			log.Warnf("leader change %s -> %s", lastAddr, addr)
			lastAddr = addr
			c.leaderCh <- addr
		}

		// watch the leader changes.
		<-watcher
	}
}