From 1f84db5cd7e390d99776a43e5db228022f871477 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 26 Sep 2018 17:09:13 +0800 Subject: [PATCH] add windows build support (#7773) --- tidb-server/main.go | 46 +++++++---------------------- util/signal/signal_posix.go | 54 +++++++++++++++++++++++++++++++++++ util/signal/signal_windows.go | 40 ++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 35 deletions(-) create mode 100644 util/signal/signal_posix.go create mode 100644 util/signal/signal_windows.go diff --git a/tidb-server/main.go b/tidb-server/main.go index ba4e060ae6eb9..44bddf593e4e0 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -18,11 +18,9 @@ import ( "fmt" "net" "os" - "os/signal" "runtime" "strconv" "strings" - "syscall" "time" "github.com/opentracing/opentracing-go" @@ -46,9 +44,10 @@ import ( "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/printer" + "github.com/pingcap/tidb/util/signal" "github.com/pingcap/tidb/util/systimemon" "github.com/pingcap/tidb/x-server" - binlog "github.com/pingcap/tipb/go-binlog" + "github.com/pingcap/tipb/go-binlog" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" @@ -142,7 +141,7 @@ func main() { setupMetrics() createStoreAndDomain() createServer() - setupSignalHandler() + signal.SetupSignalHandler(serverShutdown) runServer() cleanup() os.Exit(0) @@ -438,37 +437,14 @@ func createServer() { } } -func setupSignalHandler() { - usrDefSignalChan := make(chan os.Signal, 1) - signal.Notify(usrDefSignalChan, syscall.SIGUSR1) - go func() { - buf := make([]byte, 1<<16) - for { - sig := <-usrDefSignalChan - if sig == syscall.SIGUSR1 { - stackLen := runtime.Stack(buf, true) - log.Printf("\n=== Got signal [%s] to dump goroutine stack. ===\n%s\n=== Finished dumping goroutine stack. ===\n", sig, buf[:stackLen]) - } - } - }() - - closeSignalChan := make(chan os.Signal, 1) - signal.Notify(closeSignalChan, - syscall.SIGHUP, - syscall.SIGINT, - syscall.SIGTERM, - syscall.SIGQUIT) - go func() { - sig := <-closeSignalChan - log.Infof("Got signal [%s] to exit.", sig) - if sig == syscall.SIGQUIT { - graceful = true - } - if xsvr != nil { - xsvr.Close() // Should close xserver before server. - } - svr.Close() - }() +func serverShutdown(isgraceful bool) { + if isgraceful { + graceful = true + } + if xsvr != nil { + xsvr.Close() // Should close xserver before server. + } + svr.Close() } func setupMetrics() { diff --git a/util/signal/signal_posix.go b/util/signal/signal_posix.go new file mode 100644 index 0000000000000..3475eeb701192 --- /dev/null +++ b/util/signal/signal_posix.go @@ -0,0 +1,54 @@ +// Copyright 2018 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. +// +build linux darwin freebsd unix + +package signal + +import ( + "os" + "os/signal" + "runtime" + "syscall" + + log "github.com/sirupsen/logrus" +) + +// SetupSignalHandler setup signal handler for TiDB Server +func SetupSignalHandler(shudownFunc func(bool)) { + usrDefSignalChan := make(chan os.Signal, 1) + + signal.Notify(usrDefSignalChan, syscall.SIGUSR1) + go func() { + buf := make([]byte, 1<<16) + for { + sig := <-usrDefSignalChan + if sig == syscall.SIGUSR1 { + stackLen := runtime.Stack(buf, true) + log.Printf("\n=== Got signal [%s] to dump goroutine stack. ===\n%s\n=== Finished dumping goroutine stack. ===\n", sig, buf[:stackLen]) + } + } + }() + + closeSignalChan := make(chan os.Signal, 1) + signal.Notify(closeSignalChan, + syscall.SIGHUP, + syscall.SIGINT, + syscall.SIGTERM, + syscall.SIGQUIT) + + go func() { + sig := <-closeSignalChan + log.Infof("Got signal [%s] to exit.", sig) + shudownFunc(sig == syscall.SIGQUIT) + }() +} diff --git a/util/signal/signal_windows.go b/util/signal/signal_windows.go new file mode 100644 index 0000000000000..98560f5716d99 --- /dev/null +++ b/util/signal/signal_windows.go @@ -0,0 +1,40 @@ +// Copyright 2018 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. +// +build windows + +package signal + +import ( + "os" + "os/signal" + "syscall" + + log "github.com/sirupsen/logrus" +) + +// SetupSignalHandler setup signal handler for TiDB Server +func SetupSignalHandler(shudownFunc func(bool)) { + //todo deal with dump goroutine stack on windows + closeSignalChan := make(chan os.Signal, 1) + signal.Notify(closeSignalChan, + syscall.SIGHUP, + syscall.SIGINT, + syscall.SIGTERM, + syscall.SIGQUIT) + + go func() { + sig := <-closeSignalChan + log.Infof("Got signal [%s] to exit.", sig) + shudownFunc(sig == syscall.SIGQUIT) + }() +}