Files
tasks/common/rpc/interceptor/stream_error.go
Will DuanandGitHub c746d1073a Fix grpc stream error convert (#6991)
## What changed?
<!-- Describe what has changed in this PR -->
Service error is not properly handled on replication, this is to fix it.
## Why?
<!-- Tell your future self why have you made these changes -->
to handle shard ownership lost error and invalidate history client cache
in frontend
## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
unit test
## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
no risk
## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->
n/a
## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
yes
2024-12-16 19:09:12 +00:00

120 lines
3.1 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package interceptor
import (
"context"
"io"
"go.temporal.io/api/serviceerror"
serviceerrors "go.temporal.io/server/common/serviceerror"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type (
ClientStreamErrorInterceptor struct {
grpc.ClientStream
}
)
var _ grpc.ClientStream = (*ClientStreamErrorInterceptor)(nil)
func NewClientStreamErrorInterceptor(
clientStream grpc.ClientStream,
) *ClientStreamErrorInterceptor {
return &ClientStreamErrorInterceptor{
ClientStream: clientStream,
}
}
func (c *ClientStreamErrorInterceptor) CloseSend() error {
return errorConvert(c.ClientStream.CloseSend())
}
func (c *ClientStreamErrorInterceptor) SendMsg(m interface{}) error {
return errorConvert(c.ClientStream.SendMsg(m))
}
func (c *ClientStreamErrorInterceptor) RecvMsg(m interface{}) error {
return errorConvert(c.ClientStream.RecvMsg(m))
}
func StreamErrorInterceptor(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
clientStream, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, errorConvert(err)
}
return NewClientStreamErrorInterceptor(clientStream), nil
}
func CustomErrorStreamInterceptor(
srv interface{},
serverStream grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
err := handler(srv, serverStream)
if err != nil {
err = serviceerror.ToStatus(err).Err()
}
return err
}
func errorConvert(err error) error {
switch err {
case nil:
return nil
case io.EOF:
return io.EOF
default:
return FromStatus(status.Convert(err))
}
}
// FromStatus converts gRPC Status to service error.
func FromStatus(st *status.Status) error {
if st == nil {
return nil
}
switch st.Code() {
case codes.OK:
return nil
case codes.Unknown:
return serviceerror.NewInternal(st.Message())
default:
return serviceerrors.FromStatus(st)
}
}