util.go 4.9 KB

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