Skip to content

Commit

Permalink
mcs: use release mode for gin (#6177)
Browse files Browse the repository at this point in the history
ref #5836

Signed-off-by: Ryan Leung <rleungx@gmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
rleungx and ti-chi-bot committed Mar 21, 2023
1 parent 5c1778e commit 832047e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions pkg/mcs/resource_manager/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package apis
import (
"errors"
"net/http"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
rmserver "github.com/tikv/pd/pkg/mcs/resource_manager/server"
"github.com/tikv/pd/pkg/utils/apiutil"
Expand All @@ -31,6 +33,7 @@ import (
const APIPathPrefix = "/resource-manager/api/v1/"

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "resource-manager",
Version: "v1",
Expand All @@ -56,6 +59,11 @@ type Service struct {

// NewService returns a new Service.
func NewService(srv *rmserver.Service) *Service {
once.Do(func() {
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
apiHandlerEngine := gin.New()
apiHandlerEngine.Use(gin.Recovery())
apiHandlerEngine.Use(cors.Default())
Expand Down
8 changes: 8 additions & 0 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package apis

import (
"net/http"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
tsoserver "github.com/tikv/pd/pkg/mcs/tso/server"
"github.com/tikv/pd/pkg/tso"
"github.com/tikv/pd/pkg/utils/apiutil"
Expand All @@ -31,6 +33,7 @@ import (
const APIPathPrefix = "/tso/api/v1"

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "tso",
Version: "v1",
Expand Down Expand Up @@ -63,6 +66,11 @@ func createIndentRender() *render.Render {

// NewService returns a new Service.
func NewService(srv *tsoserver.Service) *Service {
once.Do(func() {
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
apiHandlerEngine := gin.New()
apiHandlerEngine.Use(gin.Recovery())
apiHandlerEngine.Use(cors.Default())
Expand Down
11 changes: 6 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func (s *Server) startServer(ctx context.Context) error {
}
}
}

s.encryptionKeyManager, err = encryption.NewManager(s.client, &s.cfg.Security.Encryption)
if err != nil {
return err
Expand Down Expand Up @@ -540,10 +541,9 @@ func (s *Server) startServerLoop(ctx context.Context) {
go s.etcdLeaderLoop()
go s.serverMetricsLoop()
go s.encryptionKeyManagerLoop()
if s.IsAPIServiceMode() { // disable tso service and resource manager service in api server
s.serverLoopWg.Add(2)
if s.IsAPIServiceMode() { // disable tso service in api server
s.serverLoopWg.Add(1)
go s.watchServicePrimaryAddrLoop(mcs.TSOServiceName)
go s.watchServicePrimaryAddrLoop(mcs.ResourceManagerServiceName)
} else { // enable tso service
s.serverLoopWg.Add(1)
go s.tsoAllocatorLoop()
Expand Down Expand Up @@ -1458,6 +1458,7 @@ func (s *Server) campaignLeader() {
// maintain the PD leadership, after this, TSO can be service.
s.member.KeepLeader(ctx)
log.Info(fmt.Sprintf("campaign %s leader ok", s.mode), zap.String("campaign-leader-name", s.Name()))

if !s.IsAPIServiceMode() {
allocator, err := s.tsoAllocatorManager.GetAllocator(tso.GlobalDCLocation)
if err != nil {
Expand Down Expand Up @@ -1715,7 +1716,7 @@ func (s *Server) watchServicePrimaryAddrLoop(serviceName string) {
log.Info("start to watch", zap.String("service-key", serviceKey))

primary := &tsopb.Participant{}
ok, _, err := etcdutil.GetProtoMsgWithModRev(s.client, serviceKey, primary)
ok, rev, err := etcdutil.GetProtoMsgWithModRev(s.client, serviceKey, primary)
if err != nil {
log.Error("get service primary addr failed", zap.String("service-key", serviceKey), zap.Error(err))
}
Expand All @@ -1727,7 +1728,7 @@ func (s *Server) watchServicePrimaryAddrLoop(serviceName string) {
log.Warn("service primary addr doesn't exist", zap.String("service-key", serviceKey))
}

watchChan := s.client.Watch(ctx, serviceKey, clientv3.WithPrefix())
watchChan := s.client.Watch(ctx, serviceKey, clientv3.WithPrefix(), clientv3.WithRev(rev))
for {
select {
case <-ctx.Done():
Expand Down
10 changes: 6 additions & 4 deletions tests/mcs/discovery/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ func (suite *serverRegisterTestSuite) TestServerRegister() {
for i := 0; i < 3; i++ {
suite.checkServerRegister(utils.TSOServiceName)
}
for i := 0; i < 3; i++ {
suite.checkServerRegister(utils.ResourceManagerServiceName)
}
// TODO: uncomment after resource-manager is ready
// for i := 0; i < 3; i++ {
// suite.checkServerRegister(utils.ResourceManagerServiceName)
// }
}

func (suite *serverRegisterTestSuite) checkServerRegister(serviceName string) {
Expand Down Expand Up @@ -104,7 +105,8 @@ func (suite *serverRegisterTestSuite) checkServerRegister(serviceName string) {

func (suite *serverRegisterTestSuite) TestServerPrimaryChange() {
suite.checkServerPrimaryChange(utils.TSOServiceName, 3)
suite.checkServerPrimaryChange(utils.ResourceManagerServiceName, 3)
// TODO: uncomment after resource-manager is ready
// suite.checkServerPrimaryChange(utils.ResourceManagerServiceName, 3)
}

func (suite *serverRegisterTestSuite) checkServerPrimaryChange(serviceName string, serverNum int) {
Expand Down

0 comments on commit 832047e

Please sign in to comment.