Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#51595
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
shenqidebaozi authored and hawkingrei committed Mar 27, 2024
1 parent 9220d94 commit f908c0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions server/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1870,3 +1870,25 @@ func TestCloseConn(t *testing.T) {
}
wg.Wait()
}

func TestSeverHealth(t *testing.T) {
RunInGoTestChan = make(chan struct{})
RunInGoTest = true
store := testkit.CreateMockStore(t)
tidbdrv := NewTiDBDriver(store)
cfg := newTestConfig()
cfg.Port, cfg.Status.StatusPort = 0, 0
cfg.Status.ReportStatus = false
server, err := NewServer(cfg, tidbdrv)
require.NoError(t, err)
require.True(t, server.inShutdownMode.Load(), "server should not be healthy")
go func() {
err = server.Run(nil)
require.NoError(t, err)
}()
defer server.Close()
for range RunInGoTestChan {
// wait for server to be healthy
}
require.False(t, server.inShutdownMode.Load(), "server should be healthy")
}
2 changes: 1 addition & 1 deletion server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func (s *Server) handleStatus(w http.ResponseWriter, req *http.Request) {
// If the server is in the process of shutting down, return a non-200 status.
// It is important not to return status{} as acquiring the s.ConnectionCount()
// acquires a lock that may already be held by the shutdown process.
if s.inShutdownMode {
if s.inShutdownMode || !s.health.Load() {
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sys/linux"
"github.com/pingcap/tidb/util/timeutil"
uatomic "go.uber.org/atomic"
"go.uber.org/zap"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -144,6 +145,7 @@ type Server struct {
statusServer *http.Server
grpcServer *grpc.Server
inShutdownMode bool
health *uatomic.Bool

sessionMapMutex sync.Mutex
internalSessions map[interface{}]struct{}
Expand Down Expand Up @@ -212,6 +214,8 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
globalConnID: util.NewGlobalConnID(0, true),
internalSessions: make(map[interface{}]struct{}, 100),
printMDLLogTime: time.Now(),
inShutdownMode: uatomic.NewBool(false),
health: uatomic.NewBool(false),
}
s.capability = defaultCapability
setTxnScope()
Expand Down Expand Up @@ -428,6 +432,7 @@ func (s *Server) Run(dom *domain.Domain) error {
if RunInGoTest && !isClosed(RunInGoTestChan) {
close(RunInGoTestChan)
}
s.health.Store(false)
err = <-errChan
if err != nil {
return err
Expand Down

0 comments on commit f908c0f

Please sign in to comment.