-
Notifications
You must be signed in to change notification settings - Fork 721
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
client: support use API context to create client #6482
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ const ( | |
// defaultKeySpaceGroupID is the default key space group id. | ||
// We also reserved 0 for the keyspace group for the same purpose. | ||
defaultKeySpaceGroupID = uint32(0) | ||
defaultKeyspaceName = "DEFAULT" | ||
) | ||
|
||
// Region contains information of a region's meta and its peers. | ||
|
@@ -335,6 +336,7 @@ func NewClientWithContext( | |
} | ||
|
||
// NewClientWithKeyspace creates a client with context and the specified keyspace id. | ||
// And now, it's only for test purpose. | ||
func NewClientWithKeyspace( | ||
ctx context.Context, keyspaceID uint32, svrAddrs []string, | ||
security SecurityOption, opts ...ClientOption, | ||
|
@@ -388,19 +390,87 @@ func createClientWithKeyspace( | |
return c, nil | ||
} | ||
|
||
// NewClientWithKeyspaceName creates a client with context and the specified keyspace name. | ||
func NewClientWithKeyspaceName( | ||
// APIVersion is the API version the server and the client is using. | ||
// See more details in https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#kvproto | ||
type APIVersion int | ||
|
||
// The API versions the client supports. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iosmanthus Is this suitable for the design of APIVersion? |
||
// As for V1TTL, client won't use it and we just remove it. | ||
const ( | ||
V1 APIVersion = iota | ||
_ | ||
V2 | ||
) | ||
|
||
// APIContext is the context for API version. | ||
type APIContext interface { | ||
GetAPIVersion() (apiVersion APIVersion) | ||
GetKeyspaceName() (keyspaceName string) | ||
} | ||
|
||
type apiContextV1 struct{} | ||
|
||
// NewAPIContextV1 creates a API context for V1. | ||
func NewAPIContextV1() APIContext { | ||
return &apiContextV1{} | ||
} | ||
|
||
// GetAPIVersion returns the API version. | ||
func (apiCtx *apiContextV1) GetAPIVersion() (version APIVersion) { | ||
return V1 | ||
} | ||
|
||
// GetKeyspaceName returns the keyspace name. | ||
func (apiCtx *apiContextV1) GetKeyspaceName() (keyspaceName string) { | ||
return "" | ||
} | ||
|
||
type apiContextV2 struct { | ||
keyspaceName string | ||
} | ||
|
||
// NewAPIContextV2 creates a API context with the specified keyspace name for V2. | ||
func NewAPIContextV2(keyspaceName string) APIContext { | ||
if len(keyspaceName) == 0 { | ||
keyspaceName = defaultKeyspaceName | ||
} | ||
return &apiContextV2{keyspaceName: keyspaceName} | ||
rleungx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// GetAPIVersion returns the API version. | ||
func (apiCtx *apiContextV2) GetAPIVersion() (version APIVersion) { | ||
return V2 | ||
} | ||
|
||
// GetKeyspaceName returns the keyspace name. | ||
func (apiCtx *apiContextV2) GetKeyspaceName() (keyspaceName string) { | ||
return apiCtx.keyspaceName | ||
} | ||
|
||
// NewClientWithAPIContext creates a client according to the API context. | ||
func NewClientWithAPIContext( | ||
ctx context.Context, apiCtx APIContext, svrAddrs []string, | ||
security SecurityOption, opts ...ClientOption, | ||
) (Client, error) { | ||
apiVersion, keyspaceName := apiCtx.GetAPIVersion(), apiCtx.GetKeyspaceName() | ||
switch apiVersion { | ||
case V1: | ||
return NewClientWithContext(ctx, svrAddrs, security, opts...) | ||
case V2: | ||
return newClientWithKeyspaceName(ctx, keyspaceName, svrAddrs, security, opts...) | ||
default: | ||
return nil, errors.Errorf("[pd] invalid API version %d", apiVersion) | ||
} | ||
} | ||
|
||
// newClientWithKeyspaceName creates a client with context and the specified keyspace name. | ||
func newClientWithKeyspaceName( | ||
ctx context.Context, keyspaceName 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-name", keyspaceName)) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #6482 (comment) may be better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's not exposed, IMO, we don't need to handle the API logic. |
||
// if keyspace name is empty, fall back to the legacy API | ||
if len(keyspaceName) == 0 { | ||
return NewClientWithContext(ctx, svrAddrs, security, opts...) | ||
} | ||
|
||
tlsCfg := &tlsutil.TLSConfig{ | ||
CAPath: security.CAPath, | ||
CertPath: security.CertPath, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are almost the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope. The red one has typo. Alternative "APIVersion is the API version that the server and the client are using."