Skip to content

Commit

Permalink
add windows build support (#7773)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi authored and ngaut committed Sep 26, 2018
1 parent 4b5d83b commit 1f84db5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 35 deletions.
46 changes: 11 additions & 35 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import (
"fmt"
"net"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/opentracing/opentracing-go"
Expand All @@ -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"
Expand Down Expand Up @@ -142,7 +141,7 @@ func main() {
setupMetrics()
createStoreAndDomain()
createServer()
setupSignalHandler()
signal.SetupSignalHandler(serverShutdown)
runServer()
cleanup()
os.Exit(0)
Expand Down Expand Up @@ -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() {
Expand Down
54 changes: 54 additions & 0 deletions util/signal/signal_posix.go
Original file line number Diff line number Diff line change
@@ -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)
}()
}
40 changes: 40 additions & 0 deletions util/signal/signal_windows.go
Original file line number Diff line number Diff line change
@@ -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)
}()
}

0 comments on commit 1f84db5

Please sign in to comment.