maintenance.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 releases wasted space from internal fragmentation on a given etcd member.
  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 point-in-time snapshot of etcd.
  43. Snapshot(ctx context.Context) (io.ReadCloser, error)
  44. }
  45. type maintenance struct {
  46. dial func(endpoint string) (pb.MaintenanceClient, func(), error)
  47. remote pb.MaintenanceClient
  48. callOpts []grpc.CallOption
  49. }
  50. func NewMaintenance(c *Client) Maintenance {
  51. api := &maintenance{
  52. dial: func(endpoint string) (pb.MaintenanceClient, func(), error) {
  53. conn, err := c.dial(endpoint)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. cancel := func() { conn.Close() }
  58. return RetryMaintenanceClient(c, conn), cancel, nil
  59. },
  60. remote: RetryMaintenanceClient(c, c.conn),
  61. }
  62. if c != nil {
  63. api.callOpts = c.callOpts
  64. }
  65. return api
  66. }
  67. func NewMaintenanceFromMaintenanceClient(remote pb.MaintenanceClient, c *Client) Maintenance {
  68. api := &maintenance{
  69. dial: func(string) (pb.MaintenanceClient, func(), error) {
  70. return remote, func() {}, nil
  71. },
  72. remote: remote,
  73. }
  74. if c != nil {
  75. api.callOpts = c.callOpts
  76. }
  77. return api
  78. }
  79. func (m *maintenance) AlarmList(ctx context.Context) (*AlarmResponse, error) {
  80. req := &pb.AlarmRequest{
  81. Action: pb.AlarmRequest_GET,
  82. MemberID: 0, // all
  83. Alarm: pb.AlarmType_NONE, // all
  84. }
  85. resp, err := m.remote.Alarm(ctx, req, m.callOpts...)
  86. if err == nil {
  87. return (*AlarmResponse)(resp), nil
  88. }
  89. return nil, toErr(ctx, err)
  90. }
  91. func (m *maintenance) AlarmDisarm(ctx context.Context, am *AlarmMember) (*AlarmResponse, error) {
  92. req := &pb.AlarmRequest{
  93. Action: pb.AlarmRequest_DEACTIVATE,
  94. MemberID: am.MemberID,
  95. Alarm: am.Alarm,
  96. }
  97. if req.MemberID == 0 && req.Alarm == pb.AlarmType_NONE {
  98. ar, err := m.AlarmList(ctx)
  99. if err != nil {
  100. return nil, toErr(ctx, err)
  101. }
  102. ret := AlarmResponse{}
  103. for _, am := range ar.Alarms {
  104. dresp, derr := m.AlarmDisarm(ctx, (*AlarmMember)(am))
  105. if derr != nil {
  106. return nil, toErr(ctx, derr)
  107. }
  108. ret.Alarms = append(ret.Alarms, dresp.Alarms...)
  109. }
  110. return &ret, nil
  111. }
  112. resp, err := m.remote.Alarm(ctx, req, m.callOpts...)
  113. if err == nil {
  114. return (*AlarmResponse)(resp), nil
  115. }
  116. return nil, toErr(ctx, err)
  117. }
  118. func (m *maintenance) Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error) {
  119. remote, cancel, err := m.dial(endpoint)
  120. if err != nil {
  121. return nil, toErr(ctx, err)
  122. }
  123. defer cancel()
  124. resp, err := remote.Defragment(ctx, &pb.DefragmentRequest{}, m.callOpts...)
  125. if err != nil {
  126. return nil, toErr(ctx, err)
  127. }
  128. return (*DefragmentResponse)(resp), nil
  129. }
  130. func (m *maintenance) Status(ctx context.Context, endpoint string) (*StatusResponse, 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.Status(ctx, &pb.StatusRequest{}, m.callOpts...)
  137. if err != nil {
  138. return nil, toErr(ctx, err)
  139. }
  140. return (*StatusResponse)(resp), nil
  141. }
  142. func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
  143. ss, err := m.remote.Snapshot(ctx, &pb.SnapshotRequest{}, m.callOpts...)
  144. if err != nil {
  145. return nil, toErr(ctx, err)
  146. }
  147. pr, pw := io.Pipe()
  148. go func() {
  149. for {
  150. resp, err := ss.Recv()
  151. if err != nil {
  152. pw.CloseWithError(err)
  153. return
  154. }
  155. if resp == nil && err == nil {
  156. break
  157. }
  158. if _, werr := pw.Write(resp.Blob); werr != nil {
  159. pw.CloseWithError(werr)
  160. return
  161. }
  162. }
  163. pw.Close()
  164. }()
  165. return pr, nil
  166. }