go.etcd.io/etcd@v3.3.27+incompatible/clientv3/retry.go (about)

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