Skip to content

Commit

Permalink
client: retry load keyspace meta when creating a new client (tikv#6402)
Browse files Browse the repository at this point in the history
ref tikv#5895

Signed-off-by: Ryan Leung <rleungx@gmail.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
Signed-off-by: zeminzhou <zhouzemin@pingcap.com>
  • Loading branch information
2 people authored and zeminzhou committed May 10, 2023
1 parent 3bf76e0 commit de02808
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,35 @@ func NewClientWithKeyspaceName(ctx context.Context, keyspace string, svrAddrs []
c.cancel()
return nil, err
}
if err := c.initRetry(c.loadKeyspaceMeta, keyspace); err != nil {
return nil, err
}
return c, nil
}

func (c *client) initRetry(f func(s string) error, str string) error {
var err error
for i := 0; i < c.option.maxRetryTimes; i++ {
if err = f(str); err == nil || strings.Contains(err.Error(), "ENTRY_NOT_FOUND") {
return nil
}
select {
case <-c.ctx.Done():
return err
case <-time.After(time.Second):
}
}
return errors.WithStack(err)
}

func (c *client) loadKeyspaceMeta(keyspace string) error {
keyspaceMeta, err := c.LoadKeyspace(context.TODO(), keyspace)
// Here we ignore ENTRY_NOT_FOUND error and it will set the keyspaceID to 0.
if err != nil && !strings.Contains(err.Error(), "ENTRY_NOT_FOUND") {
return nil, err
return err
}
c.keyspaceID = keyspaceMeta.GetId()
return c, nil
return nil
}

func (c *client) setup() error {
Expand Down

0 comments on commit de02808

Please sign in to comment.