retry.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "context"
  17. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "google.golang.org/grpc"
  20. "google.golang.org/grpc/codes"
  21. "google.golang.org/grpc/status"
  22. )
  23. type rpcFunc func(ctx context.Context) error
  24. type retryRpcFunc func(context.Context, rpcFunc) error
  25. type retryStopErrFunc func(error) bool
  26. func isReadStopError(err error) bool {
  27. eErr := rpctypes.Error(err)
  28. // always stop retry on etcd errors
  29. if _, ok := eErr.(rpctypes.EtcdError); ok {
  30. return true
  31. }
  32. // only retry if unavailable
  33. ev, _ := status.FromError(err)
  34. return ev.Code() != codes.Unavailable
  35. }
  36. func isWriteStopError(err error) bool {
  37. ev, _ := status.FromError(err)
  38. if ev.Code() != codes.Unavailable {
  39. return true
  40. }
  41. return rpctypes.ErrorDesc(err) != "there is no address available"
  42. }
  43. func (c *Client) newRetryWrapper(isStop retryStopErrFunc) retryRpcFunc {
  44. return func(rpcCtx context.Context, f rpcFunc) error {
  45. for {
  46. err := f(rpcCtx)
  47. if err == nil {
  48. return nil
  49. }
  50. notify := c.balancer.ConnectNotify()
  51. if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable {
  52. c.balancer.next()
  53. }
  54. if isStop(err) {
  55. return err
  56. }
  57. select {
  58. case <-notify:
  59. case <-rpcCtx.Done():
  60. return rpcCtx.Err()
  61. case <-c.ctx.Done():
  62. return c.ctx.Err()
  63. }
  64. }
  65. }
  66. }
  67. func (c *Client) newAuthRetryWrapper() retryRpcFunc {
  68. return func(rpcCtx context.Context, f rpcFunc) error {
  69. for {
  70. err := f(rpcCtx)
  71. if err == nil {
  72. return nil
  73. }
  74. // always stop retry on etcd errors other than invalid auth token
  75. if rpctypes.Error(err) == rpctypes.ErrInvalidAuthToken {
  76. gterr := c.getToken(rpcCtx)
  77. if gterr != nil {
  78. return err // return the original error for simplicity
  79. }
  80. continue
  81. }
  82. return err
  83. }
  84. }
  85. }
  86. // RetryKVClient implements a KVClient that uses the client's FailFast retry policy.
  87. func RetryKVClient(c *Client) pb.KVClient {
  88. readRetry := c.newRetryWrapper(isReadStopError)
  89. writeRetry := c.newRetryWrapper(isWriteStopError)
  90. conn := pb.NewKVClient(c.conn)
  91. retryBasic := &retryKVClient{&retryWriteKVClient{conn, writeRetry}, readRetry}
  92. retryAuthWrapper := c.newAuthRetryWrapper()
  93. return &retryKVClient{
  94. &retryWriteKVClient{retryBasic, retryAuthWrapper},
  95. retryAuthWrapper}
  96. }
  97. type retryKVClient struct {
  98. *retryWriteKVClient
  99. readRetry retryRpcFunc
  100. }
  101. func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
  102. err = rkv.readRetry(ctx, func(rctx context.Context) error {
  103. resp, err = rkv.KVClient.Range(rctx, in, opts...)
  104. return err
  105. })
  106. return resp, err
  107. }
  108. type retryWriteKVClient struct {
  109. pb.KVClient
  110. retryf retryRpcFunc
  111. }
  112. func (rkv *retryWriteKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
  113. err = rkv.retryf(ctx, func(rctx context.Context) error {
  114. resp, err = rkv.KVClient.Put(rctx, in, opts...)
  115. return err
  116. })
  117. return resp, err
  118. }
  119. func (rkv *retryWriteKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
  120. err = rkv.retryf(ctx, func(rctx context.Context) error {
  121. resp, err = rkv.KVClient.DeleteRange(rctx, in, opts...)
  122. return err
  123. })
  124. return resp, err
  125. }
  126. func (rkv *retryWriteKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
  127. err = rkv.retryf(ctx, func(rctx context.Context) error {
  128. resp, err = rkv.KVClient.Txn(rctx, in, opts...)
  129. return err
  130. })
  131. return resp, err
  132. }
  133. func (rkv *retryWriteKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
  134. err = rkv.retryf(ctx, func(rctx context.Context) error {
  135. resp, err = rkv.KVClient.Compact(rctx, in, opts...)
  136. return err
  137. })
  138. return resp, err
  139. }
  140. type retryLeaseClient struct {
  141. pb.LeaseClient
  142. retryf retryRpcFunc
  143. }
  144. // RetryLeaseClient implements a LeaseClient that uses the client's FailFast retry policy.
  145. func RetryLeaseClient(c *Client) pb.LeaseClient {
  146. retry := &retryLeaseClient{
  147. pb.NewLeaseClient(c.conn),
  148. c.newRetryWrapper(isReadStopError),
  149. }
  150. return &retryLeaseClient{retry, c.newAuthRetryWrapper()}
  151. }
  152. func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
  153. err = rlc.retryf(ctx, func(rctx context.Context) error {
  154. resp, err = rlc.LeaseClient.LeaseGrant(rctx, in, opts...)
  155. return err
  156. })
  157. return resp, err
  158. }
  159. func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
  160. err = rlc.retryf(ctx, func(rctx context.Context) error {
  161. resp, err = rlc.LeaseClient.LeaseRevoke(rctx, in, opts...)
  162. return err
  163. })
  164. return resp, err
  165. }
  166. type retryClusterClient struct {
  167. pb.ClusterClient
  168. retryf retryRpcFunc
  169. }
  170. // RetryClusterClient implements a ClusterClient that uses the client's FailFast retry policy.
  171. func RetryClusterClient(c *Client) pb.ClusterClient {
  172. return &retryClusterClient{pb.NewClusterClient(c.conn), c.newRetryWrapper(isWriteStopError)}
  173. }
  174. func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
  175. err = rcc.retryf(ctx, func(rctx context.Context) error {
  176. resp, err = rcc.ClusterClient.MemberAdd(rctx, in, opts...)
  177. return err
  178. })
  179. return resp, err
  180. }
  181. func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
  182. err = rcc.retryf(ctx, func(rctx context.Context) error {
  183. resp, err = rcc.ClusterClient.MemberRemove(rctx, in, opts...)
  184. return err
  185. })
  186. return resp, err
  187. }
  188. func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
  189. err = rcc.retryf(ctx, func(rctx context.Context) error {
  190. resp, err = rcc.ClusterClient.MemberUpdate(rctx, in, opts...)
  191. return err
  192. })
  193. return resp, err
  194. }
  195. type retryAuthClient struct {
  196. pb.AuthClient
  197. retryf retryRpcFunc
  198. }
  199. // RetryAuthClient implements a AuthClient that uses the client's FailFast retry policy.
  200. func RetryAuthClient(c *Client) pb.AuthClient {
  201. return &retryAuthClient{pb.NewAuthClient(c.conn), c.newRetryWrapper(isWriteStopError)}
  202. }
  203. func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
  204. err = rac.retryf(ctx, func(rctx context.Context) error {
  205. resp, err = rac.AuthClient.AuthEnable(rctx, in, opts...)
  206. return err
  207. })
  208. return resp, err
  209. }
  210. func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
  211. err = rac.retryf(ctx, func(rctx context.Context) error {
  212. resp, err = rac.AuthClient.AuthDisable(rctx, in, opts...)
  213. return err
  214. })
  215. return resp, err
  216. }
  217. func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
  218. err = rac.retryf(ctx, func(rctx context.Context) error {
  219. resp, err = rac.AuthClient.UserAdd(rctx, in, opts...)
  220. return err
  221. })
  222. return resp, err
  223. }
  224. func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
  225. err = rac.retryf(ctx, func(rctx context.Context) error {
  226. resp, err = rac.AuthClient.UserDelete(rctx, in, opts...)
  227. return err
  228. })
  229. return resp, err
  230. }
  231. func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
  232. err = rac.retryf(ctx, func(rctx context.Context) error {
  233. resp, err = rac.AuthClient.UserChangePassword(rctx, in, opts...)
  234. return err
  235. })
  236. return resp, err
  237. }
  238. func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
  239. err = rac.retryf(ctx, func(rctx context.Context) error {
  240. resp, err = rac.AuthClient.UserGrantRole(rctx, in, opts...)
  241. return err
  242. })
  243. return resp, err
  244. }
  245. func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
  246. err = rac.retryf(ctx, func(rctx context.Context) error {
  247. resp, err = rac.AuthClient.UserRevokeRole(rctx, in, opts...)
  248. return err
  249. })
  250. return resp, err
  251. }
  252. func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
  253. err = rac.retryf(ctx, func(rctx context.Context) error {
  254. resp, err = rac.AuthClient.RoleAdd(rctx, in, opts...)
  255. return err
  256. })
  257. return resp, err
  258. }
  259. func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
  260. err = rac.retryf(ctx, func(rctx context.Context) error {
  261. resp, err = rac.AuthClient.RoleDelete(rctx, in, opts...)
  262. return err
  263. })
  264. return resp, err
  265. }
  266. func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
  267. err = rac.retryf(ctx, func(rctx context.Context) error {
  268. resp, err = rac.AuthClient.RoleGrantPermission(rctx, in, opts...)
  269. return err
  270. })
  271. return resp, err
  272. }
  273. func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
  274. err = rac.retryf(ctx, func(rctx context.Context) error {
  275. resp, err = rac.AuthClient.RoleRevokePermission(rctx, in, opts...)
  276. return err
  277. })
  278. return resp, err
  279. }