Skip to content

Commit

Permalink
Merge branch 'master' into hot
Browse files Browse the repository at this point in the history
  • Loading branch information
rleungx authored Feb 4, 2024
2 parents 91b6383 + 6d25ef1 commit 2dd5abd
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 104 deletions.
8 changes: 0 additions & 8 deletions pkg/mcs/resourcemanager/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (
"net/http"
"reflect"
"strings"
"sync"

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

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "resource-manager",
Version: "v1",
Expand All @@ -67,11 +64,6 @@ 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: 0 additions & 8 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"net/url"
"strconv"
"strings"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
Expand All @@ -53,7 +51,6 @@ const APIPathPrefix = "/scheduling/api/v1"
const handlerKey = "handler"

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

// NewService returns a new Service.
func NewService(srv *scheserver.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: 0 additions & 8 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"fmt"
"net/http"
"strconv"
"sync"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
Expand All @@ -44,7 +42,6 @@ const (
)

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "tso",
Version: "v1",
Expand Down Expand Up @@ -77,11 +74,6 @@ 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
2 changes: 1 addition & 1 deletion pkg/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ func (m *EmbeddedEtcdMember) CampaignLeader(ctx context.Context, leaseTimeout in
})

if m.leadership.GetCampaignTimesNum() >= campaignLeaderFrequencyTimes {
m.leadership.ResetCampaignTimes()
if err := m.ResignEtcdLeader(ctx, m.Name(), ""); err != nil {
return err
}
m.leadership.ResetCampaignTimes()
return errs.ErrLeaderFrequentlyChange.FastGenByArgs(m.Name(), m.GetLeaderPath())
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/utils/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/pingcap/errcode"
"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -67,6 +70,17 @@ const (
chunkSize = 4096
)

var once sync.Once

func init() {
once.Do(func() {
// See https://github.com/pingcap/tidb-dashboard/blob/f8ecb64e3d63f4ed91c3dca7a04362418ade01d8/pkg/apiserver/apiserver.go#L84
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
}

// DeferClose captures the error returned from closing (if an error occurs).
// This is designed to be used in a defer statement.
func DeferClose(c io.Closer, err *error) {
Expand Down
10 changes: 0 additions & 10 deletions server/apiv2/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ package apiv2
import (
"context"
"net/http"
"sync"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/apiv2/handlers"
"github.com/tikv/pd/server/apiv2/middlewares"
)

var once sync.Once

var group = apiutil.APIServiceGroup{
Name: "core",
IsCore: true,
Expand All @@ -49,12 +45,6 @@ const apiV2Prefix = "/pd/api/v2/"
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /pd/api/v2
func NewV2Handler(_ context.Context, svr *server.Server) (http.Handler, apiutil.APIServiceGroup, error) {
once.Do(func() {
// See https://github.com/pingcap/tidb-dashboard/blob/f8ecb64e3d63f4ed91c3dca7a04362418ade01d8/pkg/apiserver/apiserver.go#L84
// These global modification will be effective only for the first invoke.
_ = godotenv.Load()
gin.SetMode(gin.ReleaseMode)
})
router := gin.New()
router.Use(func(c *gin.Context) {
c.Set(middlewares.ServerContextKey, svr)
Expand Down
13 changes: 10 additions & 3 deletions tools/pd-ctl/pdctl/command/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ func pauseSchedulerCommandFunc(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
path := schedulersPrefix + "/" + args[0]
path := schedulersPrefix + "/" + getEscapedSchedulerName(args[0])
input := map[string]any{"delay": delay}
postJSON(cmd, path, input)
}

// Since certain scheduler's name is defined by caller such as scatter-range,
// it's possible the name contains special characters, like "#", "&" and so on.
// So we need to escape the scheduler name here before attaching it to the URL.
func getEscapedSchedulerName(schedulerName string) string {
return url.PathEscape(schedulerName)
}

// NewResumeSchedulerCommand returns a command to resume a scheduler.
func NewResumeSchedulerCommand() *cobra.Command {
c := &cobra.Command{
Expand All @@ -92,7 +99,7 @@ func resumeSchedulerCommandFunc(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
path := schedulersPrefix + "/" + args[0]
path := schedulersPrefix + "/" + getEscapedSchedulerName(args[0])
input := map[string]any{"delay": 0}
postJSON(cmd, path, input)
}
Expand Down Expand Up @@ -475,7 +482,7 @@ func removeSchedulerCommandFunc(cmd *cobra.Command, args []string) {
case strings.HasPrefix(args[0], grantLeaderSchedulerName) && args[0] != grantLeaderSchedulerName:
redirectRemoveSchedulerToDeleteConfig(cmd, grantLeaderSchedulerName, args)
default:
path := schedulersPrefix + "/" + args[0]
path := schedulersPrefix + "/" + getEscapedSchedulerName(args[0])
_, err := doRequest(cmd, path, http.MethodDelete, http.Header{})
if err != nil {
cmd.Println(err)
Expand Down
Loading

0 comments on commit 2dd5abd

Please sign in to comment.