Skip to content

Commit

Permalink
cdc: fix server etcd may nil by create a new local client. (#2161) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jun 25, 2021
1 parent 0fc7522 commit b6451b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cdc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,32 @@ func (s *Server) setUpDataDir(ctx context.Context) error {
return nil
}

// s.etcdClient maybe nil if NewReplicaImpl is not set to true
// todo: remove this after NewReplicaImpl set to true in a specific branch, and use server.etcdClient instead.
cli := s.etcdClient
if cli == nil {
client, err := clientv3.New(clientv3.Config{
Endpoints: s.pdEndpoints,
Context: ctx,
DialTimeout: 5 * time.Second,
})
if err != nil {
return err
}
etcdClient := kv.NewCDCEtcdClient(ctx, client)
cli = &etcdClient
defer cli.Close()
}

// data-dir will be decide by exist changefeed for backward compatibility
allStatus, err := s.owner.etcdClient.GetAllChangeFeedStatus(ctx)
allStatus, err := cli.GetAllChangeFeedStatus(ctx)
if err != nil {
return errors.Trace(err)
}

candidates := make([]string, 0, len(allStatus))
for id := range allStatus {
info, err := s.owner.etcdClient.GetChangeFeedInfo(ctx, id)
info, err := cli.GetChangeFeedInfo(ctx, id)
if err != nil {
return errors.Trace(err)
}
Expand Down
7 changes: 7 additions & 0 deletions cdc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,12 @@ func (s *serverSuite) TestInitDataDir(c *check.C) {
c.Assert(conf.DataDir, check.Not(check.Equals), "")
c.Assert(conf.Sorter.SortDir, check.Equals, filepath.Join(conf.DataDir, "/tmp/sorter"))
config.StoreGlobalServerConfig(conf)

server.etcdClient = nil
conf.DataDir = ""
err = server.initDataDir(ctx)
c.Assert(err, check.IsNil)
c.Assert(conf.DataDir, check.Not(check.Equals), "")

cancel()
}

0 comments on commit b6451b6

Please sign in to comment.