Skip to content

Commit

Permalink
tidb-server, terror: add cleanup when createServer exits abnormally (
Browse files Browse the repository at this point in the history
…#6947) (#6964)

* tidb-server, terror: add cleanup in createServer
  • Loading branch information
zimulala authored and shenli committed Jul 3, 2018
1 parent 9cefb33 commit d341598
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 5 additions & 2 deletions terror/terror.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,12 @@ func ErrorNotEqual(err1, err2 error) bool {
return !ErrorEqual(err1, err2)
}

// MustNil fatals if err is not nil.
func MustNil(err error) {
// MustNil cleans up and fatals if err is not nil.
func MustNil(err error, closeFuns ...func()) {
if err != nil {
for _, f := range closeFuns {
f()
}
log.Fatalf(errors.ErrorStack(err))
}
}
Expand Down
15 changes: 10 additions & 5 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,16 @@ func createServer() {
driver = server.NewTiDBDriver(storage)
var err error
svr, err = server.NewServer(cfg, driver)
terror.MustNil(err)
// 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)
terror.MustNil(err, closeDomainAndStorage)
}
}

Expand Down Expand Up @@ -484,11 +485,15 @@ func runServer() {
}
}

func closeDomainAndStorage() {
dom.Close()
err := storage.Close()
terror.Log(errors.Trace(err))
}

func cleanup() {
if graceful {
svr.GracefulDown()
}
dom.Close()
err := storage.Close()
terror.Log(errors.Trace(err))
closeDomainAndStorage()
}

0 comments on commit d341598

Please sign in to comment.