maintenance.go 5.0 KB

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