call.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package grpc
  34. import (
  35. "bytes"
  36. "io"
  37. "time"
  38. "golang.org/x/net/context"
  39. "golang.org/x/net/trace"
  40. "google.golang.org/grpc/codes"
  41. "google.golang.org/grpc/peer"
  42. "google.golang.org/grpc/stats"
  43. "google.golang.org/grpc/status"
  44. "google.golang.org/grpc/transport"
  45. )
  46. // recvResponse receives and parses an RPC response.
  47. // On error, it returns the error and indicates whether the call should be retried.
  48. //
  49. // TODO(zhaoq): Check whether the received message sequence is valid.
  50. // TODO ctx is used for stats collection and processing. It is the context passed from the application.
  51. func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTransport, c *callInfo, stream *transport.Stream, reply interface{}) (err error) {
  52. // Try to acquire header metadata from the server if there is any.
  53. defer func() {
  54. if err != nil {
  55. if _, ok := err.(transport.ConnectionError); !ok {
  56. t.CloseStream(stream, err)
  57. }
  58. }
  59. }()
  60. c.headerMD, err = stream.Header()
  61. if err != nil {
  62. return
  63. }
  64. p := &parser{r: stream}
  65. var inPayload *stats.InPayload
  66. if dopts.copts.StatsHandler != nil {
  67. inPayload = &stats.InPayload{
  68. Client: true,
  69. }
  70. }
  71. for {
  72. if c.maxReceiveMessageSize == nil {
  73. return Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
  74. }
  75. if err = recv(p, dopts.codec, stream, dopts.dc, reply, *c.maxReceiveMessageSize, inPayload); err != nil {
  76. if err == io.EOF {
  77. break
  78. }
  79. return
  80. }
  81. }
  82. if inPayload != nil && err == io.EOF && stream.Status().Code() == codes.OK {
  83. // TODO in the current implementation, inTrailer may be handled before inPayload in some cases.
  84. // Fix the order if necessary.
  85. dopts.copts.StatsHandler.HandleRPC(ctx, inPayload)
  86. }
  87. c.trailerMD = stream.Trailer()
  88. if peer, ok := peer.FromContext(stream.Context()); ok {
  89. c.peer = peer
  90. }
  91. return nil
  92. }
  93. // sendRequest writes out various information of an RPC such as Context and Message.
  94. 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) {
  95. defer func() {
  96. if err != nil {
  97. // If err is connection error, t will be closed, no need to close stream here.
  98. if _, ok := err.(transport.ConnectionError); !ok {
  99. t.CloseStream(stream, err)
  100. }
  101. }
  102. }()
  103. var (
  104. cbuf *bytes.Buffer
  105. outPayload *stats.OutPayload
  106. )
  107. if compressor != nil {
  108. cbuf = new(bytes.Buffer)
  109. }
  110. if dopts.copts.StatsHandler != nil {
  111. outPayload = &stats.OutPayload{
  112. Client: true,
  113. }
  114. }
  115. outBuf, err := encode(dopts.codec, args, compressor, cbuf, outPayload)
  116. if err != nil {
  117. return err
  118. }
  119. if c.maxSendMessageSize == nil {
  120. return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
  121. }
  122. if len(outBuf) > *c.maxSendMessageSize {
  123. return Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(outBuf), *c.maxSendMessageSize)
  124. }
  125. err = t.Write(stream, outBuf, opts)
  126. if err == nil && outPayload != nil {
  127. outPayload.SentTime = time.Now()
  128. dopts.copts.StatsHandler.HandleRPC(ctx, outPayload)
  129. }
  130. // t.NewStream(...) could lead to an early rejection of the RPC (e.g., the service/method
  131. // does not exist.) so that t.Write could get io.EOF from wait(...). Leave the following
  132. // recvResponse to get the final status.
  133. if err != nil && err != io.EOF {
  134. return err
  135. }
  136. // Sent successfully.
  137. return nil
  138. }
  139. // Invoke sends the RPC request on the wire and returns after response is received.
  140. // Invoke is called by generated code. Also users can call Invoke directly when it
  141. // is really needed in their use cases.
  142. func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
  143. if cc.dopts.unaryInt != nil {
  144. return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
  145. }
  146. return invoke(ctx, method, args, reply, cc, opts...)
  147. }
  148. func invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (e error) {
  149. c := defaultCallInfo
  150. mc := cc.GetMethodConfig(method)
  151. if mc.WaitForReady != nil {
  152. c.failFast = !*mc.WaitForReady
  153. }
  154. if mc.Timeout != nil && *mc.Timeout >= 0 {
  155. var cancel context.CancelFunc
  156. ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
  157. defer cancel()
  158. }
  159. opts = append(cc.dopts.callOptions, opts...)
  160. for _, o := range opts {
  161. if err := o.before(&c); err != nil {
  162. return toRPCErr(err)
  163. }
  164. }
  165. defer func() {
  166. for _, o := range opts {
  167. o.after(&c)
  168. }
  169. }()
  170. c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
  171. c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
  172. if EnableTracing {
  173. c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
  174. defer c.traceInfo.tr.Finish()
  175. c.traceInfo.firstLine.client = true
  176. if deadline, ok := ctx.Deadline(); ok {
  177. c.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
  178. }
  179. c.traceInfo.tr.LazyLog(&c.traceInfo.firstLine, false)
  180. // TODO(dsymonds): Arrange for c.traceInfo.firstLine.remoteAddr to be set.
  181. defer func() {
  182. if e != nil {
  183. c.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{e}}, true)
  184. c.traceInfo.tr.SetError()
  185. }
  186. }()
  187. }
  188. ctx = newContextWithRPCInfo(ctx)
  189. sh := cc.dopts.copts.StatsHandler
  190. if sh != nil {
  191. ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
  192. begin := &stats.Begin{
  193. Client: true,
  194. BeginTime: time.Now(),
  195. FailFast: c.failFast,
  196. }
  197. sh.HandleRPC(ctx, begin)
  198. defer func() {
  199. end := &stats.End{
  200. Client: true,
  201. EndTime: time.Now(),
  202. Error: e,
  203. }
  204. sh.HandleRPC(ctx, end)
  205. }()
  206. }
  207. topts := &transport.Options{
  208. Last: true,
  209. Delay: false,
  210. }
  211. for {
  212. var (
  213. err error
  214. t transport.ClientTransport
  215. stream *transport.Stream
  216. // Record the put handler from Balancer.Get(...). It is called once the
  217. // RPC has completed or failed.
  218. put func()
  219. )
  220. // TODO(zhaoq): Need a formal spec of fail-fast.
  221. callHdr := &transport.CallHdr{
  222. Host: cc.authority,
  223. Method: method,
  224. }
  225. if cc.dopts.cp != nil {
  226. callHdr.SendCompress = cc.dopts.cp.Type()
  227. }
  228. if c.creds != nil {
  229. callHdr.Creds = c.creds
  230. }
  231. gopts := BalancerGetOptions{
  232. BlockingWait: !c.failFast,
  233. }
  234. t, put, err = cc.getTransport(ctx, gopts)
  235. if err != nil {
  236. // TODO(zhaoq): Probably revisit the error handling.
  237. if _, ok := status.FromError(err); ok {
  238. return err
  239. }
  240. if err == errConnClosing || err == errConnUnavailable {
  241. if c.failFast {
  242. return Errorf(codes.Unavailable, "%v", err)
  243. }
  244. continue
  245. }
  246. // All the other errors are treated as Internal errors.
  247. return Errorf(codes.Internal, "%v", err)
  248. }
  249. if c.traceInfo.tr != nil {
  250. c.traceInfo.tr.LazyLog(&payload{sent: true, msg: args}, true)
  251. }
  252. stream, err = t.NewStream(ctx, callHdr)
  253. if err != nil {
  254. if put != nil {
  255. if _, ok := err.(transport.ConnectionError); ok {
  256. // If error is connection error, transport was sending data on wire,
  257. // and we are not sure if anything has been sent on wire.
  258. // If error is not connection error, we are sure nothing has been sent.
  259. updateRPCInfoInContext(ctx, rpcInfo{bytesSent: true, bytesReceived: false})
  260. }
  261. put()
  262. }
  263. if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
  264. continue
  265. }
  266. return toRPCErr(err)
  267. }
  268. err = sendRequest(ctx, cc.dopts, cc.dopts.cp, &c, callHdr, stream, t, args, topts)
  269. if err != nil {
  270. if put != nil {
  271. updateRPCInfoInContext(ctx, rpcInfo{
  272. bytesSent: stream.BytesSent(),
  273. bytesReceived: stream.BytesReceived(),
  274. })
  275. put()
  276. }
  277. // Retry a non-failfast RPC when
  278. // i) there is a connection error; or
  279. // ii) the server started to drain before this RPC was initiated.
  280. if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
  281. continue
  282. }
  283. return toRPCErr(err)
  284. }
  285. err = recvResponse(ctx, cc.dopts, t, &c, stream, reply)
  286. if err != nil {
  287. if put != nil {
  288. updateRPCInfoInContext(ctx, rpcInfo{
  289. bytesSent: stream.BytesSent(),
  290. bytesReceived: stream.BytesReceived(),
  291. })
  292. put()
  293. }
  294. if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
  295. continue
  296. }
  297. return toRPCErr(err)
  298. }
  299. if c.traceInfo.tr != nil {
  300. c.traceInfo.tr.LazyLog(&payload{sent: false, msg: reply}, true)
  301. }
  302. t.CloseStream(stream, nil)
  303. if put != nil {
  304. updateRPCInfoInContext(ctx, rpcInfo{
  305. bytesSent: stream.BytesSent(),
  306. bytesReceived: stream.BytesReceived(),
  307. })
  308. put()
  309. }
  310. return stream.Status().Err()
  311. }
  312. }