maintenance.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "context"
  17. "fmt"
  18. "io"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. "google.golang.org/grpc"
  21. )
  22. type (
  23. DefragmentResponse pb.DefragmentResponse
  24. AlarmResponse pb.AlarmResponse
  25. AlarmMember pb.AlarmMember
  26. StatusResponse pb.StatusResponse
  27. HashKVResponse pb.HashKVResponse
  28. MoveLeaderResponse pb.MoveLeaderResponse
  29. )
  30. type Maintenance interface {
  31. // AlarmList gets all active alarms.
  32. AlarmList(ctx context.Context) (*AlarmResponse, error)
  33. // AlarmDisarm disarms a given alarm.
  34. AlarmDisarm(ctx context.Context, m *AlarmMember) (*AlarmResponse, error)
  35. // Defragment releases wasted space from internal fragmentation on a given etcd member.
  36. // Defragment is only needed when deleting a large number of keys and want to reclaim
  37. // the resources.
  38. // Defragment is an expensive operation. User should avoid defragmenting multiple members
  39. // at the same time.
  40. // To defragment multiple members in the cluster, user need to call defragment multiple
  41. // times with different endpoints.
  42. Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error)
  43. // Status gets the status of the endpoint.
  44. Status(ctx context.Context, endpoint string) (*StatusResponse, error)
  45. // HashKV returns a hash of the KV state at the time of the RPC.
  46. // If revision is zero, the hash is computed on all keys. If the revision
  47. // is non-zero, the hash is computed on all keys at or below the given revision.
  48. HashKV(ctx context.Context, endpoint string, rev int64) (*HashKVResponse, error)
  49. // Snapshot provides a reader for a point-in-time snapshot of etcd.
  50. // If the context "ctx" is canceled or timed out, reading from returned
  51. // "io.ReadCloser" would error out (e.g. context.Canceled, context.DeadlineExceeded).
  52. Snapshot(ctx context.Context) (io.ReadCloser, error)
  53. // MoveLeader requests current leader to transfer its leadership to the transferee.
  54. // Request must be made to the leader.
  55. MoveLeader(ctx context.Context, transfereeID uint64) (*MoveLeaderResponse, error)
  56. }
  57. type maintenance struct {
  58. dial func(endpoint string) (pb.MaintenanceClient, func(), error)
  59. remote pb.MaintenanceClient
  60. callOpts []grpc.CallOption
  61. }
  62. func NewMaintenance(c *Client) Maintenance {
  63. api := &maintenance{
  64. dial: func(endpoint string) (pb.MaintenanceClient, func(), error) {
  65. conn, err := c.Dial(endpoint)
  66. if err != nil {
  67. return nil, nil, fmt.Errorf("failed to dial endpoint %s with maintenance client: %v", endpoint, err)
  68. }
  69. cancel := func() { conn.Close() }
  70. return RetryMaintenanceClient(c, conn), cancel, nil
  71. },
  72. remote: RetryMaintenanceClient(c, c.conn),
  73. }
  74. if c != nil {
  75. api.callOpts = c.callOpts
  76. }
  77. return api
  78. }
  79. func NewMaintenanceFromMaintenanceClient(remote pb.MaintenanceClient, c *Client) Maintenance {
  80. api := &maintenance{
  81. dial: func(string) (pb.MaintenanceClient, func(), error) {
  82. return remote, func() {}, nil
  83. },
  84. remote: remote,
  85. }
  86. if c != nil {
  87. api.callOpts = c.callOpts
  88. }
  89. return api
  90. }
  91. func (m *maintenance) AlarmList(ctx context.Context) (*AlarmResponse, error) {
  92. req := &pb.AlarmRequest{
  93. Action: pb.AlarmRequest_GET,
  94. MemberID: 0, // all
  95. Alarm: pb.AlarmType_NONE, // all
  96. }
  97. resp, err := m.remote.Alarm(ctx, req, m.callOpts...)
  98. if err == nil {
  99. return (*AlarmResponse)(resp), nil
  100. }
  101. return nil, toErr(ctx, err)
  102. }
  103. func (m *maintenance) AlarmDisarm(ctx context.Context, am *AlarmMember) (*AlarmResponse, error) {
  104. req := &pb.AlarmRequest{
  105. Action: pb.AlarmRequest_DEACTIVATE,
  106. MemberID: am.MemberID,
  107. Alarm: am.Alarm,
  108. }
  109. if req.MemberID == 0 && req.Alarm == pb.AlarmType_NONE {
  110. ar, err := m.AlarmList(ctx)
  111. if err != nil {
  112. return nil, toErr(ctx, err)
  113. }
  114. ret := AlarmResponse{}
  115. for _, am := range ar.Alarms {
  116. dresp, derr := m.AlarmDisarm(ctx, (*AlarmMember)(am))
  117. if derr != nil {
  118. return nil, toErr(ctx, derr)
  119. }
  120. ret.Alarms = append(ret.Alarms, dresp.Alarms...)
  121. }
  122. return &ret, nil
  123. }
  124. resp, err := m.remote.Alarm(ctx, req, m.callOpts...)
  125. if err == nil {
  126. return (*AlarmResponse)(resp), nil
  127. }
  128. return nil, toErr(ctx, err)
  129. }
  130. func (m *maintenance) Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error) {
  131. remote, cancel, err := m.dial(endpoint)
  132. if err != nil {
  133. return nil, toErr(ctx, err)
  134. }
  135. defer cancel()
  136. resp, err := remote.Defragment(ctx, &pb.DefragmentRequest{}, m.callOpts...)
  137. if err != nil {
  138. return nil, toErr(ctx, err)
  139. }
  140. return (*DefragmentResponse)(resp), nil
  141. }
  142. func (m *maintenance) Status(ctx context.Context, endpoint string) (*StatusResponse, error) {
  143. remote, cancel, err := m.dial(endpoint)
  144. if err != nil {
  145. return nil, toErr(ctx, err)
  146. }
  147. defer cancel()
  148. resp, err := remote.Status(ctx, &pb.StatusRequest{}, m.callOpts...)
  149. if err != nil {
  150. return nil, toErr(ctx, err)
  151. }
  152. return (*StatusResponse)(resp), nil
  153. }
  154. func (m *maintenance) HashKV(ctx context.Context, endpoint string, rev int64) (*HashKVResponse, error) {
  155. remote, cancel, err := m.dial(endpoint)
  156. if err != nil {
  157. return nil, toErr(ctx, err)
  158. }
  159. defer cancel()
  160. resp, err := remote.HashKV(ctx, &pb.HashKVRequest{Revision: rev}, m.callOpts...)
  161. if err != nil {
  162. return nil, toErr(ctx, err)
  163. }
  164. return (*HashKVResponse)(resp), nil
  165. }
  166. func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
  167. ss, err := m.remote.Snapshot(ctx, &pb.SnapshotRequest{}, append(m.callOpts, withMax(defaultStreamMaxRetries))...)
  168. if err != nil {
  169. return nil, toErr(ctx, err)
  170. }
  171. pr, pw := io.Pipe()
  172. go func() {
  173. for {
  174. resp, err := ss.Recv()
  175. if err != nil {
  176. pw.CloseWithError(err)
  177. return
  178. }
  179. if resp == nil && err == nil {
  180. break
  181. }
  182. if _, werr := pw.Write(resp.Blob); werr != nil {
  183. pw.CloseWithError(werr)
  184. return
  185. }
  186. }
  187. pw.Close()
  188. }()
  189. return &snapshotReadCloser{ctx: ctx, ReadCloser: pr}, nil
  190. }
  191. type snapshotReadCloser struct {
  192. ctx context.Context
  193. io.ReadCloser
  194. }
  195. func (rc *snapshotReadCloser) Read(p []byte) (n int, err error) {
  196. n, err = rc.ReadCloser.Read(p)
  197. return n, toErr(rc.ctx, err)
  198. }
  199. func (m *maintenance) MoveLeader(ctx context.Context, transfereeID uint64) (*MoveLeaderResponse, error) {
  200. resp, err := m.remote.MoveLeader(ctx, &pb.MoveLeaderRequest{TargetID: transfereeID}, m.callOpts...)
  201. return (*MoveLeaderResponse)(resp), toErr(ctx, err)
  202. }