retry.go 10 KB

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