2015-06-07 19:14:41 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-08-17 00:40:35 +00:00
|
|
|
// ClientEndpoint endpoint is used for client interactions
|
|
|
|
type ClientEndpoint struct {
|
2015-06-07 19:14:41 +00:00
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register is used to upsert a client that is available for scheduling
|
2015-08-17 00:40:35 +00:00
|
|
|
func (c *ClientEndpoint) Register(args *structs.NodeRegisterRequest, reply *structs.NodeUpdateResponse) error {
|
2015-06-07 19:14:41 +00:00
|
|
|
if done, err := c.srv.forward("Client.Register", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client", "register"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
2015-07-04 00:47:55 +00:00
|
|
|
if args.Node == nil {
|
|
|
|
return fmt.Errorf("missing node for client registration")
|
|
|
|
}
|
|
|
|
if args.Node.ID == "" {
|
|
|
|
return fmt.Errorf("missing node ID for client registration")
|
|
|
|
}
|
|
|
|
if args.Node.Datacenter == "" {
|
2015-06-07 19:14:41 +00:00
|
|
|
return fmt.Errorf("missing datacenter for client registration")
|
|
|
|
}
|
2015-07-04 00:47:55 +00:00
|
|
|
if args.Node.Name == "" {
|
2015-06-07 19:14:41 +00:00
|
|
|
return fmt.Errorf("missing node name for client registration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default the status if none is given
|
2015-07-04 00:47:55 +00:00
|
|
|
if args.Node.Status == "" {
|
|
|
|
args.Node.Status = structs.NodeStatusInit
|
2015-06-07 19:14:41 +00:00
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
if !structs.ValidNodeStatus(args.Node.Status) {
|
|
|
|
return fmt.Errorf("invalid status for node")
|
|
|
|
}
|
2015-06-07 19:14:41 +00:00
|
|
|
|
|
|
|
// Commit this update via Raft
|
2015-07-07 16:51:42 +00:00
|
|
|
_, index, err := c.srv.raftApply(structs.NodeRegisterRequestType, args)
|
2015-06-07 19:14:41 +00:00
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: Register failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
reply.NodeModifyIndex = index
|
|
|
|
|
|
|
|
// Check if we should trigger evaluations
|
2015-08-13 23:40:51 +00:00
|
|
|
if structs.ShouldDrainNode(args.Node.Status) {
|
2015-08-06 23:39:20 +00:00
|
|
|
evalIDs, evalIndex, err := c.createNodeEvals(args.Node.ID, index)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: eval creation failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.EvalIDs = evalIDs
|
|
|
|
reply.EvalCreateIndex = evalIndex
|
|
|
|
}
|
2015-07-06 20:34:32 +00:00
|
|
|
|
2015-08-23 00:37:50 +00:00
|
|
|
// Check if we need to setup a heartbeat
|
|
|
|
if !args.Node.TerminalStatus() {
|
|
|
|
ttl, err := c.srv.resetHeartbeatTimer(args.Node.ID)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: heartbeat reset failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.HeartbeatTTL = ttl
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:34:32 +00:00
|
|
|
// Set the reply index
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:42:33 +00:00
|
|
|
// Deregister is used to remove a client from the client. If a client should
|
|
|
|
// just be made unavailable for scheduling, a status update is prefered.
|
2015-08-17 00:40:35 +00:00
|
|
|
func (c *ClientEndpoint) Deregister(args *structs.NodeDeregisterRequest, reply *structs.NodeUpdateResponse) error {
|
2015-07-06 20:42:33 +00:00
|
|
|
if done, err := c.srv.forward("Client.Deregister", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client", "deregister"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.NodeID == "" {
|
|
|
|
return fmt.Errorf("missing node ID for client deregistration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit this update via Raft
|
2015-07-07 16:51:42 +00:00
|
|
|
_, index, err := c.srv.raftApply(structs.NodeDeregisterRequestType, args)
|
2015-07-06 20:42:33 +00:00
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: Deregister failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:37:50 +00:00
|
|
|
// Clear the heartbeat timer if any
|
|
|
|
c.srv.clearHeartbeatTimer(args.NodeID)
|
|
|
|
|
2015-08-06 23:39:20 +00:00
|
|
|
// Create the evaluations for this node
|
|
|
|
evalIDs, evalIndex, err := c.createNodeEvals(args.NodeID, index)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: eval creation failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the reply
|
|
|
|
reply.EvalIDs = evalIDs
|
|
|
|
reply.EvalCreateIndex = evalIndex
|
|
|
|
reply.NodeModifyIndex = index
|
2015-07-06 20:42:33 +00:00
|
|
|
reply.Index = index
|
2015-06-07 19:14:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-06 20:50:40 +00:00
|
|
|
|
|
|
|
// UpdateStatus is used to update the status of a client node
|
2015-08-17 00:40:35 +00:00
|
|
|
func (c *ClientEndpoint) UpdateStatus(args *structs.NodeUpdateStatusRequest, reply *structs.NodeUpdateResponse) error {
|
2015-07-06 20:50:40 +00:00
|
|
|
if done, err := c.srv.forward("Client.UpdateStatus", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client", "update_status"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.NodeID == "" {
|
|
|
|
return fmt.Errorf("missing node ID for client deregistration")
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
if !structs.ValidNodeStatus(args.Status) {
|
2015-07-06 20:50:40 +00:00
|
|
|
return fmt.Errorf("invalid status for node")
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
// Look for the node
|
|
|
|
snap, err := c.srv.fsm.State().Snapshot()
|
2015-07-06 20:50:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-23 00:49:48 +00:00
|
|
|
node, err := snap.GetNodeByID(args.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("node not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit this update via Raft
|
|
|
|
var index uint64
|
|
|
|
if node.Status != args.Status {
|
|
|
|
_, index, err = c.srv.raftApply(structs.NodeUpdateStatusRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: status update failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.NodeModifyIndex = index
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
|
|
|
|
// Check if we should trigger evaluations
|
2015-08-13 23:40:51 +00:00
|
|
|
if structs.ShouldDrainNode(args.Status) {
|
2015-08-06 23:39:20 +00:00
|
|
|
evalIDs, evalIndex, err := c.createNodeEvals(args.NodeID, index)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: eval creation failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.EvalIDs = evalIDs
|
|
|
|
reply.EvalCreateIndex = evalIndex
|
|
|
|
}
|
2015-07-06 20:50:40 +00:00
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
// Check if we need to setup a heartbeat
|
|
|
|
if args.Status != structs.NodeStatusDown {
|
|
|
|
ttl, err := c.srv.resetHeartbeatTimer(args.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: heartbeat reset failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.HeartbeatTTL = ttl
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:50:40 +00:00
|
|
|
// Set the reply index
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-06 21:23:15 +00:00
|
|
|
|
2015-08-16 01:20:35 +00:00
|
|
|
// Evaluate is used to force a re-evaluation of the node
|
2015-08-17 00:40:35 +00:00
|
|
|
func (c *ClientEndpoint) Evaluate(args *structs.NodeEvaluateRequest, reply *structs.NodeUpdateResponse) error {
|
2015-08-16 01:20:35 +00:00
|
|
|
if done, err := c.srv.forward("Client.Evaluate", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client", "evaluate"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.NodeID == "" {
|
|
|
|
return fmt.Errorf("missing node ID for evaluation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the node
|
|
|
|
snap, err := c.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, err := snap.GetNodeByID(args.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("node not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the evaluation
|
|
|
|
evalIDs, evalIndex, err := c.createNodeEvals(args.NodeID, node.ModifyIndex)
|
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] nomad.client: eval creation failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.EvalIDs = evalIDs
|
|
|
|
reply.EvalCreateIndex = evalIndex
|
|
|
|
|
|
|
|
// Set the reply index
|
|
|
|
reply.Index = evalIndex
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:23:15 +00:00
|
|
|
// GetNode is used to request information about a specific ndoe
|
2015-08-17 00:40:35 +00:00
|
|
|
func (c *ClientEndpoint) GetNode(args *structs.NodeSpecificRequest,
|
2015-07-06 21:23:15 +00:00
|
|
|
reply *structs.SingleNodeResponse) error {
|
|
|
|
if done, err := c.srv.forward("Client.GetNode", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client", "get_node"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.NodeID == "" {
|
|
|
|
return fmt.Errorf("missing node ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the node
|
2015-07-06 21:38:57 +00:00
|
|
|
snap, err := c.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out, err := snap.GetNodeByID(args.NodeID)
|
2015-07-06 21:23:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the output
|
|
|
|
if out != nil {
|
|
|
|
reply.Node = out
|
|
|
|
reply.Index = out.ModifyIndex
|
|
|
|
} else {
|
2015-07-06 21:38:57 +00:00
|
|
|
// Use the last index that affected the nodes table
|
|
|
|
index, err := snap.GetIndex("nodes")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
2015-07-06 21:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
c.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
|
2015-08-23 02:17:49 +00:00
|
|
|
// GetAllocs is used to request allocations for a specific ndoe
|
|
|
|
func (c *ClientEndpoint) GetAllocs(args *structs.NodeSpecificRequest,
|
|
|
|
reply *structs.NodeAllocsResponse) error {
|
|
|
|
if done, err := c.srv.forward("Client.GetAllocs", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client", "get_allocs"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.NodeID == "" {
|
|
|
|
return fmt.Errorf("missing node ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
allocWatch: args.NodeID,
|
|
|
|
run: func() error {
|
|
|
|
// Look for the node
|
|
|
|
snap, err := c.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
allocs, err := snap.AllocsByNode(args.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the output
|
|
|
|
if len(allocs) != 0 {
|
|
|
|
reply.Allocs = allocs
|
|
|
|
for _, alloc := range allocs {
|
|
|
|
reply.Index = maxUint64(reply.Index, alloc.ModifyIndex)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reply.Allocs = nil
|
|
|
|
|
|
|
|
// Use the last index that affected the nodes table
|
|
|
|
index, err := snap.GetIndex("allocs")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must provide non-zero index to prevent blocking
|
|
|
|
// Index 1 is impossible anyways (due to Raft internals)
|
|
|
|
if index == 0 {
|
|
|
|
reply.Index = 1
|
|
|
|
} else {
|
|
|
|
reply.Index = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return c.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:39:20 +00:00
|
|
|
// createNodeEvals is used to create evaluations for each alloc on a node.
|
|
|
|
// Each Eval is scoped to a job, so we need to potentially trigger many evals.
|
2015-08-17 00:40:35 +00:00
|
|
|
func (c *ClientEndpoint) createNodeEvals(nodeID string, nodeIndex uint64) ([]string, uint64, error) {
|
2015-08-06 23:39:20 +00:00
|
|
|
// Snapshot the state
|
|
|
|
snap, err := c.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("failed to snapshot state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all the allocations for this node
|
|
|
|
allocs, err := snap.AllocsByNode(nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("failed to find allocs for '%s': %v", nodeID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path if nothing to do
|
|
|
|
if len(allocs) == 0 {
|
|
|
|
return nil, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an eval for each JobID affected
|
|
|
|
var evals []*structs.Evaluation
|
|
|
|
var evalIDs []string
|
|
|
|
jobIDs := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, alloc := range allocs {
|
|
|
|
// Deduplicate on JobID
|
|
|
|
if _, ok := jobIDs[alloc.JobID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
jobIDs[alloc.JobID] = struct{}{}
|
|
|
|
|
|
|
|
// Create a new eval
|
|
|
|
eval := &structs.Evaluation{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Priority: alloc.Job.Priority,
|
|
|
|
Type: alloc.Job.Type,
|
|
|
|
TriggeredBy: structs.EvalTriggerNodeUpdate,
|
|
|
|
JobID: alloc.JobID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
NodeModifyIndex: nodeIndex,
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
}
|
|
|
|
evals = append(evals, eval)
|
|
|
|
evalIDs = append(evalIDs, eval.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Raft transaction
|
|
|
|
update := &structs.EvalUpdateRequest{
|
|
|
|
Evals: evals,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: c.srv.config.Region},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit this evaluation via Raft
|
2015-08-16 01:03:05 +00:00
|
|
|
// XXX: There is a risk of partial failure where the node update succeeds
|
|
|
|
// but that the EvalUpdate does not.
|
2015-08-06 23:39:20 +00:00
|
|
|
_, evalIndex, err := c.srv.raftApply(structs.EvalUpdateRequestType, update)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return evalIDs, evalIndex, nil
|
|
|
|
}
|