Files
seaweedfs/seaweed-block/core/rpc/answer/grpc.go
T

108 lines
3.6 KiB
Go

package answer
import (
"context"
"errors"
"io"
"sync/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
)
type rpcEvidenceKey struct{}
type rpcEvidence struct {
started uint64
peerGeneration atomic.Uint64
peerPool atomic.Pointer[peerPool]
dial atomic.Pointer[dialObservation]
writeStarted atomic.Bool
tls atomic.Pointer[connectionTLSInfo]
}
// ClientOptions preserve per-call admission facts on every link. OutHeader
// is emitted by gRPC's NewStream before it returns after queuing headers.
// Its absence when Invoke returns proves that no request stream was sent;
// its presence is deliberately conservative across transparent retries.
func ClientOptions() []grpc.DialOption {
return []grpc.DialOption{grpc.WithDefaultServiceConfig(`{"loadBalancingConfig":[{"answer_pick_first":{}}]}`), grpc.WithStatsHandler(rpcStats{}), grpc.WithChainUnaryInterceptor(recordUnary), grpc.WithChainStreamInterceptor(recordStream)}
}
func recordStream(ctx context.Context, desc *grpc.StreamDesc, conn *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
evidence := &rpcEvidence{started: attemptSequence.Load()}
ctx = context.WithValue(ctx, rpcEvidenceKey{}, evidence)
stream, err := streamer(ctx, desc, conn, method, opts...)
if err != nil {
return nil, evidence.record(ctx, err)
}
return &observedStream{ClientStream: stream, record: func(err error) error { return evidence.record(ctx, err) }}, nil
}
func (e *rpcEvidence) record(ctx context.Context, err error) error {
if err == nil {
return nil
}
phase := Unsent
if e.writeStarted.Load() {
phase = PossiblyAdmitted
}
if phase == Unsent {
if observation := e.dial.Load(); observation != nil {
err = &failure{phase: Unsent, kind: observation.kind, cause: errors.Join(err, observation.cause)}
}
}
return RecordContext(ctx, phase, err)
}
type observedStream struct {
grpc.ClientStream
record func(error) error
}
func (s *observedStream) Header() (metadata.MD, error) {
header, err := s.ClientStream.Header()
return header, s.record(err)
}
func (s *observedStream) SendMsg(msg any) error {
err := s.ClientStream.SendMsg(msg)
if err == io.EOF {
return err
} // callers obtain the final status via RecvMsg
return s.record(err)
}
func (s *observedStream) RecvMsg(msg any) error {
err := s.ClientStream.RecvMsg(msg)
if err == io.EOF {
return err
} // successful stream completion, not a transport EOF
return s.record(err)
}
func (s *observedStream) CloseSend() error { return s.record(s.ClientStream.CloseSend()) }
func recordUnary(ctx context.Context, method string, req, reply any, conn *grpc.ClientConn, invoke grpc.UnaryInvoker, opts ...grpc.CallOption) error {
evidence := &rpcEvidence{started: attemptSequence.Load()}
ctx = context.WithValue(ctx, rpcEvidenceKey{}, evidence)
err := invoke(ctx, method, req, reply, conn, opts...)
return evidence.record(ctx, err)
}
type rpcStats struct{}
func (rpcStats) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context { return ctx }
func (rpcStats) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context { return ctx }
func (rpcStats) HandleConn(context.Context, stats.ConnStats) {}
func (rpcStats) HandleRPC(ctx context.Context, event stats.RPCStats) {
if _, ok := event.(*stats.OutHeader); ok {
if evidence, ok := ctx.Value(rpcEvidenceKey{}).(*rpcEvidence); ok {
evidence.writeStarted.Store(true)
if p, ok := peer.FromContext(ctx); ok {
if info, ok := p.AuthInfo.(*connectionTLSInfo); ok {
evidence.tls.Store(info)
}
}
}
}
}