maintenance.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2016 The etcd Authors
  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 grpcproxy
  15. import (
  16. "context"
  17. "io"
  18. "go.etcd.io/etcd/clientv3"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. )
  21. type maintenanceProxy struct {
  22. client *clientv3.Client
  23. }
  24. func NewMaintenanceProxy(c *clientv3.Client) pb.MaintenanceServer {
  25. return &maintenanceProxy{
  26. client: c,
  27. }
  28. }
  29. func (mp *maintenanceProxy) Defragment(ctx context.Context, dr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) {
  30. conn := mp.client.ActiveConnection()
  31. return pb.NewMaintenanceClient(conn).Defragment(ctx, dr)
  32. }
  33. func (mp *maintenanceProxy) Snapshot(sr *pb.SnapshotRequest, stream pb.Maintenance_SnapshotServer) error {
  34. conn := mp.client.ActiveConnection()
  35. ctx, cancel := context.WithCancel(stream.Context())
  36. defer cancel()
  37. ctx = withClientAuthToken(ctx, stream.Context())
  38. sc, err := pb.NewMaintenanceClient(conn).Snapshot(ctx, sr)
  39. if err != nil {
  40. return err
  41. }
  42. for {
  43. rr, err := sc.Recv()
  44. if err != nil {
  45. if err == io.EOF {
  46. return nil
  47. }
  48. return err
  49. }
  50. err = stream.Send(rr)
  51. if err != nil {
  52. return err
  53. }
  54. }
  55. }
  56. func (mp *maintenanceProxy) Hash(ctx context.Context, r *pb.HashRequest) (*pb.HashResponse, error) {
  57. conn := mp.client.ActiveConnection()
  58. return pb.NewMaintenanceClient(conn).Hash(ctx, r)
  59. }
  60. func (mp *maintenanceProxy) HashKV(ctx context.Context, r *pb.HashKVRequest) (*pb.HashKVResponse, error) {
  61. conn := mp.client.ActiveConnection()
  62. return pb.NewMaintenanceClient(conn).HashKV(ctx, r)
  63. }
  64. func (mp *maintenanceProxy) Alarm(ctx context.Context, r *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  65. conn := mp.client.ActiveConnection()
  66. return pb.NewMaintenanceClient(conn).Alarm(ctx, r)
  67. }
  68. func (mp *maintenanceProxy) Status(ctx context.Context, r *pb.StatusRequest) (*pb.StatusResponse, error) {
  69. conn := mp.client.ActiveConnection()
  70. return pb.NewMaintenanceClient(conn).Status(ctx, r)
  71. }
  72. func (mp *maintenanceProxy) MoveLeader(ctx context.Context, r *pb.MoveLeaderRequest) (*pb.MoveLeaderResponse, error) {
  73. conn := mp.client.ActiveConnection()
  74. return pb.NewMaintenanceClient(conn).MoveLeader(ctx, r)
  75. }