maintenance.go 3.0 KB

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