Skip to content

Commit 0d7ae8c

Browse files
committed
Making the status command return the allocs of currently registered job
1 parent 3e63908 commit 0d7ae8c

11 files changed

+109
-15
lines changed

api/jobs.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package api
22

33
import (
44
"fmt"
5+
"net/url"
56
"sort"
7+
"strconv"
68
"time"
79
)
810

@@ -89,9 +91,18 @@ func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) {
8991
}
9092

9193
// Allocations is used to return the allocs for a given job ID.
92-
func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
94+
func (j *Jobs) Allocations(jobID string, allAllocs bool, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
9395
var resp []*AllocationListStub
94-
qm, err := j.client.query("/v1/job/"+jobID+"/allocations", &resp, q)
96+
u, err := url.Parse("/v1/job/" + jobID + "/allocations")
97+
if err != nil {
98+
return nil, nil, err
99+
}
100+
101+
v := u.Query()
102+
v.Add("all", strconv.FormatBool(allAllocs))
103+
u.RawQuery = v.Encode()
104+
105+
qm, err := j.client.query(u.String(), &resp, q)
95106
if err != nil {
96107
return nil, nil, err
97108
}

command/agent/job_endpoint.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agent
22

33
import (
44
"net/http"
5+
"strconv"
56
"strings"
67

78
"github.com/hashicorp/nomad/nomad/structs"
@@ -130,8 +131,11 @@ func (s *HTTPServer) jobAllocations(resp http.ResponseWriter, req *http.Request,
130131
if req.Method != "GET" {
131132
return nil, CodedError(405, ErrInvalidMethod)
132133
}
134+
allAllocs, _ := strconv.ParseBool(req.URL.Query().Get("all"))
135+
133136
args := structs.JobSpecificRequest{
134-
JobID: jobName,
137+
JobID: jobName,
138+
AllAllocs: allAllocs,
135139
}
136140
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
137141
return nil, nil

command/fs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func (f *FSCommand) followFile(client *api.Client, alloc *api.Allocation,
348348
// but use a dead allocation if no running allocations are found
349349
func getRandomJobAlloc(client *api.Client, jobID string) (string, error) {
350350
var runningAllocs []*api.AllocationListStub
351-
allocs, _, err := client.Jobs().Allocations(jobID, nil)
351+
allocs, _, err := client.Jobs().Allocations(jobID, false, nil)
352352

353353
// Check that the job actually has allocations
354354
if len(allocs) == 0 {

command/status.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const (
2020

2121
type StatusCommand struct {
2222
Meta
23-
length int
24-
evals bool
25-
verbose bool
23+
length int
24+
evals bool
25+
allAllocs bool
26+
verbose bool
2627
}
2728

2829
func (c *StatusCommand) Help() string {
@@ -45,6 +46,9 @@ Status Options:
4546
-evals
4647
Display the evaluations associated with the job.
4748
49+
-all-allocs
50+
Display the allocs that matches the job name.
51+
4852
-verbose
4953
Display full information.
5054
`
@@ -62,6 +66,7 @@ func (c *StatusCommand) Run(args []string) int {
6266
flags.Usage = func() { c.Ui.Output(c.Help()) }
6367
flags.BoolVar(&short, "short", false, "")
6468
flags.BoolVar(&c.evals, "evals", false, "")
69+
flags.BoolVar(&c.allAllocs, "all-allocs", false, "")
6570
flags.BoolVar(&c.verbose, "verbose", false, "")
6671

6772
if err := flags.Parse(args); err != nil {
@@ -218,7 +223,7 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
218223
var evals, allocs []string
219224

220225
// Query the allocations
221-
jobAllocs, _, err := client.Jobs().Allocations(job.ID, nil)
226+
jobAllocs, _, err := client.Jobs().Allocations(job.ID, c.allAllocs, nil)
222227
if err != nil {
223228
return fmt.Errorf("Error querying job allocations: %s", err)
224229
}

nomad/job_endpoint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func (j *Job) Allocations(args *structs.JobSpecificRequest,
534534
if err != nil {
535535
return err
536536
}
537-
allocs, err := snap.AllocsByJob(args.JobID)
537+
allocs, err := snap.AllocsByJob(args.JobID, args.AllAllocs)
538538
if err != nil {
539539
return err
540540
}

nomad/state/state_store.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,19 @@ func (s *StateStore) AllocsByNodeTerminal(node string, terminal bool) ([]*struct
10731073
}
10741074

10751075
// AllocsByJob returns all the allocations by job id
1076-
func (s *StateStore) AllocsByJob(jobID string) ([]*structs.Allocation, error) {
1076+
func (s *StateStore) AllocsByJob(jobID string, all bool) ([]*structs.Allocation, error) {
10771077
txn := s.db.Txn(false)
10781078

1079+
// Get the job
1080+
var job *structs.Job
1081+
rawJob, err := txn.First("jobs", "id", jobID)
1082+
if err != nil {
1083+
return nil, err
1084+
}
1085+
if rawJob != nil {
1086+
job = rawJob.(*structs.Job)
1087+
}
1088+
10791089
// Get an iterator over the node allocations
10801090
iter, err := txn.Get("allocs", "job", jobID)
10811091
if err != nil {
@@ -1088,6 +1098,14 @@ func (s *StateStore) AllocsByJob(jobID string) ([]*structs.Allocation, error) {
10881098
if raw == nil {
10891099
break
10901100
}
1101+
1102+
alloc := raw.(*structs.Allocation)
1103+
// If the allocation belongs to a job with the same ID but a diff create
1104+
// index and we are not getting all the allocations whose Jobs matches
1105+
// the same Job ID then we skip it
1106+
if !all && job != nil && alloc.Job.CreateIndex != job.CreateIndex {
1107+
continue
1108+
}
10911109
out = append(out, raw.(*structs.Allocation))
10921110
}
10931111
return out, nil

nomad/state/state_store_test.go

+56-1
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ func TestStateStore_AllocsByJob(t *testing.T) {
23622362
t.Fatalf("err: %v", err)
23632363
}
23642364

2365-
out, err := state.AllocsByJob("foo")
2365+
out, err := state.AllocsByJob("foo", false)
23662366
if err != nil {
23672367
t.Fatalf("err: %v", err)
23682368
}
@@ -2375,6 +2375,61 @@ func TestStateStore_AllocsByJob(t *testing.T) {
23752375
}
23762376
}
23772377

2378+
func TestStateStore_AllocsForRegisteredJob(t *testing.T) {
2379+
state := testStateStore(t)
2380+
var allocs []*structs.Allocation
2381+
var allocs1 []*structs.Allocation
2382+
2383+
job := mock.Job()
2384+
job.ID = "foo"
2385+
state.UpsertJob(100, job)
2386+
for i := 0; i < 3; i++ {
2387+
alloc := mock.Alloc()
2388+
alloc.Job = job
2389+
alloc.JobID = job.ID
2390+
allocs = append(allocs, alloc)
2391+
}
2392+
if err := state.UpsertAllocs(200, allocs); err != nil {
2393+
t.Fatalf("err: %v", err)
2394+
}
2395+
2396+
if err := state.DeleteJob(250, job.ID); err != nil {
2397+
t.Fatalf("err: %v", err)
2398+
}
2399+
2400+
job1 := mock.Job()
2401+
job1.ID = "foo"
2402+
job1.CreateIndex = 50
2403+
state.UpsertJob(300, job1)
2404+
for i := 0; i < 4; i++ {
2405+
alloc := mock.Alloc()
2406+
alloc.Job = job1
2407+
alloc.JobID = job1.ID
2408+
allocs1 = append(allocs1, alloc)
2409+
}
2410+
2411+
if err := state.UpsertAllocs(1000, allocs1); err != nil {
2412+
t.Fatalf("err: %v", err)
2413+
}
2414+
2415+
out, err := state.AllocsByJob(job1.ID, true)
2416+
if err != nil {
2417+
t.Fatalf("err: %v", err)
2418+
}
2419+
2420+
expected := len(allocs) + len(allocs1)
2421+
if len(out) != expected {
2422+
t.Fatalf("expected: %v, actual: %v", expected, len(out))
2423+
}
2424+
2425+
out1, err := state.AllocsByJob(job1.ID, false)
2426+
expected = len(allocs1)
2427+
if len(out1) != expected {
2428+
t.Fatalf("expected: %v, actual: %v", expected, len(out1))
2429+
}
2430+
2431+
}
2432+
23782433
func TestStateStore_AllocsByIDPrefix(t *testing.T) {
23792434
state := testStateStore(t)
23802435
var allocs []*structs.Allocation

nomad/structs/structs.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ type JobEvaluateRequest struct {
249249

250250
// JobSpecificRequest is used when we just need to specify a target job
251251
type JobSpecificRequest struct {
252-
JobID string
252+
JobID string
253+
AllAllocs bool
253254
QueryOptions
254255
}
255256

scheduler/generic_sched.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (s *GenericScheduler) computeJobAllocs() error {
354354
}
355355

356356
// Lookup the allocations by JobID
357-
allocs, err := s.state.AllocsByJob(s.eval.JobID)
357+
allocs, err := s.state.AllocsByJob(s.eval.JobID, true)
358358
if err != nil {
359359
return fmt.Errorf("failed to get allocs for job '%s': %v",
360360
s.eval.JobID, err)

scheduler/scheduler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type State interface {
6666
Nodes() (memdb.ResultIterator, error)
6767

6868
// AllocsByJob returns the allocations by JobID
69-
AllocsByJob(jobID string) ([]*structs.Allocation, error)
69+
AllocsByJob(jobID string, all bool) ([]*structs.Allocation, error)
7070

7171
// AllocsByNode returns all the allocations by node
7272
AllocsByNode(node string) ([]*structs.Allocation, error)

scheduler/system_sched.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (s *SystemScheduler) process() (bool, error) {
178178
// existing allocations and node status to update the allocations.
179179
func (s *SystemScheduler) computeJobAllocs() error {
180180
// Lookup the allocations by JobID
181-
allocs, err := s.state.AllocsByJob(s.eval.JobID)
181+
allocs, err := s.state.AllocsByJob(s.eval.JobID, true)
182182
if err != nil {
183183
return fmt.Errorf("failed to get allocs for job '%s': %v",
184184
s.eval.JobID, err)

0 commit comments

Comments
 (0)