예제 #1
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
	}
}
예제 #2
0
파일: servers_test.go 프로젝트: pingcap/tso
func (s *testMultiServerSuite) testGetTimestamp(c *C, duration time.Duration) {
	// get leader tso
	var (
		addr  string
		watch <-chan zk.Event
		err   error

		conn net.Conn

		last proto.Response

		begin = time.Now()
	)

	for {
		if time.Now().Sub(begin) > duration {
			break
		}

		if len(addr) == 0 {
			addr, watch, err = util.GetWatchLeader(s.zkConn, s.rootPath)
			c.Assert(err, IsNil)

			conn, err = net.Dial("tcp", addr)
			c.Assert(err, IsNil)
		}

		_, err = conn.Write([]byte{0x00})
		if err == nil {
			var resp proto.Response
			err = resp.Decode(conn)
			if err == nil {
				c.Assert(resp.Physical, GreaterEqual, last.Physical)
				c.Assert(resp.Logical, Greater, last.Logical)
			}
		}

		if err != nil {
			conn.Close()

			// try connect the closed leader again, must error.
			conn, err = net.Dial("tcp", addr)
			if err == nil {
				conn.Write([]byte{0x00})
				var resp proto.Response
				err = resp.Decode(conn)
				conn.Close()
			}
			c.Assert(err, NotNil)

			// leader stop or close, we will wait some time for leader change.
			select {
			case <-watch:
				log.Warnf("leader changed")
			case <-time.After(500 * time.Millisecond):
				log.Warnf("wait change timeout, force get leader again")
			}

			addr = ""
		}
	}

	conn.Close()
}