Skip to content

Commit

Permalink
Merge branch 'master' into remove-cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot[bot] authored Aug 31, 2023
2 parents 5625b8e + 13dd27b commit de45bf5
Show file tree
Hide file tree
Showing 27 changed files with 481 additions and 525 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ linters-settings:
excludes:
- G402
- G404
- G601
21 changes: 17 additions & 4 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"os"
"os/signal"
"strings"
"syscall"

grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
Expand Down Expand Up @@ -48,6 +49,13 @@ import (
_ "github.com/tikv/pd/pkg/mcs/tso/server/install"
)

const (
apiMode = "api"
tsoMode = "tso"
rmMode = "resource-manager"
serviceModeEnv = "PD_SERVICE_MODE"
)

func main() {
rootCmd := &cobra.Command{
Use: "pd-server",
Expand Down Expand Up @@ -81,7 +89,7 @@ func NewServiceCommand() *cobra.Command {
// NewTSOServiceCommand returns the tso service command.
func NewTSOServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tso",
Use: tsoMode,
Short: "Run the TSO service",
Run: tso.CreateServerWrapper,
}
Expand Down Expand Up @@ -121,7 +129,7 @@ func NewSchedulingServiceCommand() *cobra.Command {
// NewResourceManagerServiceCommand returns the resource manager service command.
func NewResourceManagerServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "resource-manager",
Use: rmMode,
Short: "Run the resource manager service",
Run: resource_manager.CreateServerWrapper,
}
Expand All @@ -141,7 +149,7 @@ func NewResourceManagerServiceCommand() *cobra.Command {
// NewAPIServiceCommand returns the API service command.
func NewAPIServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "api",
Use: apiMode,
Short: "Run the API service",
Run: createAPIServerWrapper,
}
Expand Down Expand Up @@ -175,7 +183,12 @@ func createAPIServerWrapper(cmd *cobra.Command, args []string) {
}

func createServerWrapper(cmd *cobra.Command, args []string) {
start(cmd, args)
mode := os.Getenv(serviceModeEnv)
if len(mode) != 0 && strings.ToLower(mode) == apiMode {
start(cmd, args, apiMode)
} else {
start(cmd, args)
}
}

func start(cmd *cobra.Command, args []string, services ...string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/basicserver/basic_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// Server defines the common basic behaviors of a server
type Server interface {
// Name returns the unique Name for this server in the cluster.
// Name returns the unique name for this server in the cluster.
Name() string
// GetAddr returns the address of the server.
GetAddr() string
Expand Down
7 changes: 6 additions & 1 deletion pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
)

func TestExpireRegionCache(t *testing.T) {
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestExpireRegionCache(t *testing.T) {

re.Equal(sortIDs(cache.GetAllID()), []uint64{1, 2, 3})

// after 20ms, the key 1 will be expired
time.Sleep(20 * time.Millisecond)

value, ok = cache.Get(1)
Expand All @@ -92,7 +94,10 @@ func TestExpireRegionCache(t *testing.T) {
re.True(ok)
re.Equal(3.0, value)

re.Equal(2, cache.Len())
testutil.Eventually(re, func() bool {
// we can't ensure whether gc is executed, so we check the length of cache in a loop.
return cache.Len() == 2
}, testutil.WithWaitFor(50*time.Millisecond), testutil.WithTickInterval(time.Millisecond))
re.Equal(sortIDs(cache.GetAllID()), []uint64{2, 3})

cache.Remove(2)
Expand Down
8 changes: 2 additions & 6 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,15 +1660,11 @@ func DiffRegionKeyInfo(origin *RegionInfo, other *RegionInfo) string {
}

// String converts slice of bytes to string without copy.
func String(b []byte) (s string) {
func String(b []byte) string {
if len(b) == 0 {
return ""
}
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return
return unsafe.String(unsafe.SliceData(b), len(b))
}

// ToUpperASCIIInplace bytes.ToUpper but zero-cost
Expand Down
1 change: 1 addition & 0 deletions pkg/encryption/key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"go.etcd.io/etcd/embed"
)

// #nosec G101
const (
testMasterKey = "8fd7e3e917c170d92f3e51a981dd7bc8fba11f3df7d8df994842f6e86f69b530"
testMasterKey2 = "8fd7e3e917c170d92f3e51a981dd7bc8fba11f3df7d8df994842f6e86f69b531"
Expand Down
6 changes: 3 additions & 3 deletions pkg/encryption/master_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestPlaintextMasterKey(t *testing.T) {
func TestEncrypt(t *testing.T) {
t.Parallel()
re := require.New(t)
keyHex := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806"
keyHex := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806" // #nosec G101
key, err := hex.DecodeString(keyHex)
re.NoError(err)
masterKey := &MasterKey{key: key}
Expand All @@ -68,7 +68,7 @@ func TestEncrypt(t *testing.T) {
func TestDecrypt(t *testing.T) {
t.Parallel()
re := require.New(t)
keyHex := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806"
keyHex := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806" // #nosec G101
key, err := hex.DecodeString(keyHex)
re.NoError(err)
plaintext := "this-is-a-plaintext"
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestNewFileMasterKeyLengthMismatch(t *testing.T) {
func TestNewFileMasterKey(t *testing.T) {
t.Parallel()
re := require.New(t)
key := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806"
key := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806" // #nosec G101
dir := t.TempDir()
path := dir + "/key"
os.WriteFile(path, []byte(key), 0600)
Expand Down
Loading

0 comments on commit de45bf5

Please sign in to comment.