Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br: use random port to start mock cluster. #31167

Merged
merged 13 commits into from
Dec 31, 2021
55 changes: 11 additions & 44 deletions br/pkg/mock/mock_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"net"
"net/http"
"net/http/pprof"
"net/url"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -27,7 +25,6 @@ import (
"github.com/tikv/client-go/v2/testutils"
"github.com/tikv/client-go/v2/tikv"
pd "github.com/tikv/pd/client"
"github.com/tikv/pd/pkg/tempurl"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -87,46 +84,13 @@ func NewCluster() (*Cluster, error) {

// Start runs a mock cluster.
func (mock *Cluster) Start() error {
var (
err error
statusURL *url.URL
addrURL *url.URL
)
for i := 0; i < 10; i++ {
// retry 10 times to get available port
statusURL, err = url.Parse(tempurl.Alloc())
if err != nil {
return errors.Trace(err)
}
listen, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", statusURL.Port()))
if err == nil {
// release port listening
listen.Close()
break
}
}
statusPort, err := strconv.ParseInt(statusURL.Port(), 10, 32)
if err != nil {
return errors.Trace(err)
}
// choose a random available port
l1, _ := net.Listen("tcp", "127.0.0.1:")
statusPort := l1.Addr().(*net.TCPAddr).Port

for i := 0; i < 10; i++ {
addrURL, err = url.Parse(tempurl.Alloc())
if err != nil {
return errors.Trace(err)
}
listen, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", addrURL.Port()))
if err == nil {
// release port listening
listen.Close()
break
}
}
addrPort, err := strconv.ParseInt(addrURL.Port(), 10, 32)
if err != nil {
return errors.Trace(err)
}
_ = addrPort
// choose a random available port
l2, _ := net.Listen("tcp", "127.0.0.1:")
addrPort := l2.Addr().(*net.TCPAddr).Port

mock.TiDBDriver = server.NewTiDBDriver(mock.Storage)
cfg := config.NewConfig()
Expand All @@ -136,17 +100,20 @@ func (mock *Cluster) Start() error {
cfg.Status.ReportStatus = true
cfg.Socket = fmt.Sprintf("/tmp/tidb-mock-%d.sock", time.Now().UnixNano())

// close port for next listen in NewServer
l1.Close()
l2.Close()
svr, err := server.NewServer(cfg, mock.TiDBDriver)
if err != nil {
return errors.Trace(err)
}
mock.Server = svr
go func() {
if err1 := svr.Run(); err != nil {
if err1 := svr.Run(); err1 != nil {
panic(err1)
}
}()
mock.DSN = waitUntilServerOnline(addrURL.Host, cfg.Status.StatusPort)
mock.DSN = waitUntilServerOnline("127.0.0.1", cfg.Status.StatusPort)
return nil
}

Expand Down
10 changes: 2 additions & 8 deletions br/pkg/restore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func (s *testRestoreClientSuite) SetUpTest(c *C) {
var err error
s.mock, err = mock.NewCluster()
c.Assert(err, IsNil)
c.Assert(s.mock.Start(), IsNil)
}

func (s *testRestoreClientSuite) TearDownTest(c *C) {
s.mock.Stop()
testleak.AfterTest(c)()
}

func (s *testRestoreClientSuite) TestCreateTables(c *C) {
c.Assert(s.mock.Start(), IsNil)
defer s.mock.Stop()
client, err := restore.NewRestoreClient(gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg)
c.Assert(err, IsNil)

Expand Down Expand Up @@ -102,8 +102,6 @@ func (s *testRestoreClientSuite) TestCreateTables(c *C) {
}

func (s *testRestoreClientSuite) TestIsOnline(c *C) {
c.Assert(s.mock.Start(), IsNil)
defer s.mock.Stop()

client, err := restore.NewRestoreClient(gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg)
c.Assert(err, IsNil)
Expand All @@ -114,8 +112,6 @@ func (s *testRestoreClientSuite) TestIsOnline(c *C) {
}

func (s *testRestoreClientSuite) TestPreCheckTableClusterIndex(c *C) {
c.Assert(s.mock.Start(), IsNil)
defer s.mock.Stop()

client, err := restore.NewRestoreClient(gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -185,8 +181,6 @@ func (fpdc fakePDClient) GetAllStores(context.Context, ...pd.GetStoreOption) ([]
}

func (s *testRestoreClientSuite) TestPreCheckTableTiFlashReplicas(c *C) {
c.Assert(s.mock.Start(), IsNil)
defer s.mock.Stop()

mockStores := []*metapb.Store{
{
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/trace/tracing_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ func TestSpan(t *testing.T) {
// possible result:
// "jobA 22:02:02.545296 200.621764ms\n"
// " └─jobB 22:02:02.545297 100.293444ms\n"
require.Regexp(t, `^jobA.*20[0-9]\.[0-9]+ms\n └─jobB.*10[0-9]\.[0-9]+ms\n$`, s)
require.Regexp(t, `^jobA.*2[0-9][0-9]\.[0-9]+ms\n └─jobB.*1[0-9][0-9]\.[0-9]+ms\n$`, s)
}