maintenance.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "github.com/coreos/etcd/etcdserver"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "github.com/coreos/etcd/storage/backend"
  19. "golang.org/x/net/context"
  20. )
  21. type BackendGetter interface {
  22. Backend() backend.Backend
  23. }
  24. type Alarmer interface {
  25. Alarm(ctx context.Context, ar *pb.AlarmRequest) (*pb.AlarmResponse, error)
  26. }
  27. type maintenanceServer struct {
  28. bg BackendGetter
  29. a Alarmer
  30. hdr header
  31. }
  32. func NewMaintenanceServer(s *etcdserver.EtcdServer) pb.MaintenanceServer {
  33. return &maintenanceServer{bg: s, a: s, hdr: newHeader(s)}
  34. }
  35. func (ms *maintenanceServer) Defragment(ctx context.Context, sr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) {
  36. plog.Noticef("starting to defragment the storage backend...")
  37. err := ms.bg.Backend().Defrag()
  38. if err != nil {
  39. plog.Errorf("failed to deframent the storage backend (%v)", err)
  40. return nil, err
  41. }
  42. plog.Noticef("finished defragmenting the storage backend")
  43. return &pb.DefragmentResponse{}, nil
  44. }
  45. func (s *maintenanceServer) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
  46. h, err := s.bg.Backend().Hash()
  47. if err != nil {
  48. return nil, togRPCError(err)
  49. }
  50. resp := &pb.HashResponse{Header: &pb.ResponseHeader{Revision: s.hdr.rev()}, Hash: h}
  51. s.hdr.fill(resp.Header)
  52. return resp, nil
  53. }
  54. func (ms *maintenanceServer) Alarm(ctx context.Context, ar *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  55. plog.Warningf("alarming %+v", ar)
  56. return ms.a.Alarm(ctx, ar)
  57. }