Skip to content

Commit

Permalink
replace more
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Dec 2, 2024
1 parent fe75817 commit 071ef15
Show file tree
Hide file tree
Showing 24 changed files with 224 additions and 100 deletions.
20 changes: 10 additions & 10 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (
)

const (
apiMode = "api"
keyspaceMode = "api"
tsoMode = "tso"
serviceModeEnv = "PD_SERVICE_MODE"
)
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewServiceCommand() *cobra.Command {
}
cmd.AddCommand(NewTSOServiceCommand())
cmd.AddCommand(NewSchedulingServiceCommand())
cmd.AddCommand(NewAPIServiceCommand())
cmd.AddCommand(NewPDWithKeyspaceCommand())
return cmd
}

Expand Down Expand Up @@ -126,12 +126,12 @@ func NewSchedulingServiceCommand() *cobra.Command {
return cmd
}

// NewAPIServiceCommand returns the API service command.
func NewAPIServiceCommand() *cobra.Command {
// NewPDWithKeyspaceCommand returns the PD with keyspace command.
func NewPDWithKeyspaceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: apiMode,
Short: "Run the API service",
Run: createAPIServerWrapper,
Use: keyspaceMode,
Short: "Placement Driver server with keyspace",
Run: createServerWrapperWithKeyspace,
}
addFlags(cmd)
return cmd
Expand All @@ -158,14 +158,14 @@ func addFlags(cmd *cobra.Command) {
cmd.Flags().BoolP("force-new-cluster", "", false, "force to create a new one-member cluster")
}

func createAPIServerWrapper(cmd *cobra.Command, args []string) {
func createServerWrapperWithKeyspace(cmd *cobra.Command, args []string) {
start(cmd, args, cmd.CalledAs())
}

func createServerWrapper(cmd *cobra.Command, args []string) {
mode := os.Getenv(serviceModeEnv)
if len(mode) != 0 && strings.ToLower(mode) == apiMode {
start(cmd, args, apiMode)
if len(mode) != 0 && strings.ToLower(mode) == keyspaceMode {
start(cmd, args, keyspaceMode)
} else {
start(cmd, args)
}
Expand Down
26 changes: 13 additions & 13 deletions tests/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func NewTestServer(ctx context.Context, cfg *config.Config) (*TestServer, error)
return createTestServer(ctx, cfg, nil)
}

// NewTestAPIServer creates a new TestServer.
func NewTestAPIServer(ctx context.Context, cfg *config.Config) (*TestServer, error) {
// NewTestServerWithKeyspace creates a new TestServer.
func NewTestServerWithKeyspace(ctx context.Context, cfg *config.Config) (*TestServer, error) {
return createTestServer(ctx, cfg, []string{constant.APIServiceName})
}

Expand Down Expand Up @@ -441,12 +441,12 @@ func NewTestCluster(ctx context.Context, initialServerCount int, opts ...ConfigO
return createTestCluster(ctx, initialServerCount, false, opts...)
}

// NewTestAPICluster creates a new TestCluster with API service.
func NewTestAPICluster(ctx context.Context, initialServerCount int, opts ...ConfigOption) (*TestCluster, error) {
// NewTestClusterWithKeyspace creates a new TestCluster with keyspace.
func NewTestClusterWithKeyspace(ctx context.Context, initialServerCount int, opts ...ConfigOption) (*TestCluster, error) {
return createTestCluster(ctx, initialServerCount, true, opts...)
}

func createTestCluster(ctx context.Context, initialServerCount int, isAPIServiceMode bool, opts ...ConfigOption) (*TestCluster, error) {
func createTestCluster(ctx context.Context, initialServerCount int, isKeyspaceEnabled bool, opts ...ConfigOption) (*TestCluster, error) {
schedulers.Register()
config := newClusterConfig(initialServerCount)
servers := make(map[string]*TestServer)
Expand All @@ -456,8 +456,8 @@ func createTestCluster(ctx context.Context, initialServerCount int, isAPIService
return nil, err
}
var s *TestServer
if isAPIServiceMode {
s, err = NewTestAPIServer(ctx, serverConf)
if isKeyspaceEnabled {
s, err = NewTestServerWithKeyspace(ctx, serverConf)
} else {
s, err = NewTestServer(ctx, serverConf)
}
Expand All @@ -484,7 +484,7 @@ func RestartTestAPICluster(ctx context.Context, cluster *TestCluster) (*TestClus
}

func restartTestCluster(
ctx context.Context, cluster *TestCluster, isAPIServiceMode bool,
ctx context.Context, cluster *TestCluster, isKeyspaceEnabled bool,
) (newTestCluster *TestCluster, err error) {
schedulers.Register()
newTestCluster = &TestCluster{
Expand All @@ -511,8 +511,8 @@ func restartTestCluster(
newServer *TestServer
serverErr error
)
if isAPIServiceMode {
newServer, serverErr = NewTestAPIServer(ctx, serverCfg)
if isKeyspaceEnabled {
newServer, serverErr = NewTestServerWithKeyspace(ctx, serverCfg)
} else {
newServer, serverErr = NewTestServer(ctx, serverCfg)
}
Expand Down Expand Up @@ -746,13 +746,13 @@ func (c *TestCluster) Join(ctx context.Context, opts ...ConfigOption) (*TestServ
return s, nil
}

// JoinAPIServer is used to add a new TestAPIServer into the cluster.
func (c *TestCluster) JoinAPIServer(ctx context.Context, opts ...ConfigOption) (*TestServer, error) {
// JoinServerWithKeyspace is used to add a new TestServerWithKeyspace into the cluster.
func (c *TestCluster) JoinServerWithKeyspace(ctx context.Context, opts ...ConfigOption) (*TestServer, error) {
conf, err := c.config.join().Generate(opts...)
if err != nil {
return nil, err
}
s, err := NewTestAPIServer(ctx, conf)
s, err := NewTestServerWithKeyspace(ctx, conf)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func TestTSOFollowerProxyWithTSOService(t *testing.T) {
re.NoError(failpoint.Enable("github.com/tikv/pd/client/servicediscovery/fastUpdateServiceMode", `return(true)`))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestAPICluster(ctx, 1)
cluster, err := tests.NewTestClusterWithKeyspace(ctx, 1)
re.NoError(err)
defer cluster.Destroy()
err = cluster.RunInitialServers()
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/discovery/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite *serverRegisterTestSuite) SetupSuite() {
re := suite.Require()

suite.ctx, suite.cancel = context.WithCancel(context.Background())
suite.cluster, err = tests.NewTestAPICluster(suite.ctx, 1)
suite.cluster, err = tests.NewTestClusterWithKeyspace(suite.ctx, 1)
re.NoError(err)

err = suite.cluster.RunInitialServers()
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/keyspace/tso_keyspace_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (suite *keyspaceGroupTestSuite) SetupTest() {
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes", `return(true)`))
ctx, cancel := context.WithCancel(context.Background())
suite.ctx = ctx
cluster, err := tests.NewTestAPICluster(suite.ctx, 1)
cluster, err := tests.NewTestClusterWithKeyspace(suite.ctx, 1)
suite.cluster = cluster
re.NoError(err)
re.NoError(cluster.RunInitialServers())
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/members/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (suite *memberTestSuite) SetupTest() {
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes", `return(true)`))
ctx, cancel := context.WithCancel(context.Background())
suite.ctx = ctx
cluster, err := tests.NewTestAPICluster(suite.ctx, 1)
cluster, err := tests.NewTestClusterWithKeyspace(suite.ctx, 1)
suite.cluster = cluster
re.NoError(err)
re.NoError(cluster.RunInitialServers())
Expand Down
124 changes: 124 additions & 0 deletions tests/integrations/mcs/resourcemanager/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 resourcemanager_test

import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
"testing"

rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/client/grpcutil"
bs "github.com/tikv/pd/pkg/basicserver"
"github.com/tikv/pd/pkg/utils/tempurl"
"github.com/tikv/pd/pkg/versioninfo"
"github.com/tikv/pd/tests"
)

func TestResourceManagerServer(t *testing.T) {
re := require.New(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestClusterWithKeyspace(ctx, 1)
defer cluster.Destroy()
re.NoError(err)

err = cluster.RunInitialServers()
re.NoError(err)

leaderName := cluster.WaitLeader()
re.NotEmpty(leaderName)
leader := cluster.GetServer(leaderName)

s, cleanup := tests.StartSingleResourceManagerTestServer(ctx, re, leader.GetAddr(), tempurl.Alloc())
addr := s.GetAddr()
defer cleanup()
tests.WaitForPrimaryServing(re, map[string]bs.Server{addr: s})

// Test registered GRPC Service
cc, err := grpcutil.GetClientConn(ctx, addr, nil)
re.NoError(err)
defer cc.Close()

c := rmpb.NewResourceManagerClient(cc)
_, err = c.GetResourceGroup(context.Background(), &rmpb.GetResourceGroupRequest{
ResourceGroupName: "pingcap",
})
re.ErrorContains(err, "resource group not found")

// Test registered REST HTTP Handler
url := addr + "/resource-manager/api/v1/config"
{
resp, err := tests.TestDialClient.Get(url + "/groups")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
re.NoError(err)
re.JSONEq(`[{"name":"default","mode":1,"r_u_settings":{"r_u":{"settings":{"fill_rate":2147483647,"burst_limit":-1},"state":{"initialized":false}}},"priority":8}]`, string(respString))
}
{
group := &rmpb.ResourceGroup{
Name: "pingcap",
Mode: 1,
}
createJSON, err := json.Marshal(group)
re.NoError(err)
resp, err := tests.TestDialClient.Post(url+"/group", "application/json", strings.NewReader(string(createJSON)))
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
}
{
resp, err := tests.TestDialClient.Get(url + "/group/pingcap")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
re.NoError(err)
re.JSONEq("{\"name\":\"pingcap\",\"mode\":1,\"r_u_settings\":{\"r_u\":{\"state\":{\"initialized\":false}}},\"priority\":0}", string(respString))
}

// Test metrics handler
{
resp, err := tests.TestDialClient.Get(addr + "/metrics")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respBytes, err := io.ReadAll(resp.Body)
re.NoError(err)
re.Contains(string(respBytes), "pd_server_info")
}

// Test status handler
{
resp, err := tests.TestDialClient.Get(addr + "/status")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respBytes, err := io.ReadAll(resp.Body)
re.NoError(err)
var s versioninfo.Status
re.NoError(json.Unmarshal(respBytes, &s))
re.Equal(versioninfo.PDBuildTS, s.BuildTS)
re.Equal(versioninfo.PDGitHash, s.GitHash)
re.Equal(versioninfo.PDReleaseVersion, s.Version)
}
}
22 changes: 11 additions & 11 deletions tests/integrations/mcs/scheduling/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite *apiTestSuite) TearDownSuite() {
}

func (suite *apiTestSuite) TestGetCheckerByName() {
suite.env.RunTestInAPIMode(suite.checkGetCheckerByName)
suite.env.RunTestInKeyspaceMode(suite.checkGetCheckerByName)
}

func (suite *apiTestSuite) checkGetCheckerByName(cluster *tests.TestCluster) {
Expand Down Expand Up @@ -99,7 +99,7 @@ func (suite *apiTestSuite) checkGetCheckerByName(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestAPIForward() {
suite.env.RunTestInAPIMode(suite.checkAPIForward)
suite.env.RunTestInKeyspaceMode(suite.checkAPIForward)
}

func (suite *apiTestSuite) checkAPIForward(cluster *tests.TestCluster) {
Expand Down Expand Up @@ -375,7 +375,7 @@ func (suite *apiTestSuite) checkAPIForward(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestConfig() {
suite.env.RunTestInAPIMode(suite.checkConfig)
suite.env.RunTestInKeyspaceMode(suite.checkConfig)
}

func (suite *apiTestSuite) checkConfig(cluster *tests.TestCluster) {
Expand All @@ -398,7 +398,7 @@ func (suite *apiTestSuite) checkConfig(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestConfigForward() {
suite.env.RunTestInAPIMode(suite.checkConfigForward)
suite.env.RunTestInKeyspaceMode(suite.checkConfigForward)
}

func (suite *apiTestSuite) checkConfigForward(cluster *tests.TestCluster) {
Expand Down Expand Up @@ -449,7 +449,7 @@ func (suite *apiTestSuite) checkConfigForward(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestAdminRegionCache() {
suite.env.RunTestInAPIMode(suite.checkAdminRegionCache)
suite.env.RunTestInKeyspaceMode(suite.checkAdminRegionCache)
}

func (suite *apiTestSuite) checkAdminRegionCache(cluster *tests.TestCluster) {
Expand All @@ -476,7 +476,7 @@ func (suite *apiTestSuite) checkAdminRegionCache(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestAdminRegionCacheForward() {
suite.env.RunTestInAPIMode(suite.checkAdminRegionCacheForward)
suite.env.RunTestInKeyspaceMode(suite.checkAdminRegionCacheForward)
}

func (suite *apiTestSuite) checkAdminRegionCacheForward(cluster *tests.TestCluster) {
Expand Down Expand Up @@ -517,7 +517,7 @@ func (suite *apiTestSuite) checkFollowerForward(cluster *tests.TestCluster) {
leaderAddr := cluster.GetLeaderServer().GetAddr()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
follower, err := cluster.JoinAPIServer(ctx)
follower, err := cluster.JoinServerWithKeyspace(ctx)
re.NoError(err)
re.NoError(follower.Run())
re.NotEmpty(cluster.WaitLeader())
Expand Down Expand Up @@ -555,7 +555,7 @@ func (suite *apiTestSuite) checkFollowerForward(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestMetrics() {
suite.env.RunTestInAPIMode(suite.checkMetrics)
suite.env.RunTestInKeyspaceMode(suite.checkMetrics)
}

func (suite *apiTestSuite) checkMetrics(cluster *tests.TestCluster) {
Expand All @@ -574,7 +574,7 @@ func (suite *apiTestSuite) checkMetrics(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestStatus() {
suite.env.RunTestInAPIMode(suite.checkStatus)
suite.env.RunTestInKeyspaceMode(suite.checkStatus)
}

func (suite *apiTestSuite) checkStatus(cluster *tests.TestCluster) {
Expand All @@ -597,7 +597,7 @@ func (suite *apiTestSuite) checkStatus(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestStores() {
suite.env.RunTestInAPIMode(suite.checkStores)
suite.env.RunTestInKeyspaceMode(suite.checkStores)
}

func (suite *apiTestSuite) checkStores(cluster *tests.TestCluster) {
Expand Down Expand Up @@ -679,7 +679,7 @@ func (suite *apiTestSuite) checkStores(cluster *tests.TestCluster) {
}

func (suite *apiTestSuite) TestRegions() {
suite.env.RunTestInAPIMode(suite.checkRegions)
suite.env.RunTestInKeyspaceMode(suite.checkRegions)
}

func (suite *apiTestSuite) checkRegions(cluster *tests.TestCluster) {
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/scheduling/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (suite *configTestSuite) SetupSuite() {
schedulers.Register()
var err error
suite.ctx, suite.cancel = context.WithCancel(context.Background())
suite.cluster, err = tests.NewTestAPICluster(suite.ctx, 1)
suite.cluster, err = tests.NewTestClusterWithKeyspace(suite.ctx, 1)
re.NoError(err)
err = suite.cluster.RunInitialServers()
re.NoError(err)
Expand Down
Loading

0 comments on commit 071ef15

Please sign in to comment.