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

tidb-server, terror: add cleanup when createServer exits abnormally… #6964

Merged
merged 1 commit into from
Jul 3, 2018
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
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()
}