maintenance.go 2.3 KB

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