From 88dbc97f8e40931498ba5455bed49cbebc17945c Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 10 Apr 2020 16:50:57 +0800 Subject: [PATCH] *: fix conflicts --- server/http_status.go | 88 ++++++++----------------------------------- server/server.go | 11 +----- server/tidb_test.go | 50 +++--------------------- 3 files changed, 23 insertions(+), 126 deletions(-) diff --git a/server/http_status.go b/server/http_status.go index 3b9fe1412d131..ac5fed8a544e4 100644 --- a/server/http_status.go +++ b/server/http_status.go @@ -74,24 +74,25 @@ func (s *Server) listenStatusHTTPServer() error { s.statusAddr = fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, defaultStatusPort) } - logutil.BgLogger().Info("for status and metrics report", zap.String("listening on addr", s.statusAddr)) - tlsConfig, err := s.cfg.Security.ToTLSConfig() + var err error + logutil.Logger(context.Background()).Info("for status and metrics report", zap.String("listening on addr", s.statusAddr)) + s.statusListener, err = net.Listen("tcp", s.statusAddr) if err != nil { - logutil.BgLogger().Error("invalid TLS config", zap.Error(err)) + logutil.Logger(context.Background()).Info("listen failed", zap.Error(err)) return errors.Trace(err) } - tlsConfig = s.setCNChecker(tlsConfig) - if tlsConfig != nil { - // we need to manage TLS here for cmux to distinguish between HTTP and gRPC. - s.statusListener, err = tls.Listen("tcp", s.statusAddr, tlsConfig) - } else { - s.statusListener, err = net.Listen("tcp", s.statusAddr) - } - if err != nil { - logutil.BgLogger().Info("listen failed", zap.Error(err)) - return errors.Trace(err) + if len(s.cfg.Security.ClusterSSLCA) != 0 { + tlsConfig, err := s.cfg.Security.ToTLSConfig() + if err != nil { + logutil.Logger(context.Background()).Error("invalid TLS config", zap.Error(err)) + return errors.Trace(err) + } + tlsConfig = s.setCNChecker(tlsConfig) + logutil.Logger(context.Background()).Info("HTTP/gRPC status server secure connection is enabled", zap.Bool("CN verification enabled", tlsConfig.VerifyPeerCertificate != nil)) + s.statusListener = tls.NewListener(s.statusListener, tlsConfig) } + return nil } @@ -140,19 +141,6 @@ func (s *Server) startHTTPServer() { router.Handle("/mvcc/hex/{hexKey}", mvccTxnHandler{tikvHandlerTool, opMvccGetByHex}) router.Handle("/mvcc/index/{db}/{table}/{index}/{handle}", mvccTxnHandler{tikvHandlerTool, opMvccGetByIdx}) } -<<<<<<< HEAD - addr := fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, s.cfg.Status.StatusPort) - if s.cfg.Status.StatusPort == 0 { - addr = fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, defaultStatusPort) - } -======= - - // HTTP path for get MVCC info - router.Handle("/mvcc/key/{db}/{table}/{handle}", mvccTxnHandler{tikvHandlerTool, opMvccGetByKey}) - router.Handle("/mvcc/txn/{startTS}/{db}/{table}", mvccTxnHandler{tikvHandlerTool, opMvccGetByTxn}) - router.Handle("/mvcc/hex/{hexKey}", mvccTxnHandler{tikvHandlerTool, opMvccGetByHex}) - router.Handle("/mvcc/index/{db}/{table}/{index}/{handle}", mvccTxnHandler{tikvHandlerTool, opMvccGetByIdx}) ->>>>>>> f8b2d96... server: if status address already in use, return an error (#15177) // HTTP path for web UI. if host, port, err := net.SplitHostPort(s.statusAddr); err == nil { @@ -290,55 +278,9 @@ func (s *Server) startHTTPServer() { logutil.Logger(context.Background()).Error("write HTTP index page failed", zap.Error(err)) } }) -<<<<<<< HEAD - - logutil.Logger(context.Background()).Info("for status and metrics report", zap.String("listening on addr", addr)) - s.statusServer = &http.Server{Addr: addr, Handler: CorsHandler{handler: serverMux, cfg: s.cfg}} - - ln, err := net.Listen("tcp", addr) - if err != nil { - logutil.Logger(context.Background()).Info("listen failed", zap.Error(err)) - return - } - - if len(s.cfg.Security.ClusterSSLCA) != 0 { - tlsConfig, err := s.cfg.Security.ToTLSConfig() - if err != nil { - logutil.Logger(context.Background()).Error("invalid TLS config", zap.Error(err)) - return - } - tlsConfig = s.setCNChecker(tlsConfig) - logutil.Logger(context.Background()).Info("HTTP/gRPC status server secure connection is enabled", zap.Bool("CN verification enabled", tlsConfig.VerifyPeerCertificate != nil)) - ln = tls.NewListener(ln, tlsConfig) - } - - err = s.statusServer.Serve(ln) -======= - s.startStatusServerAndRPCServer(serverMux) -} - -func (s *Server) startStatusServerAndRPCServer(serverMux *http.ServeMux) { - m := cmux.New(s.statusListener) - // Match connections in order: - // First HTTP, and otherwise grpc. - httpL := m.Match(cmux.HTTP1Fast()) - grpcL := m.Match(cmux.Any()) s.statusServer = &http.Server{Addr: s.statusAddr, Handler: CorsHandler{handler: serverMux, cfg: s.cfg}} - s.grpcServer = NewRPCServer(s.cfg, s.dom, s) - - go util.WithRecovery(func() { - err := s.grpcServer.Serve(grpcL) - logutil.BgLogger().Error("grpc server error", zap.Error(err)) - }, nil) - - go util.WithRecovery(func() { - err := s.statusServer.Serve(httpL) - logutil.BgLogger().Error("http server error", zap.Error(err)) - }, nil) - - err := m.Serve() ->>>>>>> f8b2d96... server: if status address already in use, return an error (#15177) + err = s.statusServer.Serve(s.statusListener) if err != nil { logutil.Logger(context.Background()).Info("serve status port failed", zap.Error(err)) } diff --git a/server/server.go b/server/server.go index 66135b3ae6d29..1f51d1998043a 100644 --- a/server/server.go +++ b/server/server.go @@ -112,16 +112,9 @@ type Server struct { concurrentLimiter *TokenLimiter clients map[uint32]*clientConn capability uint32 -<<<<<<< HEAD + statusAddr string + statusListener net.Listener statusServer *http.Server -======= - dom *domain.Domain - - statusAddr string - statusListener net.Listener - statusServer *http.Server - grpcServer *grpc.Server ->>>>>>> f8b2d96... server: if status address already in use, return an error (#15177) } // ConnectionCount gets current connection count. diff --git a/server/tidb_test.go b/server/tidb_test.go index 47958d758ed22..f7e2d77135f61 100644 --- a/server/tidb_test.go +++ b/server/tidb_test.go @@ -155,12 +155,12 @@ func (ts *TidbTestSuite) TestResultFieldTableIsNull(c *C) { runTestResultFieldTableIsNull(c) } -<<<<<<< HEAD func (ts *TidbTestSuite) TestStatusAPI(c *C) { c.Parallel() runTestStatusAPI(c) -======= -func (ts *tidbTestSuite) TestStatusPort(c *C) { +} + +func (ts *TidbTestSuite) TestStatusPort(c *C) { var err error ts.store, err = mockstore.NewMockTikvStore() session.DisableStats4Test() @@ -169,56 +169,18 @@ func (ts *tidbTestSuite) TestStatusPort(c *C) { c.Assert(err, IsNil) ts.tidbdrv = NewTiDBDriver(ts.store) cfg := config.NewConfig() - cfg.Port = genPort() + cfg.Port = 4008 cfg.Status.ReportStatus = true - cfg.Status.StatusPort = ts.statusPort + cfg.Status.StatusPort = 10090 cfg.Performance.TCPKeepAlive = true server, err := NewServer(cfg, ts.tidbdrv) c.Assert(err, NotNil) c.Assert(err.Error(), Equals, - fmt.Sprintf("listen tcp 0.0.0.0:%d: bind: address already in use", ts.statusPort)) + fmt.Sprintf("listen tcp 0.0.0.0:%d: bind: address already in use", cfg.Status.StatusPort)) c.Assert(server, IsNil) } -func (ts *tidbTestSuite) TestStatusAPIWithTLS(c *C) { - caCert, caKey, err := generateCert(0, "TiDB CA 2", nil, nil, "/tmp/ca-key-2.pem", "/tmp/ca-cert-2.pem") - c.Assert(err, IsNil) - _, _, err = generateCert(1, "tidb-server-2", caCert, caKey, "/tmp/server-key-2.pem", "/tmp/server-cert-2.pem") - c.Assert(err, IsNil) - - defer func() { - os.Remove("/tmp/ca-key-2.pem") - os.Remove("/tmp/ca-cert-2.pem") - os.Remove("/tmp/server-key-2.pem") - os.Remove("/tmp/server-cert-2.pem") - }() - - cli := newTestServerClient() - cli.statusScheme = "https" - cfg := config.NewConfig() - cfg.Port = cli.port - cfg.Status.StatusPort = cli.statusPort - cfg.Security.ClusterSSLCA = "/tmp/ca-cert-2.pem" - cfg.Security.ClusterSSLCert = "/tmp/server-cert-2.pem" - cfg.Security.ClusterSSLKey = "/tmp/server-key-2.pem" - server, err := NewServer(cfg, ts.tidbdrv) - c.Assert(err, IsNil) - go server.Run() - time.Sleep(time.Millisecond * 100) - - // https connection should work. - ts.runTestStatusAPI(c) - - // but plain http connection should fail. - cli.statusScheme = "http" - _, err = cli.fetchStatus("/status") - c.Assert(err, NotNil) - - server.Close() ->>>>>>> f8b2d96... server: if status address already in use, return an error (#15177) -} - func (ts *TidbTestSuite) TestStatusAPIWithTLSCNCheck(c *C) { caPath := filepath.Join(os.TempDir(), "ca-cert-cn.pem") serverKeyPath := filepath.Join(os.TempDir(), "server-key-cn.pem")