Skip to content

Commit

Permalink
Refactor the /jobs endpoint
Browse files Browse the repository at this point in the history
It also used the list-jobs function referred to in the last commit.
This refactor removes the use of list-jobs, cleans up the /jobs endpoint,
and addresses part of twosigma#617. /jobs no longer propagates UUID's
in the liberator context.
  • Loading branch information
Scott Crosby committed May 31, 2018
1 parent ce1212f commit a9b04b3
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions scheduler/src/cook/mesos/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@

(defn fetch-job-map-from-entity
[db framework-id job]
"Fetches a job map from a job entity"
(timers/time!
(timers/timer ["cook-mesos" "internal" "fetch-job-map"])
(let [resources (util/job-ent->resources job)
Expand Down Expand Up @@ -891,6 +892,7 @@
pool (assoc :pool (:pool/name pool))))))

(defn fetch-job-map
"Fetches a job map from a UUID"
[db framework-id job-uuid]
(fetch-job-map-from-entity db framework-id (d/entity db [:job/uuid job-uuid])))

Expand Down Expand Up @@ -1118,23 +1120,18 @@
[conn framework-id ctx]
(mapv (partial fetch-job-map (db conn) framework-id) (::jobs ctx)))

(defn render-jobs-for-response
[conn framework-id ctx]
(let [db (db conn)

fetch-group
(fn fetch-group [group-uuid]
(let [group (d/entity db [:group/uuid (UUID/fromString group-uuid)])]
{:uuid group-uuid
:name (:group/name group)}))
(defn- fetch-group-for-response
[db group-uuid]
(let [group (d/entity db [:group/uuid (UUID/fromString group-uuid)])]
{:uuid group-uuid
:name (:group/name group)}))

fetch-job
(fn fetch-job [job-uuid]
(let [job (fetch-job-map db framework-id job-uuid)
groups (mapv fetch-group (:groups job))]
(assoc job :groups groups)))]

(mapv fetch-job (::jobs ctx))))
(defn render-job-for-response
"Given a job entity, create the full response including job fields and group name and group UUID's"
[db framework-id job-entity]
(let [job (fetch-job-map-from-entity db framework-id job-entity)
groups (mapv #(fetch-group-for-response db %) (:groups job))]
(assoc job :groups groups)))

(defn render-instances-for-response
[conn framework-id ctx]
Expand Down Expand Up @@ -1260,7 +1257,7 @@
(histograms/defhistogram [cook-mesos api list-request-param-limit])
(histograms/defhistogram [cook-mesos api list-response-job-count])

(defn list-jobents
(defn list-jobs
"Queries using the params from ctx and returns the jobs that were found as datomic entities."
[db include-custom-executor? ctx]
(timers/time!
Expand Down Expand Up @@ -1290,45 +1287,38 @@
(histograms/update! list-response-job-count (count job-ents))
job-ents)))

(defn list-jobs
"Queries using the params from ctx and returns the job uuids that were found"
[db include-custom-executor? ctx]
(map :job/uuid (list-jobents db include-custom-executor? ctx)))

(defn jobs-list-exist?
(defn jobs-list->context
"Do a job query, add the resulting entites into the liberator context"
[conn ctx]
[true {::jobs (list-jobs (d/db conn) true ctx)}])

(defn read-jobs-handler
[conn is-authorized-fn resource-attrs]
(base-cook-handler
(merge {:allowed-methods [:get]
:malformed? (fn [ctx]
(if (get-in ctx [:request :params :uuid])
(job-request-malformed? ctx)
(job-list-request-malformed? ctx)))
:allowed? (fn [ctx]
(if (get-in ctx [:request :params :uuid])
(job-request-allowed? conn is-authorized-fn ctx)
(job-list-request-allowed? is-authorized-fn ctx)))
:exists? (fn [ctx]
(if (get-in ctx [:request :params :uuid])
(jobs-exist? conn ctx)
(jobs-list-exist? conn ctx)))}
resource-attrs)))
[true {::job-entities (list-jobs (d/db conn) true ctx)}])

(defn read-jobs-handler-multiple
[conn framework-id is-authorized-fn]
(let [handle-ok (partial render-jobs-for-response conn framework-id)]
(read-jobs-handler conn is-authorized-fn {:handle-ok handle-ok})))
(let [db (d/db conn)
handle-ok
(fn handle-ok [ctx]
(mapv #(render-job-for-response db framework-id %) (::job-entities ctx)))]
(base-cook-handler
(merge {:allowed-methods [:get]
:malformed? job-list-request-malformed?
:allowed? (partial job-list-request-allowed? is-authorized-fn)
:exists? (partial jobs-list->context conn)
:handle-ok handle-ok}))))

(defn read-jobs-handler-single
[conn framework-id is-authorized-fn]
(let [handle-ok
(let [db (d/db conn)
handle-ok
(fn handle-ok [ctx]
(first
(render-jobs-for-response conn framework-id ctx)))]
(read-jobs-handler conn is-authorized-fn {:handle-ok handle-ok})))
(mapv #(render-job-for-response db framework-id (d/entity db [:job/uuid %]))
(::jobs ctx))))]
(base-cook-handler
(merge {:allowed-methods [:get]
:malformed? job-request-malformed?
:allowed? (partial job-request-allowed? conn is-authorized-fn)
:exists? (partial jobs-exist? conn)}
{:handle-ok handle-ok}))))

(defn instance-request-exists?
[ctx]
Expand Down Expand Up @@ -2292,7 +2282,7 @@
:handle-ok (fn [ctx]
(timers/time!
(timers/timer ["cook-scheduler" "handler" "list-endpoint-duration"])
(let [job-ents (list-jobents db false ctx)]
(let [job-ents (list-jobs db false ctx)]
(mapv (partial fetch-job-map-from-entity db framework-id) job-ents))))))

;;
Expand Down

0 comments on commit a9b04b3

Please sign in to comment.