blob: 6baa52e14a5bcc8003965ea4f3066bd6f0dcf2ea [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// 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
15package clientv3
16
17import (
18 "context"
19
khenaidood948f772021-08-11 17:49:24 -040020 "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
21 pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
khenaidooac637102019-01-14 15:44:34 -050022
23 "google.golang.org/grpc"
24 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
26)
27
28type retryPolicy uint8
29
30const (
31 repeatable retryPolicy = iota
32 nonRepeatable
33)
34
Stephane Barbarie260a5632019-02-26 16:12:49 -050035func (rp retryPolicy) String() string {
36 switch rp {
37 case repeatable:
38 return "repeatable"
39 case nonRepeatable:
40 return "nonRepeatable"
41 default:
42 return "UNKNOWN"
43 }
44}
45
Stephane Barbarie260a5632019-02-26 16:12:49 -050046// isSafeRetryImmutableRPC returns "true" when an immutable request is safe for retry.
47//
khenaidooac637102019-01-14 15:44:34 -050048// immutable requests (e.g. Get) should be retried unless it's
49// an obvious server-side error (e.g. rpctypes.ErrRequestTooLarge).
50//
Stephane Barbarie260a5632019-02-26 16:12:49 -050051// Returning "false" means retry should stop, since client cannot
khenaidooac637102019-01-14 15:44:34 -050052// handle itself even with retries.
Stephane Barbarie260a5632019-02-26 16:12:49 -050053func isSafeRetryImmutableRPC(err error) bool {
khenaidooac637102019-01-14 15:44:34 -050054 eErr := rpctypes.Error(err)
khenaidooac637102019-01-14 15:44:34 -050055 if serverErr, ok := eErr.(rpctypes.EtcdError); ok && serverErr.Code() != codes.Unavailable {
Stephane Barbarie260a5632019-02-26 16:12:49 -050056 // interrupted by non-transient server-side or gRPC-side error
57 // client cannot handle itself (e.g. rpctypes.ErrCompacted)
58 return false
khenaidooac637102019-01-14 15:44:34 -050059 }
60 // only retry if unavailable
Stephane Barbarie260a5632019-02-26 16:12:49 -050061 ev, ok := status.FromError(err)
62 if !ok {
63 // all errors from RPC is typed "grpc/status.(*statusError)"
64 // (ref. https://github.com/grpc/grpc-go/pull/1782)
65 //
66 // if the error type is not "grpc/status.(*statusError)",
67 // it could be from "Dial"
68 // TODO: do not retry for now
69 // ref. https://github.com/grpc/grpc-go/issues/1581
70 return false
71 }
72 return ev.Code() == codes.Unavailable
khenaidooac637102019-01-14 15:44:34 -050073}
74
Stephane Barbarie260a5632019-02-26 16:12:49 -050075// isSafeRetryMutableRPC returns "true" when a mutable request is safe for retry.
76//
khenaidooac637102019-01-14 15:44:34 -050077// mutable requests (e.g. Put, Delete, Txn) should only be retried
78// when the status code is codes.Unavailable when initial connection
Stephane Barbarie260a5632019-02-26 16:12:49 -050079// has not been established (no endpoint is up).
khenaidooac637102019-01-14 15:44:34 -050080//
Stephane Barbarie260a5632019-02-26 16:12:49 -050081// Returning "false" means retry should stop, otherwise it violates
khenaidooac637102019-01-14 15:44:34 -050082// write-at-most-once semantics.
Stephane Barbarie260a5632019-02-26 16:12:49 -050083func isSafeRetryMutableRPC(err error) bool {
84 if ev, ok := status.FromError(err); ok && ev.Code() != codes.Unavailable {
85 // not safe for mutable RPCs
86 // e.g. interrupted by non-transient error that client cannot handle itself,
87 // or transient error while the connection has already been established
88 return false
khenaidooac637102019-01-14 15:44:34 -050089 }
90 desc := rpctypes.ErrorDesc(err)
Stephane Barbarie260a5632019-02-26 16:12:49 -050091 return desc == "there is no address available" || desc == "there is no connection available"
khenaidooac637102019-01-14 15:44:34 -050092}
93
94type retryKVClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -050095 kc pb.KVClient
khenaidooac637102019-01-14 15:44:34 -050096}
97
98// RetryKVClient implements a KVClient.
99func RetryKVClient(c *Client) pb.KVClient {
100 return &retryKVClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500101 kc: pb.NewKVClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500102 }
103}
104func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500105 return rkv.kc.Range(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500106}
107
108func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500109 return rkv.kc.Put(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500110}
111
112func (rkv *retryKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500113 return rkv.kc.DeleteRange(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500114}
115
116func (rkv *retryKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500117 return rkv.kc.Txn(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500118}
119
120func (rkv *retryKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500121 return rkv.kc.Compact(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500122}
123
124type retryLeaseClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500125 lc pb.LeaseClient
khenaidooac637102019-01-14 15:44:34 -0500126}
127
128// RetryLeaseClient implements a LeaseClient.
129func RetryLeaseClient(c *Client) pb.LeaseClient {
130 return &retryLeaseClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500131 lc: pb.NewLeaseClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500132 }
133}
134
135func (rlc *retryLeaseClient) LeaseTimeToLive(ctx context.Context, in *pb.LeaseTimeToLiveRequest, opts ...grpc.CallOption) (resp *pb.LeaseTimeToLiveResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500136 return rlc.lc.LeaseTimeToLive(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500137}
138
139func (rlc *retryLeaseClient) LeaseLeases(ctx context.Context, in *pb.LeaseLeasesRequest, opts ...grpc.CallOption) (resp *pb.LeaseLeasesResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500140 return rlc.lc.LeaseLeases(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500141}
142
143func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500144 return rlc.lc.LeaseGrant(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500145}
146
147func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500148 return rlc.lc.LeaseRevoke(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500149}
150
151func (rlc *retryLeaseClient) LeaseKeepAlive(ctx context.Context, opts ...grpc.CallOption) (stream pb.Lease_LeaseKeepAliveClient, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500152 return rlc.lc.LeaseKeepAlive(ctx, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500153}
154
155type retryClusterClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500156 cc pb.ClusterClient
khenaidooac637102019-01-14 15:44:34 -0500157}
158
159// RetryClusterClient implements a ClusterClient.
160func RetryClusterClient(c *Client) pb.ClusterClient {
161 return &retryClusterClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500162 cc: pb.NewClusterClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500163 }
164}
165
166func (rcc *retryClusterClient) MemberList(ctx context.Context, in *pb.MemberListRequest, opts ...grpc.CallOption) (resp *pb.MemberListResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500167 return rcc.cc.MemberList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500168}
169
170func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500171 return rcc.cc.MemberAdd(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500172}
173
174func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500175 return rcc.cc.MemberRemove(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500176}
177
178func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500179 return rcc.cc.MemberUpdate(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500180}
181
182type retryMaintenanceClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500183 mc pb.MaintenanceClient
khenaidooac637102019-01-14 15:44:34 -0500184}
185
186// RetryMaintenanceClient implements a Maintenance.
187func RetryMaintenanceClient(c *Client, conn *grpc.ClientConn) pb.MaintenanceClient {
188 return &retryMaintenanceClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500189 mc: pb.NewMaintenanceClient(conn),
khenaidooac637102019-01-14 15:44:34 -0500190 }
191}
192
193func (rmc *retryMaintenanceClient) Alarm(ctx context.Context, in *pb.AlarmRequest, opts ...grpc.CallOption) (resp *pb.AlarmResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500194 return rmc.mc.Alarm(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500195}
196
197func (rmc *retryMaintenanceClient) Status(ctx context.Context, in *pb.StatusRequest, opts ...grpc.CallOption) (resp *pb.StatusResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500198 return rmc.mc.Status(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500199}
200
201func (rmc *retryMaintenanceClient) Hash(ctx context.Context, in *pb.HashRequest, opts ...grpc.CallOption) (resp *pb.HashResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500202 return rmc.mc.Hash(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500203}
204
205func (rmc *retryMaintenanceClient) HashKV(ctx context.Context, in *pb.HashKVRequest, opts ...grpc.CallOption) (resp *pb.HashKVResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500206 return rmc.mc.HashKV(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500207}
208
209func (rmc *retryMaintenanceClient) Snapshot(ctx context.Context, in *pb.SnapshotRequest, opts ...grpc.CallOption) (stream pb.Maintenance_SnapshotClient, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500210 return rmc.mc.Snapshot(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500211}
212
213func (rmc *retryMaintenanceClient) MoveLeader(ctx context.Context, in *pb.MoveLeaderRequest, opts ...grpc.CallOption) (resp *pb.MoveLeaderResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500214 return rmc.mc.MoveLeader(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500215}
216
217func (rmc *retryMaintenanceClient) Defragment(ctx context.Context, in *pb.DefragmentRequest, opts ...grpc.CallOption) (resp *pb.DefragmentResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500218 return rmc.mc.Defragment(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500219}
220
221type retryAuthClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500222 ac pb.AuthClient
khenaidooac637102019-01-14 15:44:34 -0500223}
224
225// RetryAuthClient implements a AuthClient.
226func RetryAuthClient(c *Client) pb.AuthClient {
227 return &retryAuthClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500228 ac: pb.NewAuthClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500229 }
230}
231
232func (rac *retryAuthClient) UserList(ctx context.Context, in *pb.AuthUserListRequest, opts ...grpc.CallOption) (resp *pb.AuthUserListResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500233 return rac.ac.UserList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500234}
235
236func (rac *retryAuthClient) UserGet(ctx context.Context, in *pb.AuthUserGetRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGetResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500237 return rac.ac.UserGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500238}
239
240func (rac *retryAuthClient) RoleGet(ctx context.Context, in *pb.AuthRoleGetRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGetResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500241 return rac.ac.RoleGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500242}
243
244func (rac *retryAuthClient) RoleList(ctx context.Context, in *pb.AuthRoleListRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleListResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500245 return rac.ac.RoleList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500246}
247
248func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500249 return rac.ac.AuthEnable(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500250}
251
252func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500253 return rac.ac.AuthDisable(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500254}
255
256func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500257 return rac.ac.UserAdd(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500258}
259
260func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500261 return rac.ac.UserDelete(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500262}
263
264func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500265 return rac.ac.UserChangePassword(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500266}
267
268func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500269 return rac.ac.UserGrantRole(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500270}
271
272func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500273 return rac.ac.UserRevokeRole(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500274}
275
276func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500277 return rac.ac.RoleAdd(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500278}
279
280func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500281 return rac.ac.RoleDelete(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500282}
283
284func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500285 return rac.ac.RoleGrantPermission(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500286}
287
288func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500289 return rac.ac.RoleRevokePermission(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500290}
291
292func (rac *retryAuthClient) Authenticate(ctx context.Context, in *pb.AuthenticateRequest, opts ...grpc.CallOption) (resp *pb.AuthenticateResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500293 return rac.ac.Authenticate(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500294}