call.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. "bytes"
  21. "io"
  22. "time"
  23. "golang.org/x/net/context"
  24. "golang.org/x/net/trace"
  25. "google.golang.org/grpc/balancer"
  26. "google.golang.org/grpc/codes"
  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 Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
  60. }
  61. if err = recv(p, dopts.codec, stream, dopts.dc, reply, *c.maxReceiveMessageSize, inPayload); err != nil {
  62. if err == io.EOF {
  63. break
  64. }
  65. return
  66. }
  67. }
  68. if inPayload != nil && err == io.EOF && stream.Status().Code() == codes.OK {
  69. // TODO in the current implementation, inTrailer may be handled before inPayload in some cases.
  70. // Fix the order if necessary.
  71. dopts.copts.StatsHandler.HandleRPC(ctx, inPayload)
  72. }
  73. c.trailerMD = stream.Trailer()
  74. return nil
  75. }
  76. // sendRequest writes out various information of an RPC such as Context and Message.
  77. 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) {
  78. defer func() {
  79. if err != nil {
  80. // If err is connection error, t will be closed, no need to close stream here.
  81. if _, ok := err.(transport.ConnectionError); !ok {
  82. t.CloseStream(stream, err)
  83. }
  84. }
  85. }()
  86. var (
  87. cbuf *bytes.Buffer
  88. outPayload *stats.OutPayload
  89. )
  90. if compressor != nil {
  91. cbuf = new(bytes.Buffer)
  92. }
  93. if dopts.copts.StatsHandler != nil {
  94. outPayload = &stats.OutPayload{
  95. Client: true,
  96. }
  97. }
  98. hdr, data, err := encode(dopts.codec, args, compressor, cbuf, outPayload)
  99. if err != nil {
  100. return err
  101. }
  102. if c.maxSendMessageSize == nil {
  103. return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
  104. }
  105. if len(data) > *c.maxSendMessageSize {
  106. return Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(data), *c.maxSendMessageSize)
  107. }
  108. err = t.Write(stream, hdr, data, opts)
  109. if err == nil && outPayload != nil {
  110. outPayload.SentTime = time.Now()
  111. dopts.copts.StatsHandler.HandleRPC(ctx, outPayload)
  112. }
  113. // t.NewStream(...) could lead to an early rejection of the RPC (e.g., the service/method
  114. // does not exist.) so that t.Write could get io.EOF from wait(...). Leave the following
  115. // recvResponse to get the final status.
  116. if err != nil && err != io.EOF {
  117. return err
  118. }
  119. // Sent successfully.
  120. return nil
  121. }
  122. // Invoke sends the RPC request on the wire and returns after response is received.
  123. // Invoke is called by generated code. Also users can call Invoke directly when it
  124. // is really needed in their use cases.
  125. func Invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) error {
  126. if cc.dopts.unaryInt != nil {
  127. return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
  128. }
  129. return invoke(ctx, method, args, reply, cc, opts...)
  130. }
  131. func invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (e error) {
  132. c := defaultCallInfo()
  133. mc := cc.GetMethodConfig(method)
  134. if mc.WaitForReady != nil {
  135. c.failFast = !*mc.WaitForReady
  136. }
  137. if mc.Timeout != nil && *mc.Timeout >= 0 {
  138. var cancel context.CancelFunc
  139. ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
  140. defer cancel()
  141. }
  142. opts = append(cc.dopts.callOptions, opts...)
  143. for _, o := range opts {
  144. if err := o.before(c); err != nil {
  145. return toRPCErr(err)
  146. }
  147. }
  148. defer func() {
  149. for _, o := range opts {
  150. o.after(c)
  151. }
  152. }()
  153. c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
  154. c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
  155. if EnableTracing {
  156. c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
  157. defer c.traceInfo.tr.Finish()
  158. c.traceInfo.firstLine.client = true
  159. if deadline, ok := ctx.Deadline(); ok {
  160. c.traceInfo.firstLine.deadline = deadline.Sub(time.Now())
  161. }
  162. c.traceInfo.tr.LazyLog(&c.traceInfo.firstLine, false)
  163. // TODO(dsymonds): Arrange for c.traceInfo.firstLine.remoteAddr to be set.
  164. defer func() {
  165. if e != nil {
  166. c.traceInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{e}}, true)
  167. c.traceInfo.tr.SetError()
  168. }
  169. }()
  170. }
  171. ctx = newContextWithRPCInfo(ctx, c.failFast)
  172. sh := cc.dopts.copts.StatsHandler
  173. if sh != nil {
  174. ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
  175. begin := &stats.Begin{
  176. Client: true,
  177. BeginTime: time.Now(),
  178. FailFast: c.failFast,
  179. }
  180. sh.HandleRPC(ctx, begin)
  181. defer func() {
  182. end := &stats.End{
  183. Client: true,
  184. EndTime: time.Now(),
  185. Error: e,
  186. }
  187. sh.HandleRPC(ctx, end)
  188. }()
  189. }
  190. topts := &transport.Options{
  191. Last: true,
  192. Delay: false,
  193. }
  194. for {
  195. var (
  196. err error
  197. t transport.ClientTransport
  198. stream *transport.Stream
  199. // Record the put handler from Balancer.Get(...). It is called once the
  200. // RPC has completed or failed.
  201. put func(balancer.DoneInfo)
  202. )
  203. // TODO(zhaoq): Need a formal spec of fail-fast.
  204. callHdr := &transport.CallHdr{
  205. Host: cc.authority,
  206. Method: method,
  207. }
  208. if cc.dopts.cp != nil {
  209. callHdr.SendCompress = cc.dopts.cp.Type()
  210. }
  211. if c.creds != nil {
  212. callHdr.Creds = c.creds
  213. }
  214. gopts := BalancerGetOptions{
  215. BlockingWait: !c.failFast,
  216. }
  217. t, put, err = cc.getTransport(ctx, gopts)
  218. if err != nil {
  219. // TODO(zhaoq): Probably revisit the error handling.
  220. if _, ok := status.FromError(err); ok {
  221. return err
  222. }
  223. if err == errConnClosing || err == errConnUnavailable {
  224. if c.failFast {
  225. return Errorf(codes.Unavailable, "%v", err)
  226. }
  227. continue
  228. }
  229. // All the other errors are treated as Internal errors.
  230. return Errorf(codes.Internal, "%v", err)
  231. }
  232. if c.traceInfo.tr != nil {
  233. c.traceInfo.tr.LazyLog(&payload{sent: true, msg: args}, true)
  234. }
  235. stream, err = t.NewStream(ctx, callHdr)
  236. if err != nil {
  237. if put != nil {
  238. if _, ok := err.(transport.ConnectionError); ok {
  239. // If error is connection error, transport was sending data on wire,
  240. // and we are not sure if anything has been sent on wire.
  241. // If error is not connection error, we are sure nothing has been sent.
  242. updateRPCInfoInContext(ctx, rpcInfo{bytesSent: true, bytesReceived: false})
  243. }
  244. put(balancer.DoneInfo{Err: err})
  245. }
  246. if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
  247. continue
  248. }
  249. return toRPCErr(err)
  250. }
  251. if peer, ok := peer.FromContext(stream.Context()); ok {
  252. c.peer = peer
  253. }
  254. err = sendRequest(ctx, cc.dopts, cc.dopts.cp, c, callHdr, stream, t, args, topts)
  255. if err != nil {
  256. if put != nil {
  257. updateRPCInfoInContext(ctx, rpcInfo{
  258. bytesSent: stream.BytesSent(),
  259. bytesReceived: stream.BytesReceived(),
  260. })
  261. put(balancer.DoneInfo{Err: err})
  262. }
  263. // Retry a non-failfast RPC when
  264. // i) there is a connection error; or
  265. // ii) the server started to drain before this RPC was initiated.
  266. if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
  267. continue
  268. }
  269. return toRPCErr(err)
  270. }
  271. err = recvResponse(ctx, cc.dopts, t, c, stream, reply)
  272. if err != nil {
  273. if put != nil {
  274. updateRPCInfoInContext(ctx, rpcInfo{
  275. bytesSent: stream.BytesSent(),
  276. bytesReceived: stream.BytesReceived(),
  277. })
  278. put(balancer.DoneInfo{Err: err})
  279. }
  280. if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
  281. continue
  282. }
  283. return toRPCErr(err)
  284. }
  285. if c.traceInfo.tr != nil {
  286. c.traceInfo.tr.LazyLog(&payload{sent: false, msg: reply}, true)
  287. }
  288. t.CloseStream(stream, nil)
  289. if put != nil {
  290. updateRPCInfoInContext(ctx, rpcInfo{
  291. bytesSent: stream.BytesSent(),
  292. bytesReceived: stream.BytesReceived(),
  293. })
  294. put(balancer.DoneInfo{Err: err})
  295. }
  296. return stream.Status().Err()
  297. }
  298. }