maintenance.go 4.9 KB

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