util.go 5.0 KB

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