Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyspace: add keyspace util func and ut #41353

Merged
merged 5 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion keyspace/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "keyspace",
srcs = ["keyspace.go"],
importpath = "github.com/pingcap/tidb/keyspace",
visibility = ["//visibility:public"],
deps = [
"//config",
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
"@com_github_tikv_client_go_v2//tikv",
],
)

go_test(
name = "keyspace_test",
srcs = ["keyspace_test.go"],
embed = [":keyspace"],
flaky = True,
deps = [
"//config",
"@com_github_stretchr_testify//suite",
],
)
12 changes: 12 additions & 0 deletions keyspace/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb/config"
"github.com/tikv/client-go/v2/tikv"
)

Expand All @@ -36,3 +37,14 @@ func MakeKeyspaceEtcdNamespace(c tikv.Codec) string {
}
return fmt.Sprintf(tidbKeyspaceEtcdPathPrefix+"%d", c.GetKeyspaceID())
}

// GetKeyspaceNameBySettings is used to get Keyspace name setting.
func GetKeyspaceNameBySettings() (keyspaceName string) {
keyspaceName = config.GetGlobalKeyspaceName()
return keyspaceName
}

// IsKeyspaceNameEmpty is used to determine whether keyspaceName is set.
func IsKeyspaceNameEmpty(keyspaceName string) bool {
return keyspaceName == ""
}
59 changes: 59 additions & 0 deletions keyspace/keyspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021 PingCAP, Inc.
//
// 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 keyspace

import (
"testing"

"github.com/pingcap/tidb/config"
"github.com/stretchr/testify/suite"
)

type keyspaceSuite struct {
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
suite.Suite
}

func TestSetKeyspaceName(t *testing.T) {
suite.Run(t, new(keyspaceSuite))
}

func (k *keyspaceSuite) TearDownTest() {
// Clear keyspace setting
conf := config.GetGlobalConfig()
conf.KeyspaceName = ""
config.StoreGlobalConfig(conf)
}

func (k *keyspaceSuite) TestSetKeyspaceNameInConf() {
keyspaceNameInCfg := "test_keyspace_cfg"

// Set KeyspaceName in conf
c1 := config.GetGlobalConfig()
c1.KeyspaceName = keyspaceNameInCfg

getKeyspaceName := GetKeyspaceNameBySettings()

// Check the keyspaceName which get from GetKeyspaceNameBySettings, equals keyspaceNameInCfg which is in conf.
// The cfg.keyspaceName get higher weights than KEYSPACE_NAME in system env.
k.Equal(keyspaceNameInCfg, getKeyspaceName)
k.Equal(false, IsKeyspaceNameEmpty(getKeyspaceName))
}

func (k *keyspaceSuite) TestNoKeyspaceNameSet() {
getKeyspaceName := GetKeyspaceNameBySettings()

k.Equal("", getKeyspaceName)
k.Equal(true, IsKeyspaceNameEmpty(getKeyspaceName))
}
1 change: 1 addition & 0 deletions tidb-server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//domain/infosync",
"//executor",
"//extension",
"//keyspace",
"//kv",
"//metrics",
"//parser/mysql",
Expand Down
3 changes: 2 additions & 1 deletion tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/extension"
"github.com/pingcap/tidb/keyspace"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/mysql"
Expand Down Expand Up @@ -232,7 +233,7 @@ func main() {
setupBinlogClient()
setupMetrics()

keyspaceName := config.GetGlobalKeyspaceName()
keyspaceName := keyspace.GetKeyspaceNameBySettings()

resourcemanager.InstanceResourceManager.Start()
storage, dom := createStoreAndDomain(keyspaceName)
Expand Down