maintenance.go 6.3 KB

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