maintenance.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2016 CoreOS, Inc.
  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. "io"
  17. "github.com/coreos/etcd/etcdserver"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/mvcc"
  20. "github.com/coreos/etcd/mvcc/backend"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/version"
  23. "golang.org/x/net/context"
  24. )
  25. type KVGetter interface {
  26. KV() mvcc.ConsistentWatchableKV
  27. }
  28. type BackendGetter interface {
  29. Backend() backend.Backend
  30. }
  31. type Alarmer interface {
  32. Alarm(ctx context.Context, ar *pb.AlarmRequest) (*pb.AlarmResponse, error)
  33. }
  34. type RaftStatusGetter interface {
  35. Index() uint64
  36. Term() uint64
  37. Leader() types.ID
  38. }
  39. type maintenanceServer struct {
  40. rg RaftStatusGetter
  41. kg KVGetter
  42. bg BackendGetter
  43. a Alarmer
  44. hdr header
  45. }
  46. func NewMaintenanceServer(s *etcdserver.EtcdServer) pb.MaintenanceServer {
  47. return &maintenanceServer{rg: s, kg: s, bg: s, a: s, hdr: newHeader(s)}
  48. }
  49. func (ms *maintenanceServer) Defragment(ctx context.Context, sr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) {
  50. plog.Noticef("starting to defragment the storage backend...")
  51. err := ms.bg.Backend().Defrag()
  52. if err != nil {
  53. plog.Errorf("failed to deframent the storage backend (%v)", err)
  54. return nil, err
  55. }
  56. plog.Noticef("finished defragmenting the storage backend")
  57. return &pb.DefragmentResponse{}, nil
  58. }
  59. func (ms *maintenanceServer) Snapshot(sr *pb.SnapshotRequest, srv pb.Maintenance_SnapshotServer) error {
  60. snap := ms.bg.Backend().Snapshot()
  61. pr, pw := io.Pipe()
  62. defer pr.Close()
  63. go func() {
  64. snap.WriteTo(pw)
  65. if err := snap.Close(); err != nil {
  66. plog.Errorf("error closing snapshot (%v)", err)
  67. }
  68. pw.Close()
  69. }()
  70. br := int64(0)
  71. buf := make([]byte, 32*1024)
  72. sz := snap.Size()
  73. for br < sz {
  74. n, err := io.ReadFull(pr, buf)
  75. if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
  76. return togRPCError(err)
  77. }
  78. br += int64(n)
  79. resp := &pb.SnapshotResponse{
  80. RemainingBytes: uint64(sz - br),
  81. Blob: buf[:n],
  82. }
  83. if err = srv.Send(resp); err != nil {
  84. return togRPCError(err)
  85. }
  86. }
  87. return nil
  88. }
  89. func (ms *maintenanceServer) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
  90. h, rev, err := ms.kg.KV().Hash()
  91. if err != nil {
  92. return nil, togRPCError(err)
  93. }
  94. resp := &pb.HashResponse{Header: &pb.ResponseHeader{Revision: rev}, Hash: h}
  95. ms.hdr.fill(resp.Header)
  96. return resp, nil
  97. }
  98. func (ms *maintenanceServer) Alarm(ctx context.Context, ar *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  99. return ms.a.Alarm(ctx, ar)
  100. }
  101. func (ms *maintenanceServer) Status(ctx context.Context, ar *pb.StatusRequest) (*pb.StatusResponse, error) {
  102. resp := &pb.StatusResponse{
  103. Header: &pb.ResponseHeader{Revision: ms.hdr.rev()},
  104. Version: version.Version,
  105. DbSize: ms.bg.Backend().Size(),
  106. Leader: uint64(ms.rg.Leader()),
  107. RaftIndex: ms.rg.Index(),
  108. RaftTerm: ms.rg.Term(),
  109. }
  110. ms.hdr.fill(resp.Header)
  111. return resp, nil
  112. }