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

server: exit tidb-server directly when all connections not in txn #44953

Merged
merged 2 commits into from
Jun 27, 2023
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: 7 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,13 @@ func (cc *clientConn) Run(ctx context.Context) {
return
}

// Should check InTxn() to avoid execute `begin` stmt.
if cc.server.inShutdownMode.Load() {
if !cc.ctx.GetSessionVars().InTxn() {
return
}
}

if !cc.CompareAndSwapStatus(connStatusReading, connStatusDispatching) {
return
}
Expand Down
30 changes: 29 additions & 1 deletion server/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ func TestConnExecutionTimeout(t *testing.T) {
}

func TestShutDown(t *testing.T) {
store := testkit.CreateMockStore(t)
store, dom := testkit.CreateMockStoreAndDomain(t)

cc := &clientConn{}
se, err := session.CreateSession4Test(store)
Expand All @@ -775,6 +775,34 @@ func TestShutDown(t *testing.T) {
// assert ErrQueryInterrupted
err = cc.handleQuery(context.Background(), "select 1")
require.Equal(t, exeerrors.ErrQueryInterrupted, err)

cfg := newTestConfig()
cfg.Port = 0
cfg.Status.StatusPort = 0
drv := NewTiDBDriver(store)
srv, err := NewServer(cfg, drv)
require.NoError(t, err)
srv.SetDomain(dom)

cc = &clientConn{server: srv}
cc.setCtx(tc)

// test in txn
srv.clients[dom.NextConnID()] = cc
cc.getCtx().GetSessionVars().SetInTxn(true)

waitTime := 100 * time.Millisecond
begin := time.Now()
srv.DrainClients(waitTime, waitTime)
require.Greater(t, time.Since(begin), waitTime)

// test not in txn
srv.clients[dom.NextConnID()] = cc
cc.getCtx().GetSessionVars().SetInTxn(false)

begin = time.Now()
srv.DrainClients(waitTime, waitTime)
require.Less(t, time.Since(begin), waitTime)
}

type snapshotCache interface {
Expand Down
9 changes: 3 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"crypto/tls"
"fmt"
"io"
"math/rand"
"net"
"net/http" //nolint:goimports
_ "net/http/pprof" // #nosec G108 for pprof
Expand Down Expand Up @@ -327,9 +326,6 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
}
}

// Init rand seed for randomBuf()
rand.Seed(time.Now().UTC().UnixNano())

xhebox marked this conversation as resolved.
Show resolved Hide resolved
variable.RegisterStatistics(s)

return s, nil
Expand Down Expand Up @@ -549,8 +545,6 @@ func (s *Server) closeListener() {
metrics.ServerEventCounter.WithLabelValues(metrics.EventClose).Inc()
}

var gracefulCloseConnectionsTimeout = 15 * time.Second

// Close closes the server.
func (s *Server) Close() {
s.startShutdown()
Expand Down Expand Up @@ -917,6 +911,9 @@ func (s *Server) DrainClients(drainWait time.Duration, cancelWait time.Duration)
go func() {
defer close(allDone)
for _, conn := range conns {
if !conn.getCtx().GetSessionVars().InTxn() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afrait that this is a compatibility breaker. /cc @djshow832

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behaviour is same as #32111 merged before, like v6.5.x

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think it's a compatibility breaker? @xhebox

Copy link
Contributor

@xhebox xhebox Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is different from 7.1, but same as 6.5. Just FYI, the behavior of graceful shutdown changed again.

continue
}
select {
case <-conn.quit:
case <-quitWaitingForConns:
Expand Down
6 changes: 3 additions & 3 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ func main() {
terror.RegisterFinish()

exited := make(chan struct{})
signal.SetupSignalHandler(func(graceful bool) {
signal.SetupSignalHandler(func() {
svr.Close()
cleanup(svr, storage, dom, graceful)
cleanup(svr, storage, dom)
cpuprofile.StopCPUProfiler()
resourcemanager.InstanceResourceManager.Stop()
close(exited)
Expand Down Expand Up @@ -854,7 +854,7 @@ func closeDomainAndStorage(storage kv.Storage, dom *domain.Domain) {
// We should better provider a dynamic way to set this value.
var gracefulCloseConnectionsTimeout = 15 * time.Second

func cleanup(svr *server.Server, storage kv.Storage, dom *domain.Domain, _ bool) {
func cleanup(svr *server.Server, storage kv.Storage, dom *domain.Domain) {
dom.StopAutoAnalyze()

drainClientWait := gracefulCloseConnectionsTimeout
Expand Down
4 changes: 2 additions & 2 deletions util/signal/signal_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// SetupSignalHandler setup signal handler for TiDB Server
func SetupSignalHandler(shutdownFunc func(bool)) {
func SetupSignalHandler(shutdownFunc func()) {
usrDefSignalChan := make(chan os.Signal, 1)

signal.Notify(usrDefSignalChan, syscall.SIGUSR1)
Expand All @@ -52,6 +52,6 @@ func SetupSignalHandler(shutdownFunc func(bool)) {
go func() {
sig := <-closeSignalChan
logutil.BgLogger().Info("got signal to exit", zap.Stringer("signal", sig))
shutdownFunc(sig != syscall.SIGHUP)
shutdownFunc()
}()
}