-
Notifications
You must be signed in to change notification settings - Fork 721
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
server: refactor the independent service check #8738
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,66 +133,82 @@ | |
err = errs.ErrGenerateTimestamp.FastGenByArgs("tso count should be positive") | ||
return status.Error(codes.Unknown, err.Error()) | ||
} | ||
|
||
forwardedHost, ok := s.GetServicePrimaryAddr(stream.Context(), constant.TSOServiceName) | ||
if !ok || len(forwardedHost) == 0 { | ||
tsoStreamErr = errors.WithStack(ErrNotFoundTSOAddr) | ||
forwardCtx, cancelForward, forwardStream, lastForwardedHost, tsoStreamErr, err = s.handleTSOForwarding(forwardCtx, forwardStream, stream, server, request, tsDeadlineCh, lastForwardedHost, cancelForward) | ||
if tsoStreamErr != nil { | ||
return tsoStreamErr | ||
} | ||
if forwardStream == nil || lastForwardedHost != forwardedHost { | ||
if cancelForward != nil { | ||
cancelForward() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
clientConn, err := s.getDelegateClient(s.ctx, forwardedHost) | ||
if err != nil { | ||
tsoStreamErr = errors.WithStack(err) | ||
return tsoStreamErr | ||
} | ||
forwardStream, forwardCtx, cancelForward, err = createTSOForwardStream(stream.Context(), clientConn) | ||
if err != nil { | ||
tsoStreamErr = errors.WithStack(err) | ||
return tsoStreamErr | ||
} | ||
lastForwardedHost = forwardedHost | ||
func (s *GrpcServer) handleTSOForwarding(forwardCtx context.Context, forwardStream tsopb.TSO_TsoClient, stream pdpb.PD_TsoServer, server *tsoServer, | ||
request *pdpb.TsoRequest, tsDeadlineCh chan<- *tsoutil.TSDeadline, lastForwardedHost string, cancelForward context.CancelFunc) ( | ||
context.Context, | ||
context.CancelFunc, | ||
tsopb.TSO_TsoClient, | ||
string, | ||
error, // tso stream error | ||
error, // send error | ||
) { | ||
forwardedHost, ok := s.GetServicePrimaryAddr(stream.Context(), constant.TSOServiceName) | ||
if !ok || len(forwardedHost) == 0 { | ||
return forwardCtx, cancelForward, forwardStream, lastForwardedHost, errors.WithStack(ErrNotFoundTSOAddr), nil | ||
} | ||
if forwardStream == nil || lastForwardedHost != forwardedHost { | ||
if cancelForward != nil { | ||
cancelForward() | ||
} | ||
|
||
tsopbResp, err := s.forwardTSORequestWithDeadLine(forwardCtx, cancelForward, forwardStream, request, tsDeadlineCh) | ||
clientConn, err := s.getDelegateClient(s.ctx, forwardedHost) | ||
if err != nil { | ||
tsoStreamErr = errors.WithStack(err) | ||
return tsoStreamErr | ||
return forwardCtx, cancelForward, forwardStream, lastForwardedHost, errors.WithStack(err), nil | ||
} | ||
forwardStream, forwardCtx, cancelForward, err = createTSOForwardStream(stream.Context(), clientConn) | ||
if err != nil { | ||
return forwardCtx, cancelForward, forwardStream, lastForwardedHost, errors.WithStack(err), nil | ||
} | ||
lastForwardedHost = forwardedHost | ||
} | ||
|
||
// The error types defined for tsopb and pdpb are different, so we need to convert them. | ||
var pdpbErr *pdpb.Error | ||
tsopbErr := tsopbResp.GetHeader().GetError() | ||
if tsopbErr != nil { | ||
if tsopbErr.Type == tsopb.ErrorType_OK { | ||
pdpbErr = &pdpb.Error{ | ||
Type: pdpb.ErrorType_OK, | ||
Message: tsopbErr.GetMessage(), | ||
} | ||
} else { | ||
// TODO: specify FORWARD FAILURE error type instead of UNKNOWN. | ||
pdpbErr = &pdpb.Error{ | ||
Type: pdpb.ErrorType_UNKNOWN, | ||
Message: tsopbErr.GetMessage(), | ||
} | ||
tsopbResp, err := s.forwardTSORequestWithDeadLine(forwardCtx, cancelForward, forwardStream, request, tsDeadlineCh) | ||
if err != nil { | ||
return forwardCtx, cancelForward, forwardStream, lastForwardedHost, errors.WithStack(err), nil | ||
} | ||
|
||
// The error types defined for tsopb and pdpb are different, so we need to convert them. | ||
var pdpbErr *pdpb.Error | ||
tsopbErr := tsopbResp.GetHeader().GetError() | ||
if tsopbErr != nil { | ||
if tsopbErr.Type == tsopb.ErrorType_OK { | ||
pdpbErr = &pdpb.Error{ | ||
Type: pdpb.ErrorType_OK, | ||
Message: tsopbErr.GetMessage(), | ||
} | ||
} else { | ||
// TODO: specify FORWARD FAILURE error type instead of UNKNOWN. | ||
pdpbErr = &pdpb.Error{ | ||
Type: pdpb.ErrorType_UNKNOWN, | ||
Message: tsopbErr.GetMessage(), | ||
} | ||
} | ||
} | ||
|
||
response := &pdpb.TsoResponse{ | ||
Header: &pdpb.ResponseHeader{ | ||
ClusterId: tsopbResp.GetHeader().GetClusterId(), | ||
Error: pdpbErr, | ||
}, | ||
Count: tsopbResp.GetCount(), | ||
Timestamp: tsopbResp.GetTimestamp(), | ||
} | ||
if err := server.send(response); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
response := &pdpb.TsoResponse{ | ||
Header: &pdpb.ResponseHeader{ | ||
ClusterId: tsopbResp.GetHeader().GetClusterId(), | ||
Error: pdpbErr, | ||
}, | ||
Count: tsopbResp.GetCount(), | ||
Timestamp: tsopbResp.GetTimestamp(), | ||
} | ||
if server != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We receive both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
err = server.send(response) | ||
} else { | ||
err = stream.Send(response) | ||
} | ||
return forwardCtx, cancelForward, forwardStream, lastForwardedHost, nil, errors.WithStack(err) | ||
} | ||
|
||
func (s *GrpcServer) forwardTSORequestWithDeadLine( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1417,6 +1417,18 @@ func (s *Server) GetRaftCluster() *cluster.RaftCluster { | |
return s.cluster | ||
} | ||
|
||
// IsServiceIndependent returns whether the service is independent. | ||
func (s *Server) IsServiceIndependent(name string) bool { | ||
if s.mode == APIServiceMode && !s.IsClosed() { | ||
// TODO: remove it after we support tso discovery | ||
if name == constant.TSOServiceName { | ||
return true | ||
} | ||
return s.cluster.IsServiceIndependent(name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check whether cluster is nil? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need |
||
} | ||
return false | ||
} | ||
|
||
// DirectlyGetRaftCluster returns raft cluster directly. | ||
// Only used for test. | ||
func (s *Server) DirectlyGetRaftCluster() *cluster.RaftCluster { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are so many incoming and returning parameters here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. But I don't find a better way to avoid it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any benefit after wraping
handleTSOForwarding
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for upcoming PR