Skip to content

Commit

Permalink
add scheduling microservice framework
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Jun 19, 2023
1 parent 80edddb commit 08e8978
Show file tree
Hide file tree
Showing 10 changed files with 1,280 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/mcs/resourcemanager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func CreateServerWrapper(cmd *cobra.Command, args []string) {
// Flushing any buffered log entries
defer log.Sync()

versioninfo.Log("resource manager")
versioninfo.Log("Resource Manager")
log.Info("resource manager config", zap.Reflect("config", cfg))

grpcprometheus.EnableHandlingTimeHistogram()
Expand Down
92 changes: 92 additions & 0 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2023 TiKV Project Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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"
scheserver "github.com/tikv/pd/pkg/mcs/scheduling/server"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
"github.com/unrolled/render"
)

// APIPathPrefix is the prefix of the API path.
const APIPathPrefix = "/scheduling/api/v1/"

var (
once sync.Once
apiServiceGroup = apiutil.APIServiceGroup{
Name: "scheduling",
Version: "v1",
IsCore: false,
PathPrefix: APIPathPrefix,
}
)

func init() {
scheserver.SetUpRestHandler = func(srv *scheserver.Service) (http.Handler, apiutil.APIServiceGroup) {
s := NewService(srv)
return s.apiHandlerEngine, apiServiceGroup
}
}

// Service is the tso service.
type Service struct {
apiHandlerEngine *gin.Engine
root *gin.RouterGroup

srv *scheserver.Service
rd *render.Render
}

func createIndentRender() *render.Render {
return render.New(render.Options{
IndentJSON: true,
})
}

// 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())
apiHandlerEngine.Use(gzip.Gzip(gzip.DefaultCompression))
apiHandlerEngine.Use(func(c *gin.Context) {
c.Set(multiservicesapi.ServiceContextKey, srv)
c.Next()
})
apiHandlerEngine.Use(multiservicesapi.ServiceRedirector())
apiHandlerEngine.GET("metrics", utils.PromHandler())
root := apiHandlerEngine.Group(APIPathPrefix)
s := &Service{
srv: srv,
apiHandlerEngine: apiHandlerEngine,
root: root,
rd: createIndentRender(),
}
return s
}
Loading

0 comments on commit 08e8978

Please sign in to comment.