File
Blob: chord/local_rpc.go
| 1 | package chord |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | |
| 9 | "go.miragespace.co/specter/spec/chord" |
| 10 | "go.miragespace.co/specter/spec/protocol" |
| 11 | "go.miragespace.co/specter/spec/rpc" |
| 12 | "go.miragespace.co/specter/spec/transport" |
| 13 | "go.miragespace.co/specter/util" |
| 14 | "go.miragespace.co/specter/util/ratecounter" |
| 15 | |
| 16 | "github.com/go-chi/chi/v5" |
| 17 | "github.com/go-chi/chi/v5/middleware" |
| 18 | "github.com/twitchtv/twirp" |
| 19 | "go.uber.org/zap" |
| 20 | ) |
| 21 | |
| 22 | func (n *LocalNode) logError(ctx context.Context, err twirp.Error) context.Context { |
| 23 | if err.Code() == twirp.FailedPrecondition || errors.Is(err, chord.ErrNodeGone) { |
| 24 | return ctx |
| 25 | } |
| 26 | delegation := rpc.GetDelegation(ctx) |
| 27 | if delegation != nil { |
| 28 | service, _ := twirp.ServiceName(ctx) |
| 29 | method, _ := twirp.MethodName(ctx) |
| 30 | l := n.logger.With( |
| 31 | zap.String("component", "rpc_server"), |
| 32 | zap.Object("peer", delegation.Identity), |
| 33 | zap.String("service", service), |
| 34 | zap.String("method", method), |
| 35 | ) |
| 36 | cause, key := rpc.GetErrorMeta(err) |
| 37 | if cause != "" { |
| 38 | l = l.With(zap.String("cause", cause)) |
| 39 | } |
| 40 | if key != "" { |
| 41 | l = l.With(zap.String("kv-key", key)) |
| 42 | } |
| 43 | l.Error("Error handling RPC request", zap.Error(err)) |
| 44 | n.rpcErrorCount.Inc() |
| 45 | } |
| 46 | return ctx |
| 47 | } |
| 48 | |
| 49 | func (n *LocalNode) getIncrementor(rate *ratecounter.Rate) func(ctx context.Context) (context.Context, error) { |
| 50 | return func(ctx context.Context) (context.Context, error) { |
| 51 | rate.Increment() |
| 52 | return ctx, nil |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // RPCHandler exposes the existing vnode and KV RPC routes so startup can compose |
| 57 | // the local listener with other physical-server services. |
| 58 | func (n *LocalNode) RPCHandler(ctx context.Context) http.Handler { |
| 59 | n.rpcHandlerOnce.Do(func() { |
| 60 | r := &Server{ |
| 61 | LocalNode: n, |
| 62 | Factory: func(node *protocol.Node) (chord.VNode, error) { |
| 63 | return NewRemoteNode(ctx, n.BaseLogger, n.ChordClient, node) |
| 64 | }, |
| 65 | } |
| 66 | nsTwirp := protocol.NewVNodeServiceServer( |
| 67 | r, |
| 68 | twirp.WithServerHooks(&twirp.ServerHooks{ |
| 69 | RequestReceived: n.getIncrementor(n.chordRate), |
| 70 | Error: n.logError, |
| 71 | }), |
| 72 | ) |
| 73 | ksTwirp := protocol.NewKVServiceServer( |
| 74 | r, |
| 75 | twirp.WithServerHooks(&twirp.ServerHooks{ |
| 76 | RequestReceived: n.getIncrementor(n.kvRate), |
| 77 | Error: n.logError, |
| 78 | }), |
| 79 | ) |
| 80 | |
| 81 | rpcHandler := chi.NewRouter() |
| 82 | rpcHandler.Use(middleware.Recoverer) |
| 83 | rpcHandler.Mount(nsTwirp.PathPrefix(), rpc.ExtractContext(nsTwirp)) |
| 84 | rpcHandler.Mount(ksTwirp.PathPrefix(), rpc.ExtractContext(ksTwirp)) |
| 85 | n.rpcHandler = rpcHandler |
| 86 | }) |
| 87 | |
| 88 | return n.rpcHandler |
| 89 | } |
| 90 | |
| 91 | func (n *LocalNode) AttachRouter(ctx context.Context, router *transport.StreamRouter) { |
| 92 | n.stopWg.Add(1) |
| 93 | |
| 94 | rpcHandler := n.RPCHandler(ctx) |
| 95 | srv := &http.Server{ |
| 96 | BaseContext: func(l net.Listener) context.Context { |
| 97 | return ctx |
| 98 | }, |
| 99 | ConnContext: func(ctx context.Context, c net.Conn) context.Context { |
| 100 | return rpc.WithDelegation(ctx, c.(*transport.StreamDelegate)) |
| 101 | }, |
| 102 | ReadHeaderTimeout: rpcTimeout, |
| 103 | Handler: rpcHandler, |
| 104 | ErrorLog: util.GetStdLogger(n.logger, "rpc_server"), |
| 105 | } |
| 106 | |
| 107 | go srv.Serve(n.rpcAcceptor) |
| 108 | go func() { |
| 109 | defer n.stopWg.Done() |
| 110 | |
| 111 | <-n.stopCh |
| 112 | n.rpcAcceptor.Close() |
| 113 | }() |
| 114 | |
| 115 | router.HandleChord(protocol.Stream_RPC, n.Identity(), func(delegate *transport.StreamDelegate) { |
| 116 | n.rpcAcceptor.Handle(delegate) |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | func (n *LocalNode) AttachRoot(ctx context.Context, router *transport.StreamRouter) { |
| 121 | router.HandleChord(protocol.Stream_RPC, nil, func(delegate *transport.StreamDelegate) { |
| 122 | n.rpcAcceptor.Handle(delegate) |
| 123 | }) |
| 124 | } |