Skip to content

Commit

Permalink
add NewClientWithKeyspaceName for client
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Apr 25, 2023
1 parent 4f87e9d commit d7a8ffd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
45 changes: 43 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@ func NewClientWithContext(ctx context.Context, svrAddrs []string, security Secur

// NewClientWithKeyspace creates a client with context and the specified keyspace id.
func NewClientWithKeyspace(ctx context.Context, keyspaceID uint32, svrAddrs []string, security SecurityOption, opts ...ClientOption) (Client, error) {
log.Info("[pd] create pd client with endpoints and keyspace", zap.Strings("pd-address", svrAddrs), zap.Uint32("keyspace-id", keyspaceID))

tlsCfg := &tlsutil.TLSConfig{
CAPath: security.CAPath,
CertPath: security.CertPath,
Expand Down Expand Up @@ -345,6 +343,49 @@ func NewClientWithKeyspace(ctx context.Context, keyspaceID uint32, svrAddrs []st
return c, nil
}

// NewClientWithKeyspaceName creates a client with context and the specified keyspace name.
func NewClientWithKeyspaceName(ctx context.Context, keyspace string, svrAddrs []string, security SecurityOption, opts ...ClientOption) (Client, error) {
log.Info("[pd] create pd client with endpoints and keyspace", zap.Strings("pd-address", svrAddrs), zap.String("keyspace", keyspace))

tlsCfg := &tlsutil.TLSConfig{
CAPath: security.CAPath,
CertPath: security.CertPath,
KeyPath: security.KeyPath,

SSLCABytes: security.SSLCABytes,
SSLCertBytes: security.SSLCertBytes,
SSLKEYBytes: security.SSLKEYBytes,
}

clientCtx, clientCancel := context.WithCancel(ctx)
c := &client{
updateTokenConnectionCh: make(chan struct{}, 1),
ctx: clientCtx,
cancel: clientCancel,
svrUrls: addrsToUrls(svrAddrs),
tlsCfg: tlsCfg,
option: newOption(),
}

// Inject the client options.
for _, opt := range opts {
opt(c)
}

c.pdSvcDiscovery = newPDServiceDiscovery(clientCtx, clientCancel, &c.wg, c.setServiceMode, c.svrUrls, c.tlsCfg, c.option)
if err := c.setup(); err != nil {
c.cancel()
return nil, err
}
keyspaceMeta, err := c.LoadKeyspace(context.TODO(), keyspace)
// Here we ignore ENTRY_NOT_FOUND error and it will set the keyspaceID to 0.
if err != nil && !strings.Contains(err.Error(), "ENTRY_NOT_FOUND") {
return nil, err
}
c.keyspaceID = keyspaceMeta.GetId()
return c, nil
}

func (c *client) setup() error {
// Init the client base.
if err := c.pdSvcDiscovery.Init(); err != nil {
Expand Down
10 changes: 1 addition & 9 deletions tests/integrations/mcs/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
bs "github.com/tikv/pd/pkg/basicserver"
rm "github.com/tikv/pd/pkg/mcs/resource_manager/server"
tso "github.com/tikv/pd/pkg/mcs/tso/server"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/testutil"
)
Expand All @@ -51,14 +50,7 @@ func InitLogger(cfg *tso.Config) (err error) {

// SetupClientWithKeyspace creates a TSO client for test.
func SetupClientWithKeyspace(ctx context.Context, re *require.Assertions, endpoints []string, opts ...pd.ClientOption) pd.Client {
cli, err := pd.NewClientWithKeyspace(ctx, utils.DefaultKeyspaceID, endpoints, pd.SecurityOption{}, opts...)
re.NoError(err)
return cli
}

// SetupClient creates a TSO client for test.
func SetupClient(ctx context.Context, re *require.Assertions, endpoints []string, opts ...pd.ClientOption) pd.Client {
cli, err := pd.NewClientWithContext(ctx, endpoints, pd.SecurityOption{}, opts...)
cli, err := pd.NewClientWithKeyspaceName(ctx, "", endpoints, pd.SecurityOption{}, opts...)
re.NoError(err)
return cli
}
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/mcs/tso/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func checkTSOPath(re *require.Assertions, isAPIServiceMode bool) {
re.NoError(err)
leaderName := cluster.WaitLeader()
pdLeader := cluster.GetServer(leaderName)
re.NoError(pdLeader.BootstrapCluster())
backendEndpoints := pdLeader.GetAddr()
client := pdLeader.GetEtcdClient()
if isAPIServiceMode {
Expand Down

0 comments on commit d7a8ffd

Please sign in to comment.