retry.go 11 KB

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