call.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "io"
  21. "time"
  22. "golang.org/x/net/context"
  23. "golang.org/x/net/trace"
  24. "google.golang.org/grpc/balancer"
  25. "google.golang.org/grpc/codes"
  26. "google.golang.org/grpc/encoding"
  27. "google.golang.org/grpc/peer"
  28. "google.golang.org/grpc/stats"
  29. "google.golang.org/grpc/status"
  30. "google.golang.org/grpc/transport"
  31. )
  32. // recvResponse receives and parses an RPC response.
  33. // On error, it returns the error and indicates whether the call should be retried.
  34. //
  35. // TODO(zhaoq): Check whether the received message sequence is valid.
  36. // TODO ctx is used for stats collection and processing. It is the context passed from the application.
  37. func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTransport, c *callInfo, stream *transport.Stream, reply interface{}) (err error) {
  38. // Try to acquire header metadata from the server if there is any.
  39. defer func() {
  40. if err != nil {
  41. if _, ok := err.(transport.ConnectionError); !ok {
  42. t.CloseStream(stream, err)
  43. }
  44. }
  45. }()
  46. c.headerMD, err = stream.Header()
  47. if err != nil {
  48. return
  49. }
  50. p := &parser{r: stream}
  51. var inPayload *stats.InPayload
  52. if dopts.copts.StatsHandler != nil {
  53. inPayload = &stats.InPayload{
  54. Client: true,
  55. }
  56. }
  57. for {
  58. if c.maxReceiveMessageSize == nil {
  59. return status.Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
  60. }
  61. // Set dc if it exists and matches the message compression type used,
  62. // otherwise set comp if a registered compressor exists for it.
  63. var comp encoding.Compressor
  64. var dc Decompressor
  65. if rc := stream.RecvCompress(); dopts.dc != nil && dopts.dc.Type() == rc {
  66. dc = dopts.dc
  67. } else if rc != "" && rc != encoding.Identity {
  68. comp = encoding.GetCompressor(rc)
  69. }
  70. if err = recv(p, dopts.codec, stream, dc, reply, *c.maxReceiveMessageSize, inPayload, comp); err != nil {
  71. if err == io.EOF {
  72. break
  73. }
  74. return
  75. }
  76. }
  77. if inPayload != nil && err == io.EOF && stream.Status().Code() == codes.OK {
  78. // TODO in the current implementation, inTrailer may be handled before inPayload in some cases.
  79. // Fix the order if necessary.
  80. dopts.copts.StatsHandler.HandleRPC(ctx, inPayload)
  81. }
  82. c.trailerMD = stream.Trailer()
  83. return nil
  84. }
  85. // sendRequest writes out various information of an RPC such as Context and Message.
  86. func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, c *callInfo, callHdr *transport.CallHdr, stream *transport.Stream, t transport.ClientTransport, args interface{}, opts *transport.Options) (err error) {
  87. defer func() {
  88. if err != nil {
  89. // If err is connection error, t will be closed, no need to close stream here.
  90. if _, ok := err.(transport.ConnectionError); !ok {
  91. t.CloseStream(stream, err)
  92. }
  93. }
  94. }()
  95. var (
  96. outPayload *stats.OutPayload
  97. )
  98. if dopts.copts.StatsHandler != nil {
  99. outPayload = &stats.OutPayload{
  100. Client: true,
  101. }
  102. }
  103. // Set comp and clear compressor if a registered compressor matches the type
  104. // specified via UseCompressor. (And error if a matching compressor is not
  105. // registered.)
  106. var comp encoding.Compressor
  107. if ct := c.compressorType; ct != "" && ct != encoding.Identity {
  108. compressor = nil // Disable the legacy compressor.
  109. comp = encoding.GetCompressor(ct)
  110. if comp == nil {
  111. return status.Errorf(codes.Internal, "grpc: Compressor is not installed for grpc-encoding %q", ct)
  112. }
  113. }
  114. hdr, data, err := encode(dopts.codec, args, compressor, outPayload, comp)
  115. if err != nil {
  116. return err
  117. }
  118. if c.maxSendMessageSize == nil {
  119. return status.Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
  120. }
  121. if len(data) > *c.maxSendMessageSize {
  122. return status.Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(data), *c.maxSendMessageSize)
  123. }
  124. err = t.Write(stream, hdr, data, opts)
  125. if err == nil && outPayload != nil {
  126. outPayload.SentTime = time.Now()
  127. dopts.copts.StatsHandler.HandleRPC(ctx, outPayload)
  128. }
  129. // t.NewStream(...) could lead to an early rejection of the RPC (e.g., the service/method
  130. // does not exist.) so that t.Write could get io.EOF from wait(...). Leave the following
  131. // recvResponse to get the final status.
  132. if err != nil && err != io.EOF {
  133. return err
  134. }
  135. // Sent successfully.
  136. return nil
  137. }
  138. // Invoke sends the RPC request on the wire and returns after response is
  139. // received. This is typically called by generated code.
  140. func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error {
  141. if cc.dopts.unaryInt != nil {
  142. return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
  143. }
  144. return invoke(ctx, method, args, reply, cc, opts...)
  145. }
  146. // Invoke sends the RPC request on the wire and returns after response is
  147. // received. This is typically called by generated code.
  148. //
  149. // DEPRECATED: Use ClientConn.Invoke instead.
  150. func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
  151. return cc.Invoke(ctx, method, args, reply, opts...)
  152. }
  153. func invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (e error) {
  154. c := defaultCallInfo()
  155. mc := cc.GetMethodConfig(method)
  156. if mc.WaitForReady != nil {
  157. c.failFast = !*mc.WaitForReady
  158. }
  159. if mc.Timeout != nil && *mc.Timeout >= 0 {
  160. var cancel context.CancelFunc
  161. ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
  162. defer cancel()
  163. }
  164. opts = append(cc.dopts.callOptions, opts...)
  165. for _, o := range opts {
  166. if err := o.before(c); err != nil {
  167. return toRPCErr(err)
  168. }
  169. }
  170. defer func() {
  171. for _, o := range opts {
  172. o.after(c)
  173. }
  174. }()
  175. c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
  176. c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
  177. if EnableTracing {
  178. c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
  179. defer c.traceInfo.tr.Finish()
  180. c.traceInfo.firstLine.client = true
  181. if deadline, ok := ctx.Deadline(); ok {
  182. c.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
  183. }
  184. c.traceInfo.tr.LazyLog(&c.traceInfo.firstLine, false)
  185. // TODO(dsymonds): Arrange for c.traceInfo.firstLine.remoteAddr to be set.
  186. defer func() {
  187. if e != nil {
  188. c.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{e}}, true)
  189. c.traceInfo.tr.SetError()
  190. }
  191. }()
  192. }
  193. ctx = newContextWithRPCInfo(ctx, c.failFast)
  194. sh := cc.dopts.copts.StatsHandler
  195. if sh != nil {
  196. ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
  197. begin := &stats.Begin{
  198. Client: true,
  199. BeginTime: time.Now(),
  200. FailFast: c.failFast,
  201. }
  202. sh.HandleRPC(ctx, begin)
  203. defer func() {
  204. end := &stats.End{
  205. Client: true,
  206. EndTime: time.Now(),
  207. Error: e,
  208. }
  209. sh.HandleRPC(ctx, end)
  210. }()
  211. }
  212. topts := &transport.Options{
  213. Last: true,
  214. Delay: false,
  215. }
  216. callHdr := &transport.CallHdr{
  217. Host: cc.authority,
  218. Method: method,
  219. }
  220. if c.creds != nil {
  221. callHdr.Creds = c.creds
  222. }
  223. if c.compressorType != "" {
  224. callHdr.SendCompress = c.compressorType
  225. } else if cc.dopts.cp != nil {
  226. callHdr.SendCompress = cc.dopts.cp.Type()
  227. }
  228. firstAttempt := true
  229. for {
  230. // Check to make sure the context has expired. This will prevent us from
  231. // looping forever if an error occurs for wait-for-ready RPCs where no data
  232. // is sent on the wire.
  233. select {
  234. case <-ctx.Done():
  235. return toRPCErr(ctx.Err())
  236. default:
  237. }
  238. // Record the done handler from Balancer.Get(...). It is called once the
  239. // RPC has completed or failed.
  240. t, done, err := cc.getTransport(ctx, c.failFast)
  241. if err != nil {
  242. return err
  243. }
  244. stream, err := t.NewStream(ctx, callHdr)
  245. if err != nil {
  246. if done != nil {
  247. done(balancer.DoneInfo{Err: err})
  248. }
  249. // In the event of any error from NewStream, we never attempted to write
  250. // anything to the wire, so we can retry indefinitely for non-fail-fast
  251. // RPCs.
  252. if !c.failFast {
  253. continue
  254. }
  255. return toRPCErr(err)
  256. }
  257. if peer, ok := peer.FromContext(stream.Context()); ok {
  258. c.peer = peer
  259. }
  260. if c.traceInfo.tr != nil {
  261. c.traceInfo.tr.LazyLog(&payload{sent: true, msg: args}, true)
  262. }
  263. err = sendRequest(ctx, cc.dopts, cc.dopts.cp, c, callHdr, stream, t, args, topts)
  264. if err != nil {
  265. if done != nil {
  266. done(balancer.DoneInfo{
  267. Err: err,
  268. BytesSent: true,
  269. BytesReceived: stream.BytesReceived(),
  270. })
  271. }
  272. // Retry a non-failfast RPC when
  273. // i) the server started to drain before this RPC was initiated.
  274. // ii) the server refused the stream.
  275. if !c.failFast && stream.Unprocessed() {
  276. // In this case, the server did not receive the data, but we still
  277. // created wire traffic, so we should not retry indefinitely.
  278. if firstAttempt {
  279. // TODO: Add a field to header for grpc-transparent-retry-attempts
  280. firstAttempt = false
  281. continue
  282. }
  283. // Otherwise, give up and return an error anyway.
  284. }
  285. return toRPCErr(err)
  286. }
  287. err = recvResponse(ctx, cc.dopts, t, c, stream, reply)
  288. if err != nil {
  289. if done != nil {
  290. done(balancer.DoneInfo{
  291. Err: err,
  292. BytesSent: true,
  293. BytesReceived: stream.BytesReceived(),
  294. })
  295. }
  296. if !c.failFast && stream.Unprocessed() {
  297. // In these cases, the server did not receive the data, but we still
  298. // created wire traffic, so we should not retry indefinitely.
  299. if firstAttempt {
  300. // TODO: Add a field to header for grpc-transparent-retry-attempts
  301. firstAttempt = false
  302. continue
  303. }
  304. // Otherwise, give up and return an error anyway.
  305. }
  306. return toRPCErr(err)
  307. }
  308. if c.traceInfo.tr != nil {
  309. c.traceInfo.tr.LazyLog(&payload{sent: false, msg: reply}, true)
  310. }
  311. t.CloseStream(stream, nil)
  312. err = stream.Status().Err()
  313. if done != nil {
  314. done(balancer.DoneInfo{
  315. Err: err,
  316. BytesSent: true,
  317. BytesReceived: stream.BytesReceived(),
  318. })
  319. }
  320. if !c.failFast && stream.Unprocessed() {
  321. // In these cases, the server did not receive the data, but we still
  322. // created wire traffic, so we should not retry indefinitely.
  323. if firstAttempt {
  324. // TODO: Add a field to header for grpc-transparent-retry-attempts
  325. firstAttempt = false
  326. continue
  327. }
  328. }
  329. return err
  330. }
  331. }