From 41838ceb28d2f527cfaac539394c034f815663f0 Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Tue, 15 Jan 2019 10:59:41 +0800 Subject: [PATCH] x-server: remove the X Server (#9038) --- config/config.go | 13 ----- tidb-server/main.go | 22 ------- x-server/config.go | 22 ------- x-server/conn.go | 103 -------------------------------- x-server/server.go | 139 -------------------------------------------- 5 files changed, 299 deletions(-) delete mode 100644 x-server/config.go delete mode 100644 x-server/conn.go delete mode 100644 x-server/server.go diff --git a/config/config.go b/config/config.go index 3f29d75bd9301..0d1c357c340ba 100644 --- a/config/config.go +++ b/config/config.go @@ -67,7 +67,6 @@ type Config struct { Security Security `toml:"security" json:"security"` Status Status `toml:"status" json:"status"` Performance Performance `toml:"performance" json:"performance"` - XProtocol XProtocol `toml:"xprotocol" json:"xprotocol"` PreparedPlanCache PreparedPlanCache `toml:"prepared-plan-cache" json:"prepared-plan-cache"` OpenTracing OpenTracing `toml:"opentracing" json:"opentracing"` ProxyProtocol ProxyProtocol `toml:"proxy-protocol" json:"proxy-protocol"` @@ -163,14 +162,6 @@ type Performance struct { ForcePriority string `toml:"force-priority" json:"force-priority"` } -// XProtocol is the XProtocol section of the config. -type XProtocol struct { - XServer bool `toml:"xserver" json:"xserver"` - XHost string `toml:"xhost" json:"xhost"` - XPort uint `toml:"xport" json:"xport"` - XSocket string `toml:"xsocket" json:"xsocket"` -} - // PlanCache is the PlanCache section of the config. type PlanCache struct { Enabled bool `toml:"enabled" json:"enabled"` @@ -319,10 +310,6 @@ var defaultConf = Config{ PseudoEstimateRatio: 0.8, ForcePriority: "NO_PRIORITY", }, - XProtocol: XProtocol{ - XHost: "", - XPort: 0, - }, ProxyProtocol: ProxyProtocol{ Networks: "", HeaderTimeout: 5, diff --git a/tidb-server/main.go b/tidb-server/main.go index 4ae531d9e241e..9746781617b3b 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -51,7 +51,6 @@ import ( "github.com/pingcap/tidb/util/printer" "github.com/pingcap/tidb/util/signal" "github.com/pingcap/tidb/util/systimemon" - "github.com/pingcap/tidb/x-server" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" log "github.com/sirupsen/logrus" @@ -127,7 +126,6 @@ var ( storage kv.Storage dom *domain.Domain svr *server.Server - xsvr *xserver.Server graceful bool ) @@ -394,10 +392,6 @@ func validateConfig() { log.Errorf("log max-size should not be larger than %d MB", config.MaxLogFileSize) os.Exit(-1) } - if cfg.XProtocol.XServer { - log.Error("X Server is not available") - os.Exit(-1) - } cfg.OOMAction = strings.ToLower(cfg.OOMAction) // lower_case_table_names is allowed to be 0, 1, 2 @@ -482,24 +476,12 @@ func createServer() { svr, err = server.NewServer(cfg, driver) // Both domain and storage have started, so we have to clean them before exiting. terror.MustNil(err, closeDomainAndStorage) - if cfg.XProtocol.XServer { - xcfg := &xserver.Config{ - Addr: fmt.Sprintf("%s:%d", cfg.XProtocol.XHost, cfg.XProtocol.XPort), - Socket: cfg.XProtocol.XSocket, - TokenLimit: cfg.TokenLimit, - } - xsvr, err = xserver.NewServer(xcfg) - terror.MustNil(err, closeDomainAndStorage) - } } func serverShutdown(isgraceful bool) { if isgraceful { graceful = true } - if xsvr != nil { - xsvr.Close() // Should close xserver before server. - } svr.Close() } @@ -544,10 +526,6 @@ func setupTracing() { func runServer() { err := svr.Run() terror.MustNil(err) - if cfg.XProtocol.XServer { - err := xsvr.Run() - terror.MustNil(err) - } } func closeDomainAndStorage() { diff --git a/x-server/config.go b/x-server/config.go deleted file mode 100644 index 7ed06ff2dbea9..0000000000000 --- a/x-server/config.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2017 PingCAP, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -package xserver - -// Config contains configuration options. -type Config struct { - Addr string `json:"addr" toml:"addr"` - Socket string `json:"socket" toml:"socket"` - SkipAuth bool `json:"skip-auth" toml:"skip-auth"` - TokenLimit uint `json:"token-limit" toml:"token-limit"` -} diff --git a/x-server/conn.go b/x-server/conn.go deleted file mode 100644 index d06931bbb032f..0000000000000 --- a/x-server/conn.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2017 PingCAP, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -package xserver - -import ( - "io" - "net" - - "github.com/pingcap/errors" - "github.com/pingcap/parser/terror" - "github.com/pingcap/tidb/util/arena" - log "github.com/sirupsen/logrus" -) - -// clientConn represents a connection between server and client, -// it maintains connection specific state, handles client query. -type clientConn struct { - conn net.Conn - server *Server // a reference of server instance. - connectionID uint32 // atomically allocated by a global variable, unique in process scope. - collation uint8 // collation used by client, may be different from the collation used by database. - user string // user of the client. - dbname string // default database name. - salt []byte // random bytes used for authentication. - alloc arena.Allocator // an memory allocator for reducing memory allocation. - killed bool -} - -func (cc *clientConn) Run() { - defer func() { - x := recover() - if x != nil { - log.Error(x) - } - err := cc.Close() - terror.Log(errors.Trace(err)) - }() - - for !cc.killed { - tp, payload, err := cc.readPacket() - if err != nil { - if terror.ErrorNotEqual(err, io.EOF) { - log.Errorf("con:%d read packet error, close this connection %s", - cc.connectionID, errors.ErrorStack(err)) - } - return - } - if err = cc.dispatch(tp, payload); err != nil { - if terror.ErrorEqual(err, terror.ErrResultUndetermined) { - log.Errorf("con:%d result undetermined error, close this connection %s", - cc.connectionID, errors.ErrorStack(err)) - } else if terror.ErrorEqual(err, terror.ErrCritical) { - log.Errorf("con:%d critical error, stop the server listener %s", - cc.connectionID, errors.ErrorStack(err)) - select { - case cc.server.stopListenerCh <- struct{}{}: - default: - } - } - log.Warnf("con:%d dispatch error: %s", cc.connectionID, err) - cc.writeError(err) - return - } - } -} - -func (cc *clientConn) Close() error { - err := cc.conn.Close() - return errors.Trace(err) -} - -func (cc *clientConn) handshake() error { - // TODO: implement it. - return nil -} - -// readPacket reads a full size request encoded in x protocol. -// The message struct is like: -// ______________________________________________________ -// | 4 bytes length | 1 byte type | payload[0:length-1] | -// ------------------------------------------------------ -// See: https://dev.mysql.com/doc/internals/en/x-protocol-messages-messages.html -func (cc *clientConn) readPacket() (byte, []byte, error) { - return 0x00, nil, nil -} - -func (cc *clientConn) dispatch(tp byte, payload []byte) error { - return nil -} - -func (cc *clientConn) writeError(e error) { -} diff --git a/x-server/server.go b/x-server/server.go deleted file mode 100644 index cdb7ab78c3cd8..0000000000000 --- a/x-server/server.go +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2017 PingCAP, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -package xserver - -import ( - "math/rand" - "net" - "sync" - "sync/atomic" - "time" - - "github.com/pingcap/errors" - "github.com/pingcap/parser/mysql" - "github.com/pingcap/parser/terror" - "github.com/pingcap/tidb/server" - "github.com/pingcap/tidb/util" - "github.com/pingcap/tidb/util/arena" - log "github.com/sirupsen/logrus" -) - -var ( - baseConnID uint32 -) - -// Server is the MySQL X protocol server -type Server struct { - cfg *Config - listener net.Listener - rwlock *sync.RWMutex - concurrentLimiter *server.TokenLimiter - - stopListenerCh chan struct{} -} - -// NewServer creates a new Server. -func NewServer(cfg *Config) (s *Server, err error) { - s = &Server{ - cfg: cfg, - concurrentLimiter: server.NewTokenLimiter(cfg.TokenLimit), - rwlock: &sync.RWMutex{}, - stopListenerCh: make(chan struct{}, 1), - } - if cfg.Socket != "" { - cfg.SkipAuth = true - s.listener, err = net.Listen("unix", cfg.Socket) - } else { - s.listener, err = net.Listen("tcp", s.cfg.Addr) - } - if err != nil { - return nil, errors.Trace(err) - } - rand.Seed(time.Now().UTC().UnixNano()) - log.Infof("Server run MySQL Protocol Listen at [%s]", s.cfg.Addr) - return s, nil -} - -// Close closes the server. -func (s *Server) Close() { - if s.listener != nil { - err := s.listener.Close() - terror.Log(errors.Trace(err)) - s.listener = nil - } -} - -// Run runs the server. -func (s *Server) Run() error { - for { - conn, err := s.listener.Accept() - if err != nil { - if opErr, ok := err.(*net.OpError); ok { - if opErr.Err.Error() == "use of closed network connection" { - return nil - } - } - log.Errorf("accept error %s", err.Error()) - return errors.Trace(err) - } - if s.shouldStopListener() { - err = conn.Close() - terror.Log(errors.Trace(err)) - break - } - go s.onConn(conn) - } - return nil -} - -func (s *Server) shouldStopListener() bool { - select { - case <-s.stopListenerCh: - return true - default: - return false - } -} - -// onConn runs in its own goroutine, handles queries from this connection. -func (s *Server) onConn(c net.Conn) { - conn := s.newConn(c) - defer func() { - log.Infof("con:%d close x protocol connection", conn.connectionID) - }() - if err := conn.handshake(); err != nil { - // Some keep alive services will send request to TiDB and disconnect immediately. - // So we use info log level. - log.Infof("handshake error %s", errors.ErrorStack(err)) - err := c.Close() - terror.Log(errors.Trace(err)) - return - } - conn.Run() -} - -// newConn creates a new *clientConn from a net.Conn. -// It allocates a connection ID and random salt data for authentication. -func (s *Server) newConn(conn net.Conn) *clientConn { - cc := &clientConn{ - conn: conn, - server: s, - connectionID: atomic.AddUint32(&baseConnID, 1), - collation: mysql.DefaultCollationID, - alloc: arena.NewAllocator(32 * 1024), - } - log.Infof("con:%d new x protocol connection %s", cc.connectionID, conn.RemoteAddr().String()) - cc.salt = util.RandomBuf(20) - return cc -}