-
Notifications
You must be signed in to change notification settings - Fork 777
server: validate store addresses on PutStore #11039
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/url" | ||
| "path" | ||
| "runtime" | ||
| "runtime/trace" | ||
|
|
@@ -804,6 +806,47 @@ func checkStore(rc *cluster.RaftCluster, storeID uint64) *pdpb.Error { | |
| return nil | ||
| } | ||
|
|
||
| func dialAddress(ctx context.Context, address string) error { | ||
| if strings.HasPrefix(address, "mock://") { | ||
| return nil | ||
| } | ||
|
|
||
| hostPort := address | ||
| if u, err := url.Parse(address); err == nil && u.Host != "" { | ||
| hostPort = u.Host | ||
| } | ||
| if _, _, err := net.SplitHostPort(hostPort); err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| dialCtx, cancel := context.WithTimeout(ctx, defaultGRPCDialTimeout) | ||
| defer cancel() | ||
| conn, err := (&net.Dialer{}).DialContext(dialCtx, "tcp", hostPort) | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
| return conn.Close() | ||
| } | ||
|
|
||
| func validateStoreAddress(ctx context.Context, store *metapb.Store) error { | ||
| addresses := make(map[string]struct{}, 2) | ||
| if address := store.GetAddress(); address != "" { | ||
| addresses[address] = struct{}{} | ||
| } | ||
| if address := store.GetStatusAddress(); address != "" { | ||
| addresses[address] = struct{}{} | ||
| } | ||
| if address := store.GetPeerAddress(); address != "" { | ||
| addresses[address] = struct{}{} | ||
| } | ||
| for address := range addresses { | ||
| if err := dialAddress(ctx, address); err != nil { | ||
| return err | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // PutStore implements gRPC PDServer. | ||
| func (s *GrpcServer) PutStore(ctx context.Context, request *pdpb.PutStoreRequest) (*pdpb.PutStoreResponse, error) { | ||
| done, err := s.rateLimitCheck() | ||
|
|
@@ -849,6 +892,15 @@ func (s *GrpcServer) PutStore(ctx context.Context, request *pdpb.PutStoreRequest | |
| }, nil | ||
| } | ||
|
|
||
| // TiKV puts the store before listening on its store address, so PD can only | ||
| // validate the configured address by checking whether it is dialable. | ||
| if err := validateStoreAddress(ctx, store); err != nil { | ||
|
Member
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. TiKV calls PutStore before its gRPC server is built and bound. This dial therefore returns connection refused, PD responds with INVALID_VALUE, and TiKV aborts before it can start listening. Please avoid requiring address reachability during PutStore, or coordinate a two-phase startup protocol. |
||
| return &pdpb.PutStoreResponse{ | ||
| Header: grpcutil.WrapErrorToHeader(pdpb.ErrorType_INVALID_VALUE, | ||
| fmt.Sprintf("invalid store address %s: %s", store.GetAddress(), err)), | ||
| }, nil | ||
| } | ||
|
|
||
| if err := rc.PutMetaStore(store); err != nil { | ||
| return &pdpb.PutStoreResponse{ | ||
| Header: grpcutil.WrapErrorToHeader(pdpb.ErrorType_UNKNOWN, err.Error()), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.