util.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 v3rpc
  15. import (
  16. "context"
  17. "strings"
  18. "go.etcd.io/etcd/auth"
  19. "go.etcd.io/etcd/etcdserver"
  20. "go.etcd.io/etcd/etcdserver/api/membership"
  21. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  22. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  23. "go.etcd.io/etcd/lease"
  24. "go.etcd.io/etcd/mvcc"
  25. "google.golang.org/grpc/codes"
  26. "google.golang.org/grpc/status"
  27. )
  28. var toGRPCErrorMap = map[error]error{
  29. membership.ErrIDRemoved: rpctypes.ErrGRPCMemberNotFound,
  30. membership.ErrIDNotFound: rpctypes.ErrGRPCMemberNotFound,
  31. membership.ErrIDExists: rpctypes.ErrGRPCMemberExist,
  32. membership.ErrPeerURLexists: rpctypes.ErrGRPCPeerURLExist,
  33. membership.ErrMemberNotLearner: rpctypes.ErrGRPCMemberNotLearner,
  34. membership.ErrTooManyLearners: rpctypes.ErrGRPCTooManyLearners,
  35. etcdserver.ErrNotEnoughStartedMembers: rpctypes.ErrMemberNotEnoughStarted,
  36. etcdserver.ErrLearnerNotReady: rpctypes.ErrGRPCLearnerNotReady,
  37. mvcc.ErrCompacted: rpctypes.ErrGRPCCompacted,
  38. mvcc.ErrFutureRev: rpctypes.ErrGRPCFutureRev,
  39. etcdserver.ErrRequestTooLarge: rpctypes.ErrGRPCRequestTooLarge,
  40. etcdserver.ErrNoSpace: rpctypes.ErrGRPCNoSpace,
  41. etcdserver.ErrTooManyRequests: rpctypes.ErrTooManyRequests,
  42. etcdserver.ErrNoLeader: rpctypes.ErrGRPCNoLeader,
  43. etcdserver.ErrNotLeader: rpctypes.ErrGRPCNotLeader,
  44. etcdserver.ErrLeaderChanged: rpctypes.ErrGRPCLeaderChanged,
  45. etcdserver.ErrStopped: rpctypes.ErrGRPCStopped,
  46. etcdserver.ErrTimeout: rpctypes.ErrGRPCTimeout,
  47. etcdserver.ErrTimeoutDueToLeaderFail: rpctypes.ErrGRPCTimeoutDueToLeaderFail,
  48. etcdserver.ErrTimeoutDueToConnectionLost: rpctypes.ErrGRPCTimeoutDueToConnectionLost,
  49. etcdserver.ErrUnhealthy: rpctypes.ErrGRPCUnhealthy,
  50. etcdserver.ErrKeyNotFound: rpctypes.ErrGRPCKeyNotFound,
  51. etcdserver.ErrCorrupt: rpctypes.ErrGRPCCorrupt,
  52. etcdserver.ErrBadLeaderTransferee: rpctypes.ErrGRPCBadLeaderTransferee,
  53. lease.ErrLeaseNotFound: rpctypes.ErrGRPCLeaseNotFound,
  54. lease.ErrLeaseExists: rpctypes.ErrGRPCLeaseExist,
  55. lease.ErrLeaseTTLTooLarge: rpctypes.ErrGRPCLeaseTTLTooLarge,
  56. auth.ErrRootUserNotExist: rpctypes.ErrGRPCRootUserNotExist,
  57. auth.ErrRootRoleNotExist: rpctypes.ErrGRPCRootRoleNotExist,
  58. auth.ErrUserAlreadyExist: rpctypes.ErrGRPCUserAlreadyExist,
  59. auth.ErrUserEmpty: rpctypes.ErrGRPCUserEmpty,
  60. auth.ErrUserNotFound: rpctypes.ErrGRPCUserNotFound,
  61. auth.ErrRoleAlreadyExist: rpctypes.ErrGRPCRoleAlreadyExist,
  62. auth.ErrRoleNotFound: rpctypes.ErrGRPCRoleNotFound,
  63. auth.ErrRoleEmpty: rpctypes.ErrGRPCRoleEmpty,
  64. auth.ErrAuthFailed: rpctypes.ErrGRPCAuthFailed,
  65. auth.ErrPermissionDenied: rpctypes.ErrGRPCPermissionDenied,
  66. auth.ErrRoleNotGranted: rpctypes.ErrGRPCRoleNotGranted,
  67. auth.ErrPermissionNotGranted: rpctypes.ErrGRPCPermissionNotGranted,
  68. auth.ErrAuthNotEnabled: rpctypes.ErrGRPCAuthNotEnabled,
  69. auth.ErrInvalidAuthToken: rpctypes.ErrGRPCInvalidAuthToken,
  70. auth.ErrInvalidAuthMgmt: rpctypes.ErrGRPCInvalidAuthMgmt,
  71. }
  72. func togRPCError(err error) error {
  73. // let gRPC server convert to codes.Canceled, codes.DeadlineExceeded
  74. if err == context.Canceled || err == context.DeadlineExceeded {
  75. return err
  76. }
  77. grpcErr, ok := toGRPCErrorMap[err]
  78. if !ok {
  79. return status.Error(codes.Unknown, err.Error())
  80. }
  81. return grpcErr
  82. }
  83. func isClientCtxErr(ctxErr error, err error) bool {
  84. if ctxErr != nil {
  85. return true
  86. }
  87. ev, ok := status.FromError(err)
  88. if !ok {
  89. return false
  90. }
  91. switch ev.Code() {
  92. case codes.Canceled, codes.DeadlineExceeded:
  93. // client-side context cancel or deadline exceeded
  94. // "rpc error: code = Canceled desc = context canceled"
  95. // "rpc error: code = DeadlineExceeded desc = context deadline exceeded"
  96. return true
  97. case codes.Unavailable:
  98. msg := ev.Message()
  99. // client-side context cancel or deadline exceeded with TLS ("http2.errClientDisconnected")
  100. // "rpc error: code = Unavailable desc = client disconnected"
  101. if msg == "client disconnected" {
  102. return true
  103. }
  104. // "grpc/transport.ClientTransport.CloseStream" on canceled streams
  105. // "rpc error: code = Unavailable desc = stream error: stream ID 21; CANCEL")
  106. if strings.HasPrefix(msg, "stream error: ") && strings.HasSuffix(msg, "; CANCEL") {
  107. return true
  108. }
  109. }
  110. return false
  111. }
  112. // in v3.4, learner is allowed to serve serializable read and endpoint status
  113. func isRPCSupportedForLearner(req interface{}) bool {
  114. switch r := req.(type) {
  115. case *pb.StatusRequest:
  116. return true
  117. case *pb.RangeRequest:
  118. return r.Serializable
  119. default:
  120. return false
  121. }
  122. }