Skip to content

Commit

Permalink
backend, infosync: optimize TestHandlerReturnError and `TestEtcdSer…
Browse files Browse the repository at this point in the history
…verDown4Sync` (#398)
  • Loading branch information
djshow832 authored Nov 13, 2023
1 parent 39626dd commit b8ce9c0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
26 changes: 12 additions & 14 deletions pkg/manager/infosync/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,18 @@ func TestEtcdServerDown4Sync(t *testing.T) {
ts := newEtcdTestSuite(t)
t.Cleanup(ts.close)
var ttl string
for i := 0; i < 5; i++ {
// Make the server down for some time.
addr := ts.shutdownServer()
time.Sleep(time.Second)
ts.startServer(addr)
require.Eventually(t, func() bool {
newTTL, info := ts.getTTLAndInfo(tiproxyTopologyPath)
satisfied := newTTL != ttl && len(info) > 0
if satisfied {
ttl = newTTL
}
return satisfied
}, 5*time.Second, 100*time.Millisecond)
}
// Make the server down for some time.
addr := ts.shutdownServer()
time.Sleep(time.Second)
ts.startServer(addr)
require.Eventually(t, func() bool {
newTTL, info := ts.getTTLAndInfo(tiproxyTopologyPath)
satisfied := newTTL != ttl && len(info) > 0
if satisfied {
ttl = newTTL
}
return satisfied
}, 5*time.Second, 100*time.Millisecond)
}

// TTL and info are erased after the client shuts down normally.
Expand Down
5 changes: 2 additions & 3 deletions pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/binary"
"fmt"
"net"
"time"

"github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/tidb/util/hack"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (auth *Authenticator) verifyBackendCaps(logger *zap.Logger, backendCapabili
return nil
}

type backendIOGetter func(ctx ConnContext, auth *Authenticator, resp *pnet.HandshakeResp, timeout time.Duration) (*pnet.PacketIO, error)
type backendIOGetter func(ctx ConnContext, auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error)

func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, cctx ConnContext, clientIO *pnet.PacketIO, handshakeHandler HandshakeHandler,
getBackendIO backendIOGetter, frontendTLSConfig, backendTLSConfig *tls.Config) error {
Expand Down Expand Up @@ -162,7 +161,7 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, cctx ConnConte
RECONNECT:

// In case of testing, backendIO is passed manually that we don't want to bother with the routing logic.
backendIO, err := getBackendIO(cctx, auth, clientResp, 15*time.Second)
backendIO, err := getBackendIO(cctx, auth, clientResp)
if err != nil {
return pnet.WrapUserError(err, connectErrMsg)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ var (
)

const (
DialTimeout = 1 * time.Second
// DialTimeout is the timeout for each dial.
DialTimeout = 1 * time.Second
// ConnectTimeout is the timeout for choosing and connecting to an available backend.
ConnectTimeout = 15 * time.Second
// CheckBackendInterval is the interval for checking if the backend is still connected.
CheckBackendInterval = time.Minute
)

Expand Down Expand Up @@ -74,6 +78,7 @@ type BCConfig struct {
HealthyKeepAlive config.KeepAlive
UnhealthyKeepAlive config.KeepAlive
CheckBackendInterval time.Duration
ConnectTimeout time.Duration
ConnBufferSize int
ProxyProtocol bool
RequireBackendTLS bool
Expand All @@ -83,6 +88,9 @@ func (cfg *BCConfig) check() {
if cfg.CheckBackendInterval == time.Duration(0) {
cfg.CheckBackendInterval = CheckBackendInterval
}
if cfg.ConnectTimeout == time.Duration(0) {
cfg.ConnectTimeout = ConnectTimeout
}
}

// BackendConnManager migrates a session from one BackendConnection to another.
Expand Down Expand Up @@ -180,15 +188,15 @@ func (mgr *BackendConnManager) Connect(ctx context.Context, clientIO *pnet.Packe
return nil
}

func (mgr *BackendConnManager) getBackendIO(cctx ConnContext, auth *Authenticator, resp *pnet.HandshakeResp, timeout time.Duration) (*pnet.PacketIO, error) {
func (mgr *BackendConnManager) getBackendIO(cctx ConnContext, auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error) {
r, err := mgr.handshakeHandler.GetRouter(cctx, resp)
if err != nil {
return nil, pnet.WrapUserError(err, err.Error())
}
// Reasons to wait:
// - The TiDB instances may not be initialized yet
// - One TiDB may be just shut down and another is just started but not ready yet
bctx, cancel := context.WithTimeout(context.Background(), timeout)
bctx, cancel := context.WithTimeout(context.Background(), mgr.config.ConnectTimeout)
selector := r.GetBackendSelector()
startTime := time.Now()
var addr string
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/backend/backend_conn_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ func TestHandlerReturnError(t *testing.T) {
quitSource: SrcProxyErr,
},
{
// TODO: make it fail faster.
cfg: func(config *testConfig) {
config.proxyConfig.bcConfig.ConnectTimeout = time.Second
config.proxyConfig.handler.getRouter = func(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error) {
return router.NewStaticRouter(nil), nil
}
Expand Down Expand Up @@ -857,7 +857,7 @@ func TestGetBackendIO(t *testing.T) {
},
}
lg, _ := logger.CreateLoggerForTest(t)
mgr := NewBackendConnManager(lg, handler, 0, &BCConfig{})
mgr := NewBackendConnManager(lg, handler, 0, &BCConfig{ConnectTimeout: time.Second})
var wg waitgroup.WaitGroup
for i := 0; i <= len(listeners); i++ {
wg.Run(func() {
Expand All @@ -867,7 +867,7 @@ func TestGetBackendIO(t *testing.T) {
require.NoError(t, cn.Close())
}
})
io, err := mgr.getBackendIO(mgr, mgr.authenticator, nil, time.Second)
io, err := mgr.getBackendIO(mgr, mgr.authenticator, nil)
if err == nil {
require.NoError(t, io.Close())
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/proxy/backend/mock_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package backend
import (
"crypto/tls"
"testing"
"time"

gomysql "github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/tiproxy/lib/util/logger"
Expand Down Expand Up @@ -58,7 +57,7 @@ func newMockProxy(t *testing.T, cfg *proxyConfig) *mockProxy {
}

func (mp *mockProxy) authenticateFirstTime(clientIO, backendIO *pnet.PacketIO) error {
if err := mp.authenticator.handshakeFirstTime(mp.logger, mp, clientIO, mp.handshakeHandler, func(ctx ConnContext, auth *Authenticator, resp *pnet.HandshakeResp, timeout time.Duration) (*pnet.PacketIO, error) {
if err := mp.authenticator.handshakeFirstTime(mp.logger, mp, clientIO, mp.handshakeHandler, func(ctx ConnContext, auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error) {
return backendIO, nil
}, mp.frontendTLSConfig, mp.backendTLSConfig); err != nil {
return err
Expand Down

0 comments on commit b8ce9c0

Please sign in to comment.