Skip to content

Commit

Permalink
fix etcd not found
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jul 31, 2023
1 parent 16926ad commit 56f5844
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,9 @@ const globalConfigPath = "/global/config/"
// Since item value needs to support marshal of different struct types,
// it should be set to `Payload bytes` instead of `Value string`
func (s *GrpcServer) StoreGlobalConfig(_ context.Context, request *pdpb.StoreGlobalConfigRequest) (*pdpb.StoreGlobalConfigResponse, error) {
if s.client == nil {
return nil, errors.New("failed to store global config, etcd client not found")
}
configPath := request.GetConfigPath()
if configPath == "" {
configPath = globalConfigPath
Expand Down Expand Up @@ -2316,6 +2319,9 @@ func (s *GrpcServer) StoreGlobalConfig(_ context.Context, request *pdpb.StoreGlo
// - `Names` iteratively get value from `ConfigPath/Name` but not care about revision
// - `ConfigPath` if `Names` is nil can get all values and revision of current path
func (s *GrpcServer) LoadGlobalConfig(ctx context.Context, request *pdpb.LoadGlobalConfigRequest) (*pdpb.LoadGlobalConfigResponse, error) {
if s.client == nil {
return nil, errors.New("failed to load global config, etcd client not found")
}
configPath := request.GetConfigPath()
if configPath == "" {
configPath = globalConfigPath
Expand Down Expand Up @@ -2352,6 +2358,9 @@ func (s *GrpcServer) LoadGlobalConfig(ctx context.Context, request *pdpb.LoadGlo
// by Etcd.Watch() as long as the context has not been canceled or timed out.
// Watch on revision which greater than or equal to the required revision.
func (s *GrpcServer) WatchGlobalConfig(req *pdpb.WatchGlobalConfigRequest, server pdpb.PD_WatchGlobalConfigServer) error {
if s.client == nil {
return errors.Errorf("failed to watch global config, etcd client not found")
}
ctx, cancel := context.WithCancel(s.Context())
defer cancel()
configPath := req.GetConfigPath()
Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1940,3 +1940,8 @@ func (s *Server) GetTSOUpdatePhysicalInterval() time.Duration {
func (s *Server) GetMaxResetTSGap() time.Duration {
return s.persistOptions.GetMaxResetTSGap()
}

// SetClient [JUST FOR TEST] sets the etcd client.
func (s *Server) SetClient(client *clientv3.Client) {
s.client = client
}
30 changes: 30 additions & 0 deletions tests/integrations/client/global_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package client_test
import (
"path"
"strconv"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -53,6 +54,7 @@ type globalConfigTestSuite struct {
server *server.GrpcServer
client pd.Client
cleanup testutil.CleanupFunc
mu sync.Mutex
}

func TestGlobalConfigTestSuite(t *testing.T) {
Expand Down Expand Up @@ -322,3 +324,31 @@ func (suite *globalConfigTestSuite) TestClientWatchWithRevision() {
}
}
}

func (suite *globalConfigTestSuite) TestEtcdNotStart() {
cli := suite.server.GetClient()
defer func() {
suite.mu.Lock()
suite.server.SetClient(cli)
suite.mu.Unlock()
}()
suite.mu.Lock()
suite.server.SetClient(nil)
suite.mu.Unlock()
err := suite.server.WatchGlobalConfig(&pdpb.WatchGlobalConfigRequest{
ConfigPath: globalConfigPath,
Revision: 0,
}, nil)
suite.Error(err)

_, err = suite.server.StoreGlobalConfig(suite.server.Context(), &pdpb.StoreGlobalConfigRequest{
ConfigPath: globalConfigPath,
Changes: []*pdpb.GlobalConfigItem{{Kind: pdpb.EventType_PUT, Name: "0", Payload: []byte("0")}},
})
suite.Error(err)

_, err = suite.server.LoadGlobalConfig(suite.server.Context(), &pdpb.LoadGlobalConfigRequest{
Names: []string{"test_etcd"},
})
suite.Error(err)
}

0 comments on commit 56f5844

Please sign in to comment.