2015-07-27 22:31:49 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-01-27 01:14:08 +00:00
|
|
|
"fmt"
|
2015-07-27 22:31:49 +00:00
|
|
|
"time"
|
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
2015-07-27 22:31:49 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Plan endpoint is used for plan interactions
|
|
|
|
type Plan struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
2022-12-01 15:05:15 +00:00
|
|
|
ctx *RPCContext
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
2022-02-02 20:03:18 +00:00
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
func NewPlanEndpoint(srv *Server, ctx *RPCContext) *Plan {
|
|
|
|
return &Plan{srv: srv, ctx: ctx, logger: srv.logger.Named("plan")}
|
2015-07-27 22:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Submit is used to submit a plan to the leader
|
|
|
|
func (p *Plan) Submit(args *structs.PlanRequest, reply *structs.PlanResponse) error {
|
2023-01-26 20:04:25 +00:00
|
|
|
|
|
|
|
authErr := p.srv.Authenticate(p.ctx, args)
|
|
|
|
|
2022-02-05 01:35:20 +00:00
|
|
|
// Ensure the connection was initiated by another server if TLS is used.
|
|
|
|
err := validateTLSCertificateLevel(p.srv, p.ctx, tlsCertificateLevelServer)
|
|
|
|
if err != nil {
|
2015-07-27 22:31:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-05 01:35:20 +00:00
|
|
|
if done, err := p.srv.forward("Plan.Submit", args, args, reply); done {
|
|
|
|
return err
|
2022-02-02 20:03:18 +00:00
|
|
|
}
|
2023-01-26 20:04:25 +00:00
|
|
|
p.srv.MeasureRPCRate("plan", structs.RateMetricWrite, args)
|
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-02-05 01:35:20 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "plan", "submit"}, time.Now())
|
2022-02-02 20:03:18 +00:00
|
|
|
|
2022-01-27 01:14:08 +00:00
|
|
|
if args.Plan == nil {
|
|
|
|
return fmt.Errorf("cannot submit nil plan")
|
|
|
|
}
|
|
|
|
|
2016-03-04 20:50:44 +00:00
|
|
|
// Pause the Nack timer for the eval as it is making progress as long as it
|
|
|
|
// is in the plan queue. We resume immediately after we get a result to
|
|
|
|
// handle the case that the receiving worker dies.
|
|
|
|
plan := args.Plan
|
|
|
|
id := plan.EvalID
|
|
|
|
token := plan.EvalToken
|
|
|
|
if err := p.srv.evalBroker.PauseNackTimeout(id, token); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer p.srv.evalBroker.ResumeNackTimeout(id, token)
|
|
|
|
|
2015-07-27 22:31:49 +00:00
|
|
|
// Submit the plan to the queue
|
2016-03-04 20:50:44 +00:00
|
|
|
future, err := p.srv.planQueue.Enqueue(plan)
|
2015-07-27 22:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the results
|
|
|
|
result, err := future.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Package the result
|
|
|
|
reply.Result = result
|
|
|
|
reply.Index = result.AllocIndex
|
|
|
|
return nil
|
|
|
|
}
|