Skip to content

Commit

Permalink
*: remove logutil in proxy (pingcap#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox committed Mar 13, 2023
1 parent f15a8a7 commit f47e02a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
15 changes: 8 additions & 7 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/pingcap/TiProxy/pkg/manager/router"
pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -64,6 +63,7 @@ type redirectResult struct {
// - If it retries after each command: the latency will be unacceptable afterwards if it always fails.
// - If it stops receiving signals: the previous new backend may be abnormal but the next new backend may be good.
type BackendConnManager struct {
logger *zap.Logger
connectionID uint64
authenticator *Authenticator
cmdProcessor *CmdProcessor
Expand All @@ -84,8 +84,9 @@ type BackendConnManager struct {
}

// NewBackendConnManager creates a BackendConnManager.
func NewBackendConnManager(connectionID uint64) *BackendConnManager {
func NewBackendConnManager(logger *zap.Logger, connectionID uint64) *BackendConnManager {
return &BackendConnManager{
logger: logger,
connectionID: connectionID,
cmdProcessor: NewCmdProcessor(),
authenticator: &Authenticator{},
Expand Down Expand Up @@ -270,12 +271,12 @@ func (mgr *BackendConnManager) tryRedirect(ctx context.Context) {
}
if rs.err != nil {
if ignoredErr := newConn.Close(); ignoredErr != nil {
logutil.Logger(ctx).Warn("close new backend connection failed", zap.Error(ignoredErr))
mgr.logger.Warn("close new backend connection failed", zap.Error(ignoredErr))
}
return
}
if ignoredErr := mgr.backendConn.Close(); ignoredErr != nil {
logutil.Logger(ctx).Warn("close previous backend connection failed", zap.Error(ignoredErr))
mgr.logger.Warn("close previous backend connection failed", zap.Error(ignoredErr))
}
mgr.backendConn = newConn
}
Expand Down Expand Up @@ -327,12 +328,12 @@ func (mgr *BackendConnManager) notifyRedirectResult(ctx context.Context, rs *red
}
if rs.err != nil {
err := eventReceiver.OnRedirectFail(rs.from, rs.to, mgr)
logutil.Logger(ctx).Warn("redirect connection failed", zap.String("from", rs.from),
mgr.logger.Warn("redirect connection failed", zap.String("from", rs.from),
zap.String("to", rs.to), zap.Uint64("conn", mgr.connectionID),
zap.NamedError("redirect_err", rs.err), zap.NamedError("notify_err", err))
} else {
err := eventReceiver.OnRedirectSucceed(rs.from, rs.to, mgr)
logutil.Logger(ctx).Info("redirect connection succeeds", zap.String("from", rs.from),
mgr.logger.Info("redirect connection succeeds", zap.String("from", rs.from),
zap.String("to", rs.to), zap.Uint64("conn", mgr.connectionID), zap.NamedError("notify_err", err))
}
}
Expand Down Expand Up @@ -364,7 +365,7 @@ func (mgr *BackendConnManager) Close() error {
// Just notify it with the current address.
if len(addr) > 0 {
if err := eventReceiver.OnConnClosed(addr, mgr); err != nil {
logutil.BgLogger().Error("close connection error", zap.String("addr", addr),
mgr.logger.Error("close connection error", zap.String("addr", addr),
zap.Uint64("conn", mgr.connectionID), zap.NamedError("notify_err", err))
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/proxy/backend/mock_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package backend

import (
"crypto/tls"
"testing"

gomysql "github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/TiProxy/lib/util/logger"
pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
)

Expand Down Expand Up @@ -46,10 +48,10 @@ type mockProxy struct {
holdRequest bool
}

func newMockProxy(cfg *proxyConfig) *mockProxy {
func newMockProxy(t *testing.T, cfg *proxyConfig) *mockProxy {
mp := &mockProxy{
proxyConfig: cfg,
BackendConnManager: NewBackendConnManager(0),
BackendConnManager: NewBackendConnManager(logger.CreateLoggerForTest(t), 0),
}
mp.cmdProcessor.capability = cfg.capability
return mp
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/backend/testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func newTestSuite(t *testing.T, tc *tcpConnSuite, overriders ...cfgOverrider) (*
config.clientConfig.tlsConfig = tc.clientTLSConfig
})...)
ts.mb = newMockBackend(cfg.backendConfig)
ts.mp = newMockProxy(cfg.proxyConfig)
ts.mp = newMockProxy(t, cfg.proxyConfig)
ts.mc = newMockClient(cfg.clientConfig)
ts.tc = tc
ts.testSuiteConfig = cfg.testSuiteConfig
Expand Down
3 changes: 1 addition & 2 deletions pkg/proxy/client/client_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/pingcap/TiProxy/pkg/proxy/backend"
pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -73,7 +72,7 @@ func (cc *ClientConnection) connectBackend(ctx context.Context) error {

func (cc *ClientConnection) Run(ctx context.Context) {
if err := cc.connectBackend(ctx); err != nil {
logutil.Logger(ctx).Info("new connection fails", zap.String("remoteAddr", cc.Addr()), zap.Error(err))
cc.logger.Info("new connection fails", zap.String("remoteAddr", cc.Addr()), zap.Error(err))
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *SQLServer) onConn(ctx context.Context, conn net.Conn) {
connID := s.mu.connID
s.mu.connID++
logger := s.logger.With(zap.Uint64("connID", connID))
clientConn := client.NewClientConnection(logger, conn, s.serverTLSConfig, s.clusterTLSConfig, s.nsmgr, backend.NewBackendConnManager(connID))
clientConn := client.NewClientConnection(logger.Named("cliconn"), conn, s.serverTLSConfig, s.clusterTLSConfig, s.nsmgr, backend.NewBackendConnManager(logger.Named("bemgr"), connID))
s.mu.clients[connID] = clientConn
s.mu.Unlock()

Expand Down

0 comments on commit f47e02a

Please sign in to comment.