retry.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. select {
  47. case <-c.balancer.ConnectNotify():
  48. case <-rpcCtx.Done():
  49. return rpcCtx.Err()
  50. case <-c.ctx.Done():
  51. return c.ctx.Err()
  52. }
  53. err := f(rpcCtx)
  54. if err == nil {
  55. return nil
  56. }
  57. if logger.V(4) {
  58. logger.Infof("clientv3/retry: retry for error %v", err)
  59. }
  60. notify := c.balancer.ConnectNotify()
  61. if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable {
  62. c.balancer.next()
  63. }
  64. if isStop(err) {
  65. return err
  66. }
  67. select {
  68. case <-notify:
  69. case <-rpcCtx.Done():
  70. return rpcCtx.Err()
  71. case <-c.ctx.Done():
  72. return c.ctx.Err()
  73. }
  74. }
  75. }
  76. }
  77. func (c *Client) newAuthRetryWrapper() retryRpcFunc {
  78. return func(rpcCtx context.Context, f rpcFunc) error {
  79. for {
  80. err := f(rpcCtx)
  81. if err == nil {
  82. return nil
  83. }
  84. if logger.V(4) {
  85. logger.Infof("clientv3/auth-retry: retry for error %v", err)
  86. }
  87. // always stop retry on etcd errors other than invalid auth token
  88. if rpctypes.Error(err) == rpctypes.ErrInvalidAuthToken {
  89. gterr := c.getToken(rpcCtx)
  90. if gterr != nil {
  91. return err // return the original error for simplicity
  92. }
  93. continue
  94. }
  95. return err
  96. }
  97. }
  98. }
  99. // RetryKVClient implements a KVClient that uses the client's FailFast retry policy.
  100. func RetryKVClient(c *Client) pb.KVClient {
  101. readRetry := c.newRetryWrapper(isReadStopError)
  102. writeRetry := c.newRetryWrapper(isWriteStopError)
  103. conn := pb.NewKVClient(c.conn)
  104. retryBasic := &retryKVClient{&retryWriteKVClient{conn, writeRetry}, readRetry}
  105. retryAuthWrapper := c.newAuthRetryWrapper()
  106. return &retryKVClient{
  107. &retryWriteKVClient{retryBasic, retryAuthWrapper},
  108. retryAuthWrapper}
  109. }
  110. type retryKVClient struct {
  111. *retryWriteKVClient
  112. readRetry retryRpcFunc
  113. }
  114. func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
  115. err = rkv.readRetry(ctx, func(rctx context.Context) error {
  116. resp, err = rkv.KVClient.Range(rctx, in, opts...)
  117. return err
  118. })
  119. return resp, err
  120. }
  121. type retryWriteKVClient struct {
  122. pb.KVClient
  123. retryf retryRpcFunc
  124. }
  125. func (rkv *retryWriteKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
  126. err = rkv.retryf(ctx, func(rctx context.Context) error {
  127. resp, err = rkv.KVClient.Put(rctx, in, opts...)
  128. return err
  129. })
  130. return resp, err
  131. }
  132. func (rkv *retryWriteKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
  133. err = rkv.retryf(ctx, func(rctx context.Context) error {
  134. resp, err = rkv.KVClient.DeleteRange(rctx, in, opts...)
  135. return err
  136. })
  137. return resp, err
  138. }
  139. func (rkv *retryWriteKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
  140. err = rkv.retryf(ctx, func(rctx context.Context) error {
  141. resp, err = rkv.KVClient.Txn(rctx, in, opts...)
  142. return err
  143. })
  144. return resp, err
  145. }
  146. func (rkv *retryWriteKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
  147. err = rkv.retryf(ctx, func(rctx context.Context) error {
  148. resp, err = rkv.KVClient.Compact(rctx, in, opts...)
  149. return err
  150. })
  151. return resp, err
  152. }
  153. type retryLeaseClient struct {
  154. pb.LeaseClient
  155. retryf retryRpcFunc
  156. }
  157. // RetryLeaseClient implements a LeaseClient that uses the client's FailFast retry policy.
  158. func RetryLeaseClient(c *Client) pb.LeaseClient {
  159. retry := &retryLeaseClient{
  160. pb.NewLeaseClient(c.conn),
  161. c.newRetryWrapper(isReadStopError),
  162. }
  163. return &retryLeaseClient{retry, c.newAuthRetryWrapper()}
  164. }
  165. func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
  166. err = rlc.retryf(ctx, func(rctx context.Context) error {
  167. resp, err = rlc.LeaseClient.LeaseGrant(rctx, in, opts...)
  168. return err
  169. })
  170. return resp, err
  171. }
  172. func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
  173. err = rlc.retryf(ctx, func(rctx context.Context) error {
  174. resp, err = rlc.LeaseClient.LeaseRevoke(rctx, in, opts...)
  175. return err
  176. })
  177. return resp, err
  178. }
  179. type retryClusterClient struct {
  180. pb.ClusterClient
  181. retryf retryRpcFunc
  182. }
  183. // RetryClusterClient implements a ClusterClient that uses the client's FailFast retry policy.
  184. func RetryClusterClient(c *Client) pb.ClusterClient {
  185. return &retryClusterClient{pb.NewClusterClient(c.conn), c.newRetryWrapper(isWriteStopError)}
  186. }
  187. func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
  188. err = rcc.retryf(ctx, func(rctx context.Context) error {
  189. resp, err = rcc.ClusterClient.MemberAdd(rctx, in, opts...)
  190. return err
  191. })
  192. return resp, err
  193. }
  194. func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
  195. err = rcc.retryf(ctx, func(rctx context.Context) error {
  196. resp, err = rcc.ClusterClient.MemberRemove(rctx, in, opts...)
  197. return err
  198. })
  199. return resp, err
  200. }
  201. func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
  202. err = rcc.retryf(ctx, func(rctx context.Context) error {
  203. resp, err = rcc.ClusterClient.MemberUpdate(rctx, in, opts...)
  204. return err
  205. })
  206. return resp, err
  207. }
  208. type retryAuthClient struct {
  209. pb.AuthClient
  210. retryf retryRpcFunc
  211. }
  212. // RetryAuthClient implements a AuthClient that uses the client's FailFast retry policy.
  213. func RetryAuthClient(c *Client) pb.AuthClient {
  214. return &retryAuthClient{pb.NewAuthClient(c.conn), c.newRetryWrapper(isWriteStopError)}
  215. }
  216. func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
  217. err = rac.retryf(ctx, func(rctx context.Context) error {
  218. resp, err = rac.AuthClient.AuthEnable(rctx, in, opts...)
  219. return err
  220. })
  221. return resp, err
  222. }
  223. func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
  224. err = rac.retryf(ctx, func(rctx context.Context) error {
  225. resp, err = rac.AuthClient.AuthDisable(rctx, in, opts...)
  226. return err
  227. })
  228. return resp, err
  229. }
  230. func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
  231. err = rac.retryf(ctx, func(rctx context.Context) error {
  232. resp, err = rac.AuthClient.UserAdd(rctx, in, opts...)
  233. return err
  234. })
  235. return resp, err
  236. }
  237. func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
  238. err = rac.retryf(ctx, func(rctx context.Context) error {
  239. resp, err = rac.AuthClient.UserDelete(rctx, in, opts...)
  240. return err
  241. })
  242. return resp, err
  243. }
  244. func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
  245. err = rac.retryf(ctx, func(rctx context.Context) error {
  246. resp, err = rac.AuthClient.UserChangePassword(rctx, in, opts...)
  247. return err
  248. })
  249. return resp, err
  250. }
  251. func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
  252. err = rac.retryf(ctx, func(rctx context.Context) error {
  253. resp, err = rac.AuthClient.UserGrantRole(rctx, in, opts...)
  254. return err
  255. })
  256. return resp, err
  257. }
  258. func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
  259. err = rac.retryf(ctx, func(rctx context.Context) error {
  260. resp, err = rac.AuthClient.UserRevokeRole(rctx, in, opts...)
  261. return err
  262. })
  263. return resp, err
  264. }
  265. func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
  266. err = rac.retryf(ctx, func(rctx context.Context) error {
  267. resp, err = rac.AuthClient.RoleAdd(rctx, in, opts...)
  268. return err
  269. })
  270. return resp, err
  271. }
  272. func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
  273. err = rac.retryf(ctx, func(rctx context.Context) error {
  274. resp, err = rac.AuthClient.RoleDelete(rctx, in, opts...)
  275. return err
  276. })
  277. return resp, err
  278. }
  279. func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
  280. err = rac.retryf(ctx, func(rctx context.Context) error {
  281. resp, err = rac.AuthClient.RoleGrantPermission(rctx, in, opts...)
  282. return err
  283. })
  284. return resp, err
  285. }
  286. func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
  287. err = rac.retryf(ctx, func(rctx context.Context) error {
  288. resp, err = rac.AuthClient.RoleRevokePermission(rctx, in, opts...)
  289. return err
  290. })
  291. return resp, err
  292. }