blob: 38ad00ac9a025e85147b56aeae7647adf175e6d6 [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -07001// 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
20 "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
21 pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
22
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
35func (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
46type rpcFunc func(ctx context.Context) error
47type retryRPCFunc func(context.Context, rpcFunc, retryPolicy) error
48type retryStopErrFunc func(error) bool
49
50// isSafeRetryImmutableRPC returns "true" when an immutable request is safe for retry.
51//
52// immutable requests (e.g. Get) should be retried unless it's
53// an obvious server-side error (e.g. rpctypes.ErrRequestTooLarge).
54//
55// Returning "false" means retry should stop, since client cannot
56// handle itself even with retries.
57func isSafeRetryImmutableRPC(err error) bool {
58 eErr := rpctypes.Error(err)
59 if serverErr, ok := eErr.(rpctypes.EtcdError); ok && serverErr.Code() != codes.Unavailable {
60 // interrupted by non-transient server-side or gRPC-side error
61 // client cannot handle itself (e.g. rpctypes.ErrCompacted)
62 return false
63 }
64 // only retry if unavailable
65 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
77}
78
79// isSafeRetryMutableRPC returns "true" when a mutable request is safe for retry.
80//
81// mutable requests (e.g. Put, Delete, Txn) should only be retried
82// when the status code is codes.Unavailable when initial connection
83// has not been established (no endpoint is up).
84//
85// Returning "false" means retry should stop, otherwise it violates
86// write-at-most-once semantics.
87func 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
93 }
94 desc := rpctypes.ErrorDesc(err)
95 return desc == "there is no address available" || desc == "there is no connection available"
96}
97
98type retryKVClient struct {
99 kc pb.KVClient
100}
101
102// RetryKVClient implements a KVClient.
103func RetryKVClient(c *Client) pb.KVClient {
104 return &retryKVClient{
105 kc: pb.NewKVClient(c.conn),
106 }
107}
108func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
109 return rkv.kc.Range(ctx, in, append(opts, withRetryPolicy(repeatable))...)
110}
111
112func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
113 return rkv.kc.Put(ctx, in, opts...)
114}
115
116func (rkv *retryKVClient) DeleteRange(ctx context.Context, in *pb.DeleteRangeRequest, opts ...grpc.CallOption) (resp *pb.DeleteRangeResponse, err error) {
117 return rkv.kc.DeleteRange(ctx, in, opts...)
118}
119
120func (rkv *retryKVClient) Txn(ctx context.Context, in *pb.TxnRequest, opts ...grpc.CallOption) (resp *pb.TxnResponse, err error) {
121 return rkv.kc.Txn(ctx, in, opts...)
122}
123
124func (rkv *retryKVClient) Compact(ctx context.Context, in *pb.CompactionRequest, opts ...grpc.CallOption) (resp *pb.CompactionResponse, err error) {
125 return rkv.kc.Compact(ctx, in, opts...)
126}
127
128type retryLeaseClient struct {
129 lc pb.LeaseClient
130}
131
132// RetryLeaseClient implements a LeaseClient.
133func RetryLeaseClient(c *Client) pb.LeaseClient {
134 return &retryLeaseClient{
135 lc: pb.NewLeaseClient(c.conn),
136 }
137}
138
139func (rlc *retryLeaseClient) LeaseTimeToLive(ctx context.Context, in *pb.LeaseTimeToLiveRequest, opts ...grpc.CallOption) (resp *pb.LeaseTimeToLiveResponse, err error) {
140 return rlc.lc.LeaseTimeToLive(ctx, in, append(opts, withRetryPolicy(repeatable))...)
141}
142
143func (rlc *retryLeaseClient) LeaseLeases(ctx context.Context, in *pb.LeaseLeasesRequest, opts ...grpc.CallOption) (resp *pb.LeaseLeasesResponse, err error) {
144 return rlc.lc.LeaseLeases(ctx, in, append(opts, withRetryPolicy(repeatable))...)
145}
146
147func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
148 return rlc.lc.LeaseGrant(ctx, in, append(opts, withRetryPolicy(repeatable))...)
149}
150
151func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
152 return rlc.lc.LeaseRevoke(ctx, in, append(opts, withRetryPolicy(repeatable))...)
153}
154
155func (rlc *retryLeaseClient) LeaseKeepAlive(ctx context.Context, opts ...grpc.CallOption) (stream pb.Lease_LeaseKeepAliveClient, err error) {
156 return rlc.lc.LeaseKeepAlive(ctx, append(opts, withRetryPolicy(repeatable))...)
157}
158
159type retryClusterClient struct {
160 cc pb.ClusterClient
161}
162
163// RetryClusterClient implements a ClusterClient.
164func RetryClusterClient(c *Client) pb.ClusterClient {
165 return &retryClusterClient{
166 cc: pb.NewClusterClient(c.conn),
167 }
168}
169
170func (rcc *retryClusterClient) MemberList(ctx context.Context, in *pb.MemberListRequest, opts ...grpc.CallOption) (resp *pb.MemberListResponse, err error) {
171 return rcc.cc.MemberList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
172}
173
174func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
175 return rcc.cc.MemberAdd(ctx, in, opts...)
176}
177
178func (rcc *retryClusterClient) MemberRemove(ctx context.Context, in *pb.MemberRemoveRequest, opts ...grpc.CallOption) (resp *pb.MemberRemoveResponse, err error) {
179 return rcc.cc.MemberRemove(ctx, in, opts...)
180}
181
182func (rcc *retryClusterClient) MemberUpdate(ctx context.Context, in *pb.MemberUpdateRequest, opts ...grpc.CallOption) (resp *pb.MemberUpdateResponse, err error) {
183 return rcc.cc.MemberUpdate(ctx, in, opts...)
184}
185
186type retryMaintenanceClient struct {
187 mc pb.MaintenanceClient
188}
189
190// RetryMaintenanceClient implements a Maintenance.
191func RetryMaintenanceClient(c *Client, conn *grpc.ClientConn) pb.MaintenanceClient {
192 return &retryMaintenanceClient{
193 mc: pb.NewMaintenanceClient(conn),
194 }
195}
196
197func (rmc *retryMaintenanceClient) Alarm(ctx context.Context, in *pb.AlarmRequest, opts ...grpc.CallOption) (resp *pb.AlarmResponse, err error) {
198 return rmc.mc.Alarm(ctx, in, append(opts, withRetryPolicy(repeatable))...)
199}
200
201func (rmc *retryMaintenanceClient) Status(ctx context.Context, in *pb.StatusRequest, opts ...grpc.CallOption) (resp *pb.StatusResponse, err error) {
202 return rmc.mc.Status(ctx, in, append(opts, withRetryPolicy(repeatable))...)
203}
204
205func (rmc *retryMaintenanceClient) Hash(ctx context.Context, in *pb.HashRequest, opts ...grpc.CallOption) (resp *pb.HashResponse, err error) {
206 return rmc.mc.Hash(ctx, in, append(opts, withRetryPolicy(repeatable))...)
207}
208
209func (rmc *retryMaintenanceClient) HashKV(ctx context.Context, in *pb.HashKVRequest, opts ...grpc.CallOption) (resp *pb.HashKVResponse, err error) {
210 return rmc.mc.HashKV(ctx, in, append(opts, withRetryPolicy(repeatable))...)
211}
212
213func (rmc *retryMaintenanceClient) Snapshot(ctx context.Context, in *pb.SnapshotRequest, opts ...grpc.CallOption) (stream pb.Maintenance_SnapshotClient, err error) {
214 return rmc.mc.Snapshot(ctx, in, append(opts, withRetryPolicy(repeatable))...)
215}
216
217func (rmc *retryMaintenanceClient) MoveLeader(ctx context.Context, in *pb.MoveLeaderRequest, opts ...grpc.CallOption) (resp *pb.MoveLeaderResponse, err error) {
218 return rmc.mc.MoveLeader(ctx, in, append(opts, withRetryPolicy(repeatable))...)
219}
220
221func (rmc *retryMaintenanceClient) Defragment(ctx context.Context, in *pb.DefragmentRequest, opts ...grpc.CallOption) (resp *pb.DefragmentResponse, err error) {
222 return rmc.mc.Defragment(ctx, in, opts...)
223}
224
225type retryAuthClient struct {
226 ac pb.AuthClient
227}
228
229// RetryAuthClient implements a AuthClient.
230func RetryAuthClient(c *Client) pb.AuthClient {
231 return &retryAuthClient{
232 ac: pb.NewAuthClient(c.conn),
233 }
234}
235
236func (rac *retryAuthClient) UserList(ctx context.Context, in *pb.AuthUserListRequest, opts ...grpc.CallOption) (resp *pb.AuthUserListResponse, err error) {
237 return rac.ac.UserList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
238}
239
240func (rac *retryAuthClient) UserGet(ctx context.Context, in *pb.AuthUserGetRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGetResponse, err error) {
241 return rac.ac.UserGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
242}
243
244func (rac *retryAuthClient) RoleGet(ctx context.Context, in *pb.AuthRoleGetRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGetResponse, err error) {
245 return rac.ac.RoleGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
246}
247
248func (rac *retryAuthClient) RoleList(ctx context.Context, in *pb.AuthRoleListRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleListResponse, err error) {
249 return rac.ac.RoleList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
250}
251
252func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
253 return rac.ac.AuthEnable(ctx, in, opts...)
254}
255
256func (rac *retryAuthClient) AuthDisable(ctx context.Context, in *pb.AuthDisableRequest, opts ...grpc.CallOption) (resp *pb.AuthDisableResponse, err error) {
257 return rac.ac.AuthDisable(ctx, in, opts...)
258}
259
260func (rac *retryAuthClient) UserAdd(ctx context.Context, in *pb.AuthUserAddRequest, opts ...grpc.CallOption) (resp *pb.AuthUserAddResponse, err error) {
261 return rac.ac.UserAdd(ctx, in, opts...)
262}
263
264func (rac *retryAuthClient) UserDelete(ctx context.Context, in *pb.AuthUserDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthUserDeleteResponse, err error) {
265 return rac.ac.UserDelete(ctx, in, opts...)
266}
267
268func (rac *retryAuthClient) UserChangePassword(ctx context.Context, in *pb.AuthUserChangePasswordRequest, opts ...grpc.CallOption) (resp *pb.AuthUserChangePasswordResponse, err error) {
269 return rac.ac.UserChangePassword(ctx, in, opts...)
270}
271
272func (rac *retryAuthClient) UserGrantRole(ctx context.Context, in *pb.AuthUserGrantRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGrantRoleResponse, err error) {
273 return rac.ac.UserGrantRole(ctx, in, opts...)
274}
275
276func (rac *retryAuthClient) UserRevokeRole(ctx context.Context, in *pb.AuthUserRevokeRoleRequest, opts ...grpc.CallOption) (resp *pb.AuthUserRevokeRoleResponse, err error) {
277 return rac.ac.UserRevokeRole(ctx, in, opts...)
278}
279
280func (rac *retryAuthClient) RoleAdd(ctx context.Context, in *pb.AuthRoleAddRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleAddResponse, err error) {
281 return rac.ac.RoleAdd(ctx, in, opts...)
282}
283
284func (rac *retryAuthClient) RoleDelete(ctx context.Context, in *pb.AuthRoleDeleteRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleDeleteResponse, err error) {
285 return rac.ac.RoleDelete(ctx, in, opts...)
286}
287
288func (rac *retryAuthClient) RoleGrantPermission(ctx context.Context, in *pb.AuthRoleGrantPermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGrantPermissionResponse, err error) {
289 return rac.ac.RoleGrantPermission(ctx, in, opts...)
290}
291
292func (rac *retryAuthClient) RoleRevokePermission(ctx context.Context, in *pb.AuthRoleRevokePermissionRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleRevokePermissionResponse, err error) {
293 return rac.ac.RoleRevokePermission(ctx, in, opts...)
294}
295
296func (rac *retryAuthClient) Authenticate(ctx context.Context, in *pb.AuthenticateRequest, opts ...grpc.CallOption) (resp *pb.AuthenticateResponse, err error) {
297 return rac.ac.Authenticate(ctx, in, opts...)
298}