nomad: adding plan endpoint

This commit is contained in:
Armon Dadgar
2015-07-27 15:31:49 -07:00
parent 7436df9b99
commit d16e96e8d8
4 changed files with 88 additions and 0 deletions

38
nomad/plan_endpoint.go Normal file
View File

@@ -0,0 +1,38 @@
package nomad
import (
"time"
"github.com/armon/go-metrics"
"github.com/hashicorp/nomad/nomad/structs"
)
// Plan endpoint is used for plan interactions
type Plan struct {
srv *Server
}
// Submit is used to submit a plan to the leader
func (p *Plan) Submit(args *structs.PlanRequest, reply *structs.PlanResponse) error {
if done, err := p.srv.forward("Plan.Submit", args, args, reply); done {
return err
}
defer metrics.MeasureSince([]string{"nomad", "plan", "submit"}, time.Now())
// Submit the plan to the queue
future, err := p.srv.planQueue.Enqueue(args.Plan)
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
}

View File

@@ -0,0 +1,33 @@
package nomad
import (
"testing"
"github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
)
func TestPlanEndpoint_Submit(t *testing.T) {
s1 := testServer(t, nil)
defer s1.Shutdown()
codec := rpcClient(t, s1)
testutil.WaitForLeader(t, s1.RPC)
// Submit a plan
plan := mockPlan()
req := &structs.PlanRequest{
Plan: plan,
WriteRequest: structs.WriteRequest{Region: "region1"},
}
var resp structs.PlanResponse
if err := msgpackrpc.CallWithCodec(codec, "Plan.Submit", req, &resp); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Index == 0 {
t.Fatalf("Bad index: %d", resp.Index)
}
if resp.Result == nil {
t.Fatalf("missing result")
}
}

View File

@@ -111,6 +111,7 @@ type endpoints struct {
Client *Client
Job *Job
Eval *Eval
Plan *Plan
}
// NewServer is used to construct a new Nomad server from the
@@ -300,12 +301,14 @@ func (s *Server) setupRPC(tlsWrap tlsutil.DCWrapper) error {
s.endpoints.Client = &Client{s}
s.endpoints.Job = &Job{s}
s.endpoints.Eval = &Eval{s}
s.endpoints.Plan = &Plan{s}
// Register the handlers
s.rpcServer.Register(s.endpoints.Status)
s.rpcServer.Register(s.endpoints.Client)
s.rpcServer.Register(s.endpoints.Job)
s.rpcServer.Register(s.endpoints.Eval)
s.rpcServer.Register(s.endpoints.Plan)
list, err := net.ListenTCP("tcp", s.config.RPCAddr)
if err != nil {

View File

@@ -186,6 +186,12 @@ type EvalDequeueRequest struct {
WriteRequest
}
// PlanRequest is used to submit an allocation plan to the leader
type PlanRequest struct {
Plan *Plan
WriteRequest
}
// GenericResponse is used to respond to a request where no
// specific response information is needed.
type GenericResponse struct {
@@ -210,6 +216,12 @@ type SingleEvalResponse struct {
QueryMeta
}
// PlanResponse is used to return from a PlanRequest
type PlanResponse struct {
Result *PlanResult
WriteMeta
}
const (
NodeStatusInit = "initializing"
NodeStatusReady = "ready"
@@ -538,7 +550,9 @@ type Plan struct {
EvalCreateIndex uint64
}
// PlanResult is the result of a plan submitted to the leader.
type PlanResult struct {
AllocIndex uint64
}
// msgpackHandle is a shared handle for encoding/decoding of structs