retry.go 10 KB

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