Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <ihusharp@gmail.com>
  • Loading branch information
HuSharp committed Aug 4, 2023
1 parent 4ce9584 commit 1fab56c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 56 deletions.
8 changes: 6 additions & 2 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2481,10 +2481,14 @@ func (c *RaftCluster) GetMinResolvedTS() uint64 {
func (c *RaftCluster) GetStoreMinResolvedTS(storeID uint64) uint64 {
c.RLock()
defer c.RUnlock()
if !c.isInitialized() || !core.IsAvailableForMinResolvedTS(c.GetStore(storeID)) {
if c.GetStore(storeID) == nil {
return math.MaxUint64
}
return c.GetStore(storeID).GetMinResolvedTS()
store := c.GetStore(storeID)
if !c.isInitialized() || !core.IsAvailableForMinResolvedTS(store) {
return math.MaxUint64
}
return store.GetMinResolvedTS()
}

// GetExternalTS returns the external timestamp.
Expand Down
1 change: 0 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,6 @@ func (s *Server) GetMinResolvedTS() uint64 {

// GetMinResolvedTSByStoreIDs returns the min resolved ts of the stores.
func (s *Server) GetMinResolvedTSByStoreIDs(ids []uint64) (uint64, []*pdpb.StoreMinResolvedTS) {
println("GetMinResolvedTSByStoreIDs", ids)
minResolvedTS := uint64(math.MaxUint64)
stores := make([]*pdpb.StoreMinResolvedTS, len(ids))
for i, storeID := range ids {
Expand Down
104 changes: 53 additions & 51 deletions tools/pd-api-bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"crypto/tls"
"flag"
"fmt"
"github.com/tikv/pd/client/tlsutil"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"log"
"net/http"
"os"
Expand All @@ -30,6 +27,12 @@ import (
"time"

pd "github.com/tikv/pd/client"
"github.com/tikv/pd/client/tlsutil"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)

var (
Expand All @@ -46,7 +49,6 @@ var (
)

func main() {
log.SetFlags(0)
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
sc := make(chan os.Signal, 1)
Expand All @@ -65,7 +67,6 @@ func main() {
go handleMinResolvedTSByHTTP(ctx)

<-ctx.Done()
log.Println("Exit")
switch sig {
case syscall.SIGTERM:
exit(0)
Expand All @@ -86,14 +87,14 @@ func handleMinResolvedTSByGRPC(ctx context.Context) {
for {
select {
case <-ticker.C:
pdCli := newClient()
_, _, err := pdCli.GetMinResolvedTimestamp(ctx, []uint64{1, 2, 3})
pdCli := newPDClient()
_, _, err := pdCli.GetMinResolvedTimestamp(ctx, []uint64{1})
if err != nil {
log.Println(err)
continue
}
case <-ctx.Done():
log.Println("Got signal to exit handleScanRegions")
log.Println("Got signal to exit handleMinResolvedTSByGRPC")
return
}
}
Expand All @@ -106,7 +107,13 @@ func handleMinResolvedTSByHTTP(ctx context.Context) {
log.Println("handleMinResolvedTSByHTTP qps = 0, exit")
return
}
url := "https://" + *pdAddr + "/pd/api/v1/min-resolved-ts"
// Judge whether with tls.
protocol := "http"
if len(*caPath) != 0 {
protocol = "https"
}
url := fmt.Sprintf("%s://%s/pd/api/v1/min-resolved-ts", protocol, *pdAddr)

for i := 0; i < *concurrency; i++ {
go func() {
// Mock client-go's request frequency.
Expand All @@ -117,7 +124,7 @@ func handleMinResolvedTSByHTTP(ctx context.Context) {
case <-ticker.C:
storeID := 1
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%d", url, storeID), nil)
httpsCli := httpClient()
httpsCli := newHttpClient()
res, err := httpsCli.Do(req)
if err != nil {
log.Println("error: ", err)
Expand All @@ -128,7 +135,7 @@ func handleMinResolvedTSByHTTP(ctx context.Context) {
res.Body.Close()
httpsCli.CloseIdleConnections()
case <-ctx.Done():
log.Println("Got signal to exit handleScanRegions")
log.Println("Got signal to exit handleMinResolvedTSByHTTP")
return
}
}
Expand All @@ -143,12 +150,12 @@ type minResolvedTS struct {
StoresMinResolvedTS map[uint64]uint64 `json:"stores_min_resolved_ts"`
}

// httpClient returns an HTTP(s) client.
func httpClient() *http.Client {
tlsConf := loadTLSConfig()
// newHttpClient returns an HTTP(s) client.
func newHttpClient() *http.Client {
// defaultTimeout for non-context requests.
const defaultTimeout = 30 * time.Second
cli := &http.Client{Timeout: defaultTimeout}
tlsConf := loadTLSConfig()
if tlsConf != nil {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = tlsConf
Expand All @@ -157,18 +164,45 @@ func httpClient() *http.Client {
return cli
}

// newPDClient returns a pd client.
func newPDClient() pd.Client {
const (
keepaliveTime = 10 * time.Second
keepaliveTimeout = 3 * time.Second
)

pdCli, err := pd.NewClient([]string{*pdAddr}, pd.SecurityOption{
CAPath: *caPath,
CertPath: *certPath,
KeyPath: *keyPath,
},
pd.WithGRPCDialOptions(
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: keepaliveTime,
Timeout: keepaliveTimeout,
}),
))
if err != nil {
log.Fatal("fail to create pd client", zap.Error(err))
}
return pdCli
}

func loadTLSConfig() *tls.Config {
if len(*caPath) == 0 {
return nil
}
caData, err := os.ReadFile(*caPath)
if err != nil {
println("fail to read ca file", err)
log.Println("fail to read ca file", zap.Error(err))
}
certData, err := os.ReadFile(*certPath)
if err != nil {
println("fail to read cert file", err)
log.Println("fail to read cert file", zap.Error(err))
}
keyData, err := os.ReadFile(*keyPath)
if err != nil {
println("fail to read key file", err)
log.Println("fail to read key file", zap.Error(err))
}

tlsConf, err := tlsutil.TLSConfig{
Expand All @@ -177,44 +211,12 @@ func loadTLSConfig() *tls.Config {
SSLKEYBytes: keyData,
}.ToTLSConfig()
if err != nil {
println("fail to load tls config", err)
log.Fatal("failed to load tlc config", zap.Error(err))
}
return tlsConf
}

const (
keepaliveTime = 10 * time.Second
keepaliveTimeout = 3 * time.Second
)

// newClient returns a pd client.
func newClient() pd.Client {
pdcli, err := pd.NewClient([]string{*pdAddr}, pd.SecurityOption{})
if err != nil {
println("fail to create pd client", err)
}
return pdcli
return tlsConf
}

//func newClientWithCLS() pd.Client {
// tlsConf := loadTLSConfig()
// pdCli, err := pd.NewClient([]string{}, pd.SecurityOption{
// SSLKEYBytes: tlsConf.,
// SSLCertBytes: certData,
// SSLCABytes: caData,
// },
// pd.WithGRPCDialOptions(
// grpc.WithKeepaliveParams(keepalive.ClientParameters{
// Time: keepaliveTime,
// Timeout: keepaliveTimeout,
// }),
// ))
// if err != nil {
// println("fail to create pd client", err)
// }
// return pdCli
//}

func exit(code int) {
os.Exit(code)
}
2 changes: 2 additions & 0 deletions tools/pd-tso-bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ require (
)

replace github.com/tikv/pd/client => ../../client

replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230803025623-dfd11f435356
4 changes: 2 additions & 2 deletions tools/pd-tso-bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zum
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/HuSharp/kvproto v0.0.0-20230803025623-dfd11f435356 h1:zHAKmiju08roolLYyj5GazNdpShn3q9OHRAvXEqsk2U=
github.com/HuSharp/kvproto v0.0.0-20230803025623-dfd11f435356/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
Expand Down Expand Up @@ -864,8 +866,6 @@ github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c h1:xpW9bvK+HuuTm
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00 h1:C3N3itkduZXDZFh4N3vQ5HEtld3S+Y+StULhWVvumU0=
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew=
github.com/pingcap/kvproto v0.0.0-20230727073445-53e1f8730c30 h1:EvqKcDT7ceGLW0mXqM8Cp5Z8DfgQRnwj2YTnlCLj2QI=
github.com/pingcap/kvproto v0.0.0-20230727073445-53e1f8730c30/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 h1:HR/ylkkLmGdSSDaD8IDP+SZrdhV1Kibl9KrHxJ9eciw=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down

0 comments on commit 1fab56c

Please sign in to comment.