maintenance.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 clientv3
  15. import (
  16. "io"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "golang.org/x/net/context"
  19. "google.golang.org/grpc"
  20. )
  21. type (
  22. DefragmentResponse pb.DefragmentResponse
  23. AlarmResponse pb.AlarmResponse
  24. AlarmMember pb.AlarmMember
  25. StatusResponse pb.StatusResponse
  26. )
  27. type Maintenance interface {
  28. // AlarmList gets all active alarms.
  29. AlarmList(ctx context.Context) (*AlarmResponse, error)
  30. // AlarmDisarm disarms a given alarm.
  31. AlarmDisarm(ctx context.Context, m *AlarmMember) (*AlarmResponse, error)
  32. // Defragment defragments storage backend of the etcd member with given endpoint.
  33. // Defragment is only needed when deleting a large number of keys and want to reclaim
  34. // the resources.
  35. // Defragment is an expensive operation. User should avoid defragmenting multiple members
  36. // at the same time.
  37. // To defragment multiple members in the cluster, user need to call defragment multiple
  38. // times with different endpoints.
  39. Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error)
  40. // Status gets the status of the endpoint.
  41. Status(ctx context.Context, endpoint string) (*StatusResponse, error)
  42. // Snapshot provides a reader for a snapshot of a backend.
  43. Snapshot(ctx context.Context) (io.ReadCloser, error)
  44. }
  45. type maintenance struct {
  46. c *Client
  47. remote pb.MaintenanceClient
  48. }
  49. func NewMaintenance(c *Client) Maintenance {
  50. return &maintenance{c: c, remote: pb.NewMaintenanceClient(c.conn)}
  51. }
  52. func (m *maintenance) AlarmList(ctx context.Context) (*AlarmResponse, error) {
  53. req := &pb.AlarmRequest{
  54. Action: pb.AlarmRequest_GET,
  55. MemberID: 0, // all
  56. Alarm: pb.AlarmType_NONE, // all
  57. }
  58. for {
  59. resp, err := m.remote.Alarm(ctx, req, grpc.FailFast(false))
  60. if err == nil {
  61. return (*AlarmResponse)(resp), nil
  62. }
  63. if isHaltErr(ctx, err) {
  64. return nil, toErr(ctx, err)
  65. }
  66. }
  67. }
  68. func (m *maintenance) AlarmDisarm(ctx context.Context, am *AlarmMember) (*AlarmResponse, error) {
  69. req := &pb.AlarmRequest{
  70. Action: pb.AlarmRequest_DEACTIVATE,
  71. MemberID: am.MemberID,
  72. Alarm: am.Alarm,
  73. }
  74. if req.MemberID == 0 && req.Alarm == pb.AlarmType_NONE {
  75. ar, err := m.AlarmList(ctx)
  76. if err != nil {
  77. return nil, toErr(ctx, err)
  78. }
  79. ret := AlarmResponse{}
  80. for _, am := range ar.Alarms {
  81. dresp, derr := m.AlarmDisarm(ctx, (*AlarmMember)(am))
  82. if derr != nil {
  83. return nil, toErr(ctx, derr)
  84. }
  85. ret.Alarms = append(ret.Alarms, dresp.Alarms...)
  86. }
  87. return &ret, nil
  88. }
  89. resp, err := m.remote.Alarm(ctx, req, grpc.FailFast(false))
  90. if err == nil {
  91. return (*AlarmResponse)(resp), nil
  92. }
  93. return nil, toErr(ctx, err)
  94. }
  95. func (m *maintenance) Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error) {
  96. conn, err := m.c.Dial(endpoint)
  97. if err != nil {
  98. return nil, toErr(ctx, err)
  99. }
  100. defer conn.Close()
  101. remote := pb.NewMaintenanceClient(conn)
  102. resp, err := remote.Defragment(ctx, &pb.DefragmentRequest{}, grpc.FailFast(false))
  103. if err != nil {
  104. return nil, toErr(ctx, err)
  105. }
  106. return (*DefragmentResponse)(resp), nil
  107. }
  108. func (m *maintenance) Status(ctx context.Context, endpoint string) (*StatusResponse, error) {
  109. conn, err := m.c.Dial(endpoint)
  110. if err != nil {
  111. return nil, toErr(ctx, err)
  112. }
  113. defer conn.Close()
  114. remote := pb.NewMaintenanceClient(conn)
  115. resp, err := remote.Status(ctx, &pb.StatusRequest{}, grpc.FailFast(false))
  116. if err != nil {
  117. return nil, toErr(ctx, err)
  118. }
  119. return (*StatusResponse)(resp), nil
  120. }
  121. func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
  122. ss, err := m.remote.Snapshot(ctx, &pb.SnapshotRequest{}, grpc.FailFast(false))
  123. if err != nil {
  124. return nil, toErr(ctx, err)
  125. }
  126. pr, pw := io.Pipe()
  127. go func() {
  128. for {
  129. resp, err := ss.Recv()
  130. if err != nil {
  131. pw.CloseWithError(err)
  132. return
  133. }
  134. if resp == nil && err == nil {
  135. break
  136. }
  137. if _, werr := pw.Write(resp.Blob); werr != nil {
  138. pw.CloseWithError(werr)
  139. return
  140. }
  141. }
  142. pw.Close()
  143. }()
  144. return pr, nil
  145. }