maintenance.go 3.3 KB

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