2017-02-10 01:58:20 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2020-06-04 21:14:43 +00:00
|
|
|
"errors"
|
2017-02-10 01:58:20 +00:00
|
|
|
"fmt"
|
2020-05-21 14:05:04 +00:00
|
|
|
"io"
|
2017-02-10 01:58:20 +00:00
|
|
|
"net"
|
2020-06-04 22:11:17 +00:00
|
|
|
"time"
|
2017-02-10 01:58:20 +00:00
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2020-05-21 14:05:04 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilot"
|
2020-06-04 21:14:43 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2020-05-21 14:05:04 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/snapshot"
|
2017-02-10 01:58:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Operator endpoint is used to perform low-level operator tasks for Nomad.
|
|
|
|
type Operator struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
|
|
|
logger log.Logger
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 14:05:04 +00:00
|
|
|
func (op *Operator) register() {
|
|
|
|
op.srv.streamingRpcs.Register("Operator.SnapshotSave", op.snapshotSave)
|
2020-06-04 21:14:43 +00:00
|
|
|
op.srv.streamingRpcs.Register("Operator.SnapshotRestore", op.snapshotRestore)
|
2020-05-21 14:05:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
// RaftGetConfiguration is used to retrieve the current Raft configuration.
|
|
|
|
func (op *Operator) RaftGetConfiguration(args *structs.GenericRequest, reply *structs.RaftConfigurationResponse) error {
|
|
|
|
if done, err := op.srv.forward("Operator.RaftGetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:02:59 +00:00
|
|
|
// Check management permissions
|
2017-10-12 22:16:33 +00:00
|
|
|
if aclObj, err := op.srv.ResolveToken(args.AuthToken); err != nil {
|
2017-10-06 23:02:59 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
// We can't fetch the leader and the configuration atomically with
|
|
|
|
// the current Raft API.
|
|
|
|
future := op.srv.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index the Nomad information about the servers.
|
|
|
|
serverMap := make(map[raft.ServerAddress]serf.Member)
|
|
|
|
for _, member := range op.srv.serf.Members() {
|
|
|
|
valid, parts := isNomadServer(member)
|
|
|
|
if !valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := (&net.TCPAddr{IP: member.Addr, Port: parts.Port}).String()
|
|
|
|
serverMap[raft.ServerAddress(addr)] = member
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill out the reply.
|
|
|
|
leader := op.srv.raft.Leader()
|
|
|
|
reply.Index = future.Index()
|
|
|
|
for _, server := range future.Configuration().Servers {
|
|
|
|
node := "(unknown)"
|
2017-11-22 00:29:11 +00:00
|
|
|
raftProtocolVersion := "unknown"
|
2017-02-10 01:58:20 +00:00
|
|
|
if member, ok := serverMap[server.Address]; ok {
|
|
|
|
node = member.Name
|
2017-11-27 22:46:37 +00:00
|
|
|
if raftVsn, ok := member.Tags["raft_vsn"]; ok {
|
|
|
|
raftProtocolVersion = raftVsn
|
|
|
|
}
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry := &structs.RaftServer{
|
2017-11-27 22:46:37 +00:00
|
|
|
ID: server.ID,
|
|
|
|
Node: node,
|
|
|
|
Address: server.Address,
|
|
|
|
Leader: server.Address == leader,
|
|
|
|
Voter: server.Suffrage == raft.Voter,
|
2017-11-22 00:29:11 +00:00
|
|
|
RaftProtocol: raftProtocolVersion,
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
|
|
|
reply.Servers = append(reply.Servers, entry)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftRemovePeerByAddress is used to kick a stale peer (one that it in the Raft
|
|
|
|
// quorum but no longer known to Serf or the catalog) by address in the form of
|
|
|
|
// "IP:port". The reply argument is not used, but it required to fulfill the RPC
|
|
|
|
// interface.
|
|
|
|
func (op *Operator) RaftRemovePeerByAddress(args *structs.RaftPeerByAddressRequest, reply *struct{}) error {
|
|
|
|
if done, err := op.srv.forward("Operator.RaftRemovePeerByAddress", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:02:59 +00:00
|
|
|
// Check management permissions
|
2017-10-12 22:16:33 +00:00
|
|
|
if aclObj, err := op.srv.ResolveToken(args.AuthToken); err != nil {
|
2017-10-06 23:02:59 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
// Since this is an operation designed for humans to use, we will return
|
|
|
|
// an error if the supplied address isn't among the peers since it's
|
|
|
|
// likely they screwed up.
|
|
|
|
{
|
|
|
|
future := op.srv.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, s := range future.Configuration().Servers {
|
|
|
|
if s.Address == args.Address {
|
|
|
|
goto REMOVE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("address %q was not found in the Raft configuration",
|
|
|
|
args.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
REMOVE:
|
|
|
|
// The Raft library itself will prevent various forms of foot-shooting,
|
|
|
|
// like making a configuration with no voters. Some consideration was
|
|
|
|
// given here to adding more checks, but it was decided to make this as
|
|
|
|
// low-level and direct as possible. We've got ACL coverage to lock this
|
|
|
|
// down, and if you are an operator, it's assumed you know what you are
|
|
|
|
// doing if you are calling this. If you remove a peer that's known to
|
|
|
|
// Serf, for example, it will come back when the leader does a reconcile
|
|
|
|
// pass.
|
|
|
|
future := op.srv.raft.RemovePeer(args.Address)
|
|
|
|
if err := future.Error(); err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
op.logger.Warn("failed to remove Raft peer", "peer", args.Address, "error", err)
|
2017-02-10 01:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
op.logger.Warn("removed Raft peer", "peer", args.Address)
|
2017-02-10 01:58:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
// RaftRemovePeerByID is used to kick a stale peer (one that is in the Raft
|
|
|
|
// quorum but no longer known to Serf or the catalog) by address in the form of
|
|
|
|
// "IP:port". The reply argument is not used, but is required to fulfill the RPC
|
|
|
|
// interface.
|
|
|
|
func (op *Operator) RaftRemovePeerByID(args *structs.RaftPeerByIDRequest, reply *struct{}) error {
|
|
|
|
if done, err := op.srv.forward("Operator.RaftRemovePeerByID", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check management permissions
|
|
|
|
if aclObj, err := op.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since this is an operation designed for humans to use, we will return
|
|
|
|
// an error if the supplied id isn't among the peers since it's
|
|
|
|
// likely they screwed up.
|
|
|
|
var address raft.ServerAddress
|
|
|
|
{
|
|
|
|
future := op.srv.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, s := range future.Configuration().Servers {
|
|
|
|
if s.ID == args.ID {
|
|
|
|
address = s.Address
|
|
|
|
goto REMOVE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("id %q was not found in the Raft configuration",
|
|
|
|
args.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
REMOVE:
|
|
|
|
// The Raft library itself will prevent various forms of foot-shooting,
|
|
|
|
// like making a configuration with no voters. Some consideration was
|
|
|
|
// given here to adding more checks, but it was decided to make this as
|
|
|
|
// low-level and direct as possible. We've got ACL coverage to lock this
|
|
|
|
// down, and if you are an operator, it's assumed you know what you are
|
|
|
|
// doing if you are calling this. If you remove a peer that's known to
|
|
|
|
// Serf, for example, it will come back when the leader does a reconcile
|
|
|
|
// pass.
|
|
|
|
minRaftProtocol, err := op.srv.autopilot.MinRaftProtocol()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var future raft.Future
|
|
|
|
if minRaftProtocol >= 2 {
|
|
|
|
future = op.srv.raft.RemoveServer(args.ID, 0, 0)
|
|
|
|
} else {
|
|
|
|
future = op.srv.raft.RemovePeer(address)
|
|
|
|
}
|
|
|
|
if err := future.Error(); err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
op.logger.Warn("failed to remove Raft peer", "peer_id", args.ID, "error", err)
|
2018-01-16 21:35:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
op.logger.Warn("removed Raft peer", "peer_id", args.ID)
|
2018-01-16 21:35:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
// AutopilotGetConfiguration is used to retrieve the current Autopilot configuration.
|
2018-01-30 03:53:34 +00:00
|
|
|
func (op *Operator) AutopilotGetConfiguration(args *structs.GenericRequest, reply *structs.AutopilotConfig) error {
|
2017-12-18 21:16:23 +00:00
|
|
|
if done, err := op.srv.forward("Operator.AutopilotGetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
|
|
|
rule, err := op.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule != nil && !rule.AllowOperatorRead() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
state := op.srv.fsm.State()
|
|
|
|
_, config, err := state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if config == nil {
|
|
|
|
return fmt.Errorf("autopilot config not initialized yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = *config
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotSetConfiguration is used to set the current Autopilot configuration.
|
|
|
|
func (op *Operator) AutopilotSetConfiguration(args *structs.AutopilotSetConfigRequest, reply *bool) error {
|
|
|
|
if done, err := op.srv.forward("Operator.AutopilotSetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator write access.
|
|
|
|
rule, err := op.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule != nil && !rule.AllowOperatorWrite() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:50:36 +00:00
|
|
|
// All servers should be at or above 0.8.0 to apply this operatation
|
2019-03-04 09:49:32 +00:00
|
|
|
if !ServersMeetMinimumVersion(op.srv.Members(), minAutopilotVersion, false) {
|
2019-01-29 21:50:36 +00:00
|
|
|
return fmt.Errorf("All servers should be running version %v to update autopilot config", minAutopilotVersion)
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
// Apply the update
|
|
|
|
resp, _, err := op.srv.raftApply(structs.AutopilotRequestType, args)
|
|
|
|
if err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
op.logger.Error("failed applying AutoPilot configuration", "error", err)
|
2017-12-18 21:16:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the return type is a bool.
|
|
|
|
if respBool, ok := resp.(bool); ok {
|
|
|
|
*reply = respBool
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerHealth is used to get the current health of the servers.
|
|
|
|
func (op *Operator) ServerHealth(args *structs.GenericRequest, reply *autopilot.OperatorHealthReply) error {
|
|
|
|
// This must be sent to the leader, so we fix the args since we are
|
|
|
|
// re-using a structure where we don't support all the options.
|
|
|
|
args.AllowStale = false
|
|
|
|
if done, err := op.srv.forward("Operator.ServerHealth", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
|
|
|
rule, err := op.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule != nil && !rule.AllowOperatorRead() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit early if the min Raft version is too low
|
|
|
|
minRaftProtocol, err := op.srv.autopilot.MinRaftProtocol()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting server raft protocol versions: %s", err)
|
|
|
|
}
|
|
|
|
if minRaftProtocol < 3 {
|
|
|
|
return fmt.Errorf("all servers must have raft_protocol set to 3 or higher to use this endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = op.srv.autopilot.GetClusterHealth()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
// SchedulerSetConfiguration is used to set the current Scheduler configuration.
|
2018-11-10 16:31:10 +00:00
|
|
|
func (op *Operator) SchedulerSetConfiguration(args *structs.SchedulerSetConfigRequest, reply *structs.SchedulerSetConfigurationResponse) error {
|
2018-09-28 04:27:38 +00:00
|
|
|
if done, err := op.srv.forward("Operator.SchedulerSetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator write access.
|
|
|
|
rule, err := op.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-18 03:45:40 +00:00
|
|
|
} else if rule != nil && !rule.AllowOperatorWrite() {
|
2018-09-28 04:27:38 +00:00
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:36:09 +00:00
|
|
|
// All servers should be at or above 0.9.0 to apply this operation
|
2019-03-04 09:49:32 +00:00
|
|
|
if !ServersMeetMinimumVersion(op.srv.Members(), minSchedulerConfigVersion, false) {
|
2019-01-29 21:50:36 +00:00
|
|
|
return fmt.Errorf("All servers should be running version %v to update scheduler config", minSchedulerConfigVersion)
|
|
|
|
}
|
2020-07-20 13:25:09 +00:00
|
|
|
|
2018-09-28 04:27:38 +00:00
|
|
|
// Apply the update
|
2018-11-10 16:31:10 +00:00
|
|
|
resp, index, err := op.srv.raftApply(structs.SchedulerConfigRequestType, args)
|
2018-09-28 04:27:38 +00:00
|
|
|
if err != nil {
|
|
|
|
op.logger.Error("failed applying Scheduler configuration", "error", err)
|
|
|
|
return err
|
2018-10-18 03:45:40 +00:00
|
|
|
} else if respErr, ok := resp.(error); ok {
|
2018-09-28 04:27:38 +00:00
|
|
|
return respErr
|
|
|
|
}
|
2020-05-19 19:05:24 +00:00
|
|
|
// If CAS request, raft returns a boolean indicating if the update was applied.
|
|
|
|
// Otherwise, assume success
|
2020-05-19 16:36:09 +00:00
|
|
|
reply.Updated = true
|
2018-09-28 04:27:38 +00:00
|
|
|
if respBool, ok := resp.(bool); ok {
|
2018-11-10 16:31:10 +00:00
|
|
|
reply.Updated = respBool
|
2018-09-28 04:27:38 +00:00
|
|
|
}
|
2022-07-06 14:13:48 +00:00
|
|
|
|
2018-11-10 16:31:10 +00:00
|
|
|
reply.Index = index
|
2022-07-06 14:13:48 +00:00
|
|
|
|
|
|
|
// If we updated the configuration, handle any required state changes within
|
|
|
|
// the eval broker and blocked evals processes. The state change and
|
|
|
|
// restore functions have protections around leadership transitions and
|
|
|
|
// restoring into non-running brokers.
|
|
|
|
if reply.Updated {
|
|
|
|
if op.srv.handleEvalBrokerStateChange(&args.Config) {
|
|
|
|
return op.srv.restoreEvals()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 04:27:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SchedulerGetConfiguration is used to retrieve the current Scheduler configuration.
|
2018-10-29 18:10:43 +00:00
|
|
|
func (op *Operator) SchedulerGetConfiguration(args *structs.GenericRequest, reply *structs.SchedulerConfigurationResponse) error {
|
2018-09-28 04:27:38 +00:00
|
|
|
if done, err := op.srv.forward("Operator.SchedulerGetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
|
|
|
rule, err := op.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-18 03:45:40 +00:00
|
|
|
} else if rule != nil && !rule.AllowOperatorRead() {
|
2018-09-28 04:27:38 +00:00
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
state := op.srv.fsm.State()
|
2018-11-10 16:31:10 +00:00
|
|
|
index, config, err := state.SchedulerConfig()
|
|
|
|
|
2018-09-28 04:27:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-18 03:45:40 +00:00
|
|
|
} else if config == nil {
|
2018-09-28 04:27:38 +00:00
|
|
|
return fmt.Errorf("scheduler config not initialized yet")
|
|
|
|
}
|
|
|
|
|
2018-11-10 16:31:10 +00:00
|
|
|
reply.SchedulerConfig = config
|
|
|
|
reply.QueryMeta.Index = index
|
|
|
|
op.srv.setQueryMeta(&reply.QueryMeta)
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-21 14:05:04 +00:00
|
|
|
|
|
|
|
func (op *Operator) forwardStreamingRPC(region string, method string, args interface{}, in io.ReadWriteCloser) error {
|
|
|
|
server, err := op.srv.findRegionServer(region)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return op.forwardStreamingRPCToServer(server, method, args, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *Operator) forwardStreamingRPCToServer(server *serverParts, method string, args interface{}, in io.ReadWriteCloser) error {
|
|
|
|
srvConn, err := op.srv.streamingRpc(server, method)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srvConn.Close()
|
|
|
|
|
|
|
|
outEncoder := codec.NewEncoder(srvConn, structs.MsgpackHandle)
|
|
|
|
if err := outEncoder.Encode(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
structs.Bridge(in, srvConn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *Operator) snapshotSave(conn io.ReadWriteCloser) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
var args structs.SnapshotSaveRequest
|
|
|
|
var reply structs.SnapshotSaveResponse
|
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
|
|
|
handleFailure := func(code int, err error) {
|
|
|
|
encoder.Encode(&structs.SnapshotSaveResponse{
|
|
|
|
ErrorCode: code,
|
|
|
|
ErrorMsg: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(&args); err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward to appropriate region
|
|
|
|
if args.Region != op.srv.Region() {
|
|
|
|
err := op.forwardStreamingRPC(args.Region, "Operator.SnapshotSave", args, conn)
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// forward to leader
|
|
|
|
if !args.AllowStale {
|
|
|
|
remoteServer, err := op.srv.getLeaderForRPC()
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if remoteServer != nil {
|
|
|
|
err := op.forwardStreamingRPCToServer(remoteServer, "Operator.SnapshotSave", args, conn)
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check agent permissions
|
|
|
|
if aclObj, err := op.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
code := 500
|
|
|
|
if err == structs.ErrTokenNotFound {
|
|
|
|
code = 400
|
|
|
|
}
|
|
|
|
handleFailure(code, err)
|
|
|
|
return
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
handleFailure(403, structs.ErrPermissionDenied)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
op.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
|
|
|
|
// Take the snapshot and capture the index.
|
2020-06-01 01:38:45 +00:00
|
|
|
snap, err := snapshot.New(op.logger.Named("snapshot"), op.srv.raft)
|
2020-05-21 14:05:04 +00:00
|
|
|
reply.SnapshotChecksum = snap.Checksum()
|
|
|
|
reply.Index = snap.Index()
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer snap.Close()
|
|
|
|
|
2020-06-04 21:14:43 +00:00
|
|
|
if err := encoder.Encode(&reply); err != nil {
|
2020-05-21 14:05:04 +00:00
|
|
|
handleFailure(500, fmt.Errorf("failed to encode response: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if snap != nil {
|
|
|
|
if _, err := io.Copy(conn, snap); err != nil {
|
|
|
|
handleFailure(500, fmt.Errorf("failed to stream snapshot: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 21:14:43 +00:00
|
|
|
|
|
|
|
func (op *Operator) snapshotRestore(conn io.ReadWriteCloser) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
var args structs.SnapshotRestoreRequest
|
|
|
|
var reply structs.SnapshotRestoreResponse
|
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
|
|
|
handleFailure := func(code int, err error) {
|
|
|
|
encoder.Encode(&structs.SnapshotRestoreResponse{
|
|
|
|
ErrorCode: code,
|
|
|
|
ErrorMsg: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(&args); err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward to appropriate region
|
|
|
|
if args.Region != op.srv.Region() {
|
|
|
|
err := op.forwardStreamingRPC(args.Region, "Operator.SnapshotRestore", args, conn)
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// forward to leader
|
|
|
|
remoteServer, err := op.srv.getLeaderForRPC()
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if remoteServer != nil {
|
|
|
|
err := op.forwardStreamingRPCToServer(remoteServer, "Operator.SnapshotRestore", args, conn)
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check agent permissions
|
|
|
|
if aclObj, err := op.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
code := 500
|
|
|
|
if err == structs.ErrTokenNotFound {
|
|
|
|
code = 400
|
|
|
|
}
|
|
|
|
handleFailure(code, err)
|
|
|
|
return
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
handleFailure(403, structs.ErrPermissionDenied)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
op.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
|
|
|
|
reader, errCh := decodeStreamOutput(decoder)
|
|
|
|
|
|
|
|
err = snapshot.Restore(op.logger.Named("snapshot"), reader, op.srv.raft)
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, fmt.Errorf("failed to restore from snapshot: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = <-errCh
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(400, fmt.Errorf("failed to read stream: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:11:17 +00:00
|
|
|
// This'll be used for feedback from the leader loop.
|
|
|
|
timeoutCh := time.After(time.Minute)
|
|
|
|
|
|
|
|
lerrCh := make(chan error, 1)
|
|
|
|
|
|
|
|
select {
|
2020-06-09 16:00:09 +00:00
|
|
|
// Reassert leader actions and update all leader related state
|
|
|
|
// with new state store content.
|
2020-06-04 22:11:17 +00:00
|
|
|
case op.srv.reassertLeaderCh <- lerrCh:
|
|
|
|
|
|
|
|
// We might have lost leadership while waiting to kick the loop.
|
|
|
|
case <-timeoutCh:
|
|
|
|
handleFailure(500, fmt.Errorf("timed out waiting to re-run leader actions"))
|
|
|
|
|
|
|
|
// Make sure we don't get stuck during shutdown
|
|
|
|
case <-op.srv.shutdownCh:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
// Wait for the leader loop to finish up.
|
|
|
|
case err := <-lerrCh:
|
|
|
|
if err != nil {
|
|
|
|
handleFailure(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We might have lost leadership while the loop was doing its
|
|
|
|
// thing.
|
|
|
|
case <-timeoutCh:
|
|
|
|
handleFailure(500, fmt.Errorf("timed out waiting for re-run of leader actions"))
|
|
|
|
|
|
|
|
// Make sure we don't get stuck during shutdown
|
|
|
|
case <-op.srv.shutdownCh:
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:14:43 +00:00
|
|
|
reply.Index, _ = op.srv.State().LatestIndex()
|
|
|
|
op.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
encoder.Encode(reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeStreamOutput(decoder *codec.Decoder) (io.Reader, <-chan error) {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(errCh)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var wrapper cstructs.StreamErrWrapper
|
|
|
|
|
|
|
|
err := decoder.Decode(&wrapper)
|
|
|
|
if err != nil {
|
|
|
|
pw.CloseWithError(fmt.Errorf("failed to decode input: %v", err))
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(wrapper.Payload) != 0 {
|
|
|
|
_, err = pw.Write(wrapper.Payload)
|
|
|
|
if err != nil {
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
errCh <- err
|
2020-06-15 12:32:16 +00:00
|
|
|
return
|
2020-06-04 21:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errW := wrapper.Error; errW != nil {
|
|
|
|
if errW.Message == io.EOF.Error() {
|
|
|
|
pw.CloseWithError(io.EOF)
|
|
|
|
} else {
|
|
|
|
pw.CloseWithError(errors.New(errW.Message))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return pr, errCh
|
|
|
|
}
|