Skip to content

Commit

Permalink
*: fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Apr 10, 2020
1 parent ae1bb61 commit 88dbc97
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 126 deletions.
88 changes: 15 additions & 73 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down
11 changes: 2 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 6 additions & 44 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand Down

0 comments on commit 88dbc97

Please sign in to comment.