Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"
"io"
"net"
"net/url"
"path"
"runtime"
"runtime/trace"
Expand Down Expand Up @@ -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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
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
}
Comment thread
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()
Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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()),
Expand Down
53 changes: 53 additions & 0 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math"
"net"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -2219,3 +2220,55 @@ func TestPutStoreInvalidEngineLabel(t *testing.T) {
})
}
}

func TestPutStoreValidateStoreAddress(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tc, err := tests.NewTestCluster(ctx, 1)
re.NoError(err)
defer tc.Destroy()

err = tc.RunInitialServers()
re.NoError(err)
tc.WaitLeader()
leaderServer := tc.GetLeaderServer()
grpcPDClient, conn := testutil.MustNewGrpcClient(re, leaderServer.GetAddr())
defer conn.Close()
clusterID := leaderServer.GetClusterID()

bootstrapCluster(re, clusterID, grpcPDClient)

listener, err := net.Listen("tcp", "127.0.0.1:0")
re.NoError(err)
defer re.NoError(listener.Close())

resp, err := putStore(grpcPDClient, clusterID, &metapb.Store{
Id: 101,
Address: listener.Addr().String(),
Version: "2.0.1",
})
re.NoError(err)
re.Nil(resp.GetHeader().GetError())

unreachableListener, err := net.Listen("tcp", "127.0.0.1:0")
re.NoError(err)
unreachableAddr := unreachableListener.Addr().String()
re.NoError(unreachableListener.Close())

resp, err = putStore(grpcPDClient, clusterID, &metapb.Store{
Id: 102,
Address: unreachableAddr,
Version: "2.0.1",
})
re.NoError(err)
re.Equal(pdpb.ErrorType_INVALID_VALUE, resp.GetHeader().GetError().GetType())

resp, err = putStore(grpcPDClient, clusterID, &metapb.Store{
Id: 103,
Address: "mock://tikv-103:103",
Version: "2.0.1",
})
re.NoError(err)
re.Nil(resp.GetHeader().GetError())
}
Loading