2016-05-13 06:50:58 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-03-10 20:29:30 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package clientv3
|
|
|
|
|
|
|
|
import (
|
2016-04-08 10:54:05 +03:00
|
|
|
"io"
|
2016-03-30 05:24:44 +03:00
|
|
|
|
2016-03-10 20:29:30 +03:00
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
2017-03-27 23:45:39 +03:00
|
|
|
|
2016-03-23 03:10:28 +03:00
|
|
|
"golang.org/x/net/context"
|
2016-07-15 03:47:30 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-03-10 20:29:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
DefragmentResponse pb.DefragmentResponse
|
2016-03-30 05:24:44 +03:00
|
|
|
AlarmResponse pb.AlarmResponse
|
|
|
|
AlarmMember pb.AlarmMember
|
2016-04-03 08:46:34 +03:00
|
|
|
StatusResponse pb.StatusResponse
|
2017-06-23 22:56:33 +03:00
|
|
|
MoveLeaderResponse pb.MoveLeaderResponse
|
2016-03-10 20:29:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Maintenance interface {
|
2016-03-30 05:24:44 +03:00
|
|
|
// AlarmList gets all active alarms.
|
|
|
|
AlarmList(ctx context.Context) (*AlarmResponse, error)
|
|
|
|
|
|
|
|
// AlarmDisarm disarms a given alarm.
|
|
|
|
AlarmDisarm(ctx context.Context, m *AlarmMember) (*AlarmResponse, error)
|
|
|
|
|
2016-03-10 20:29:30 +03:00
|
|
|
// Defragment defragments storage backend of the etcd member with given endpoint.
|
|
|
|
// Defragment is only needed when deleting a large number of keys and want to reclaim
|
|
|
|
// the resources.
|
|
|
|
// Defragment is an expensive operation. User should avoid defragmenting multiple members
|
|
|
|
// at the same time.
|
|
|
|
// To defragment multiple members in the cluster, user need to call defragment multiple
|
|
|
|
// times with different endpoints.
|
|
|
|
Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error)
|
2016-04-03 08:46:34 +03:00
|
|
|
|
2016-04-13 01:57:24 +03:00
|
|
|
// Status gets the status of the endpoint.
|
2016-04-03 08:46:34 +03:00
|
|
|
Status(ctx context.Context, endpoint string) (*StatusResponse, error)
|
2016-04-08 10:54:05 +03:00
|
|
|
|
|
|
|
// Snapshot provides a reader for a snapshot of a backend.
|
|
|
|
Snapshot(ctx context.Context) (io.ReadCloser, error)
|
2017-06-23 22:56:33 +03:00
|
|
|
|
|
|
|
// MoveLeader requests current leader to transfer its leadership to the transferee.
|
|
|
|
// Request must be made to the leader.
|
|
|
|
MoveLeader(ctx context.Context, transfereeID uint64) (*MoveLeaderResponse, error)
|
2016-03-10 20:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type maintenance struct {
|
2017-03-27 23:45:39 +03:00
|
|
|
dial func(endpoint string) (pb.MaintenanceClient, func(), error)
|
2016-03-30 05:24:44 +03:00
|
|
|
remote pb.MaintenanceClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMaintenance(c *Client) Maintenance {
|
2017-03-27 23:45:39 +03:00
|
|
|
return &maintenance{
|
|
|
|
dial: func(endpoint string) (pb.MaintenanceClient, func(), error) {
|
|
|
|
conn, err := c.dial(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cancel := func() { conn.Close() }
|
|
|
|
return pb.NewMaintenanceClient(conn), cancel, nil
|
|
|
|
},
|
|
|
|
remote: pb.NewMaintenanceClient(c.conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMaintenanceFromMaintenanceClient(remote pb.MaintenanceClient) Maintenance {
|
|
|
|
return &maintenance{
|
|
|
|
dial: func(string) (pb.MaintenanceClient, func(), error) {
|
|
|
|
return remote, func() {}, nil
|
|
|
|
},
|
|
|
|
remote: remote,
|
|
|
|
}
|
2016-03-30 05:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *maintenance) AlarmList(ctx context.Context) (*AlarmResponse, error) {
|
|
|
|
req := &pb.AlarmRequest{
|
|
|
|
Action: pb.AlarmRequest_GET,
|
|
|
|
MemberID: 0, // all
|
|
|
|
Alarm: pb.AlarmType_NONE, // all
|
|
|
|
}
|
|
|
|
for {
|
2016-07-15 03:47:30 +03:00
|
|
|
resp, err := m.remote.Alarm(ctx, req, grpc.FailFast(false))
|
2016-03-30 05:24:44 +03:00
|
|
|
if err == nil {
|
|
|
|
return (*AlarmResponse)(resp), nil
|
|
|
|
}
|
2016-04-28 22:17:41 +03:00
|
|
|
if isHaltErr(ctx, err) {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-03-30 05:24:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *maintenance) AlarmDisarm(ctx context.Context, am *AlarmMember) (*AlarmResponse, error) {
|
|
|
|
req := &pb.AlarmRequest{
|
|
|
|
Action: pb.AlarmRequest_DEACTIVATE,
|
|
|
|
MemberID: am.MemberID,
|
|
|
|
Alarm: am.Alarm,
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.MemberID == 0 && req.Alarm == pb.AlarmType_NONE {
|
|
|
|
ar, err := m.AlarmList(ctx)
|
|
|
|
if err != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-03-30 05:24:44 +03:00
|
|
|
}
|
|
|
|
ret := AlarmResponse{}
|
|
|
|
for _, am := range ar.Alarms {
|
|
|
|
dresp, derr := m.AlarmDisarm(ctx, (*AlarmMember)(am))
|
|
|
|
if derr != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, derr)
|
2016-03-30 05:24:44 +03:00
|
|
|
}
|
|
|
|
ret.Alarms = append(ret.Alarms, dresp.Alarms...)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:47:30 +03:00
|
|
|
resp, err := m.remote.Alarm(ctx, req, grpc.FailFast(false))
|
2016-03-30 05:24:44 +03:00
|
|
|
if err == nil {
|
|
|
|
return (*AlarmResponse)(resp), nil
|
|
|
|
}
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-03-10 20:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *maintenance) Defragment(ctx context.Context, endpoint string) (*DefragmentResponse, error) {
|
2017-03-27 23:45:39 +03:00
|
|
|
remote, cancel, err := m.dial(endpoint)
|
2016-03-10 20:29:30 +03:00
|
|
|
if err != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-03-10 20:29:30 +03:00
|
|
|
}
|
2017-03-27 23:45:39 +03:00
|
|
|
defer cancel()
|
2016-07-15 03:47:30 +03:00
|
|
|
resp, err := remote.Defragment(ctx, &pb.DefragmentRequest{}, grpc.FailFast(false))
|
2016-03-10 20:29:30 +03:00
|
|
|
if err != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-03-10 20:29:30 +03:00
|
|
|
}
|
|
|
|
return (*DefragmentResponse)(resp), nil
|
|
|
|
}
|
2016-03-30 05:24:44 +03:00
|
|
|
|
2016-04-03 08:46:34 +03:00
|
|
|
func (m *maintenance) Status(ctx context.Context, endpoint string) (*StatusResponse, error) {
|
2017-03-27 23:45:39 +03:00
|
|
|
remote, cancel, err := m.dial(endpoint)
|
2016-04-03 08:46:34 +03:00
|
|
|
if err != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-04-03 08:46:34 +03:00
|
|
|
}
|
2017-03-27 23:45:39 +03:00
|
|
|
defer cancel()
|
2016-07-15 03:47:30 +03:00
|
|
|
resp, err := remote.Status(ctx, &pb.StatusRequest{}, grpc.FailFast(false))
|
2016-04-03 08:46:34 +03:00
|
|
|
if err != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-04-03 08:46:34 +03:00
|
|
|
}
|
|
|
|
return (*StatusResponse)(resp), nil
|
|
|
|
}
|
|
|
|
|
2016-04-08 10:54:05 +03:00
|
|
|
func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) {
|
2016-07-15 03:47:30 +03:00
|
|
|
ss, err := m.remote.Snapshot(ctx, &pb.SnapshotRequest{}, grpc.FailFast(false))
|
2016-04-08 10:54:05 +03:00
|
|
|
if err != nil {
|
2016-06-08 03:03:48 +03:00
|
|
|
return nil, toErr(ctx, err)
|
2016-04-08 10:54:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
resp, err := ss.Recv()
|
|
|
|
if err != nil {
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if resp == nil && err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, werr := pw.Write(resp.Blob); werr != nil {
|
|
|
|
pw.CloseWithError(werr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
return pr, nil
|
|
|
|
}
|
2017-06-23 22:56:33 +03:00
|
|
|
|
|
|
|
func (m *maintenance) MoveLeader(ctx context.Context, transfereeID uint64) (*MoveLeaderResponse, error) {
|
|
|
|
resp, err := m.remote.MoveLeader(ctx, &pb.MoveLeaderRequest{TargetID: transfereeID}, grpc.FailFast(false))
|
|
|
|
return (*MoveLeaderResponse)(resp), toErr(ctx, err)
|
|
|
|
}
|