maintenance.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. "crypto/sha256"
  18. "io"
  19. "go.etcd.io/etcd/auth"
  20. "go.etcd.io/etcd/etcdserver"
  21. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  22. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  23. "go.etcd.io/etcd/mvcc"
  24. "go.etcd.io/etcd/mvcc/backend"
  25. "go.etcd.io/etcd/raft"
  26. "go.etcd.io/etcd/version"
  27. "go.uber.org/zap"
  28. )
  29. type KVGetter interface {
  30. KV() mvcc.ConsistentWatchableKV
  31. }
  32. type BackendGetter interface {
  33. Backend() backend.Backend
  34. }
  35. type Alarmer interface {
  36. // Alarms is implemented in Server interface located in etcdserver/server.go
  37. // It returns a list of alarms present in the AlarmStore
  38. Alarms() []*pb.AlarmMember
  39. Alarm(ctx context.Context, ar *pb.AlarmRequest) (*pb.AlarmResponse, error)
  40. }
  41. type LeaderTransferrer interface {
  42. MoveLeader(ctx context.Context, lead, target uint64) error
  43. }
  44. type AuthGetter interface {
  45. AuthInfoFromCtx(ctx context.Context) (*auth.AuthInfo, error)
  46. AuthStore() auth.AuthStore
  47. }
  48. type ClusterStatusGetter interface {
  49. IsLearner() bool
  50. }
  51. type maintenanceServer struct {
  52. lg *zap.Logger
  53. rg etcdserver.RaftStatusGetter
  54. kg KVGetter
  55. bg BackendGetter
  56. a Alarmer
  57. lt LeaderTransferrer
  58. hdr header
  59. cs ClusterStatusGetter
  60. }
  61. func NewMaintenanceServer(s *etcdserver.EtcdServer) pb.MaintenanceServer {
  62. srv := &maintenanceServer{lg: s.Cfg.Logger, rg: s, kg: s, bg: s, a: s, lt: s, hdr: newHeader(s), cs: s}
  63. return &authMaintenanceServer{srv, s}
  64. }
  65. func (ms *maintenanceServer) Defragment(ctx context.Context, sr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) {
  66. if ms.lg != nil {
  67. ms.lg.Info("starting defragment")
  68. } else {
  69. plog.Noticef("starting to defragment the storage backend...")
  70. }
  71. err := ms.bg.Backend().Defrag()
  72. if err != nil {
  73. if ms.lg != nil {
  74. ms.lg.Warn("failed to defragment", zap.Error(err))
  75. } else {
  76. plog.Errorf("failed to defragment the storage backend (%v)", err)
  77. }
  78. return nil, err
  79. }
  80. if ms.lg != nil {
  81. ms.lg.Info("finished defragment")
  82. } else {
  83. plog.Noticef("finished defragmenting the storage backend")
  84. }
  85. return &pb.DefragmentResponse{}, nil
  86. }
  87. func (ms *maintenanceServer) Snapshot(sr *pb.SnapshotRequest, srv pb.Maintenance_SnapshotServer) error {
  88. snap := ms.bg.Backend().Snapshot()
  89. pr, pw := io.Pipe()
  90. defer pr.Close()
  91. go func() {
  92. snap.WriteTo(pw)
  93. if err := snap.Close(); err != nil {
  94. if ms.lg != nil {
  95. ms.lg.Warn("failed to close snapshot", zap.Error(err))
  96. } else {
  97. plog.Errorf("error closing snapshot (%v)", err)
  98. }
  99. }
  100. pw.Close()
  101. }()
  102. // send file data
  103. h := sha256.New()
  104. br := int64(0)
  105. buf := make([]byte, 32*1024)
  106. sz := snap.Size()
  107. for br < sz {
  108. n, err := io.ReadFull(pr, buf)
  109. if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
  110. return togRPCError(err)
  111. }
  112. br += int64(n)
  113. resp := &pb.SnapshotResponse{
  114. RemainingBytes: uint64(sz - br),
  115. Blob: buf[:n],
  116. }
  117. if err = srv.Send(resp); err != nil {
  118. return togRPCError(err)
  119. }
  120. h.Write(buf[:n])
  121. }
  122. // send sha
  123. sha := h.Sum(nil)
  124. hresp := &pb.SnapshotResponse{RemainingBytes: 0, Blob: sha}
  125. if err := srv.Send(hresp); err != nil {
  126. return togRPCError(err)
  127. }
  128. return nil
  129. }
  130. func (ms *maintenanceServer) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
  131. h, rev, err := ms.kg.KV().Hash()
  132. if err != nil {
  133. return nil, togRPCError(err)
  134. }
  135. resp := &pb.HashResponse{Header: &pb.ResponseHeader{Revision: rev}, Hash: h}
  136. ms.hdr.fill(resp.Header)
  137. return resp, nil
  138. }
  139. func (ms *maintenanceServer) HashKV(ctx context.Context, r *pb.HashKVRequest) (*pb.HashKVResponse, error) {
  140. h, rev, compactRev, err := ms.kg.KV().HashByRev(r.Revision)
  141. if err != nil {
  142. return nil, togRPCError(err)
  143. }
  144. resp := &pb.HashKVResponse{Header: &pb.ResponseHeader{Revision: rev}, Hash: h, CompactRevision: compactRev}
  145. ms.hdr.fill(resp.Header)
  146. return resp, nil
  147. }
  148. func (ms *maintenanceServer) Alarm(ctx context.Context, ar *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  149. return ms.a.Alarm(ctx, ar)
  150. }
  151. func (ms *maintenanceServer) Status(ctx context.Context, ar *pb.StatusRequest) (*pb.StatusResponse, error) {
  152. hdr := &pb.ResponseHeader{}
  153. ms.hdr.fill(hdr)
  154. resp := &pb.StatusResponse{
  155. Header: hdr,
  156. Version: version.Version,
  157. Leader: uint64(ms.rg.Leader()),
  158. RaftIndex: ms.rg.CommittedIndex(),
  159. RaftAppliedIndex: ms.rg.AppliedIndex(),
  160. RaftTerm: ms.rg.Term(),
  161. DbSize: ms.bg.Backend().Size(),
  162. DbSizeInUse: ms.bg.Backend().SizeInUse(),
  163. IsLearner: ms.cs.IsLearner(),
  164. }
  165. if resp.Leader == raft.None {
  166. resp.Errors = append(resp.Errors, etcdserver.ErrNoLeader.Error())
  167. }
  168. for _, a := range ms.a.Alarms() {
  169. resp.Errors = append(resp.Errors, a.String())
  170. }
  171. return resp, nil
  172. }
  173. func (ms *maintenanceServer) MoveLeader(ctx context.Context, tr *pb.MoveLeaderRequest) (*pb.MoveLeaderResponse, error) {
  174. if ms.rg.ID() != ms.rg.Leader() {
  175. return nil, rpctypes.ErrGRPCNotLeader
  176. }
  177. if err := ms.lt.MoveLeader(ctx, uint64(ms.rg.Leader()), tr.TargetID); err != nil {
  178. return nil, togRPCError(err)
  179. }
  180. return &pb.MoveLeaderResponse{}, nil
  181. }
  182. type authMaintenanceServer struct {
  183. *maintenanceServer
  184. ag AuthGetter
  185. }
  186. func (ams *authMaintenanceServer) isAuthenticated(ctx context.Context) error {
  187. authInfo, err := ams.ag.AuthInfoFromCtx(ctx)
  188. if err != nil {
  189. return err
  190. }
  191. return ams.ag.AuthStore().IsAdminPermitted(authInfo)
  192. }
  193. func (ams *authMaintenanceServer) Defragment(ctx context.Context, sr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) {
  194. if err := ams.isAuthenticated(ctx); err != nil {
  195. return nil, err
  196. }
  197. return ams.maintenanceServer.Defragment(ctx, sr)
  198. }
  199. func (ams *authMaintenanceServer) Snapshot(sr *pb.SnapshotRequest, srv pb.Maintenance_SnapshotServer) error {
  200. if err := ams.isAuthenticated(srv.Context()); err != nil {
  201. return err
  202. }
  203. return ams.maintenanceServer.Snapshot(sr, srv)
  204. }
  205. func (ams *authMaintenanceServer) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
  206. if err := ams.isAuthenticated(ctx); err != nil {
  207. return nil, err
  208. }
  209. return ams.maintenanceServer.Hash(ctx, r)
  210. }
  211. func (ams *authMaintenanceServer) HashKV(ctx context.Context, r *pb.HashKVRequest) (*pb.HashKVResponse, error) {
  212. if err := ams.isAuthenticated(ctx); err != nil {
  213. return nil, err
  214. }
  215. return ams.maintenanceServer.HashKV(ctx, r)
  216. }
  217. func (ams *authMaintenanceServer) Status(ctx context.Context, ar *pb.StatusRequest) (*pb.StatusResponse, error) {
  218. return ams.maintenanceServer.Status(ctx, ar)
  219. }
  220. func (ams *authMaintenanceServer) MoveLeader(ctx context.Context, tr *pb.MoveLeaderRequest) (*pb.MoveLeaderResponse, error) {
  221. return ams.maintenanceServer.MoveLeader(ctx, tr)
  222. }