blob: 38ad00ac9a025e85147b56aeae7647adf175e6d6 [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
Stephane Barbarie260a5632019-02-26 16:12:49 -050020 "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
21 pb "go.etcd.io/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
khenaidooac637102019-01-14 15:44:34 -050046type rpcFunc func(ctx context.Context) error
47type retryRPCFunc func(context.Context, rpcFunc, retryPolicy) error
48type retryStopErrFunc func(error) bool
49
Stephane Barbarie260a5632019-02-26 16:12:49 -050050// isSafeRetryImmutableRPC returns "true" when an immutable request is safe for retry.
51//
khenaidooac637102019-01-14 15:44:34 -050052// immutable requests (e.g. Get) should be retried unless it's
53// an obvious server-side error (e.g. rpctypes.ErrRequestTooLarge).
54//
Stephane Barbarie260a5632019-02-26 16:12:49 -050055// Returning "false" means retry should stop, since client cannot
khenaidooac637102019-01-14 15:44:34 -050056// handle itself even with retries.
Stephane Barbarie260a5632019-02-26 16:12:49 -050057func isSafeRetryImmutableRPC(err error) bool {
khenaidooac637102019-01-14 15:44:34 -050058 eErr := rpctypes.Error(err)
khenaidooac637102019-01-14 15:44:34 -050059 if serverErr, ok := eErr.(rpctypes.EtcdError); ok && serverErr.Code() != codes.Unavailable {
Stephane Barbarie260a5632019-02-26 16:12:49 -050060 // interrupted by non-transient server-side or gRPC-side error
61 // client cannot handle itself (e.g. rpctypes.ErrCompacted)
62 return false
khenaidooac637102019-01-14 15:44:34 -050063 }
64 // only retry if unavailable
Stephane Barbarie260a5632019-02-26 16:12:49 -050065 ev, ok := status.FromError(err)
66 if !ok {
67 // all errors from RPC is typed "grpc/status.(*statusError)"
68 // (ref. https://github.com/grpc/grpc-go/pull/1782)
69 //
70 // if the error type is not "grpc/status.(*statusError)",
71 // it could be from "Dial"
72 // TODO: do not retry for now
73 // ref. https://github.com/grpc/grpc-go/issues/1581
74 return false
75 }
76 return ev.Code() == codes.Unavailable
khenaidooac637102019-01-14 15:44:34 -050077}
78
Stephane Barbarie260a5632019-02-26 16:12:49 -050079// isSafeRetryMutableRPC returns "true" when a mutable request is safe for retry.
80//
khenaidooac637102019-01-14 15:44:34 -050081// mutable requests (e.g. Put, Delete, Txn) should only be retried
82// when the status code is codes.Unavailable when initial connection
Stephane Barbarie260a5632019-02-26 16:12:49 -050083// has not been established (no endpoint is up).
khenaidooac637102019-01-14 15:44:34 -050084//
Stephane Barbarie260a5632019-02-26 16:12:49 -050085// Returning "false" means retry should stop, otherwise it violates
khenaidooac637102019-01-14 15:44:34 -050086// write-at-most-once semantics.
Stephane Barbarie260a5632019-02-26 16:12:49 -050087func isSafeRetryMutableRPC(err error) bool {
88 if ev, ok := status.FromError(err); ok && ev.Code() != codes.Unavailable {
89 // not safe for mutable RPCs
90 // e.g. interrupted by non-transient error that client cannot handle itself,
91 // or transient error while the connection has already been established
92 return false
khenaidooac637102019-01-14 15:44:34 -050093 }
94 desc := rpctypes.ErrorDesc(err)
Stephane Barbarie260a5632019-02-26 16:12:49 -050095 return desc == "there is no address available" || desc == "there is no connection available"
khenaidooac637102019-01-14 15:44:34 -050096}
97
98type retryKVClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -050099 kc pb.KVClient
khenaidooac637102019-01-14 15:44:34 -0500100}
101
102// RetryKVClient implements a KVClient.
103func RetryKVClient(c *Client) pb.KVClient {
104 return &retryKVClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500105 kc: pb.NewKVClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500106 }
107}
108func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500109 return rkv.kc.Range(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500110}
111
112func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500113 return rkv.kc.Put(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500114}
115
116func (rkv *retryKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500117 return rkv.kc.DeleteRange(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500118}
119
120func (rkv *retryKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500121 return rkv.kc.Txn(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500122}
123
124func (rkv *retryKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500125 return rkv.kc.Compact(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500126}
127
128type retryLeaseClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500129 lc pb.LeaseClient
khenaidooac637102019-01-14 15:44:34 -0500130}
131
132// RetryLeaseClient implements a LeaseClient.
133func RetryLeaseClient(c *Client) pb.LeaseClient {
134 return &retryLeaseClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500135 lc: pb.NewLeaseClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500136 }
137}
138
139func (rlc *retryLeaseClient) LeaseTimeToLive(ctx context.Context, in *pb.LeaseTimeToLiveRequest, opts ...grpc.CallOption) (resp *pb.LeaseTimeToLiveResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500140 return rlc.lc.LeaseTimeToLive(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500141}
142
143func (rlc *retryLeaseClient) LeaseLeases(ctx context.Context, in *pb.LeaseLeasesRequest, opts ...grpc.CallOption) (resp *pb.LeaseLeasesResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500144 return rlc.lc.LeaseLeases(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500145}
146
147func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500148 return rlc.lc.LeaseGrant(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500149}
150
151func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500152 return rlc.lc.LeaseRevoke(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500153}
154
155func (rlc *retryLeaseClient) LeaseKeepAlive(ctx context.Context, opts ...grpc.CallOption) (stream pb.Lease_LeaseKeepAliveClient, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500156 return rlc.lc.LeaseKeepAlive(ctx, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500157}
158
159type retryClusterClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500160 cc pb.ClusterClient
khenaidooac637102019-01-14 15:44:34 -0500161}
162
163// RetryClusterClient implements a ClusterClient.
164func RetryClusterClient(c *Client) pb.ClusterClient {
165 return &retryClusterClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500166 cc: pb.NewClusterClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500167 }
168}
169
170func (rcc *retryClusterClient) MemberList(ctx context.Context, in *pb.MemberListRequest, opts ...grpc.CallOption) (resp *pb.MemberListResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500171 return rcc.cc.MemberList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500172}
173
174func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500175 return rcc.cc.MemberAdd(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500176}
177
178func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500179 return rcc.cc.MemberRemove(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500180}
181
182func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500183 return rcc.cc.MemberUpdate(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500184}
185
186type retryMaintenanceClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500187 mc pb.MaintenanceClient
khenaidooac637102019-01-14 15:44:34 -0500188}
189
190// RetryMaintenanceClient implements a Maintenance.
191func RetryMaintenanceClient(c *Client, conn *grpc.ClientConn) pb.MaintenanceClient {
192 return &retryMaintenanceClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500193 mc: pb.NewMaintenanceClient(conn),
khenaidooac637102019-01-14 15:44:34 -0500194 }
195}
196
197func (rmc *retryMaintenanceClient) Alarm(ctx context.Context, in *pb.AlarmRequest, opts ...grpc.CallOption) (resp *pb.AlarmResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500198 return rmc.mc.Alarm(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500199}
200
201func (rmc *retryMaintenanceClient) Status(ctx context.Context, in *pb.StatusRequest, opts ...grpc.CallOption) (resp *pb.StatusResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500202 return rmc.mc.Status(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500203}
204
205func (rmc *retryMaintenanceClient) Hash(ctx context.Context, in *pb.HashRequest, opts ...grpc.CallOption) (resp *pb.HashResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500206 return rmc.mc.Hash(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500207}
208
209func (rmc *retryMaintenanceClient) HashKV(ctx context.Context, in *pb.HashKVRequest, opts ...grpc.CallOption) (resp *pb.HashKVResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500210 return rmc.mc.HashKV(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500211}
212
213func (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 -0500214 return rmc.mc.Snapshot(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500215}
216
217func (rmc *retryMaintenanceClient) MoveLeader(ctx context.Context, in *pb.MoveLeaderRequest, opts ...grpc.CallOption) (resp *pb.MoveLeaderResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500218 return rmc.mc.MoveLeader(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500219}
220
221func (rmc *retryMaintenanceClient) Defragment(ctx context.Context, in *pb.DefragmentRequest, opts ...grpc.CallOption) (resp *pb.DefragmentResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500222 return rmc.mc.Defragment(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500223}
224
225type retryAuthClient struct {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500226 ac pb.AuthClient
khenaidooac637102019-01-14 15:44:34 -0500227}
228
229// RetryAuthClient implements a AuthClient.
230func RetryAuthClient(c *Client) pb.AuthClient {
231 return &retryAuthClient{
Stephane Barbarie260a5632019-02-26 16:12:49 -0500232 ac: pb.NewAuthClient(c.conn),
khenaidooac637102019-01-14 15:44:34 -0500233 }
234}
235
236func (rac *retryAuthClient) UserList(ctx context.Context, in *pb.AuthUserListRequest, opts ...grpc.CallOption) (resp *pb.AuthUserListResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500237 return rac.ac.UserList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500238}
239
240func (rac *retryAuthClient) UserGet(ctx context.Context, in *pb.AuthUserGetRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGetResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500241 return rac.ac.UserGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500242}
243
244func (rac *retryAuthClient) RoleGet(ctx context.Context, in *pb.AuthRoleGetRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGetResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500245 return rac.ac.RoleGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500246}
247
248func (rac *retryAuthClient) RoleList(ctx context.Context, in *pb.AuthRoleListRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleListResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500249 return rac.ac.RoleList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
khenaidooac637102019-01-14 15:44:34 -0500250}
251
252func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500253 return rac.ac.AuthEnable(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500254}
255
256func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500257 return rac.ac.AuthDisable(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500258}
259
260func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500261 return rac.ac.UserAdd(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500262}
263
264func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500265 return rac.ac.UserDelete(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500266}
267
268func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500269 return rac.ac.UserChangePassword(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500270}
271
272func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500273 return rac.ac.UserGrantRole(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500274}
275
276func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500277 return rac.ac.UserRevokeRole(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500278}
279
280func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500281 return rac.ac.RoleAdd(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500282}
283
284func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500285 return rac.ac.RoleDelete(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500286}
287
288func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500289 return rac.ac.RoleGrantPermission(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500290}
291
292func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500293 return rac.ac.RoleRevokePermission(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500294}
295
296func (rac *retryAuthClient) Authenticate(ctx context.Context, in *pb.AuthenticateRequest, opts ...grpc.CallOption) (resp *pb.AuthenticateResponse, err error) {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500297 return rac.ac.Authenticate(ctx, in, opts...)
khenaidooac637102019-01-14 15:44:34 -0500298}