Skip to content
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

TSO Proxy: improve tso proxy reliability #6585

Merged
merged 16 commits into from
Jun 12, 2023
Merged
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
Prev Previous commit
Next Next commit
Add deadline for API leader forwarding request to TSO service.
Signed-off-by: Bin Shi <binshi.bing@gmail.com>
binshi-bing committed Jun 10, 2023

Verified

This commit was signed with the committer’s verified signature.
chenrui333 Rui Chen
commit 623ec6d7747b1f2b8757eaf91e57a6183c328424
87 changes: 66 additions & 21 deletions server/grpc_service.go
Original file line number Diff line number Diff line change
@@ -468,25 +468,8 @@ func (s *GrpcServer) forwardTSO(stream pdpb.PD_TsoServer) error {
lastForwardedHost = forwardedHost
}

tsoReq := &tsopb.TsoRequest{
Header: &tsopb.RequestHeader{
ClusterId: request.GetHeader().GetClusterId(),
SenderId: request.GetHeader().GetSenderId(),
KeyspaceId: utils.DefaultKeyspaceID,
KeyspaceGroupId: utils.DefaultKeyspaceGroupID,
},
Count: request.GetCount(),
DcLocation: request.GetDcLocation(),
}
if err := forwardStream.Send(tsoReq); err != nil {
return errors.WithStack(err)
}

tsopbResp, err := forwardStream.Recv()
tsopbResp, err := s.forwardTSORequestWithDeadLine(stream.Context(), request, forwardStream)
if err != nil {
if strings.Contains(err.Error(), errs.NotLeaderErr) {
s.tsoPrimaryWatcher.ForceLoad()
}
return errors.WithStack(err)
}

@@ -522,14 +505,76 @@ func (s *GrpcServer) forwardTSO(stream pdpb.PD_TsoServer) error {
}
}

func (s *GrpcServer) forwardTSORequestWithDeadLine(
binshi-bing marked this conversation as resolved.
Show resolved Hide resolved
ctx context.Context, request *pdpb.TsoRequest, forwardStream tsopb.TSO_TsoClient,
) (*tsopb.TsoResponse, error) {
// Create a context with deadline for forwarding TSO request to TSO service.
ctxTimeout, cancel := context.WithTimeout(ctx, tsoutil.DefaultTSOProxyTimeout)
defer cancel()

// used to receive the result from doSomething function
tsoRespCh := make(chan *tsopbTSOResponse)
binshi-bing marked this conversation as resolved.
Show resolved Hide resolved
go s.forwardTSORequestAsync(ctxTimeout, request, forwardStream, tsoRespCh)
select {
case <-ctxTimeout.Done():
return nil, ErrForwardTSOTimeout
case tsoResp := <-tsoRespCh:
return tsoResp.response, tsoResp.err
}
}

func (s *GrpcServer) forwardTSORequestAsync(
ctxTimeout context.Context,
request *pdpb.TsoRequest,
forwardStream tsopb.TSO_TsoClient,
tsoRespCh chan<- *tsopbTSOResponse,
) {
tsopbReq := &tsopb.TsoRequest{
Header: &tsopb.RequestHeader{
ClusterId: request.GetHeader().GetClusterId(),
SenderId: request.GetHeader().GetSenderId(),
KeyspaceId: utils.DefaultKeyspaceID,
KeyspaceGroupId: utils.DefaultKeyspaceGroupID,
},
Count: request.GetCount(),
DcLocation: request.GetDcLocation(),
}
if err := forwardStream.Send(tsopbReq); err != nil {
tsoRespCh <- &tsopbTSOResponse{err: err}
return
}

select {
case <-ctxTimeout.Done():
tsoRespCh <- &tsopbTSOResponse{err: ErrForwardTSOTimeout}
return
default:
}

response, err := forwardStream.Recv()
if err != nil {
if strings.Contains(err.Error(), errs.NotLeaderErr) {
s.tsoPrimaryWatcher.ForceLoad()
}
Comment on lines +570 to +572
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to retry when meet this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be retried anyway. Suppose this error happens, many forward streams will encounter this problem and report the error to the client. The client will retry to create tso stream again and send tso request, if tso server hasn't elected new primary, the NotLeaderErr will trigger force load again every 200 ms.

tsoRespCh <- &tsopbTSOResponse{err: err}
}

tsoRespCh <- &tsopbTSOResponse{response: response, err: nil}
}

type tsopbTSOResponse struct {
response *tsopb.TsoResponse
err error
}

// tsoServer wraps PD_TsoServer to ensure when any error
// occurs on Send() or Recv(), both endpoints will be closed.
type tsoServer struct {
stream pdpb.PD_TsoServer
closed int32
}

type tsoRequest struct {
type pdpbTSORequest struct {
request *pdpb.TsoRequest
err error
}
@@ -559,11 +604,11 @@ func (s *tsoServer) Recv() (*pdpb.TsoRequest, error) {
if atomic.LoadInt32(&s.closed) == 1 {
return nil, io.EOF
}
requestCh := make(chan *tsoRequest, 1)
requestCh := make(chan *pdpbTSORequest, 1)
go func() {
defer logutil.LogPanic()
request, err := s.stream.Recv()
requestCh <- &tsoRequest{request: request, err: err}
requestCh <- &pdpbTSORequest{request: request, err: err}
}()
select {
case req := <-requestCh: