retry.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  18. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  19. "google.golang.org/grpc"
  20. "google.golang.org/grpc/codes"
  21. "google.golang.org/grpc/status"
  22. )
  23. type retryPolicy uint8
  24. const (
  25. repeatable retryPolicy = iota
  26. nonRepeatable
  27. )
  28. func (rp retryPolicy) String() string {
  29. switch rp {
  30. case repeatable:
  31. return "repeatable"
  32. case nonRepeatable:
  33. return "nonRepeatable"
  34. default:
  35. return "UNKNOWN"
  36. }
  37. }
  38. // isSafeRetryImmutableRPC returns "true" when an immutable request is safe for retry.
  39. //
  40. // immutable requests (e.g. Get) should be retried unless it's
  41. // an obvious server-side error (e.g. rpctypes.ErrRequestTooLarge).
  42. //
  43. // Returning "false" means retry should stop, since client cannot
  44. // handle itself even with retries.
  45. func isSafeRetryImmutableRPC(err error) bool {
  46. eErr := rpctypes.Error(err)
  47. if serverErr, ok := eErr.(rpctypes.EtcdError); ok && serverErr.Code() != codes.Unavailable {
  48. // interrupted by non-transient server-side or gRPC-side error
  49. // client cannot handle itself (e.g. rpctypes.ErrCompacted)
  50. return false
  51. }
  52. // only retry if unavailable
  53. ev, ok := status.FromError(err)
  54. if !ok {
  55. // all errors from RPC is typed "grpc/status.(*statusError)"
  56. // (ref. https://github.com/grpc/grpc-go/pull/1782)
  57. //
  58. // if the error type is not "grpc/status.(*statusError)",
  59. // it could be from "Dial"
  60. // TODO: do not retry for now
  61. // ref. https://github.com/grpc/grpc-go/issues/1581
  62. return false
  63. }
  64. return ev.Code() == codes.Unavailable
  65. }
  66. // isSafeRetryMutableRPC returns "true" when a mutable request is safe for retry.
  67. //
  68. // mutable requests (e.g. Put, Delete, Txn) should only be retried
  69. // when the status code is codes.Unavailable when initial connection
  70. // has not been established (no endpoint is up).
  71. //
  72. // Returning "false" means retry should stop, otherwise it violates
  73. // write-at-most-once semantics.
  74. func isSafeRetryMutableRPC(err error) bool {
  75. if ev, ok := status.FromError(err); ok && ev.Code() != codes.Unavailable {
  76. // not safe for mutable RPCs
  77. // e.g. interrupted by non-transient error that client cannot handle itself,
  78. // or transient error while the connection has already been established
  79. return false
  80. }
  81. desc := rpctypes.ErrorDesc(err)
  82. return desc == "there is no address available" || desc == "there is no connection available"
  83. }
  84. type retryKVClient struct {
  85. kc pb.KVClient
  86. }
  87. // RetryKVClient implements a KVClient.
  88. func RetryKVClient(c *Client) pb.KVClient {
  89. return &retryKVClient{
  90. kc: pb.NewKVClient(c.conn),
  91. }
  92. }
  93. func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
  94. return rkv.kc.Range(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  95. }
  96. func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
  97. return rkv.kc.Put(ctx, in, opts...)
  98. }
  99. func (rkv *retryKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
  100. return rkv.kc.DeleteRange(ctx, in, opts...)
  101. }
  102. func (rkv *retryKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
  103. return rkv.kc.Txn(ctx, in, opts...)
  104. }
  105. func (rkv *retryKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
  106. return rkv.kc.Compact(ctx, in, opts...)
  107. }
  108. type retryLeaseClient struct {
  109. lc pb.LeaseClient
  110. }
  111. // RetryLeaseClient implements a LeaseClient.
  112. func RetryLeaseClient(c *Client) pb.LeaseClient {
  113. return &retryLeaseClient{
  114. lc: pb.NewLeaseClient(c.conn),
  115. }
  116. }
  117. func (rlc *retryLeaseClient) LeaseTimeToLive(ctx context.Context, in *pb.LeaseTimeToLiveRequest, opts ...grpc.CallOption) (resp *pb.LeaseTimeToLiveResponse, err error) {
  118. return rlc.lc.LeaseTimeToLive(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  119. }
  120. func (rlc *retryLeaseClient) LeaseLeases(ctx context.Context, in *pb.LeaseLeasesRequest, opts ...grpc.CallOption) (resp *pb.LeaseLeasesResponse, err error) {
  121. return rlc.lc.LeaseLeases(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  122. }
  123. func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
  124. return rlc.lc.LeaseGrant(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  125. }
  126. func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
  127. return rlc.lc.LeaseRevoke(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  128. }
  129. func (rlc *retryLeaseClient) LeaseKeepAlive(ctx context.Context, opts ...grpc.CallOption) (stream pb.Lease_LeaseKeepAliveClient, err error) {
  130. return rlc.lc.LeaseKeepAlive(ctx, append(opts, withRetryPolicy(repeatable))...)
  131. }
  132. type retryClusterClient struct {
  133. cc pb.ClusterClient
  134. }
  135. // RetryClusterClient implements a ClusterClient.
  136. func RetryClusterClient(c *Client) pb.ClusterClient {
  137. return &retryClusterClient{
  138. cc: pb.NewClusterClient(c.conn),
  139. }
  140. }
  141. func (rcc *retryClusterClient) MemberList(ctx context.Context, in *pb.MemberListRequest, opts ...grpc.CallOption) (resp *pb.MemberListResponse, err error) {
  142. return rcc.cc.MemberList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  143. }
  144. func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
  145. return rcc.cc.MemberAdd(ctx, in, opts...)
  146. }
  147. func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
  148. return rcc.cc.MemberRemove(ctx, in, opts...)
  149. }
  150. func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
  151. return rcc.cc.MemberUpdate(ctx, in, opts...)
  152. }
  153. func (rcc *retryClusterClient) MemberPromote(ctx context.Context, in *pb.MemberPromoteRequest, opts ...grpc.CallOption) (resp *pb.MemberPromoteResponse, err error) {
  154. return rcc.cc.MemberPromote(ctx, in, opts...)
  155. }
  156. type retryMaintenanceClient struct {
  157. mc pb.MaintenanceClient
  158. }
  159. // RetryMaintenanceClient implements a Maintenance.
  160. func RetryMaintenanceClient(c *Client, conn *grpc.ClientConn) pb.MaintenanceClient {
  161. return &retryMaintenanceClient{
  162. mc: pb.NewMaintenanceClient(conn),
  163. }
  164. }
  165. func (rmc *retryMaintenanceClient) Alarm(ctx context.Context, in *pb.AlarmRequest, opts ...grpc.CallOption) (resp *pb.AlarmResponse, err error) {
  166. return rmc.mc.Alarm(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  167. }
  168. func (rmc *retryMaintenanceClient) Status(ctx context.Context, in *pb.StatusRequest, opts ...grpc.CallOption) (resp *pb.StatusResponse, err error) {
  169. return rmc.mc.Status(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  170. }
  171. func (rmc *retryMaintenanceClient) Hash(ctx context.Context, in *pb.HashRequest, opts ...grpc.CallOption) (resp *pb.HashResponse, err error) {
  172. return rmc.mc.Hash(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  173. }
  174. func (rmc *retryMaintenanceClient) HashKV(ctx context.Context, in *pb.HashKVRequest, opts ...grpc.CallOption) (resp *pb.HashKVResponse, err error) {
  175. return rmc.mc.HashKV(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  176. }
  177. func (rmc *retryMaintenanceClient) Snapshot(ctx context.Context, in *pb.SnapshotRequest, opts ...grpc.CallOption) (stream pb.Maintenance_SnapshotClient, err error) {
  178. return rmc.mc.Snapshot(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  179. }
  180. func (rmc *retryMaintenanceClient) MoveLeader(ctx context.Context, in *pb.MoveLeaderRequest, opts ...grpc.CallOption) (resp *pb.MoveLeaderResponse, err error) {
  181. return rmc.mc.MoveLeader(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  182. }
  183. func (rmc *retryMaintenanceClient) Defragment(ctx context.Context, in *pb.DefragmentRequest, opts ...grpc.CallOption) (resp *pb.DefragmentResponse, err error) {
  184. return rmc.mc.Defragment(ctx, in, opts...)
  185. }
  186. type retryAuthClient struct {
  187. ac pb.AuthClient
  188. }
  189. // RetryAuthClient implements a AuthClient.
  190. func RetryAuthClient(c *Client) pb.AuthClient {
  191. return &retryAuthClient{
  192. ac: pb.NewAuthClient(c.conn),
  193. }
  194. }
  195. func (rac *retryAuthClient) UserList(ctx context.Context, in *pb.AuthUserListRequest, opts ...grpc.CallOption) (resp *pb.AuthUserListResponse, err error) {
  196. return rac.ac.UserList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  197. }
  198. func (rac *retryAuthClient) UserGet(ctx context.Context, in *pb.AuthUserGetRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGetResponse, err error) {
  199. return rac.ac.UserGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  200. }
  201. func (rac *retryAuthClient) RoleGet(ctx context.Context, in *pb.AuthRoleGetRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGetResponse, err error) {
  202. return rac.ac.RoleGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  203. }
  204. func (rac *retryAuthClient) RoleList(ctx context.Context, in *pb.AuthRoleListRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleListResponse, err error) {
  205. return rac.ac.RoleList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
  206. }
  207. func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
  208. return rac.ac.AuthEnable(ctx, in, opts...)
  209. }
  210. func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
  211. return rac.ac.AuthDisable(ctx, in, opts...)
  212. }
  213. func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
  214. return rac.ac.UserAdd(ctx, in, opts...)
  215. }
  216. func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
  217. return rac.ac.UserDelete(ctx, in, opts...)
  218. }
  219. func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
  220. return rac.ac.UserChangePassword(ctx, in, opts...)
  221. }
  222. func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
  223. return rac.ac.UserGrantRole(ctx, in, opts...)
  224. }
  225. func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
  226. return rac.ac.UserRevokeRole(ctx, in, opts...)
  227. }
  228. func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
  229. return rac.ac.RoleAdd(ctx, in, opts...)
  230. }
  231. func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
  232. return rac.ac.RoleDelete(ctx, in, opts...)
  233. }
  234. func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
  235. return rac.ac.RoleGrantPermission(ctx, in, opts...)
  236. }
  237. func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
  238. return rac.ac.RoleRevokePermission(ctx, in, opts...)
  239. }
  240. func (rac *retryAuthClient) Authenticate(ctx context.Context, in *pb.AuthenticateRequest, opts ...grpc.CallOption) (resp *pb.AuthenticateResponse, err error) {
  241. return rac.ac.Authenticate(ctx, in, opts...)
  242. }