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

router, backend: refine some logs #259

Merged
merged 5 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/manager/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type RedirectableConn interface {
Redirect(addr string)
GetRedirectingAddr() string
NotifyBackendStatus(status BackendStatus)
ConnectionID() uint64
}

// backendWrapper contains the connections on the backend.
Expand Down
11 changes: 7 additions & 4 deletions pkg/manager/router/router_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ func (router *ScoreBasedRouter) OnBackendChanged(backends map[string]BackendStat
for addr, status := range backends {
be := router.lookupBackend(addr, true)
if be == nil && status != StatusCannotConnect {
router.logger.Info("find new backend", zap.String("addr", addr),
zap.String("status", status.String()))
router.logger.Info("update backend", zap.String("backend_addr", addr),
zap.String("prev_status", "none"), zap.String("cur_status", status.String()))
router.backends.PushBack(&backendWrapper{
status: status,
addr: addr,
connList: glist.New[*connWrapper](),
})
} else if be != nil {
backend := be.Value
router.logger.Info("update backend", zap.String("addr", addr),
router.logger.Info("update backend", zap.String("backend_addr", addr),
zap.String("prev_status", backend.status.String()), zap.String("cur_status", status.String()))
backend.status = status
if !router.removeBackendIfEmpty(be) {
Expand Down Expand Up @@ -367,8 +367,11 @@ func (router *ScoreBasedRouter) rebalance(maxNum int) {
if ce == nil {
break
}
router.removeConn(busiestEle, ce)
conn := ce.Value
router.logger.Info("begin redirect connection", zap.Uint64("connID", conn.ConnectionID()),
zap.String("from", busiestBackend.addr), zap.String("to", idlestBackend.addr),
zap.Int("from_score", busiestBackend.score()), zap.Int("to_score", idlestBackend.score()))
router.removeConn(busiestEle, ce)
conn.phase = phaseRedirectNotify
conn.lastRedirect = curTime
router.addConn(idlestEle, conn)
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, cctx ConnConte
// but TiDB does not send all of its supported capabilities
// thus we must ignore server capabilities
// however, I will log something
logger.Info("backend does not support capabilities from proxy", zap.Stringer("common", common), zap.Stringer("proxy", proxyCapability^common), zap.Stringer("backend", backendCapability^common))
logger.Debug("backend does not support capabilities from proxy", zap.Stringer("common", common), zap.Stringer("proxy", proxyCapability^common), zap.Stringer("backend", backendCapability^common))
}

// forward client handshake resp
Expand Down
18 changes: 9 additions & 9 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ func (mgr *BackendConnManager) getBackendIO(cctx ConnContext, auth *Authenticato
if err = selector.Succeed(mgr); err != nil {
// Bad luck: the backend has been recycled or shut down just after the selector returns it.
if ignoredErr := cn.Close(); ignoredErr != nil {
mgr.logger.Error("close backend connection failed", zap.String("addr", addr), zap.Error(ignoredErr))
mgr.logger.Error("close backend connection failed", zap.String("backend_addr", addr), zap.Error(ignoredErr))
}
return nil, err
}

mgr.logger.Info("connected to backend", zap.String("addr", addr))
mgr.logger.Info("connected to backend", zap.String("backend_addr", addr))
// NOTE: should use DNS name as much as possible
// Usually certs are signed with domain instead of IP addrs
// And `RemoteAddr()` will return IP addr
Expand Down Expand Up @@ -443,7 +443,7 @@ func (mgr *BackendConnManager) tryRedirect(ctx context.Context) {
if errors.Is(rs.err, net.ErrClosed) || pnet.IsDisconnectError(rs.err) || errors.Is(rs.err, os.ErrDeadlineExceeded) {
mgr.quitSource = SrcBackendQuit
if ignoredErr := mgr.clientIO.GracefulClose(); ignoredErr != nil {
mgr.logger.Warn("graceful close client IO error", zap.Stringer("addr", mgr.clientIO.RemoteAddr()), zap.Error(ignoredErr))
mgr.logger.Warn("graceful close client IO error", zap.Stringer("client_addr", mgr.clientIO.RemoteAddr()), zap.Error(ignoredErr))
}
}
return
Expand Down Expand Up @@ -558,7 +558,7 @@ func (mgr *BackendConnManager) tryGracefulClose(ctx context.Context) {
mgr.quitSource = SrcProxyQuit
// Closing clientIO will cause the whole connection to be closed.
if err := mgr.clientIO.GracefulClose(); err != nil {
mgr.logger.Warn("graceful close client IO error", zap.Stringer("addr", mgr.clientIO.RemoteAddr()), zap.Error(err))
mgr.logger.Warn("graceful close client IO error", zap.Stringer("client_addr", mgr.clientIO.RemoteAddr()), zap.Error(err))
}
mgr.closeStatus.Store(statusClosing)
}
Expand All @@ -573,11 +573,11 @@ func (mgr *BackendConnManager) checkBackendActive() {
defer mgr.processLock.Unlock()
backendIO := mgr.backendIO.Load()
if !backendIO.IsPeerActive() {
mgr.logger.Info("backend connection is closed, close client connection", zap.Stringer("client", mgr.clientIO.RemoteAddr()),
zap.Stringer("backend", backendIO.RemoteAddr()))
mgr.logger.Info("backend connection is closed, close client connection",
zap.Stringer("client_addr", mgr.clientIO.RemoteAddr()), zap.Stringer("backend_addr", backendIO.RemoteAddr()))
mgr.quitSource = SrcBackendQuit
if err := mgr.clientIO.GracefulClose(); err != nil {
mgr.logger.Warn("graceful close client IO error", zap.Stringer("addr", mgr.clientIO.RemoteAddr()), zap.Error(err))
mgr.logger.Warn("graceful close client IO error", zap.Stringer("client_addr", mgr.clientIO.RemoteAddr()), zap.Error(err))
}
mgr.closeStatus.Store(statusClosing)
}
Expand Down Expand Up @@ -669,7 +669,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 {
mgr.logger.Error("close connection error", zap.String("addr", addr), zap.NamedError("notify_err", err))
mgr.logger.Error("close connection error", zap.String("client_addr", addr), zap.NamedError("notify_err", err))
}
}
}
Expand All @@ -695,7 +695,7 @@ func (mgr *BackendConnManager) setKeepAlive(cfg config.KeepAlive) {
return
}
if err := backendIO.SetKeepalive(cfg); err != nil {
mgr.logger.Warn("failed to set keepalive", zap.Error(err), zap.Stringer("backend", backendIO.RemoteAddr()))
mgr.logger.Warn("failed to set keepalive", zap.Stringer("backend_addr", backendIO.RemoteAddr()), zap.Error(err))
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/client/client_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ clean:
switch src {
case backend.SrcClientQuit, backend.SrcClientErr, backend.SrcProxyQuit:
default:
cc.logger.Info(msg, zap.Error(err), zap.Stringer("quit source", src))
cc.logger.Info(msg, zap.String("backend_addr", cc.connMgr.ServerAddr()), zap.Stringer("quit source", src), zap.Error(err))
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ func (s *SQLServer) onConn(ctx context.Context, conn net.Conn) {
// 'maxConns == 0' => unlimited connections
if maxConns != 0 && conns >= maxConns {
s.mu.Unlock()
s.logger.Warn("too many connections", zap.Uint64("max connections", maxConns), zap.String("addr", conn.RemoteAddr().Network()), zap.Error(conn.Close()))
s.logger.Warn("too many connections", zap.Uint64("max connections", maxConns), zap.String("client_addr", conn.RemoteAddr().Network()), zap.Error(conn.Close()))
return
}
if s.mu.inShutdown {
s.mu.Unlock()
s.logger.Warn("in shutdown", zap.String("addr", conn.RemoteAddr().Network()), zap.Error(conn.Close()))
s.logger.Warn("in shutdown", zap.String("client_addr", conn.RemoteAddr().Network()), zap.Error(conn.Close()))
return
}

connID := s.mu.connID
s.mu.connID++
logger := s.logger.With(zap.Uint64("connID", connID), zap.String("remoteAddr", conn.RemoteAddr().String()))
logger := s.logger.With(zap.Uint64("connID", connID), zap.String("client_addr", conn.RemoteAddr().String()))
clientConn := client.NewClientConnection(logger.Named("conn"), conn, s.certMgr.ServerTLS(), s.certMgr.SQLTLS(),
s.hsHandler, connID, &backend.BCConfig{
ProxyProtocol: s.mu.proxyProtocol,
Expand Down